Design mode study notes --- 1. Simple factory mode

  • This design pattern is more like a selector, everyone has used it but the concept is relatively vague;

  • This type of design pattern belongs to the creation mode, which provides an optimal way to create objects. In the factory mode, when we create an object, we will not expose the creation logic to the client, and point to the newly created object through a common interface;

  • Intent: define an interface for creating objects, let its subclasses decide which factory class, the factory mode uses its creation process to delay until the subclass;

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

  • When to use: We explicitly plan to create different instances under different conditions;

  • How to solve it: Let its subclass implement the factory interface, and the returned product is also an abstract product;

  • Key code: the creation process is carried out in its subclasses;

  • Applications:

    • 1. You need a car, you can directly pick up the goods from the factory, regardless of how the car is made, and the specific implementation in this car;

    • 2. Hibernate only needs to change the dialect and driver to change the database;

  • advantage

    • 1. A caller who wants to create an object only needs to know the name;

    • 2. High scalability, if you want to add a product, you only need to expand a factory class;

    • 3. The specific implementation of the shielding product, the caller only cares about the interface of the product;

  • Disadvantages: Each time you add a product, you need to add a specific class and object to achieve the factory, which makes the number of classes in the system multiply, which increases the complexity of the system to a certain extent, and also increases the specific class rely;

  • scenes to be used:

    • 1. Log recorder: records may be recorded to local disks, system events, remote servers, etc. The user knows where to choose the recording date;

    • 2. Database access, when the user does not know which type of database the system uses in the end, and the database may change;

    • 3. To design a framework to connect to the server, three protocols are required, "POP3", "IMAP", and "HTTP". These three can be used as product categories to jointly implement an interface;

  • Precautions:

    • As a creation class pattern, the factory method pattern can be used wherever complex objects need to be generated. One thing to note is that complex objects are suitable for using the factory pattern, while simple objects, especially objects that can be created only by new, without using the factory pattern, you need to introduce a factory class, which will increase the complexity of the system;

  • Actual combat

    • We will implement a Shape interface and an entity class that implements the Shape interface. The next step is the factory class ShapeFactory;

    • FactoryPatternDemo, the demo class uses ShapeFactory to get Shape objects.

 

Code link

 

package factorypattern;

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

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

 

package factorypattern;

/**
 * @author yangxin_ryan
 * create FactoryPatternDemo Class
 */
public class FactoryPatternDemo {

    public static void main(String[] args) {
        ShapeFactory shapeFactory = new ShapeFactory();
        Shape shape1 = shapeFactory.getShape("CIRCLE");
        shape1.draw();
        Shape shape2 = shapeFactory.getShape("RECTANGLE");
        shape2.draw();
        Shape shape3 = shapeFactory.getShape("SQUARE");
        shape3.draw();
    }

}

 

package factorypattern;

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

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

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

 

package factorypattern;

/**
 * @author yangxin_ryan 
 * Create ShapeFactory class
 */
public class ShapeFactory {

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

 

package factorypattern;

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

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

Results of the:

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

Guess you like

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