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、Ευχαριστ