设计模式之"工厂方法"模式

先看下别人的例子:

一.概念

     定义了创建对象的接口,但有子类决定要实例化的类是哪一个。工厂方法把类实例化推迟到了子类(这个是GOF上的定义,总感觉定义的不好)。

 

二.实例分析

 

 

     CPU

Java代码   收藏代码
  1. package com.zzy.factoryMethod;  
  2. /** 
  3.  * CPU接口 
  4.  * @author eason 
  5.  * 
  6.  */  
  7. public interface CPU {  
  8.     public void work();  
  9. }  

 

    CPUFactory

interface CUPFactory { 
  public CUP creatCPU();
} 

     WDCPU/SamSungCPU/SeagateCPU类似,只贴出一个

Java代码   收藏代码
  1. package com.zzy.factoryMethod;  
  2. /** 
  3.  * 西数CPU 
  4.  * @author eason 
  5.  * 
  6.  */  
  7. public class WDCPU implements CPU{  
  8.   
  9.     @Override  
  10.     public void work() {  
  11.         System.out.println("西数CPU is working...");  
  12.     }  
  13.   
  14. }  

 

     WDCPUFactory/SamSungCPUFactory/SeagateCPUFactory类似,只贴出一个

Java代码   收藏代码
  1. package com.zzy.factoryMethod;  
  2. /** 
  3.  * 三星CPU工厂 
  4.  * @author eason 
  5.  * 
  6.  */  
  7. public class WDCPUFactory implements CPUFactory{  
  8.     public CPU createCPU() {  
  9.         return new WDCPU();  
  10.     }  
  11. }  

 

     TestFactoryMethod

Java代码   收藏代码
  1. package com.zzy.factoryMethod;  
  2. /** 
  3.  * 测试类 
  4.  * @author eason 
  5.  * 
  6.  */  
  7. public class TestFactoryMethod {  
  8.     public static void main(String[] args) {  
  9.         CPUFactory factory = new WDCPUFactory();  
  10.         CPU cpu = factory.createCPU();  
  11.         cpu.work();  
  12.     }  
  13. }  
 

三.工厂方法模式的产品等级与产品族

  1. 工厂模式:用来生产同一等级结构中的固定产品。
  2. 工厂方法是针对每一种产品提供一个工厂类。通过不同的工厂实例来创建不同的产品实例。



    我们再来看个例子:

    interface Cycle { 
      int wheels(); 
    } 
     
    interface CycleFactory { 
      Cycle getCycle(); 
    } 
     
    class Unicycle implements Cycle { 
      public int wheels() { return 1; } 
    } 
     
    class UnicycleFactory implements CycleFactory { 
      public Unicycle getCycle() { return new Unicycle(); } 
    } 
     
    class Bicycle implements Cycle { 
      public int wheels() { return 2; } 
    } 
     
    class BicycleFactory implements CycleFactory { 
      public Bicycle getCycle() { return new Bicycle(); } 
    } 
     
    class Tricycle implements Cycle { 
      public int wheels() { return 3; } 
    } 
     
    class TricycleFactory implements CycleFactory { 
      public Tricycle getCycle() { return new Tricycle(); } 
    
    } 
     
    public class Test { 
      public static void ride(CycleFactory fact) { 
         Cycle c = fact.getCycle(); 
         System.out.println("Num. of wheels: " + c.wheels()); 
      } 
      public static void main(String[] args) { 
        ride(new UnicycleFactory()); 
        ride(new BicycleFactory ()); 
        ride(new TricycleFactory ()); 
      } 
    }
    /*
    Num. of wheels: 1
    Num. of wheels: 2
    Num. of wheels: 3
    **/


    我们使用工厂方法来创建一个框架,它可以执行抛硬币和掷骰子功能:

    interface Tossing { boolean event(); } 
     
    interface TossingFactory { Tossing getTossing(); } 
     
    class CoinTossing implements Tossing { 
      private int events; 
      private static final int EVENTS = 2; 
      public boolean event() { 
        System.out.println("Coin tossing event " + events); 
        return ++events != EVENTS; 
      } 
    } 
     
    
    class CoinTossingFactory implements TossingFactory { 
      public CoinTossing getTossing() { 
        return new CoinTossing(); 
      } 
    } 
     
    class DiceTossing implements Tossing { 
      private int events; 
      private static final int EVENTS = 6; 
      public boolean event() { 
        System.out.println("Dice tossing event " + events); 
        return ++events != EVENTS; 
      } 
    } 
     
    class DiceTossingFactory implements TossingFactory { 
      public DiceTossing getTossing() { 
        return new DiceTossing(); 
      } 
    } 
     
    public class c { 
      public static void simulate(TossingFactory fact) { 
        Tossing t = fact.getTossing(); 
        while(t.event()) 
          ; 
      } 
      public static void main(String[] args) { 
        simulate(new CoinTossingFactory()); 
        simulate(new DiceTossingFactory()); 
      } 
    } /* Output: 
    Coin tossing event 0 
    Coin tossing event 1 
    Dice tossing event 0 
    Dice tossing event 1 
    Dice tossing event 2 
    Dice tossing event 3 
    Dice tossing event 4 
    Dice tossing event 5 
    **/



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值