Design Mode-factory method mode, design mode factory Mode

Source: Internet
Author: User

Design Mode-factory method mode, design mode factory Mode

Scenario Description:

Assume that there is a User class (as shown below). The original design program uses SqlServer for Access. Now, Access is required due to changes in customer requirements.

public class User    {        private int id;        private string name;        public int ID        {               get { return id; }            set { id = value; }        }        public string Name        {            get { return name; }            set { name = value; }        }    }

In this scenario, you can simply modify the database Access code and change the SQL server hosts class to the Access hosts class, which will not only cause many problems, because the two database query statements are different, the most important thing is that the scalability is very poor. If you need to change it to MySql for access later, it is also a change. The factory method is used here.
First define a User object interface

// User object interface class public interface IUser {// business logic void Insert (User user User); User GetUser (int I );}

The User objects implemented by the two databases are inherited from the IUser interface.

// Sqldatabase User object public class SqlUser: IUser {public void Insert (User user User) {Console. writeLine ("InsertSqlUser");} public User GetUser (int I) {Console. writeLine ("SqlUserList"); return null ;}// Access database User object public class AccessUser: IUser {public void Insert (User user User) {Console. writeLine ("InsertAccessUser");} public User GetUser (int I) {Console. writeLine ("AccessUserList"); return null ;}}

Define a factory Interface

// Factory interface ifacloud {// inheritance class can generate different User objects IUser CreateUser () according to different requirements ();}

The specific factory generates different User objects according to different requirements.

// Sqldatabase factory public class SqlFactory: ifacloud {// User object accessing sqldatabase public IUser CreateUser () {return new SqlUser () ;}// Access Database factory public class AccessFactory: ifacloud {// The User object accessing the Access database public IUser CreateUser () {return new AccessUser ();}}

Client implementation

Public static void Main (string [] args) {User user User = new User (); // instantiate the factory object ifac1_factory = new AccessFactory () of different databases based on different requirements (); // decoupling business logic from database access. The iu object does not need to know which database the IUser iu = factory is generated. createUser (); iu. insert (user); iu. getUser (1 );}

UML class diagram

Factory method mode

The factory method mode is an extension of the simple factory mode. First, define an interface for creating objects, and then generate a subclass to determine which class to instantiate, the factory method delays the instantiation of a class to its subclass.

When implementing the factory method mode, the client needs to decide which specific factory to instantiate. That is to say, the factory method moves the internal logic judgment of the simple factory to the client code. You want to add functions, the factory class was changed, but now the client is modified.

Advantages and disadvantages of factory method mode

Advantages: the customer service violates the open-closed principle in a simple factory, and maintains the advantages of the encapsulated object creation process.
Disadvantage: each time a product is added, a specific product factory type needs to be added to increase the development workload.

 

This article is based on the big talk design model.

Related Article

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.