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: