Design Model-simple factory Model

Source: Internet
Author: User

The factory model has the following forms:

· Simple factory: Also known as the static factory method mode;

· Factory method: Also known as polymorphic factory or virtualconstructor );

· Abstract Factory mode (Abstract Factory): Toolkit mode (kit or Toolkit );

Simple FactoryPurpose: Defines an interface for creating objects.

Class DiagramAs follows:

Let's take a look at itsComposition:

1) Factory role (creator): This is the core of this model and contains some commercial logic and judgment logic. In Java, it is often implemented by a specific class.

2) Abstract Product role (concreteproduct): it is generally the parent class or implemented interface inherited by a specific product. It is implemented by interfaces or abstract classes in Java.

3) specific product role: the object created by the factory class is the instance of this role. It is implemented by a specific class in Java.

Example:

A decade ago, he had three cars in his house, Benz, BMW, Audi, and hired a driver to drive for him. However, it is always strange for the nouveau riche to take a taxi: the Benz car was followed by the driver and said, "drive a taxi !", When he got on BMW, he said, "kaibao carriage !", Get on with Audi and say, "Drive on AUDI !". You must say: this person is ill! Just say you can't drive ?!

How can we use the simple factory model? We will use a simple factory model to transform the mode of transportation for the nouveau riche.

-- Now, the nouveau riche just needs to sit in the car and say "Drive" to the driver.

// Abstract Product role

Public interface car {

Public void drive ();

}

// Specific product role

Public class Benz implements car {

Public void drive (){

System. Out. println ("drivingbenz ");

}

}

Public class BMW implements car {

Public void drive (){

System. Out. println ("drivingbmw ");

}

}

... (I will not write about Audi: P)

// Factory role

Public class driver {

// Factory method. Note that the return type is an abstract Product role.

Public static car drivercar (strings) throws exception {

// Judge the logic and return the specific product role to the client

If (S. equalsignorecase ("Benz "))

Return newbenz ();

Elseif (S. equalsignorecase ("BMW "))

Return new BMW ();

......

Else throw newexception ();

}

}

// Welcome to the startup ......

Public class magnate {

Public static void main (string [] ARGs ){

Try {

// Tell the driver that I am taking the Mercedes today

Car car = driver. drivercar ("Benz ");

// Run the following command: Drive

Car. Drive ();

}

}

Advantages:

1. Decoupling

The core of the mode is the factory class, which contains the necessary judgment logic to determine when to create a product class instance. The client is not responsible for directly creating product objects, but for "consuming" products. The simple factory model separates responsibility.

  1. Help Encapsulation
    Simple factory is very simple, but it helps us to encapsulate components very well, and then let the component external be truly interface-oriented.

Disadvantages:

  1. It may increase the complexity of the client.
    If you select a specific implementation class using client parameters, you must make the client understand the specific functions and meanings represented by each parameter, which increases the difficulty of the client, some of the internal implementations are exposed. You can use configurable methods to achieve this.
  2. Not convenient to expand sub-factory
    The constructor of a private simple factory uses a static method to create an interface, so you cannot change the method of creating an interface by writing a subclass of a simple factory class. However, generally, you do not need to create a subclass for a simple factory.

 We recommend that you use a simple factory in the following situations::

1. if you want to completely encapsulate and isolate the specific implementation so that the external can only operate the encapsulation body through the interface, you can select a simple factory to allow the client to obtain the corresponding interface through the factory, without worrying about the specific implementation.

2. if you want to centrally manage and control the responsibilities of creating external objects, you can select a simple factory. A simple factory can create many irrelevant objects, the responsibility for creating external objects can be concentrated in a simple factory to achieve centralized management and control.

Question and Answer

1. Does the factory mode instantiate classes without using the new operator? Why is the new operator still used in the specific implementation?

A: For the entire system, the factory model uses the details of the new operator to encapsulate and hide it. Of course, as long as the program is written in the Java language, the characteristics of the Java language will certainly appear in the details.

2. Exercise questions:

Use the simple factory mode to design an arttracer system that creates different shapes, such as circles, squares, and triangles. Each ry has the draw () and erase () methods. A badshapeexception must be thrown when the surveyors receive a command to create a collection image that is not supported.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.