Design pattern study notes --- 2. Abstract factory pattern

  • Compared with the simple factory pattern, the abstract factory pattern is aimed at multiple objects and multiple factories, but the principles are all factory patterns, and the operation is similar;

  • Abstract Factory Pattern (Abstract Factory Pattern) is another factory around a super factory. The super factory is also known as other factories. This type of design pattern belongs to the model creation mode, which provides an optimal way to create objects;

  • In the abstract factory pattern, the interface is a factory that is responsible for creating a related object. There is no need to display the class specifying them. Each generated factory can provide objects according to the factory pattern

  • Intent: to provide an interface for creating a series of related or interdependent objects without specifying their specific classes;

  • Main solution: mainly solve the problem of interface selection;

  • When to use: The system's products have more than one product family, and the system consumes only one of the family's products;

  • How to solve: Define multiple products in a product family;

  • Key code: aggregate multiple similar products in a factory;

  • Application examples: At some parties, two or more sets of clothes will definitely be used, such as business wear (complete set, a series of specific products), fashion wear (complete set, a series of products), or even for a family, may be business Women's clothing, business men's clothing, these are also complete sets, that is, some specific products. Suppose a situation that a certain wardrobe (specific factory) can only store certain such clothes (sets, a series of specific products), and every time you take this set of clothes, you will naturally have to take them out of the wardrobe. To understand with the idea of ​​OOP, all wardrobes (specific factories) are wardrobes (abstract factories), and each set of clothes includes a specific top (a specific product) and pants (a specific product) ), These specific tops are actually tops (abstract products), and the specific Ku people are also pants (abstract products);

  • Advantages: When multiple objects in a product family are designed to work together, it can ensure that the client always uses only objects in the same product family;

  • It is very difficult to expand the product family. To add a certain product in a series, you need to add code in the abstract Creator and add diamanté in the concrete;

  • scenes to be used:

    • 1. Change the skin and change the whole set together;

    • 2. Generate programs for different operating systems;

  • Precautions:

    • Product family is difficult to expand;

    • Product grade is easy to expand;

  • Actual combat:

    • Create Shape and Color interfaces and entity classes that implement these interfaces. The next step is to create AbstractFactory class AbstractFactory.

    • Then define the factory classes ShapeFactory and ColorFactory. Both factories extend AbstractFactory. Then create a factory creator / generator class FactoryProducer;

    • AbstractFactoryPatternDemo, our demo class uses FactoryProducer to get AbstractFactory object. It will pass Shape Information (CIRCLE / RECTANGLE / SQUARE) to AbstractFactory in order to get the type of object he needs. At the same time, it also transmits Color information (RED / GREEN / BLUE) to Abstractory in order to obtain the type he needs;

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();
    }
}

Published 1980 original articles · praised 708 · 3.66 million views +

Guess you like

Origin blog.csdn.net/u012965373/article/details/105584644