zoukankan      html  css  js  c++  java
  • 简单工厂模式

    工厂模式属于创建型模式,由一个工厂对象决定创建出哪一种产品类的实例。

    角色:

    IProduct: 产品共同的接口

    Product1:具体的产品类

    Creator:工厂类,可根据参数决定创建的产品类型

    示例:

    public interface IProduct {
        void myfunction();
    }

    ---

    class Product1 implements IProduct{
        public void myfunction(){
            System.out.println("function1");
        }
    }

    ---

    class Product2 implements IProduct {
        public void myfunction() {
            System.out.println("function2");
        }
    }

    ---

    public class Factory{
        public static IProduct product(int k){
            if (k == 1) {
                return new Product1();
            } else if (k == 2) {
                return new Product2();
            }
            return null;
        }
    }

    ---

    public class FactoryTest{
        public static void main(String[] args){
            IProduct product = Factory.product(2);
            if (product != null) {
                product.myfunction();
            }
        }
    }

     

    end

  • 相关阅读:
    js forEach方法
    day1总结
    jupyter notebook
    java_13网络编程
    原生 input radio 优化
    JQ 获取 input file 图片 显示在对应位置
    math.js 使用
    前端优化
    文字动态颜色变化效果
    谷歌,火狐隐藏滚动条
  • 原文地址:https://www.cnblogs.com/luangeng/p/6556997.html
Copyright © 2011-2022 走看看