zoukankan      html  css  js  c++  java
  • 抽象工厂

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


  • 相关阅读:
    LocalSessionFactoryBean有几个属性查找hibernate映射文件
    关于Spring中配置LocalSessionFactoryBean来生成SessionFactory
    【Spring源码分析】配置文件读取流程
    Java序列化接口的作用总结1
    Java序列化接口的作用总结
    hibernate抓取策略
    170531、FormData 对象的使用
    170530、java 迭代hashmap常用的三种方法
    170529、springMVC 的工作原理和机制
    170526、spring 执行定时任务
  • 原文地址:https://www.cnblogs.com/YUJIE666/p/9898474.html
Copyright © 2011-2022 走看看