Design Mode (Creation Mode)

Source: Internet
Author: User

 
Design mode Creation Mode 1. Create Mode

The creation mode includes five design modes: Singleton, Factory, AbstractFactory, Builder, and ), prototype: The Creation Mode abstracts the instantiation process. They help a system to create, combine, and represent its objects independently. A class creation mode uses inheritance to change the class to be instantiated. In the creation mode, an object is instantiated and delegated to another object.

2. Singleton (Singleton Mode)

The Singleton mode has three key points: 1. this class can have an instance 2. create this instance by yourself. 3. like how the system provides this instance, how can we use C # To write a singleton class?

Public class Singleton {private static Singleton thisinstance = null; public static object obj = new object (); public Singleton instance () {if (thisinstance = null) {lock (obj) {if (thisinstance = null ){
// Create a class instance thisinstance = new Singleton ();}}}
Return thisinstance;} // privatized enough to function private Singleton (){}}

In the above Code. We use the private constructor to make the Singleton class unavailable to New users. By providing an external instance, the caller can use its unique instance. And ensure that only one instance can be generated internally

3. Factory (Factory Mode)

Key points of the factory mode: the parent class is responsible for defining the public interfaces for creating objects, while the subclass is responsible for generating specific objects. That is, the subclass determines the class of the specific instance.

Above class diagram:

Product-defined product Interface

ConcretePordcut: responsible for implementing the interface product

Creator declares a factory method to return a product instance

ConcretePordcut is responsible for implementing factory methods

 

/// <Summary> /// product interface /// </summary> public interface Pordcut {void ();} /// <summary >/// actual product /// </summary> public class ConcretePordcut: Pordcut {public void a () {throw new NotImplementedException ();}} /// <summary >/// declare the factory /// </summary> public class Creator {public abstract Pordcut CreatorPordcut ();} /// <summary> /// implement the factory class /// </summary> public class ConcreteCreator: Creator {public override Pordcut CreatorPordcut () {return new ConcretePordcut ();}}

4. AbstractFactory (Abstract Factory Mode)

Factory method mode: an abstract product class that can be derived from multiple specific product classes. One abstract factory class can be derived from multiple specific factory classes. Each specific factory class can only create an instance of a specific product class.

Abstract Factory mode: Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes. An abstract factory class can be derived from multiple factory classes. You can create multiple product instances for each specific factory class.

The difference between the two: the factory method model has only one abstract product class, while the abstract factory model has multiple. The factory method mode can only create one instance for a specific product type, while the abstract factory mode can create multiple instances.

Implementation Code:

/// <Summary> /// abstract product A /// </summary> public abstract class PordcutA {public abstract void RunA ();} /// <summary> /// abstract product B /// </summary> public abstract class PordcutB {public abstract void RunB ();} /// <summary> /// actual product A /// </summary> public class ConcretePordcutA: PordcutA {public override void RunA () {throw new NotImplementedException ();}} /// <summary> /// actual product B /// </summary> public class ConcretePordcutB: PordcutB {public override void RunB () {throw new NotImplementedException ();}} public abstract class PordcutFactory {public abstract PordcutA CreatA (); public abstract PordcutB CreatB ();} public class ConcreteFactory: PordcutFactory {public override PordcutA CreatA () {return new ConcretePordcutA ();} public override PordcutB CreatB () {return new ConcretePordcutB ();}}

 5. Builder (creator)

The Creation Mode separates the construction of a complex object from its representation, so that different representations can be created for the same build process. The creation mode is to create a complex object step by step.

It allows users to create and construct objects only by taking charge of object types and content, and do not know the construction details, such as building a house requires a foundation-> housing beams-> roof-> fencing the following code practices

Public interface IBuilder {// <summary> // create part 1 /// </summary> void CretaPart1 (); /// <summary> /// create part 2 /// </summary> void CretaPart2 (); /// <summary> /// create part 3 /// </summary> void CretaPart3 (); /// <summary> /// create part 4 /// </summary> void CretaPart4 ();} /// <summary> /// actual creator /// </summary> public class conncreteBuilder: IBuilder {public House _ house; public conncreteBuilder (House builder) {_ house = builder;} public void CretaPart1 () {_ house. creatBeams ();} public void CretaPart2 () {_ house. creatFence ();} public void CretaPart3 () {_ house. creatFoundation ();} public void CretaPart4 () {_ house. creatRoof () ;}/// <summary> // instructor // </summary> public class Director {public void Build (IBuilder builder) {builder. cretaPart1 (); builder. cretaPart2 (); builder. cretaPart3 (); builder. cretaPart4 () ;}} public class House {/// <summary> // roof // </summary> public void CreatRoof () {}/// <summary> /// Housing Beam /// </summary> public void CreatBeams () {}/// <summary> /// barrier /// </summary> public void CreatFence () {}/// <summary> /// Foundation /// </summary> public void CreatFoundation (){}

 6. Prototype (Prototype)

The prototype is characterized by "copying" an existing instance to return a new instance, rather than creating a new instance. The principle is to pass a prototype object to the object to be created, the object to be created requests the prototype object. To replicate itself, and I don't feel very familiar with this.

 

/// <Summary> /// define the Student Score /// </summary> public interface IStudent {IStudent Clone (); /// <summary> // Chinese // </summary> int Chinese {get; set ;} /// <summary> /// school /// </summary> int Mathematics {get; set ;} /// <summary> // English // </summary> int English {get; set ;}} /// <summary> // first-year students /// </summary> public class FirstGrade: IStudent {// <summary> // Chinese // </summary> private int _ Chinese; /// <summary> /// Mathematics /// </summary> private int _ Mathematics; /// <summary> // English // </summary> private int _ English; public IStudent Clone () {return (IStudent) this. memberwiseClone ();} public int Chinese {get {return _ Chinese;} set {_ Chinese = value ;}} public int Mathematics {get {return _ Mathematics ;} set {_ Mathematics = value ;}} public int English {get {return _ English ;}set {_ English = value ;}}}

(Hope to help you, haha)

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.