JAVA设计模式学习之----创建模式:工厂模式(抽象工厂)

 前面学习了工厂方法,那么现在假如有多个Person接口,如Person,Person2,实现两个接口的类分别为Zhangshan,Zhansi;Wangwu,Wangliu;那么这个时候工厂类应该怎么设计呢?
当然可以分别设计两个工厂类来实现,不过因为他们有一定的关联性,所以我们应该将原来的工厂类抽象成抽象工厂,然后分别对这个类有两个实现类,在这两个实现的工厂类里面就可以分别产生出两组对象了。如下:
Person类:

  1. package com.foactory.eus.interfaces;
  2. public   interface  Person {
  3.      public   static  final String className =  "Person" ;
  4.     String getName();
  5.      int  getAge();
  6. }


Person2类:

  1. package com.foactory.eus.interfaces;
  2. public   interface  Person2 {
  3.      public   static  final String className =  "Person2" ;
  4.     String getName();
  5.      int  getAge();
  6. }

他们的实现类分别为:
Zhangshan类:

  1. package com.foactory.eus.implement;
  2. import com.foactory.eus.interfaces.Person;
  3. public   class  Zhangshan implements Person{
  4.      public  String getName() {
  5.         System. out .println( "this is zhangshan is name" );
  6.          return   "" ;
  7.     }
  8.      public   int  getAge() {
  9.          return  0;
  10.     }
  11. }


Zhangsi类:

  1. package com.foactory.eus.implement;
  2. import com.foactory.eus.interfaces.Person;
  3. public   class  Zhangsi implements Person {
  4.      public   int  getAge() {
  5.          return  0;
  6.     }
  7.      public  String getName() {
  8.         System. out .println( "this is zhangsi is name" );
  9.          return   null ;
  10.     }
  11. }


Wangwu类:

  1. package com.foactory.eus.implement;
  2. import com.foactory.eus.interfaces.Person2;
  3. public   class  Wangwu implements Person2{
  4.      public  String getName() {
  5.         System. out .println( "this is Wangwu is name" );
  6.          return   "" ;
  7.     }
  8.      public   int  getAge() {
  9.          return  0;
  10.     }
  11. }


Wangliu类:

  1. package com.foactory.eus.implement;
  2. import com.foactory.eus.interfaces.Person2;
  3. public   class  Wangliu implements Person2{
  4.      public  String getName() {
  5.         System. out .println( "this is Wangliu is name" );
  6.          return   "" ;
  7.     }
  8.      public   int  getAge() {
  9.          return  0;
  10.     }
  11. }


工厂类:
PersonFactory:

  1. package com.foactory.eus.factory;
  2. import com.foactory.eus.interfaces.Person;
  3. import com.foactory.eus.interfaces.Person2;
  4. public   abstract   class  PersonFactory {
  5.     
  6.      public   abstract  Person instance();
  7.     
  8.      public   abstract  Person2 instance1();
  9.     
  10. }


实现工厂类:
Person1Factory:

  1. package com.foactory.eus.factory;
  2. import com.foactory.eus.implement.Wangwu;
  3. import com.foactory.eus.implement.Zhangshan;
  4. import com.foactory.eus.interfaces.Person;
  5. import com.foactory.eus.interfaces.Person2;
  6. public   class  Person1Factory extends PersonFactory{
  7.      public  Person instance() {
  8.          return   new  Zhangshan();
  9.     }
  10.      public  Person2 instance1() {
  11.          return   new  Wangwu();
  12.     }
  13.     
  14.     
  15. }


Person2Factory:

  1. package com.foactory.eus.factory;
  2. import com.foactory.eus.implement.Wangliu;
  3. import com.foactory.eus.implement.Zhangsi;
  4. import com.foactory.eus.interfaces.Person;
  5. import com.foactory.eus.interfaces.Person2;
  6. public   class  Person2Factory extends PersonFactory{
  7.      public  Person instance() {
  8.          return   new  Zhangsi();
  9.     }
  10.      public  Person2 instance1() {
  11.          return   new  Wangliu();
  12.     }
  13.     
  14.     
  15. }

现在针对上面的工厂设计一测试类:

  1. import com.foactory.eus.factory.Person1Factory;
  2. import com.foactory.eus.factory.Person2Factory;
  3. import com.foactory.eus.interfaces.Person;
  4. import com.foactory.eus.interfaces.Person2;
  5. public   class  Test {
  6.     
  7.      public   static   void  main(String[] args){
  8.         Person1Factory pf1 =  new  Person1Factory();
  9.         Person p1 = pf1.instance();
  10.         Person2 p2 = pf1.instance1();
  11.         p1.getName();
  12.         p2.getName();
  13.         
  14.         Person2Factory pf2 =  new  Person2Factory();
  15.         Person p3 = pf2.instance();
  16.         Person2 p4 = pf2.instance1();
  17.         p3.getName();
  18.         p4.getName();
  19.     }
  20.     
  21. }

结果为:

  1. this   is  zhangshan  is  name
  2. this   is  Wangwu  is  name
  3. this   is  zhangsi  is  name
  4. this   is  Wangliu  is  name

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值