[Big talk design model] -- three sisters in the factory and three sisters in the big talk Design Model

Source: Internet
Author: User

[Big talk design model] -- three sisters in the factory and three sisters in the big talk Design Model

Recently, I 've been watching the design model. It's fun to have a story, but there are also a lot of code. Sometimes I don't quite understand it. To make myself interested and stick to it, after reading it for the first time at a macro level, we classified the 23 design patterns, and then looked for similar patterns to learn together, and understood them through comparative learning, it is indeed easier. The three sisters of the factory in the first model of creation, learn the three models in one, compare their advantages and disadvantages, and know when to use the mode.


I. Introduction to concepts

1. Simple Factory:Simple Factory is a product instance created by a Factory object. It is the simplest and Practical Model in the Factory model family.


Personal Understanding:Separate the interface from the business logic and separate a class to create an instance.


2. Factory method:Factory Method: defines an interface used to create objects, so that the subclass determines which class to instantiate. The Factory Method delays the instantiation of a class to its subclass.


Personal Understanding:In order to follow the extended open and closed principle, the simple factory class is converted into a factory method interface, and its Switch branch is separated into a subclass to inherit the factory method interface class, smoothly delays the process of creating objects to sub-classes.


3. Abstract Factory:Abstract Factory provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.


Personal Understanding:With the transformation of the factory method mode, the abstract factory is reconstructed by adding the factory method interface.


Ii. instance description (taking database access as an example)
1. Simple Factory


Create a simple factory to create the instantiated User class and Department class. You can directly obtain the database Access instance in the client code without relying on Sqlserver and Access database.
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


Three factories are required to separate the business logic from the data access. The client code does not need to know which database to access when running.
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


The tables in the accessed database are added. You only need to add the corresponding classes and interfaces and add the corresponding object creation methods in the ifacloud interface.
Client code (Similar to the factory Method ):
Static void Main (string [] args) {User user = new User (); Department department = new Department (); Ifactory factory = new AccessFactory (); // determine which database access object is instantiated to the factory IUser su = factory. createUser (); // The dependency su is removed from the specific database. insert (user); su. getUser (1); IDepartment ide = factory. createDepartment (); ide. insert (department); ide. getDepartment (343); Console. read ();}

Iii. Comparison
1. Simple factory VS factory Method
Similarities:The separation of business logic and access data is achieved.
Differences:
(1) simple factory: The instantiation of objects in the factory requires the use of Switch for conditional selection, which violates the principle of modifying and disabling, but for the client, the dependency on specific products is removed.
(2) Factory method: delay the instantiation process to the subclass. The factory method transfers the internal logic judgment to the client. You only need to modify the client. The principles of openness and closures are followed.
2. Advantages of abstract factory VS disadvantages
Advantages:When changing a product series, you only need to change the specific factory. There are no other changes, and they are irrelevant to the specific product. Follow the principle of openness-ending the original and dependency reversal.
Disadvantages:If you add a function, you need to add the corresponding classes and interfaces, and change the Ifactory factory, SqlserverFactory, and AccessFactory factory classes.
4. At the beginning, I felt that some of the points were still not quite understandable. When the summary was about drawing, I suddenly understood some of them. It seems that the role of drawing was great, when learning the design pattern, you need to draw pictures and work on the Code. This will be better understood. Disadvantages:These three models have been studied for a long time and have been lost in terms of macro learning. Next, adjust the strategy.

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.