Novice tutorial design pattern learning~

Recommended novice tutorial: https://www.runoob.com/design-pattern/design-pattern-intro.html

Factory mode

The factory pattern allows us to obtain new objects through a common interface when creating objects without having to create them ourselves.
Advantage
1 The caller can obtain the object only by the product name.
2 If you want to add a product, you only need to add the corresponding product class in the factory.
3 The caller can get the corresponding product (instance of the class) only by calling the function.
Disadvantage
1 Every When adding a product each time, you need to add a class and change the object's realization factory. When the number of products is large, the management is more complicated.
In short, the factory mode encapsulates the realization of the specific product once, so that the caller does not need to pay attention to the specific product. The details of the creation time, the factory pattern can be used when the code for creating the object is more complex, but if the creation of the object can be completed only through new, then you need to consider whether you need to use the factory pattern.
Code practice:
Class organization chart: Shape in theInsert picture description here
figure above represents some basic public attributes of different products. Circle, Square, and Rectangle represent specific products. ShapeFactory represents a factory through which we can obtain the products we want. FactoryPatternDemo is the main function entrance of Demo. The inheritance and call structure of my own code is as shown in the figure above: I have slightly changed the names of specific parts of the code according to my own understanding.




Shape (basic features of the product)

public interface Shape {
    
    
   void draw();
}

Specific products-Rectangle, Square, Circle

public class Rectangle implements Shape {
    
    
 
   @Override
   public void draw() {
    
    
      System.out.println("Inside Rectangle::draw() method.");
   }
}
public class Square implements Shape {
    
    
 
   @Override
   public void draw() {
    
    
      System.out.println("Inside Square::draw() method.");
   }
}
public class Circle implements Shape {
    
    
 
   @Override
   public void draw() {
    
    
      System.out.println("Inside Circle::draw() method.");
   }
}

Factory where the product is produced

public class Factory {
    
    
    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();
        } else {
    
    
            return null;
        }
    }
}

The caller directly obtains the corresponding product through the interface

public class FactoryDemoMain {
    
    
    public static void main(String[] args) {
    
    
        Factory shapeFactory = new Factory();
        //通过工厂去获取 Circle 的对象,并调用它的 draw 方法
        Shape product1 = shapeFactory.getShape("CIRCLE");
        //调用 Circle 的 draw 方法
        product1.draw();
    }
}

Output:

Inside Circle::draw() method.

Guess you like

Origin blog.csdn.net/liu_12345_liu/article/details/103443078