C # Design pattern-Simple Factory mode

Source: Internet
Author: User
Tags lenovo

First, simple factory model introduction:

The Simple Factory mode is a model of Create mode, also called Static Factory method (Factory mode). A simple factory model is a factory object that determines which product class instances are created. Simple Factory mode is the simplest and most practical mode in the factory model family, which can be understood as a special implementation of different factory patterns.

Second, the background:

In our daily programming, when using the new object, the class is dependent on this object, that is, the coupling between them is high, when the demand changes, we have to modify this type of source code, we can use the simple factory model to solve the problem.

Iii. Examples of situations:

In real life, we buy laptops are basically to go to the electronics store to buy, we want to what brand as long as and the salesman said they will give us, this is the real life of a simple factory example. All of the notebook electrical stores, electrical stores are equivalent to a simple factory parent class, each notebook brand is inherited from his sub-class, when we need to buy a notebook is equivalent to a new notebook class, we can go directly to the electronics store to buy, That is, through the electronics store factory parent class to new we need the notebook subclass. In this way we reduce the dependent subclass object, and we need to use the subclass to create it only through the factory class.

Four, the code implementation:

1, abstract out of the parent class (notebook Class), here directly abstract notebook This parent class more convenient to understand, equivalent to the above scenario in the electrical shop.

     Public Abstract class NoteBook    {        publicabstractvoid  SayHello ();    }

2. Each subclass inherits from the parent class (Lenovo brand and IBM brand)

 public  class   Lenovo:notebook { public  over Ride  void   SayHello () {CONSOLE.W Riteline (  "  I'm a Lenovo notebook, and you don't think about it.   " ); }    }
     Public class Ibm:notebook    {        publicoverridevoid  SayHello ()        {            Console.WriteLine ( " I am an IBM notebook " );        }    }

3, called when the subclass assignment to the parent class

       Static voidMain (string[] args) {Console.WriteLine ("Please enter the notebook brand you want"); stringBrand =Console.ReadLine (); NoteBook NB=Getnotebook (brand); nb.            SayHello ();        Console.readkey (); }         Public StaticNoteBook Getnotebook (stringbrand) {NoteBook NB=NULL; Switch(brand) { Case "Lenovo": NB =NewLenovo (); Break;  Case "IBM": NB =NewIBM (); Break;            }            returnNB; }

Five, Summary:

The simple factory model just moves the changes to the factory class, without changing the problem, because if we want a new notebook brand, we still need to modify the method in the factory class, that is, more case statements. This is the disadvantage of the simple factory model, and more than that, the entire system will be affected once the factory class is not working properly.

However, the simple factory model and the previous implementation also have its advantages:

1. The Simple Factory mode solves the problem that the client directly depends on the object, the client can eliminate the responsibility of directly creating the object, but only the consumer product. The simple factory model realizes the division of responsibility.

2, the simple factory model also played the role of code reuse, to buy a notebook owner only need to be responsible for consumption, do not need to implement in their own class, at this time the simple factory method let all customers share.

C # Design pattern-Simple Factory mode

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.