Latest web development tutorials

Factory mode

Factory pattern (Factory Pattern) Java is one of the most commonly used design patterns. This type of design patterns belong 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: The definition of a created object's interface, let subclasses decide which class to instantiate a factory, the factory pattern to make the process of creating the delay to the sub-class.

Mainly to solve: The main problem of the interface options.

When to use: when we explicitly create different instances under different conditions plan.

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

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

Application examples: 1, you need a car, you can pick up from inside the factory directly, without having to pipe the car is how to do it, as well as the implementation inside the car.2, Hibernate exchange database simply changing dialect and driving can be.

Advantages: 1, a caller wanted to create an object, just know its 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, shielding product realization, the caller only concerned with product interface.

Disadvantages: each increase a product, we need to add classes and objects to achieve a concrete factory, so the number of classes in the system increases exponentially, to a certain extent, increased the complexity of the system, but also increases the system specific class rely.This is not a good thing.

Usage scenarios: 1, Logger: Records may be recorded to your local hard disk, the system event, remote server, the user can choose to log somewhere.2, database access, when the user does not know what kind of final system uses a database, and the database might change. 3, design a framework for connection to the server, you need three agreements, "POP3", "IMAP", "HTTP", can these three as a product category, to achieve a common interface.

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

achieve

We will create an entity class interface and implementationShapeShape interface. The next step is to define factory classesShapeFactory.

FactoryPatternDemo,our demo to get the class to useShapeFactoryShape object. It will transmit information(CIRCLE/ RECTANGLE / SQUARE) toShapeFactory,it needs to get the type of object.

Factory pattern UML diagram

step 1

Create an interface.

Shape.java

public interface Shape {
   void draw ();
}

Step 2

Create 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 {
	
   // Use getShape method to get an object of type shape 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

Use the factory to obtain an entity object of the class by passing type information.

FactoryPatternDemo.java

public class FactoryPatternDemo {

   public static void main (String [] args) {
      ShapeFactory shapeFactory = new ShapeFactory ();

      // Get the Circle object, and call its draw method Shape shape1 = shapeFactory.getShape ( "CIRCLE");

      // Call the draw method of shape1.draw Circle ();

      // Get the Rectangle object, and call its draw method Shape shape2 = shapeFactory.getShape ( "RECTANGLE");

      // Call the draw method of shape2.draw Rectangle ();

      // Get the Square object, and call its draw method Shape shape3 = shapeFactory.getShape ( "SQUARE");

      // Call the draw method Square shape3.draw ();
   }
}

Step 5

Verify output.

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