Factory Pattern

Factory pattern (Factory Pattern) Java is one of the most commonly used design patterns. This type of design pattern belongs create schema, which provides the best way to create objects.

In Factory mode, we will not expose the client to create a logical when you create an object, and through the use of a common interface to point to the newly created object.

Introduction

Intent: to create a defined object's interface, let subclasses decide which instance of a class factory , the factory model so that it delay the process to create subclasses .

Mainly to solve: the main problem of interface options.

When to use: When you create different instances in different conditions we definitely plan.

How to solve: let subclasses to implement the factory interface, it is an abstract of the returned product.

The key code: the creation process is executed in its subclasses.

Application examples:  1, you need a car, you can pick up directly from inside the factory, the car is how to do it, and this particular implementation inside the car without having tube. 2, Hibernate change just to change the database dialect and driving can be.

Advantages:  1, a caller wanted to create an object, just know that the name on it. 2, high scalability , if you want to add a product, as long as the expansion of a factory class can be. 3, concrete realization of shielding products, the caller only concerned with the interface product .

Disadvantages: Each time a product increases, increasing the need to achieve a concrete factory classes and objects, such that the number of classes in the system increases exponentially, increasing the complexity of the system to some extent, but also increases the system specific classes rely. This is not a good thing.

Usage scenarios:  1, Logger : record may be recorded to your local hard disk, system events, such as a remote server, the user can choose to log somewhere. 2, database access , when users do not know what kind of final system uses a database, and the database may have changes. 3, design a framework for connection to the server , you need three protocol, "POP3", "IMAP" , "HTTP", can these three products as a class, implement a common interface.

Note: As a class to create a pattern in any place need to generate complex objects, you can use the factory method pattern. Local One thing to note is that complex objects suitable for factory mode, and simple objects, in particular, need only to complete the objects created by new, without the use of the factory model. If the factory mode, it is necessary to introduce a factory class, it will increase the complexity of the system.

achieve

We will create a  Shape  interface and implementation  Shape  entity class interface. The next step is the definition of a factory class  ShapeFactory .

FactoryPatternDemo , our demo class uses  ShapeFactory  to get the  Shape  object. It to  ShapeFactory  transfer information ( CIRCLE / the RECTANGLE / SQUARE ), it needs to obtain the type of the object.

FIG factory pattern UML

step 1

Create an interface:

Shape.java

public interface Shape {
   void draw();
}

Step 2

Creating an entity class that implements the interface.

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.");
   }
}

Step 3

Create a factory to produce the object entity classes based on the given information.

ShapeFactory.java

public class ShapeFactory {
    
   //使用 getShape 方法获取形状类型的对象
   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;
   }
}

Step 4

Using this facility, the object acquired entity classes by passing type information.

FactoryPatternDemo.java

public  class FactoryPatternDemo { 
 
   public  static  void main (String [] args) { 
      ShapeFactory shapeFactory = new new ShapeFactory (); 
 
      // get the object of Circle, and invoke the draw method of 
      the Shape Shape1 = shapeFactory.getShape ( "CIRCLE" ); 
 
      // Circle of calling the draw method 
      shape1.draw (); 
 
      // get the object of the Rectangle, and then call its draw method of 
      the Shape Shape2 = shapeFactory.getShape ( "the RECTANGLE" ); 
 
      // call the draw method of Rectangle 
      shape2.draw (); 
 
      / / Gets Square object, and call its draw method of 
      the Shape shape3 = shapeFactory.getShape ( "SQUARE" ); 
 
      //The method of the draw call Square 
      shape3.draw (); 
   } 
}

Step 5

The implementation of the program, output:

Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.

 

Other Related Articles