设计模式学习笔记 --- 2.抽象工厂模式

  • 相对于简单工厂模式来说,抽象工厂模式针对于多中对象多个工厂,但是原理都是工厂模式,操作类似;

  • 抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂的其他工厂。该超级工厂又称为其他工厂。这种类型的设计模式属于创建模型模式,它提供了一种创建对象的最佳方式;

  • 在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显示指定它们的类,每个生成的工厂都能按照工厂模式提供对象;

  • 意图:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类;

  • 主要解决:主要解决接口选择的问题;

  • 何时使用:系统的产品有多于一个的产品族,而系统只消费其中某一个族的产品;

  • 如何解决:在一个产品族里面,定义多个产品;

  • 关键代码:在一个工厂里聚合多个同类产品;

  • 应用实例:参加一些聚会,肯定会用到两套或者多套衣服,比如说商务装(成套,一系列具体产品)、时尚装(成套,一系列产品),甚至对于一个家庭来说,可能商务女装、商务男装、这些也都是成套的,即一些列具体产品。假设一种情况某一个衣柜(具体的工厂)只能存放某一种这样的衣服(成套,一系列具体产品),每次拿这种成套的衣服时,自然也要从这个衣柜中取出。用OOP的思想去理解,所有的衣柜(具体工厂)都是衣柜类(抽象工厂)某一个,而每一件成套的衣服又包括具体的上衣(某一具体产品),裤子(某一具体产品),这些具体的上衣其实也都是上衣(抽象产品),具体的苦族也都是裤子(抽象产品);

  • 优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象;

  • 产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的Creator里加代码,,又要在具体的里面加diamanté;

  • 使用场景:

    • 1.换皮肤,一整套一起换;

    • 2.生成不同操作系统的程序;

  • 注意事项:

    • 产品族难扩展;

    • 产品等级易扩展;

  • 实战:

    • 创建Shape和Color接口和实现这些接口的实体类,下一步是创建抽象工厂类AbstractFactory。

    • 接着定义工厂类ShapeFactory和ColorFactory。这两个工厂都是扩展了AbstractFactory。然后创建一个工厂创造器/生成器类FactoryProducer;

    • AbstractFactoryPatternDemo,我们的演示类使用FactoryProducer来获取AbstractFactory对象。它将向AbstractFactory传递形状信息Shape(CIRCLE/RECTANGLE/SQUARE),以便获取他所需要对象的类型。同时它还向Abstractory传递颜色信息Color(RED/GREEN/BLUE),以便获取他需要的类型;

package abstractfactory;

/**
 * @author yangxin_ryan
 * Create interface Shape
 */
public interface Shape {
    void draw();
}
package abstractfactory;

/**
 * @author yangxin_ryan
 * create Circle Class
 */
public class Circle implements Shape {

    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}
package abstractfactory;

/**
 * @author yangxin_ryan
 * Create Rectangle class
 */
public class Rectangle implements Shape {

    public void draw() {
        System.out.println("Inside Rectangle::draw() method");
    }
}
package abstractfactory;

/**
 * @author yangxin_ryan
 * create a Square Class
 */
public class Square implements Shape {

    public void draw() {
        System.out.println("Inside Square::draw() method.");
    }
}
package abstractfactory;

/**
 * @author yangxin_ryan
 * Create Color interface
 */
public interface Color {
    void fill();
}
package abstractfactory;

/**
 * @author yangxin_ryan
 * Create Blue Class
 */
public class Blue implements Color {

    public void fill() {
        System.out.println("Inside Blue::fill() method.");
    }
}
package abstractfactory;

/**
 * @author yangxin_ryan
 * Create Green Class
 */
public class Green implements Color {

    public void fill() {
        System.out.println("Inside Green::fill() method.");
    }
}
package abstractfactory;

/**
 * @author yangxin_ryan
 * Create Red Class
 */
public class Red implements Color {

    public void fill() {
        System.out.println("Inside Red::fill() method.");
    }
}
package abstractfactory;

/**
 * @author yangxin_ryan
 * Crate AbstractFactor Abstruct Class
 */
public abstract class AbstractFactory {
    public abstract Color getColor(String color);
    public abstract Shape getShape(String shape);
}
package abstractfactory;

/**
 * @author yangxin_ryan
 */
public class ColorFactory extends AbstractFactory {

    @Override
    public Shape getShape(String shapeType) {
        return null;
    }

    @Override
    public Color getColor(String color) {
        if (color == null) {
            return null;
        }
        if (color.equalsIgnoreCase("RED")) {
            return new Red();
        } else if (color.equalsIgnoreCase("GREEN")) {
            return new Green();
        } else if (color.equalsIgnoreCase("BLUE")) {
            return new Blue();
        }
        return null;
    }
}
package abstractfactory;

/**
 * @author yangxin_ryan
 */
public class ShapeFactory extends AbstractFactory {

    @Override
    public Shape getShape(String shapeType) {
        if (shapeType == null) {
            return null;
        }
        if (shapeType.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        } else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
            return new Rectangle();
        } else if (shapeType.equalsIgnoreCase("SQUARE")) {
            return new Square();
        }
        return null;
    }

    @Override
    public Color getColor(String color) {
        return null;
    }
}
package abstractfactory;

/**
 * @author yangxin_ryan
 */
public class FactoryProducer {

    public static AbstractFactory getFactory(String choine) {
        if (choine.equalsIgnoreCase("SHAPE")) {
            return new ShapeFactory();
        } else if (choine.equalsIgnoreCase("COLOR")) {
            return new ColorFactory();
        }
        return null;
    }
}
package abstractfactory;

/**
 * @author yangxin_ryan
 */
public class AbstractFactoryPatternDemo {

    public static void main(String[] args) {
        // get shape factory
        AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
        // get shape of Circle object
        Shape shape1 = shapeFactory.getShape("CIRCLE");
        // call Circle's draw Function
        shape1.draw();

        Shape shape2 = shapeFactory.getShape("RECTANGLE");
        shape2.draw();

        Shape shape3 = shapeFactory.getShape("SQUARE");
        shape3.draw();
        // get color factory
        AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
        // get color of RED object
        Color color1 = colorFactory.getColor("RED");
        // call RED's fill function
        color1.fill();

        Color color2 = colorFactory.getColor("GREEN");
        color2.fill();

        Color color3 = colorFactory.getColor("BLUE");
        color3.fill();
    }
}

发布了1980 篇原创文章 · 获赞 708 · 访问量 366万+

猜你喜欢

转载自blog.csdn.net/u012965373/article/details/105584644