Design mode (created)

Source: Internet
Author: User
Tags abstract ticket
One: The overall summary of the creation design pattern

Second: Specific design Patterns

1, factory method model

1) Definition:

Define an interface for creating objects, and let subclasses decide which classes to instantiate

2) Advantages:

Overcomes the shortcomings of the simple factory violating the open closure principle, and preserves the advantages of the object creation process.

3) class diagram

2, Abstract factory

1) Definition:

Provides an interface to create a series of related or interdependent objects without specifying their specific class

2) Advantages:

Let the concrete creation process and the client separate, the client is through their abstract interface to manipulate the instance, the specific class name of the product is also separated by the implementation of the specific factory, will not appear in the client code

3) class diagram

3, single-case mode

1) Definition:

Ensure that a class has only one instance and provides a global access point to access it

4, prototype mode

1) Definition:

Specify the kind of object created with the prototype instance and create a new object by copying the prototype instances

2) class diagram

5, builder mode

1) Definition:

Separating the construction of a complex object from his presentation allows the same build process to create different representations

2) class diagram

Three: Code implementation

The Chinese New Year is coming, many students buy fire tickets to go home online, sometimes they can not buy tickets. Now take train tickets as an example, using the idea of design patterns, to practice the consolidation of design patterns.

1, Simple factory

If, for a good ride, the student ticket fare is 100, the adult fare is 200, it is required to simply show the ticket type extremely priced

Class Diagram:

Code implementation:

Using System;
Using System.Collections.Generic;
Using System.Linq;

Using System.Text; Namespace train ticket {class Program {static void Main (string[] args) {Ticket Ticket = Fact  Oryticket.createticket ("Student Ticket"); Pass in the parameter "Student Ticket", let train ticket factory to instantiate the student ticket class ticket of the object.
 
            GetTicket ();
            Ticket Ticket2 = factoryticket.createticket ("adult ticket"); Ticket2.
 
            GetTicket ();
        Console.read (); }}//Train ticket factory class Factoryticket {public static Ticket Createticket (String type)//According to the type provided, to instantiate the specific
            The train ticket Object {Ticket Ticket = null;  Switch (type) {case "Student Ticket": ticket = new Studentticket ();
                If it is a student ticket, return the student tickets category break;  Case "Adult Ticket": ticket = new Generalticket ();
            If it is an adult ticket, return the adult tickets category break;
        } return ticket;
       }}//train ticket base class Ticket { private string money;
            public string Money {get {return money;}
        set {money = value;}
    } public virtual void GetTicket ()//Print Get train ticket type and price {}}//Student Ticket class Studentticket:ticket
        
        {private String money = "100"; public override void GetTicket () {Console.
        WriteLine ("You have selected a student ticket, the fare is: {0} yuan", money);
        }}//Adult ticket class Generalticket:ticket {private String money = "200";
        public override void GetTicket () {Console.WriteLine ("You select an adult ticket, the fare is: {0} yuan", money); }
    }
}


Operation Result:



2, Abstract factory

Suppose the person who buys the train ticket has only the name of a property, which is required to display the purchase train ticket or adult ticket.

Class diagram

Class Diagram:


Code implementation:

Using System;
Using System.Collections.Generic;
Using System.Linq;

Using System.Text; Namespace train ticket {class Program {static void Main (string[] args) {People li = new peop
            Le ("li");//Instantiate the person who purchased the train ticket Li Ifactoryticket factory = new Stuticketfactory (); Iticket Iticket = Factory.
            Createticket (); Iticket.

            GetTicket ();


        Console.read ();
    }}//Train ticket Factory interface, define an abstract factory interface for creating train tickets interface Ifactoryticket {iticket createticket (); }//Implement Ifactoryticket interface, instantiate Studentticket class Stuticketfactory:ifactoryticket {public Iticket Crea
        Teticket () {return new Studentticket ();  }}//Implement Ifactoryticket interface, instantiate Generalticket class Genticketfactory:ifactoryticket {public iticket
        Createticket () {return new Generalticket (); }}//train ticket base class interface Iticket {void GetTicket ();//PrintThe type and price of the train ticket}//Student Ticket class Studentticket:iticket {public void GetTicket () { Console.
        WriteLine ("You have selected a student ticket");
            }}//Adult ticket class Generalticket:iticket {public void GetTicket () {
        Console.WriteLine ("You have selected an adult ticket");
        }}//define the Human class people {private string name that purchased the train ticket;
            public string Name {get {return Name;}
        set {name = value;}
        Public people () {} public people (string name) {this.name = name; }
    }
}


Operation Result:



3, prototype mode

Suppose the ticket only shows the name of the person who purchased the ticket, the type of ticket (Student ticket, adult ticket), the student ticket and the adult ticket, the only one student ticket, one adult ticket (the number of tickets printed, the prototype model is convenient).

Use prototype mode to create different train tickets by copying prototypes.


Class Diagram:



Code implementation:

Namespace train ticket
{
    class program
    {
        static void Main (string[] args)
        {
            Ticket ticket1 = new Ticket (" Li ");
            Ticket1. Type ("Student Ticket");
            Ticket1. GetTicket ();


            Ticket Ticket2 = new Ticket ("Lisz");
            Ticket2. Type ("adult ticket");
            Ticket2. GetTicket ();


            Console.read ();
                     
        }
    }


    Class Ticket:icloneable
    {
        private string username;
        private string type;

        Public Ticket (string username)
        {
            this.username = username;
        }
        public void type (string type)
        {
            this.type = type;
        }
        public void GetTicket ()
        {
            Console.WriteLine ("{0}: You purchased {1}", username, type);
        }

        public Object Clone ()
        {
            Return (object) this. MemberwiseClone ();}}}



Operation Result:



4, single-case mode

A user can only purchase a ticket for a train, if the time period is determined, the train is determined.

Double Lock, code implementation:

namespace train Ticket {class Program {static void Main (string[] args) {
            Ticket Ticket = Ticket.getticket ();

            Ticket Ticket1 = Ticket.getticket ();
            if (ticket = = Ticket1) {Console.WriteLine ("Two objects are the same instance");
        } console.read ();
        }} class Ticket {private static Ticket instance;
        private static readonly Object syncRoot = new Object ();//When the program runs, create a static read-only process helper object private string username; Public Ticket () {} public static Ticket GetTicket () {if (instance = = NULL)//First determine if the instance exists, there is no
                    Lock Processing {lock (SyncRoot) {if (instance = = null)
                    {instance = new Ticket ();
        }}} return instance; }
    }


}

Four: summary Applicability

1, factory method model

Factory mode for the production of stationary products in the same grade structure

When the class does not know the class of the object that he created

2, abstract factory model

Abstract Factory mode is used to produce all products of different product families.

When you want to emphasize a series of related product object designs for joint use

When providing a product class library and just want to show their interface instead of implementing it

3, single-case mode

When a class can only be made by one instance, and the customer can access it from a well-known access point

When the only instance is instantiated by subclasses and can be extended without the need for code changes

4, prototype mode

When the class to instantiate is specified at run time

When an instance of a class can only be one of several different combinations of states

5, builder mode

When creating complex objects, the algorithm is independent of the part of the object

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.