Three brothers' simple factory model (2)

Source: Internet
Author: User
2 Overview of simple factory models

The simple factory model does not belong to the 23 classic gof design models, but is usually used as the basis for learning other factory models. Its design philosophy is very simple. The basic process is as follows:

First, encapsulate the code of the various objects to be created (for example, different chart objects) into different classes. These classes are calledProductAnd encapsulate their common code inAbstract Product, Each specific product class is a subclass of the abstract product class; then provideFactoryThis method is used to create products. A factory method is provided in the factory class to create products. Different product objects can be created based on different input parameters; the client only needs to call the factory method of the factory class and input the corresponding parameters to obtain a product object.

The simple factory mode is defined as follows:

Simple factory pattern: defines a factory class, which can return instances of different classes according to different parameters. The created instances usually have a common parent class. Because the method used to create an instance in the simple factory mode is the static method, the simple factory mode is also called the static factory method mode, it belongs to the class creation mode.

The main points of the simple factory model are:When you need something, you only need to pass in a correct parameter to get the object you need without knowing the creation details.The simple factory model structure is relatively simple. Its core is the design of the factory class. Its structure 1 is shown below:

Figure 1 Structure of simple factory Mode

The simple factory mode structure includes the following roles:

● Factory (factory role ):The factory role is the factory class. It is the core of the simple factory model and is responsible for implementing the internal logic of creating all product instances. The factory class can be directly called by the outside to create the required product objects; A static factory method factorymethod () is provided in the factory class, and its return type is abstract product type.

● Product (Abstract Product role ):It is the parent class of all objects created by the factory class and encapsulates the public methods of various product objects. Its introduction will improve the flexibility of the system, so that you only need to define a common factory method in the factory class, because all the created product objects are their subclass objects.

● Concreteproduct (specific product role ):It is the creation target of the simple factory mode. All created Objects Act as instances of a specific class of this role. Each specific product role inherits the abstract Product role and must implement the abstract method declared in the abstract product.

In the simple factory mode, the client creates a product class instance through the factory class without directly using the new keyword to create an object. It is the simplest member of the factory model family.

When using the simple factory model, you must first reconstruct the product class,Instead of designing an all-encompassing product category, you need to design a product hierarchy based on the actual situation., Move all common product code to the abstract product class, and declare some abstract methods in the abstract product class for different specific product classes. The typical abstract product class code is as follows:

Abstract class product {// public business methods of all product classes public void methodsame () {// implementation of public methods} // declare abstract Business Methods public abstract void methoddiff ();}

Abstract Business Methods declared in abstract product classes are implemented in specific product classes. Different product classes can provide different implementations. The typical product class code is as follows:

Class concreteproduct {// implement the business method public void methoddiff () {// implement the business method }}

The core of the simple factory mode is the factory class. Before there is no factory class, the client usually uses the New Keyword to directly create the product object. After the factory class is introduced, the client can create products through the factory class. In the simple factory mode, the factory class provides a static factory method for the client to use. Different product objects can be created based on different input parameters, the typical Factory Code is as follows:

Class factory {// static factory method public static product getproduct (string Arg) {product Product = NULL; If (Arg. inclusignorecase ("A") {Product = new concreteproducta (); // initialize the set product} else if (Arg. repeated signorecase ("B") {Product = new concreteproductb (); // initialize the product} return product ;}}

In the client code, we can obtain the product object by calling the factory method of the factory class. The typical code is as follows:

Class client {public static void main (string ARGs []) {product; Product = factory. getproduct ("A"); // create the product object through the factory class. methodsame (); product. methoddiff ();}}

【Author】 Liu WeiHttp://]

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: 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.