• 抽象工厂模式


    抽象工厂模式在工厂模式的基础上又增加了一层抽象。将抽象工厂模式与工厂模式比较,很明显是添加了一个新的抽象层。抽象工厂是一个创建其他工厂的超级工厂。我们可以把它叫做“工厂的工厂”。

    代码:

    interface CPU {
        void process();
    }
      
    interface CPUFactory {
        CPU produceCPU();
    }
      
    class AMDFactory implements CPUFactory {
        public CPU produceCPU() {
            return new AMDCPU();
        }
    }
      
    class IntelFactory implements CPUFactory {
        public CPU produceCPU() {
            return new IntelCPU();
        }
    }
      
    class AMDCPU implements CPU {
        public void process() {
            System.out.println("AMD is processing...");
        }
    }
      
    class IntelCPU implements CPU {
        public void process() {
            System.out.println("Intel is processing...");
        }
    }
      
    class Computer {
        CPU cpu;
      
        public Computer(CPUFactory factory) {
            cpu = factory.produceCPU();
            cpu.process();
        }
    }
      
    public class Client {
        public static void main(String[] args) {
            new Computer(createSpecificFactory());
        }
      
        public static CPUFactory createSpecificFactory() {
            int sys = 0; // based on specific requirement
            if (sys == 0)
                return new AMDFactory();
            else
                return new IntelFactory();
        }
    }
  • 相关阅读:
    《构建之法》阅读笔记07
    学习进度条——第六周
    《构建之法》阅读笔记06
    团队开发
    《构建之法》阅读笔记05
    数组3——返回二维数组中最大联通子数组的和
    学习进度条——第五周
    坯布检验管控系统
    DateTime日期格式转换,不受系统格式的影响
    多层下firebird自增长字段的处理
  • 原文地址:https://www.cnblogs.com/jibingeXper/p/3475684.html
Copyright © 2020-2023  润新知