先看下别人的例子:
一.概念
定义了创建对象的接口,但有子类决定要实例化的类是哪一个。工厂方法把类实例化推迟到了子类(这个是GOF上的定义,总感觉定义的不好)。
二.实例分析
CPU
- package com.zzy.factoryMethod;
- /**
- * CPU接口
- * @author eason
- *
- */
- public interface CPU {
- public void work();
- }
CPUFactory
interface CUPFactory {
public CUP creatCPU();
}
WDCPU/SamSungCPU/SeagateCPU类似,只贴出一个
- package com.zzy.factoryMethod;
- /**
- * 西数CPU
- * @author eason
- *
- */
- public class WDCPU implements CPU{
- @Override
- public void work() {
- System.out.println("西数CPU is working...");
- }
- }
WDCPUFactory/SamSungCPUFactory/SeagateCPUFactory类似,只贴出一个
- package com.zzy.factoryMethod;
- /**
- * 三星CPU工厂
- * @author eason
- *
- */
- public class WDCPUFactory implements CPUFactory{
- public CPU createCPU() {
- return new WDCPU();
- }
- }
TestFactoryMethod
- package com.zzy.factoryMethod;
- /**
- * 测试类
- * @author eason
- *
- */
- public class TestFactoryMethod {
- public static void main(String[] args) {
- CPUFactory factory = new WDCPUFactory();
- CPU cpu = factory.createCPU();
- cpu.work();
- }
- }
三.工厂方法模式的产品等级与产品族
- 工厂模式:用来生产同一等级结构中的固定产品。
-
工厂方法是针对每一种产品提供一个工厂类。通过不同的工厂实例来创建不同的产品实例。
我们再来看个例子:
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 **/