"Big Talk design mode"--three sisters in the factory

Source: Internet
Author: User

Recently in the design mode, the story fun is fun, but the code is also a lot of ah, sometimes look at the picture is not very understanding, in order to let oneself have interest and persist, in the first time macroscopic looked at once, put 23 kinds of design pattern to carry on the classification, then find similar pattern study together, through comparative study again understanding, It's a little easier indeed. First learn to create a model of the factory Three sisters, a study of these three models, compare their pros and cons, know when to use what mode.

first, Concept introduction1, Simple factory: EasyFactory, is determined by a factory object to create a product class instance, is the factory model family of the simplest practical mode. Personal Understanding: separating the interface from the business logic and separating out a class dedicated to creating instances. 2, Factory methods:Factory method, defines an interface for creating objects, so that subclasses decide which class to instantiate, and the factory method defers the instantiation of a class to its subclasses. Personal Understanding: to follow the extended open, modify the shutdown principle, the simple factory class into the factory method interface, the switch branch into a subclass to inherit the factory method interface class, the process of creating objects smoothly deferred to the subclass. 3. Abstraction Factory: AbstractFactory, which provides an interface to create a series of related or interdependent objects without specifying their specific classes. Personal Understanding: The transformation of the factory method model, the method of increasing the interface of the factory method, reconstructs the abstract factory. Second, examples of interpretation (to access the DB as an example)
1, simple Factory


Create a simple factory that is used to create instantiated user classes and department classes. The database access instance is directly available in the client code and is not dependent on SQL Server and Access databases.
Client code:
static void Main (string [] args)        {            User user = new User ();            Department Department = new Department ();                        Iuser Su=dataaccess.createuser ();            Su. Insert (user);            Su. GetUser (1);            Idepartment IDE = Dataaccess.createdepartment ();            Ide. Insert (department);            Ide. Getdepartment (343);            Console.read ();        }



2, Factory Method


   It takes 3 factories to separate the business logic from the data access, and the client code does not need to know which database to access at run time.
Client code:
static void Main (string [] args)        {            User user = new User ();                        Ifactory factory=new accessfactory ();            Iuser su=factory. CreateUser ();            Su. Insert (user);            Su. GetUser (1);            Console.read ();        }


3. Abstract Factory


By adding the tables in the accessed database, simply add the corresponding classes and interfaces, and add the corresponding method to create the object in the Ifactory interface.
Client code ( similar to factory method ):
static void Main (string [] args)        {            User user = new User ();            Department Department = new Department ();                       Ifactory factory=new accessfactory ();//determines which database access object is instantiated to factory                       Iuser su=factory. CreateUser ();//The dependent SU is removed from the specific database.            Insert (user);            Su. GetUser (1);            Idepartment IDE = Factory. Createdepartment ();            Ide. Insert (department);            Ide. Getdepartment (343);            Console.read ();        }

Third, Comparison1. Simple Factory vs Factory methodThe same point: the separation of the business logic from the access data is achieved. different points:(1) Simple factory: Object instantiation in the factory, you need to use switch to make conditional selection, which violates the principle of modifying the closure, but for the client, it removes the dependence on the specific product. (2) Factory method: Defer the instantiation process to the subclass, the factory method transfers the internal logic judgment to the client, only needs to modify the client. Followed the open-closure principle. 2, abstract factory advantages vs Disadvantages Pros: when changing a product family, just change the specific factory, no other changes, not related to the specific product. Follows the principle of openness--the closed original and dependence reversal. Cons: If you add a function, add the corresponding class, interface, and also change the Ifactory factory, Sqlserverfactory and accessfactory three factory classes.
Iv. Feelingat the beginning of the feeling that some places are still a little less understanding, until the summary to draw when suddenly understand some, it seems that the role of drawing is big Ah, down to learn the design pattern when you want to draw while the code, so understanding will be better. deficiencies: The learning time for these three models has been a bit longer, lost macro. The next step is to make a good adjustment to the strategy.

"Big Talk design mode"--three sisters in the factory

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.