1.概述
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
2.适用性
1)一个系统要独立于它的产品的创建、组合和表示时
2)一个系统要由多个产品系列中的一个来配置时
3)当你要强调一系列相关的产品对象的设计以便进行联合使用时,
4)当你使用一个产品类库,而只想显示它们的接口而不是实现时,
3.参与者
1)AbstractFactory
声明一个创建抽象产品对象的操作接口
2)CreateFactory
实现创建具体产品对象的操作
3)AbstractProduct
为一类产品对象声明一个接口
4)CreateProduct
定义一个将被相应的具体工厂创建的产品对象,实现AbstractProduct
5)Client
仅使用由AbstractFactory和AbstractProduct类声明的接口
代码如下:
AbstractFactory包:
1 package abstractfactory; 2 3 import abstractproduct.ICat; 4 import abstractproduct.IDog; 5 6 public interface IAnimalFactory { 7 ICat createCat(); 8 IDog createDog(); 9 }
CreateFactory包:
package createfactory; import createproduct.BlackCat; import createproduct.BlackDog; import abstractfactory.IAnimalFactory; import abstractproduct.ICat; import abstractproduct.IDog; public class BlackAnimalFactory implements IAnimalFactory { @Override public ICat createCat() { // TODO Auto-generated method stub return new BlackCat(); } @Override public IDog createDog() { // TODO Auto-generated method stub return new BlackDog(); } } package createfactory; import createproduct.whileDog; import createproduct.whiteCat; import abstractfactory.IAnimalFactory; import abstractproduct.ICat; import abstractproduct.IDog; public class WhiteAnimalFactory implements IAnimalFactory { @Override public ICat createCat() { // TODO Auto-generated method stub return new whiteCat(); } @Override public IDog createDog() { // TODO Auto-generated method stub return new whileDog(); } }
AbstractProduct包:
1 package abstractproduct; 2 3 public interface ICat { 4 void eat(); 5 } 6 7 8 9 package abstractproduct; 10 11 public interface IDog { 12 void eat(); 13 }
CreateProduct包:
1 package createproduct; 2 3 import abstractproduct.ICat; 4 5 public class BlackCat implements ICat { 6 public void eat(){ 7 System.out.println("the black cat is eating"); 8 } 9 } 10 11 12 13 package createproduct; 14 15 import abstractproduct.IDog; 16 17 public class BlackDog implements IDog { 18 public void eat(){ 19 System.out.println("the black dog is eating"); 20 } 21 } 22 23 24 25 package createproduct; 26 27 import abstractproduct.IDog; 28 29 public class whileDog implements IDog { 30 public void eat(){ 31 System.out.println("the white dog is eating"); 32 } 33 } 34 35 36 37 package createproduct; 38 39 import abstractproduct.ICat; 40 41 public class whiteCat implements ICat { 42 public void eat(){ 43 System.out.println("the white cat is eating"); 44 } 45 }
Client包:
1 package client; 2 3 import createfactory.BlackAnimalFactory; 4 import createfactory.WhiteAnimalFactory; 5 import abstractfactory.IAnimalFactory; 6 import abstractproduct.ICat; 7 import abstractproduct.IDog; 8 9 public class Client { 10 11 public static void main(String args[]){ 12 IAnimalFactory blackAnimalFactory = new BlackAnimalFactory(); 13 ICat blackCat = blackAnimalFactory.createCat(); 14 blackCat.eat(); 15 IDog blackDog = blackAnimalFactory.createDog(); 16 blackDog.eat(); 17 18 IAnimalFactory whiteAnimalFactory = new WhiteAnimalFactory(); 19 ICat whiteCat = whiteAnimalFactory.createCat(); 20 whiteCat.eat(); 21 IDog whiteDog = whiteAnimalFactory.createDog(); 22 whiteDog.eat(); 23 } 24 }