Simple factory Model

Source: Internet
Author: User

In terms of the design pattern type, the simple factory pattern is a creation pattern, also called a static factory method (one of the GOF design patterns. The simple factory mode is determined by a factory object to create a product instance. The simple factory model is the simplest and Practical Model in the factory model family. It can be understood as a special implementation of different factory models.

Implementation Method (as shown in the figure below)

Simple factory Model

 

The essence of the simple factory mode is that a factory class dynamically determines the product class to be created (these product classes inherit from a parent class or interface) based on the input parameters.

Roles and responsibilities in this mode

Factory role

The core of the simple factory mode is to implement the internal logic for creating all instances. The factory class can be directly called by the outside world to create the required product objects.

Abstract product (Car) Role

The parent class of all objects created in simple factory mode, which describes the common public interfaces of all instances.

Specific product (BMW) Role

Is the creation target of the simple factory mode. All created objects are instances of a specific class that acts as this role.

Advantages

The factory class is the Key Software Architecture Optimization of the entire model.

Disadvantages

Because the factory class integrates the creation logic of all instances, in violation of the High Cohesion responsibility allocation principle, all creation logic is centralized into a factory class; the class it can create can only be considered in advance, if you need to add a new class, you need to change the factory class.

When the number of product categories in the system increases, the requirements for the factory category to create different instances based on different conditions may arise. this kind of judgment on the condition is intertwined with the judgment on the specific product type, and it is difficult to avoid the spread of module functions, which is very unfavorable for system maintenance and expansion;

These shortcomings have been overcome in the factory method model.

Note

The factory class is responsible for creating fewer objects;

The customer only knows the parameters passed into the factory class and does not care about how to create objects (logic;

Because simple factories are easy to violate the High Cohesion responsibility allocation principle, they are generally used only in simple cases.

The Code is as follows:

CarFactory cf = CarFactory (); Car c = cf. getCar ("BMW" (c! = C = BMW (); System. out. println ("Hello, we have already produced your BMW for you. Please come and pick up the goods and pay for it if you are free! "} System. out. println (" sorry, I can't do anything! "Car c = (" BMW ". equals (type) c = BMW ();} ("Benz" c = Benz (); Benz System. out. println ("running in a Mercedes-Benz" System. out. println ("Mercedes-Benz parking" BMW System. out. println ("BMW driving" System. out. println ("BMW stopped "}

 

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.