Big talk Design Pattern _ abstract factory pattern

Source: Internet
Author: User

Take two database implementations of one method as an example.


First: Abstract Factory

Package COM. WZS. three;/*** big talk design mode -- page128 Abstract Factory ** @ author administrator **/public class abstractfactory {public static void main (string [] ARGs) {// ifactory factory = new sqlserverfactory (); ifactory factory = new accessfactory (); User user = new user (); iuser IU = factory. createuser (); IU. insert (User); IU. getuser (1); Department = new Department (); idepartment id = factory. createdepartment (); Id. insert (Department); Id. getdepartment (1) ;}}/** user */class user {private int ID; private string name; Public int GETID () {return ID ;} public void setid (int id) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name ;}}/** Department */class Department {private int ID; private string name; Public int GETID () {return ID;} public void setid (int id) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name ;}}/** Abstract Factory */interface ifactory {iuser createuser (); idepartment createdepartment ();} /** SQL factory */class sqlserverfactory implements ifacloud {@ overridepublic iuser createuser () {return New sqlserveruser () ;}@ overridepublic idepartment createdepartment () {return New sqlserverdepartment () ;}/ ** access factory */class accessfactory implements ifacloud {@ overridepublic iuser createuser () {return New accessuser ();} @ overridepublic idepartment createdepartment () {return New accessdepartment () ;}}/** user method abstract interface */interface iuser {void insert (User user ); user getuser (int id);}/** SQL user Method */class sqlserveruser implements iuser {@ overridepublic user getuser (INT ID) {system. out. println ("obtain the user record ID =" + ID + "in SQL Server. "); return NULL ;}@ overridepublic void insert (User user) {system. out. println ("Insert a new user record in SQL Server. ") ;}}/** access user Method */class accessuser implements iuser {@ overridepublic user getuser (int id) {system. out. println ("Get the user ID =" + ID + "in access. "); return NULL ;}@ overridepublic void insert (User user) {system. out. println ("Insert a new user record in access. ") ;}}/** Department abstract method interface */interface idepartment {void insert (Department); Department getdepartment (int id );} /** SQL Department Method */class sqlserverdepartment implements idepartment {@ overridepublic Department getdepartment (INT ID) {system. out. println ("obtain the department record ID =" + ID + "in SQL Server. "); return NULL ;}@ overridepublic void insert (Department) {system. out. println ("Insert a new department record in SQL Server. ") ;}}/** access Department Method */class accessdepartment implements idepartment {@ overridepublic Department getdepartment (int id) {system. out. println ("obtain the department record ID =" + ID + "in access. "); return NULL ;}@ overridepublic void insert (Department) {system. out. println ("Insert a new department record in access. ");}}

Second: Reflection + Abstract Factory

Package COM. WZS. two;/*** big talk design mode -- page128 reflection + Abstract Factory ** @ author administrator **/public class abstractfactory {public static void main (string [] ARGs) throws instantiationexception, illegalaccessexception, classnotfoundexception {// ifactory factory = new sqlserverfactory (); User user = new user (); iuser IU = dataaccess. createuser (); IU. insert (User); IU. getuser (1); Department = new Department (); idepartment id = dataaccess. createdepartment (); Id. insert (Department); Id. getdepartment (1) ;}} class dataaccess {Private Static string DB = "access"; public static iuser createuser () throws instantiationexception, illegalaccessexception, classnotfoundexception {return (iuser) class. forname ("com. WZS. two. "+ dB +" user "). newinstance ();} public static idepartment createdepartment () throws instantiationexception, illegalaccessexception, classnotfoundexception {return (idepartment) class. forname ("com. WZS. two. "+ dB +" Department "). newinstance () ;}} class datasqlserver {Private Static string DB = "sqlserver"; public static iuser createuser () throws instantiationexception, illegalaccessexception, classnotfoundexception {return (iuser) class. forname ("com. WZS. two. "+ dB +" user "). newinstance ();} public static idepartment createdepartment () throws instantiationexception, illegalaccessexception, classnotfoundexception {return (idepartment) class. forname ("com. WZS. two. "+ dB +" Department "). newinstance () ;}/ ** user */class user {private int ID; private string name; Public int GETID () {return ID;} public void setid (int id) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name ;}}/** Department */class Department {private int ID; private string name; Public int GETID () {return ID;} public void setid (int id) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name ;}}/** user method abstract interface */interface iuser {void insert (User user); User getuser (int id );} /** SQL user Method */class sqlserveruser implements iuser {@ overridepublic user getuser (INT ID) {system. out. println ("obtain the user record ID =" + ID + "in SQL Server. "); return NULL ;}@ overridepublic void insert (User user) {system. out. println ("Insert a new user record in SQL Server. ") ;}}/** access user Method */class accessuser implements iuser {@ overridepublic user getuser (int id) {system. out. println ("Get the user ID =" + ID + "in access. "); return NULL ;}@ overridepublic void insert (User user) {system. out. println ("Insert a new user record in access. ") ;}}/** Department abstract method interface */interface idepartment {void insert (Department); Department getdepartment (int id );} /** SQL Department Method */class sqlserverdepartment implements idepartment {@ overridepublic Department getdepartment (INT ID) {system. out. println ("obtain the department record ID =" + ID + "in SQL Server. "); return NULL ;}@ overridepublic void insert (Department) {system. out. println ("Insert a new department record in SQL Server. ") ;}}/** access Department Method */class accessdepartment implements idepartment {@ overridepublic Department getdepartment (int id) {system. out. println ("obtain the department record ID =" + ID + "in access. "); return NULL ;}@ overridepublic void insert (Department) {system. out. println ("Insert a new department record in access. ");}}

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.