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. ");}}