. Net factory pattern)

Source: Internet
Author: User
Directory
    1. Definition
    2. UML
    3. Participants
    4. Example
Definition:

Define an interface for creating objects, but let the subclass determine which class will be instantiated. The factory method delays the instantiation of a class to the subclass.

UML:

Participants:

Product (Project) Role: Define a product interface.

Product (concreteproduct) role: the specific class that implements the product role interface.

Factory role: defines an interface to create a product.

Concretefactory role: a specific class that implements the interface of the factory role.

 

Example
 Code  Using System; Namespace Dofactory. gangoffour. Factory. Structural { /// <Summary>    /// Mainapp startup class for structural   /// Factory method design pattern.    /// </Summary>    Class Mainapp { /// <Summary>      /// Entry point into console application.      /// </Summary>      Static   Void Main (){ // An array of creators Creator [] creators = New Creator [2]; creators [0] = New Concretecreatora (); creators [1] = New Concretecreatorb (); // Iterate over creators and create products        Foreach (Creator In Creators) {product Product = creator. factorymethod (); console. writeline (" Created {0} ", Product. GetType (). Name );} // Wait for user Console. readkey ();}} /// <Summary>    /// The 'product' abstract class    /// </Summary>    Abstract   Class Product {}/// <Summary>    /// A' concreteproduct' class    /// </Summary>    Class Concreteproducta: Product {} /// <Summary>    /// A' concreteproduct' class    /// </Summary>    Class Concreteproductb: Product {} /// <Summary>    /// The 'creator' abstract class    /// </Summary>    Abstract   Class Creator { Public   Abstract Product factorymethod ();} /// <Summary>    /// A' concretecreator' class    /// </Summary>    Class Concretecreatora: Creator { Public   Override Product factorymethod (){ Return   New Concreteproducta ();}} /// <Summary>    /// A' concretecreator' class   /// </Summary>    Class Concretecreatorb: Creator { Public   Override Product factorymethod (){ Return   New Concreteproductb ();}}}

Output:

Created concreteproducta
Created concreteproductb

 

Practical application:
 Code    /// <Summary>      /// Product Interface      /// </Summary>      Public  Interface ICAR { Void Opendoor ();} /// <Summary>      /// BMW      /// </Summary>      Public   Class Baomacar: ICAR { Public   Void Opendoor () {console. writeline (" Open the BMW door ");}} /// <Summary>      /// Audi     /// </Summary>      Public   Class Aodicar: ICAR { Public   Void Opendoor () {console. writeline (" Open Audi door ");}} /// <Summary>      /// The factory interface used to create a car      /// </Summary>      Public   Interface Ifacloud {ICAR create ();} /// <Summary>     /// BMW Factory      /// </Summary>      Public   Class Baomafactory: ifacloud { Public ICAR create (){ Return   New Baomacar ();}} /// <Summary>      /// Audi Factory      /// </Summary>      Public   Class Aodifactory: ifacloud { Public ICAR create (){ Return   New Aodicar ();}} Static   Void Main ( String [] ARGs) {ifactory factory = New Aodifactory (); ICAR car = factory. Create (); car. opendoor (); console. Read ();}

Output:

Open Audi door

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.