设计模式——工厂方法模式
工厂方法模式:生产那种产品不再是由参数决定,定义一个创建对象的工厂接口,让实现工厂接口的具体类决定实例化哪一个产品类,将实际创建工作推迟到子类当中。
① 一个产品接口
② 一个工厂接口
③ 实现了产品接口的具体产品类
④ 实现了工厂接口的具体工厂类
⑤ 消费者类,用于测试
例子一
角色:一个产品接口
1 package com.test.factoryMethod2; 2 3 public interface Shape { 4 public abstract void draw(); 5 }
角色:一个工厂接口
1 package com.test.factoryMethod2; 2 3 public interface ShapeFactory { 4 public abstract Shape getShape(); 5 }
角色:具体的产品类 Circle
1 package com.test.factoryMethod2; 2 3 public class Circle implements Shape { 4 @Override 5 public void draw() { 6 System.out.println(" I am the Circle"); 7 } 8 }
角色:具体的产品类 Rectangle
1 package com.test.factoryMethod2; 2 3 public class Rectangle implements Shape { 4 @Override 5 public void draw() { 6 System.out.println("I am the Rectangle"); 7 } 8 }
角色:具体的产品类 Square
1 package com.test.factoryMethod2; 2 3 public class Square implements Shape { 4 @Override 5 public void draw() { 6 System.out.println("I am the Square"); 7 } 8 }
角色:与产品类Circle对应具体的工厂类
1 package com.test.factoryMethod2; 2 3 public class CircleFactory implements ShapeFactory { 4 @Override 5 public Shape getShape() { 6 return new Circle(); 7 } 8 }
角色:与产品类Rectangle 对应具体的工厂类
1 package com.test.factoryMethod2; 2 3 public class RectangleFactory implements ShapeFactory { 4 @Override 5 public Shape getShape() { 6 return new Rectangle(); 7 } 8 }
角色:与产品类Square对应具体的工厂类
1 package com.test.factoryMethod2; 2 3 public class SquareFactory implements ShapeFactory { 4 @Override 5 public Shape getShape() { 6 return new Square(); 7 } 8 }
测试
1 package com.test.factoryMethod2; 2 3 public class FactoryMethodDemo { 4 public static void main(String[] args){ 5 ShapeFactory circleFactory = new CircleFactory(); 6 circleFactory.getShape().draw(); 7 8 ShapeFactory rectangleFactory = new RectangleFactory(); 9 rectangleFactory.getShape().draw(); 10 11 ShapeFactory squareFactory = new SquareFactory(); 12 squareFactory.getShape().draw(); 13 } 14 }
测试结果:
例子二 在产品接口和具体产品类之间增加了一个抽象类
角色:产品接口
1 package com.test.factoryMethod; 2 3 import java.util.Map; 4 5 public interface MyMessage { 6 /* 7 * 产品接口 8 */ 9 public abstract Map<String, Object> getMessageParam(); 10 public abstract void setMessageParam(Map<String, Object> messageParam); 11 public abstract void sendMessage() throws Exception; 12 }
角色:抽象产品类 实现了一些共有方法
1 package com.test.factoryMethod; 2 3 import java.util.Map; 4 5 public abstract class MyAbstractMessage implements MyMessage { 6 /* 7 * 抽象产品类 8 */ 9 private Map<String, Object> messageParam; 10 @Override 11 public Map<String, Object> getMessageParam(){ 12 return messageParam; 13 } 14 @Override 15 public void setMessageParam(Map<String, Object> messageParam){ 16 this.messageParam = messageParam; 17 } 18 }
角色:具体产品类 EmialMessage
1 package com.test.factoryMethod; 2 3 public class EmailMessage extends MyAbstractMessage { 4 @Override 5 public void sendMessage() throws Exception { 6 if (null == getMessageParam() || null == getMessageParam().get("EMAIL") || "".equals(getMessageParam().get("EMAIL"))){ 7 throw new Exception("发送短信,需要传入EMAIL参数"); 8 } 9 System.out.println("我是邮件,发送通知给" + getMessageParam().get("EMAIL")); 10 } 11 }
角色:具体产品类 OAMessage
1 package com.test.factoryMethod; 2 3 public class OAMessage extends MyAbstractMessage { 4 @Override 5 public void sendMessage() throws Exception { 6 if (null == getMessageParam() || null == getMessageParam().get("OA_userName") || "".equals(getMessageParam().get("OA_userName"))){ 7 throw new Exception("发送OA待办,需要传入 OA_userName 参数"); 8 } 9 System.out.println("我是OA待办,发送通知给" + getMessageParam().get("OA_userName")); 10 } 11 }
角色:具体产品类 SMSMessage
1 package com.test.factoryMethod; 2 3 public class SMSMessage extends MyAbstractMessage { 4 @Override 5 public void sendMessage() throws Exception { 6 if (null == getMessageParam() || null == getMessageParam().get("phone_number") || "".equals(getMessageParam().get("phone_number"))){ 7 throw new Exception("发送短信,需要传入phone_number参数"); 8 } 9 System.out.println("我是短信,发送通知给" + getMessageParam().get("phone_number")); 10 } 11 }
角色:工厂接口
1 package com.test.factoryMethod; 2 3 public interface FactoryMethod { 4 /* 5 * 工厂接口 6 */ 7 public abstract MyMessage createMessage(String messageType); 8 }
角色:具体工厂类
1 package com.test.factoryMethod; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class FactoryMethod1 implements FactoryMethod { 7 @Override 8 public MyMessage createMessage(String messageType) { 9 MyMessage myMessage; 10 Map<String, Object> messageParam = new HashMap<>(); 11 if ("SMS".equals(messageType)){ 12 myMessage = new SMSMessage(); 13 messageParam.put("phone_number", "123456789"); 14 } else if ("OA".equals(messageType)){ 15 myMessage = new OAMessage(); 16 messageParam.put("OA_userName", "testUser"); 17 } else if ("EMAIL".equals(messageType)){ 18 myMessage = new EmailMessage(); 19 messageParam.put("EMAIL", "test@test.com"); 20 } else { 21 myMessage = new EmailMessage(); 22 messageParam.put("EMAIL", "test@test.com"); 23 } 24 myMessage.setMessageParam(messageParam); 25 return myMessage; 26 } 27 }
测试
1 package com.test.factoryMethod; 2 3 public class FactoryMethodTest { 4 public static void main(String[] args){ 5 FactoryMethod messageFactory = new FactoryMethod1(); 6 MyMessage myMessage; 7 try{ 8 myMessage = messageFactory.createMessage("SMS"); 9 myMessage.sendMessage(); 10 11 myMessage = messageFactory.createMessage("OA"); 12 myMessage.sendMessage(); 13 14 myMessage = messageFactory.createMessage("EMAIL"); 15 myMessage.sendMessage(); 16 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } 20 } 21 }
测试结果
1 package com.test.factoryMethod2; 2 3 public interface Shape { 4 public abstract void draw(