• 抽象工厂模式


    1.抽象工厂模式概述
    抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属
    于创建型模式,它提供了一种创建对象的最佳方式。
    在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

    意图:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
    主要解决:主要解决接口选择的问题。
    何时使用:系统的产品有多于一个的产品族,而系统只消费其中某一族的产品。
    如何解决:在一个产品族里面,定义多个产品。
    关键代码:在一个工厂里聚合多个同类产品。
    使用场景:(1)QQ 换皮肤,一整套一起换。 (2)生成不同操作系统的程序。
    注意事项:产品族难扩展,产品等级易扩展。

    2.Java Demo

    interface Shape {
        void draw();
    }
    
    class Rectangle implements Shape {
        @Override
        public void draw() {
            System.out.println("Rectangle::draw()");
        }
    }
    
    class Scquare implements Shape {
        @Override
        public void draw() {
            System.out.println("Scquare::draw()");
        }
    }
    
    
    interface Color {
        void fill();
    };
    
    class Red implements Color {
        public void fill() {
            System.out.println("Red::fill()");
        }
    };
    
    class Green implements Color {
        public void fill() {
            System.out.println("Green::fill()");
        }
    };
    
    
    
    abstract class AbstractFactory {
        public abstract Shape getShape(String shapeType);
        public abstract Color getColor(String colorType);
    };
    
    
    
    class ShapeFactory extends AbstractFactory {
        @Override
        public Shape getShape(String shapeType) {
            if (shapeType == null) {
                return null;
            }
    
            if (shapeType.equalsIgnoreCase("rectangle")) {
                return new Rectangle();
            } else if (shapeType.equalsIgnoreCase("scquare")) {
                return new Scquare();
            }
    
            return null;
        }
        @Override
        public Color getColor(String colorType) { //没用上, 去掉public不行,因为父类中是public的,子类中不能修改权限(C++中是可以的)
            return null;
        }
    }
    
    class ColorFactory extends AbstractFactory {
        public Color getColor(String colorType) {
            if (colorType == null) {
                return null;
            }
    
            if (colorType.equalsIgnoreCase("red")) {
                return new Red();
            } else if (colorType.equalsIgnoreCase("green")) {
                return new Green();
            }
    
            return null;
        }
        @Override
        public Shape getShape(String shapeType) { //没用上也要实现,因为下面使用了new ColorFactory()实例化对象了
            return null;
        }
    }
    
    class FactoryProducer {
        public static AbstractFactory getFactory(String type) { //static方法,通过"类名.方法名"直接调用(C++中是使用作用域描述符"::")
            if (type == null) {
                return null;
            }
            if (type.equalsIgnoreCase("shape")) {
                return new ShapeFactory();
            } else if (type.equalsIgnoreCase("color")) {
                return new ColorFactory();
            }
    
            return null;
        }
    }
    
    
    public class FactoryPatternDemo {
        public static void main(String args[]) {
    
            AbstractFactory shape_factory = FactoryProducer.getFactory("shape"); //java中的作用域描述符(类比于C++)也是也是通过"."来实现
            AbstractFactory color_factory = FactoryProducer.getFactory("color");
    
            Shape rectangle = shape_factory.getShape("rectangle");
            Shape scquare = shape_factory.getShape("scquare");
            rectangle.draw();
            scquare.draw();
    
            Color red = color_factory.getColor("red");
            Color green = color_factory.getColor("green");
            red.fill();
            green.fill();
    
        }
    }

    参考:http://www.runoob.com/design-pattern/abstract-factory-pattern.html

  • 相关阅读:
    反射/元类/项目的生命周期
    面向对象高级
    面向对象之封装/鸭子类型
    面向对象之继承
    面向对象1
    包/logging模块/hashlib模块/openpyxl模块/深浅拷贝
    python 几行代码实现自动回复功能
    python crypto rsa 加密运用
    pytohn 单下划线与双下划线的区别
    python 线程小练习
  • 原文地址:https://www.cnblogs.com/hellokitty2/p/10662660.html
Copyright © 2020-2023  润新知