Factory method model (plant model)

Model name and classification

Factory

intention

Create a defined object's interface, let subclasses decide which class to instantiate technology. The method of making a factory to instantiate the class to delay its subclasses.

Note 1: factory method is to instantiate a class to delay its subclasses. It refers to a factory interface implementation class.
Note 2: Factory Method pattern just an optimization model factory, often do not tend to be a simple way to distinguish industrial areas and factories. Or divided into: the factory model - model factory method , simple factory mode - the factory model .

Aliases

FactoryMethod

motivation

Referring simple factory pattern: https://www.cnblogs.com/dhcao/p/11151858.html
This simple model optimized plant:

  • While simple in the use of the plant is more than convenient, but in the design, more in line with the opening and closing design principles: closed for modification, open to the new .
  • In a simple plant, we selected according to the Factory if-else or other case, or what routing instance is created, but if a new instance of the product, it is necessary to add the route is determined in the factory, the wording that mode contrary to the principle of opening and closing.

structure

Participants

  • You need to create product modules - Shape, Circle, Square, Rectangle
  • Factory module - ShapeFactory, CircleFactory, SquareFactory, RectangleFactory
  • Call the client (not shown in Figure shows)

cooperation

Comparative simple factory pattern can be understood that the memory:

  • Each product has a proprietary factory to create each plant create exclusive products.
  • Which specific use of the plant, is determined by the client.

Code examples

//步骤 1
//创建一个接口:
//Shape.java
public interface Shape {
   void draw();
}


//步骤 2
//创建实现接口的实体类。
//Rectangle.java
public class Rectangle implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}

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

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

//步骤 3
//创建一个工厂接口
//ShapeFactory.java
public class ShapeFactory {
    
   //使用 getShape 方法获取形状类型的对象
   public Shape getShape();
}

// 创建具体的工厂类
//CircleFactory.java
public class CircleFactory implements ShapeFactory{
    
   //使用 getShape 方法获取形状类型的对象
    public Shape getShape(){
        return new Circle();
    }
}

//SquareFactory.java
public class SquareFactory implements ShapeFactory{
    
   //使用 getShape 方法获取形状类型的对象
    public Shape getShape(){
        return new SquareFactory();
    }
}

//RectangleFactory.java
public class RectangleFactory implements ShapeFactory{
    
   //使用 getShape 方法获取形状类型的对象
    public Shape getShape(){
        return new RectangleFactory();
    }
}


// 步骤 4
// 客户端使用工厂。

//FactoryPatternDemo.java
public class FactoryPatternDemo {
 
   public static void main(String[] args) {
      ShapeFactory shapeFactory = new ShapeFactory();
 
      //通过Circle工厂获取 Circle 的对象,并调用它的 draw 方法
      ShapeFactory cFactory = new CircleFactory();
      Shape circle = cFactory.getShape();
 
      //调用 Circle 的 draw 方法
      circle.draw();
 
      //通过Square工厂获取 Square 的对象,并调用它的 draw 方法
      ShapeFactory sFactory = new SquareFactory();
      Shape square = sFactory.getShape();
 
      //调用 Square 的 draw 方法
      square.draw();
   }
}
    

Related Patterns

Simple factory pattern, factory pattern, abstract factory pattern

Guess you like

Origin www.cnblogs.com/dhcao/p/11265692.html