Factory three Brothers Simple factory model (ii)

Source: Internet
Author: User
Tags abstract
2 Simple Factory mode Overview

The simple factory model does not belong to the Gof 23 classic design pattern, but it is often used as the basis for learning other factory models, and its design concept is simple and its basic flow is as follows:

The related code for the various objects that need to be created, such as various chart objects, is first encapsulated in different classes, which are called concrete product classes , and their common code is abstracted and extracted and encapsulated in an abstract product class . Each specific product class is a subclass of an abstract product class; then provide a factory class to create a variety of products, in the factory class to provide a factory method to create a product, the method can be different from the parameters of the input to create different specific product objects The client simply calls the factory method of the plant class and passes in the corresponding parameters to get a product object.

The simple factory pattern is defined as follows:

Simple Factory pattern: Defines a factory class that can return instances of different classes depending on the parameters, and the instances that are created usually have a common parent class. Because the method used to create the instance in the Simple Factory mode is static, the simple factory pattern is also known as the Static Factory method (static Factory) mode, which belongs to the class-creation pattern.

The point of the simple factory model is that when you need something, just pass in the correct parameter, and you get the object you want without having to know its creation details. Simple factory model structure is relatively simple, its core is the factory class design, its structure is shown in Figure 1:

Figure 1 Simple Factory mode structure diagram

The following roles are included in the simple factory pattern structure diagram:

Factory (Factory role): factory role is the factory class, it is the core of the simple factory model, responsible for the creation of all product instances of the internal logic; Factory classes can be called directly by the outside world to create the desired product objects A static factory method, FactoryMethod (), is provided in the factory class, and its return type is abstract product type products.

Product (Abstract products role): It is the parent class of all objects created by the factory class, encapsulating the public methods of various product objects, and its introduction will increase the flexibility of the system so that only a common factory method can be defined in the factory class. Because all of the specific product objects that are created are their child class objects.

concreteproduct (Specific product role): It is the creation target of the simple factory pattern, and all created objects act as instances of a specific class of this role. Each specific product role inherits the abstract product role and needs to implement the abstract methods declared in the abstract product.

In the simple factory model, the client creates an instance of a product class from the factory class without having to create the object directly using the New keyword, which is the simplest of the factory pattern family.

When using the simple factory model, the first need to refactor the product class, not to design an all-encompassing product class, but to design a product hierarchy according to the actual situation , the common code of all product classes moved to the abstract product class, and in the abstract product class to declare some abstract methods, For different specific product classes, the typical abstract product class code is as follows: [Java] view Plain Copy abstract class Product {//Public business method for all product classes Publ   IC void Methodsame () {//implementation of public method}//Declaration abstract business method publicly abstract void Methoddiff (); }

Abstract business methods that are declared in abstract product classes are implemented in specific product classes, and different specific product classes can provide different implementations, typical specific product class codes are as follows: [Java] view plain copy class Concreteproduct ext Ends Product {//Implement business method public void Methoddiff () {//Implementation of business Method}}

The core of the simple factory model is the factory class, where the client typically uses the new keyword to create the product object directly before the factory class, and after the factory class is introduced, the client can create the product through the factory class, in the simple factory model, the factory class provides a static factory method for use by the client. Different product objects can be created depending on the parameters passed in, and the typical factory class code is as follows:[Java]  View Plain  copy   class factory {       //static Factory method         public static product getproduct (String arg)  {            Product product = null;            if  (Arg.equalsignorecase ("A"))  {                product = new concreteproducta ();                //Initialization Settings product           }            else if  (Arg.equalsignorecase ("B"))  {                PRODUCT = NEW CONCRETEPRODUCTB ();                //Initialization Settings product            }           return product;        }  

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.