抽象工厂
2018-11-02 20:54:09 开始写
ifactory.java类(顶层)
1 package abstractfactory; 2 3 public interface ifactory { 4 public iuser createuser(); 5 public idepartment createdepartment(); 6 }
iuser.java类(下层)
1 package abstractfactory; 2 3 public interface iuser { 4 public void insert(); 5 public void getbyid(); 6 }
idepartment.java类(下层)
1 package abstractfactory; 2 3 public interface idepartment { 4 public void insert(); 5 public void getdepartmentbyid(); 6 }
accessuser.java类
1 package abstractfactory; 2 3 public class accessdepartment implements idepartment { 4 5 @override 6 public void insert() { 7 // todo auto-generated method stub 8 system.out.println("insert department into sqlserver."); 9 } 10 11 @override 12 public void getdepartmentbyid() { 13 // todo auto-generated method stub 14 system.out.println("get department in ssqlserver by id."); 15 } 16 17 }
accessdepartment.java类
1 package abstractfactory; 2 3 public class accessdepartment implements idepartment { 4 5 @override 6 public void insert() { 7 // todo auto-generated method stub 8 system.out.println("insert department into sqlserver."); 9 } 10 11 @override 12 public void getdepartmentbyid() { 13 // todo auto-generated method stub 14 system.out.println("get department in ssqlserver by id."); 15 } 16 17 }
accessfactory.java类
1 package abstractfactory; 2 3 public class accessdepartment implements idepartment { 4 5 @override 6 public void insert() { 7 // todo auto-generated method stub 8 system.out.println("insert department into sqlserver."); 9 } 10 11 @override 12 public void getdepartmentbyid() { 13 // todo auto-generated method stub 14 system.out.println("get department in ssqlserver by id."); 15 } 16 17 }
factorytest.java类
1 package abstractfactory; 2 3 public class factorytest { 4 5 public static void main(string[] args) { 6 // todo auto-generated method stub 7 ifactory factory = new accessfactory(); 8 iuser user = factory.createuser(); 9 idepartment department = factory.createdepartment(); 10 user.insert(); 11 user.getbyid(); 12 department.insert(); 13 department.getdepartmentbyid(); 14 } 15 16 }
说明:
需要一个*接口、两个下层接口、两个实现下层接口的具体实现的类、1个顶层接口具体实现的类(用于创建下层接口具体实现的类)、主方法调用
ifactory factory = new accessfactory();向上转型1次(顶层接口)(顶层接口中有两个抽象方法,下层的向上转型是同级别的)
iuser user = factory.createuser();向上转型1次(下层接口)
idepartment department = factory.createdepartment();向上转型1次(下层接口)
user.insert();最后调用子类方法返回结果
user.getbyid();最后调用子类方法返回结果
department.insert();最后调用子类方法返回结果
department.getdepartmentbyid();最后调用子类方法返回结果
谢谢、thank you、salamat do(撒拉玛特朵)、あリがcám o*n(嘉蒙)とゥ(阿里嘎都)、감사합니다 (勘三哈咪瘩)、terima kasih(得力马卡系)、kob-khun(寇布库恩)、dhanyavaad(达尼阿瓦德)、toda(透达)、te?ekkür ederim(特谢库尔埃戴里姆)、dzie,kuje(以库以而)、spasibo(思巴喜柏)、danke(但可)、shokran(休克朗)、obrigada(欧布哩嘎搭)、dank u(当q)、merci(梅呵西)、gracias(格拉喜亚思)、grazie(格啦姬)、благодарности、tak、kiitos、díky、hvala、mul?umesc、takk、ευχαριστ