Design Mode Study Notes-factory method mode

Source: Internet
Author: User

1. Overview:
Defines an interface used to create objects, so that the subclass determines which class to instantiate. The factory method delays the instantiation of a class to the subclass.

2. Role in the Mode

2.1 Abstract Factory: This abstract class (or interface) declares a factory method for creating an object to return an object of the product type.

2.2 concretecreator: redefines the factory method and returns a specific concrete product instance.

2.3 abstract product: defines the object created by the factory method.

2.4 concreteproduct: a product that is inherited from the product abstract class.

3. Interpretation of the Mode

3.1 General class diagram of factory method mode

  

3.2 code implementation of factory method mode

/// <Summary> /// abstract factory, declare a method for creating a product. // </Summary> public abstract class creator {// <summary> // This method is used to return the product. // </Summary >/// <returns> </returns> public abstract product createproduct ();} /// <summary> /// the creator of product class A in the specific production factory, inherited from the abstract factory creator /// </Summary> public class productacreator: creator {public override product createproduct () {return New concreteproducta () ;}/// <summary> // The creator of the specific production factory and product class B, inherited from the abstract factory creator // </Summary> public class productbcreator: Creator {public productbcreator () {} public override product createproduct () {return New concreteproductb ();}} /// <summary> /// abstract product, define the object created in the factory method /// </Summary> public abstract class product {public product () {} public abstract void opration () ;}/// <summary> // product A, inherited from product /// </Summary> public class concreteproducta: product {public concreteproducta () {} public override void opration () {// This is product A }}/// <summary> // product B, inherited from product // </Summary> public class concreteproductb: Product {public concreteproductb () {} public override void opration () {// This is product B }}

 

4. Summary

4.1 advantages:

The factory method removes the condition Branch (removing the coupling between the factory class and the branch) and solves the problem that the simple factory is open to modification.
4.2 disadvantages:
When implementing the factory method mode, the client needs to decide which factory to instantiate to build a specific product. The selection and determination still exists, that is, in the factory method mode, the logic judgment of the simple factory is handed over to the client for processing.

For the simple factory mode, you need to modify the factory class to add the function. For the factory method mode, the client is modified.
4.3 Use Cases:
For a product, the caller knows exactly which factory service should be used, instantiate the specific factory, and produce specific products.
The number of sub-classes is not fixed, and new function sub-classes may appear at any time.

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.