Step by step. NET Design Pattern Study note 24, Factory Method (Factory Method pattern)

Source: Internet
Author: User

Overview

In software systems, the creation of "an object" is often faced. due to changes in requirements, the specific implementation of this object is often subject to drastic changes, however, it has relatively stable interfaces. How to deal with this change? A new packaging mechanism is provided to isolate the changes of "this variable object", so as to keep the "other objects dependent on this object" in the system from changing as required? This is the Factory Method mode.

Intention

Defines an interface for users to create objects, so that the subclass determines which class to instantiate. Factory Method delays the instantiation of a class to its subclass.

Structure chart

Role description:

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. There are two such roles: BulbCreator and TubeCreator.

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. In, this role is Light.

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.

Examples in life

The factory method defines an interface for creating objects, but the subclass determines which class to instantiate. Injection Molding demonstrates this mode. Plastic toys manufacturers process plastic powder and inject the plastic into the desired shape of the mold. The type of toys (cars, people, etc.) is determined by the mold.

 

Example

If you go to McDonald's for a hamburger, the hamburger production process is a factory method. The example is shown below:

 

Code Design

Create Hamburger. cs First:

Html # viewSource "commandName =" viewSource "highlighterId =" highlighter_679260 "> view sourceprint?
1 public abstract class Hamburger
2 {
3     public abstract string BuyHamburger();
4 }

 

Create CheeseBurger. cs again:

View sourceprint?
1 public class CheeseBurger : Hamburger
2 {
3     public override string BuyHamburger()
4     {
5         return "What you want is cheese burger";
6     }
7 }

 

Create RoastedChickenBurger. cs again:

View sourceprint
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.