大家好,现在咱们来讨论一下抽象工厂模式吧。还是以我之前给的例子(详情可以看之前的简单工厂)但是现在要做出一点假设:
假设 苹果公司和小米公司都有生产 Phone,MP3之类的...(纯粹是为了方便说明)
好了,现在说说产品等级结构和产品族的概念
所谓的产品等级结构:就是指某一类产品的继承结构,比如说电话,它包括IPhone电话和XiaoMi电话,他们就是属于同一个产品等级结构
所谓的产品族:就是指处于不同产品等级结构的一组产品,且是由同一个工厂产生的。比如说 Apple公司既可以生产IPhone,也可以生产IPOD,而小米公司既可以生产XiaoMi手机,也可以生产XiaoMiPod(假设...)
好了,现在让我们来看看UML图吧:
现在看看代码吧
package edu.fjnu.cs.hwb.abstractfactory; public interface Phone { void call(); }
package edu.fjnu.cs.hwb.abstractfactory; public interface Pod { void play(); }
package edu.fjnu.cs.hwb.abstractfactory; public interface AbstractFactory { Phone getPhone(String name); Pod getPod(String name); }
首先是Apple公司的产品
package edu.fjnu.cs.hwb.abstractfactory; public class IPhone4 implements Phone { @Override public void call() { System.out.println("this is IPhone4"); } }
package edu.fjnu.cs.hwb.abstractfactory; public class IPhone4s implements Phone { @Override public void call() { System.out.println("this is IPhone4s"); } }
package edu.fjnu.cs.hwb.abstractfactory; public class IPod1 implements Pod { @Override public void play() { System.out.println("this is IPod1"); } }
package edu.fjnu.cs.hwb.abstractfactory; public class IPod2 implements Pod { @Override public void play() { System.out.println("this is IPod2"); } }
package edu.fjnu.cs.hwb.abstractfactory; public class XiaoMi2 implements Phone { @Override public void call() { System.out.println("this is XiaoMi2"); } }
package edu.fjnu.cs.hwb.abstractfactory; public class XiaoMi2S implements Phone { @Override public void call() { System.out.println("this is XiaoMi2S"); } }
package edu.fjnu.cs.hwb.abstractfactory; public class XiaoMiPod1 implements Pod { @Override public void play() { System.out.println("this is XiaoMiPod1"); } }
package edu.fjnu.cs.hwb.abstractfactory; public class XiaoMiPod2 implements Pod { @Override public void play() { System.out.println("this is XiaoMiPod2"); } }
package edu.fjnu.cs.hwb.abstractfactory; public class AppleFactory implements AbstractFactory { @Override public Phone getPhone(String name) { if ("IPhone4".equalsIgnoreCase(name)) return new IPhone4(); else if ("IPhone4s".equalsIgnoreCase(name)) return new IPhone4s(); return null; } @Override public Pod getPod(String name) { if ("IPod1".equalsIgnoreCase(name)) return new IPod1(); else if ("IPod2".equalsIgnoreCase(name)) return new IPod2(); return null; } }
package edu.fjnu.cs.hwb.abstractfactory; public class XiaoMiFactory implements AbstractFactory { @Override public Phone getPhone(String name) { if ("XiaoMi2".equalsIgnoreCase(name)) return new XiaoMi2(); else if ("XiaoMi2s".equalsIgnoreCase(name)) return new XiaoMi2S(); return null; } @Override public Pod getPod(String name) { if ("XiaoMiPod1".equalsIgnoreCase(name)) return new XiaoMiPod1(); else if ("XiaoMiPod2".equalsIgnoreCase(name)) return new XiaoMiPod2(); return null; } }看看客户端程序员怎么使用:
package edu.fjnu.cs.hwb.abstractfactory; public class Client { public static void main(String[] args) { AbstractFactory abstractFactory = new AppleFactory(); Phone phone = abstractFactory.getPhone("Iphone4"); phone.call(); phone = abstractFactory.getPhone("Iphone4s"); phone.call(); Pod pod = abstractFactory.getPod("Ipod1"); pod.play(); pod = abstractFactory.getPod("Ipod2"); pod.play(); System.out.println(); abstractFactory = new XiaoMiFactory(); phone = abstractFactory.getPhone("XiaoMi2"); phone.call(); phone = abstractFactory.getPhone("XiaoMi2s"); phone.call(); pod = abstractFactory.getPod("XiaoMiPod1"); pod.play(); pod = abstractFactory.getPod("XiaoMiPod2"); pod.play(); } }结果如下:
好了..代码虽然多,但是很简单的..
如果有错误,希望大家指正出来
原文:http://blog.csdn.net/hwb1992/article/details/22760185