注明 : 以下整理的资料来源于实验楼网站.
工厂模式(Factory Pattern)的意义就跟它的名字一样,在面向对象程序设计中,工厂通常是一个用来创建其他对象的对象。工厂模式根据不同的参数来实现不同的分配方案和创建对象。
静态工厂最常见的应用场景就是java库中的getInstance()方法会返回不同的Calendar对象
1静态工厂
UML图如下所示 : (创建一个人类工厂,根据需求创建对应的对象)
步骤如下 :
1创建一个Human接口
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 9:54 4 * @Version 1.0 5 */ 6 public interface Human { 7 void eat(); 8 void sleep(); 9 void beat(); 10 }
2创建Male和FeMale实现类实现Human接口
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 9:55 4 * @Version 1.0 5 */ 6 public class Female implements Human { 7 @Override 8 public void eat() { 9 System.out.println("Female eating ...."); 10 11 } 12 13 @Override 14 public void sleep() { 15 System.out.println("Female sleeping ...."); 16 17 } 18 19 @Override 20 public void beat() { 21 System.out.println("Female beating ...."); 22 23 } 24 }
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 9:55 4 * @Version 1.0 5 */ 6 public class Male implements Human { 7 @Override 8 public void eat() { 9 System.out.println("Male eating ...."); 10 } 11 12 @Override 13 public void sleep() { 14 System.out.println("Male slieeping ...."); 15 16 } 17 18 @Override 19 public void beat() { 20 System.out.println("Male beating...."); 21 22 } 23 }
3创建一个静态工厂
1 public class ManyHumanFactory { 2 public static Male createMale(){ 3 return new Male(); 4 } 5 6 public static Female createFemlae(){ 7 return new Female(); 8 } 9 }
4测试类:
1 public class ManyFacotoryTest { 2 public static void main(String[] args) { 3 Male male = ManyHumanFactory.createMale(); 4 male.eat(); 5 Female femlae = ManyHumanFactory.createFemlae(); 6 femlae.eat(); 7 } 8 } 9 10 Result : 11 Male eating .... 12 Female eating ....
2
抽象工厂模式 (Abstract Factory Pattern) ----抽象工厂是一个父类的工厂(可以创建其它的工厂类 alias(工厂的工厂))
可以将具有同一主题的单独的工厂封装起来.在工厂模式之上增加一层抽象的概念.
抽象工厂特别适合这样一种产品结构,产品分为几个系列,在每一个系列中,产品的布局都是类似的.
UML图:
步骤如下:
1创建餐具和食物的接口:
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:34 4 * @Version 1.0 5 */ 6 public interface TableWare { 7 String getToolName(); 8 }
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:33 4 * @Version 1.0 5 */ 6 public interface Food { 7 String getFoodName(); 8 }
2创建餐具的实现类Knife和Cup:
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:39 4 * @Version 1.0 5 */ 6 public class Knife implements TableWare { 7 @Override 8 public String getToolName() { 9 return "knife"; 10 } 11 } 12 13 14 public class Cup implements TableWare { 15 @Override 16 public String getToolName() { 17 return "cup"; 18 } 19 }
3创建食物的实现类Milk和Apple
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:38 4 * @Version 1.0 5 */ 6 public class Milk implements Food { 7 @Override 8 public String getFoodName() { 9 return "milk"; 10 } 11 } 12 13 public class Apple implements Food{ 14 @Override 15 public String getFoodName() { 16 return "apple"; 17 } 18 }
4创建厨房的顶级接口
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:36 4 * @Version 1.0 5 */ 6 public interface KitchenFactory { 7 Food getFood(); 8 TableWare getTableWare(); 9 }
5根据自己的需求创建厨房实现类(实现厨房的顶级接口
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:41 4 * @Version 1.0 5 */ 6 public class AKitchen implements KitchenFactory { 7 @Override 8 public Food getFood() { 9 return new Apple(); 10 } 11 12 @Override 13 public TableWare getTableWare() { 14 return new Knife(); 15 } 16 }
6创建顶级的吃货类,根据自己的需求传入厨房(含测试)
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:43 4 * @Version 1.0 5 */ 6 public class Foodahollc { 7 public static void eat(KitchenFactory k){ 8 System.out.println("A foodaholic id eating " + k.getFood().getFoodName()+ 9 " with "+k.getTableWare().getToolName()); 10 } 11 public static void main(String[] args) { 12 AKitchen aKitchen = new AKitchen(); 13 Foodahollc.eat(aKitchen); 14 } 15 }
最后简单总结一下两种工厂模式的区别 :
工厂方法模式 : 针对的是一个产品的等级结构
抽象工厂模式 : 针对的是多个产品的等级结构