Traditional design mode (iv) factory model (simple factory, ordinary factory, abstract factory)

Source: Internet
Author: User
Tags abstract

About Factory mode and single case mode most of the 2 models are common

For example, in an ORM framework, factory patterns are often used to encapsulate the creation of a database, and we'll look at it in 3 different ways. Simple Factory mode abstract factory pattern

The abstract point is that the factory pattern provides an interface to create a series of interrelated or interdependent objects without specifying their specific classes. See examples directly

General inertial thinking that's what happens when we come across branch judgments.

public class Normalcase
    {
        private dbinstance dbinstance;
        Public normalcase (String type)
        {
              if (type). Equals ("SQL"))
            {
                dbinstance=  new Sqlinstance ();
            }
            else if (type. Equals ("Oracle"))
            {
                dbinstance = new Oracleinstance ();
            }
            else if (type. Equals ("Mysql"))
            {
                dbinstance = new Mysqlinstance ();
            }
        }
        public void ExecuteNonQuery ()
        {
            this.dbInstance.ExecuteNonQuery ();
        }
        public void ExecuteDataset ()
        {
            this.dbInstance.ExecuteDataset ();
        }
    }

So what's wrong with new, new technology is no fault, but the good design for the expansion of open and closed to the modification.

For interface programming, you can isolate a whole host of changes that might occur later in the system. If the code is written for an interface, then through polymorphism, it can implement the interface with any new class.

Let's look at how the factory model solves the problem

Let's take a look at the simple factory

public class Simplefactory {public dbinstance CreateInstance (string type) {DBInstance D
            i = null; if (type.
            Equals ("SQL")) {return new sqlinstance (); else if (type.
            Equals ("Oracle")) {return new oracleinstance (); else if (type.
            Equals ("Mysql")) {return new mysqlinstance ();
                
        return di;
        } public class Simplecase {simplefactory facotory;
        DBInstance di;
        Public Simplecase (Simplefactory facotory) {this.facotory = facotory; Public dbinstance CreateInstance (string type) {di = facotory.createinstance (ty
             PE);
    
        return di;
        public void ExecuteNonQuery () {this.di.ExecuteNonQuery (); } PubLIC void ExecuteDataset () {this.di.ExecuteDataset (); }
    
    }

To be exact, a simple factory is not a design pattern, but rather a programming habit. The case just moves the problem from one object to another, and the problem persists. But Simplefactory can have a lot of customers, wrapping the code that creates the instance into a class, and when it's time to change, just modify the class. Items are also being removed from the customer's code by the specific instantiation process.

Let me introduce the next two heavyweight models!

Factory method Mode

Public abstract class Facotorycase {dbinstance di;
            Public dbinstance CreateInstance (string type) {di = create (type);
        return di;
    
       Public abstract DBInstance Create (string type);
       public void ExecuteNonQuery () {this.di.ExecuteNonQuery ();
       public void ExecuteDataset () {this.di.ExecuteDataset ();
        } public class Facotorycasea:facotorycase {public override DBInstance Create (String type) {if (type.
            Equals ("SQL")) {return new sqlinstance (); else if (type.
            Equals ("Oracle")) {return new oracleinstance ();
        return null;
        } public class Facotorycaseb:facotorycase {public override DBInstance Create (String type) {if (type.
 Equals ("Mysql"))           {return new mysqlinstance ();
        return null; }
    }

The factory method pattern defines an interface to create an object, but the subclass determines which class is to be instantiated. The factory method lets classes defer instantiation to subclasses.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

Design principles: To rely on abstractions do not rely on specific classes.

And then look at the abstract factory pattern.

public class Param {} public class Sqlparm:param {} public class Oracleparam:param {} public class con
    
    
    Nection {} public class Sqlconnecttion:connection {} public class Oracleconnecttion:connection {}
        Public interface Abstractcase {Param getparameter ();
    Connection getconnection ();
        Public abstract class Dbinstanceforabstract {public Param p;
        public connection C;
        public abstract void ExecuteNonQuery ();
    public abstract void ExecuteDataset ();
        public class Dbinstanceforabstracta:dbinstanceforabstract {abstractcase ac;
        Public dbinstanceforabstracta (Abstractcase ac) {this.ac = AC; public override void ExecuteNonQuery () {p = AC.         
        GetParameter (); public override void ExecuteDataset () {c = AC.
              
        Getconnection ();
 }            
    public class Abstractcasea:abstractcase {dbinstanceforabstract di;
        Public Param GetParameter () {return new sqlparm ();
        Public Connection getconnection () {return new sqlconnecttion ();
        } public class Abstractcaseb:abstractcase {dbinstanceforabstract di;
        Public Param GetParameter () {return new Oracleparam ();
        Public Connection getconnection () {return new oracleconnecttion (); }
    }

The abstract factory pattern provides an interface for creating families of related or dependent objects without explicitly specifying specific classes.

Author:cnblogs bugs that stay up late

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.