C # factory mode and abstract factory Mode

Source: Internet
Author: User

I. Factory Method mode

The FactoryMethod mode is the class creation mode. It defines a factory interface for creating product objects and delays the actual creation to the subclass.

The factory method mode is further abstract and promoted by the simple factory mode. Due to the use of polymorphism, the factory method pattern maintains the advantages of the simple factory pattern and overcomes its shortcomings.

In the factory method mode, the core factory class is no longer responsible for the creation of all products, but rather the specific creation work is handed over to the Child class. This core class is only responsible for providing the interface that must be implemented by a specific factory, without touching the details of which product class is instantiated. This allows the factory method mode to allow the system to introduce new products without modifying the factory role.

In the Factory Method mode, Factory classes and product classes usually have parallel hierarchical structures, which correspond one to one.

Ii. Factory Method role and structure:

Abstract Factory role: it is the core of the factory method model and has nothing to do with applications. Any factory class of the object created in the mode must implement this interface.

Concrete Creator: this is a specific factory class that implements Abstract Factory interfaces. It contains the logic closely related to applications and is called by applications to create product objects.

Abstract Product role: the super type of the object created in the factory method mode, that is, the common parent class of the Product object or the common interfaces.

Specific Product role: this role implements the interface defined by the abstract Product role. A specific product is created in a specific factory, which is usually one-to-one.

 

Factory mode and abstract factory mode are constructor modes that are widely used. First, let's briefly introduce the application background of this constructor mode: In the process of developing a class, we usually need to develop corresponding constructors for this class (but in most cases ,. NET development environment will automatically create constructor by default), so that customers using this class can use constructor to instantiate this class. however, sometimes this happens: customers who need to use an object do not know (or should not know) which class of the several classes should be initialized. to solve this problem, we can use the factory mode to define an interface. Customers can use this interface to create an object. at the same time, we can also control which class to instantiate. A method is used in the instantiation process. This method needs to use external factors to determine which class to instantiate. sometimes, these external factors are a huge research topic and often involve multiple classes. for this reason, the abstract factory model is used in this scenario to provide

Related Article

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.