Differences between simple factory mode and abstract factory mode: interface-Oriented Programming

Source: Internet
Author: User

Let's take a look at the big talk design model and write down our own experiences and understanding: There are some differences between the simple factory model and the abstract factory model. The abstract factory model clearly reflects the idea of interface-oriented programming, demo of abstract factory in "big talk Design Model:

A user class:

Code
Public class user
{
Private string _ name;
Private int _ id;
Public string name
{
Set {_ name = value ;}
Get {return _ name ;}
}

Public int ID
{
Set {_ id = value ;}
Get {return _ id ;}
}

 

Then define two interfaces for creating a user:

 

Code
Interface ifacloud
{
Iuser createuser ();
}
Interface iuser
{
Void insertuser (User user );
User getuser (int id );
}

 

Next we describe the two classes that implement the interface:

Code
Class sqlserverfactory: ifacloud
{
Public iuser createuser ()
{
Return new sqlserveruser ();
}
}
Class acessfactory: ifacloud
{
Public iuser createuser ()
{
Return new accessuser ();
}
}
Public class sqlserveruser: iuser
{
Public void insertuser (User user)
{
Console. writeline ("insert a record to the user table in sqlserver! ");
}

Public user getuser (int id)
{
Console. writeline ("getting a record of the User table in sqlserver ");
Return NULL;
}
}

Public class accessuser: iuser
{
Public void insertuser (User user)
{
Console. writeline ("insert a record to the user table in access! ");
}

Public user getuser (int id)
{
Console. writeline ("get a record of the User table in access ");
Return NULL;
}
}

 

The presentation is also simple:

 

Code
Static void main (string [] ARGs)
{
User _ User = new user ();
_ User. ID = 1;
_ User. Name = "chunchill ";
Sqlserverfactory sqlfactory = new sqlserverfactory ();
Ifactory factory = new acessfactory ();
Iuser = factory. createuser ();
Iuser. insertuser (_ User );
Iuser. getuser (1 );
Console. Readline ();

}

If you want to change to the asqlserver creation mode, the method is also very simple:

You only need to change the code by a little bit:

Code
Ifactory factory = new sqlserverfactory ();

 

A simple and clear demo shows the powerful functions of the abstract factory model without explanation. Besides interface-oriented programming!

Easy to understand: provides a class design model diagram:

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.