简单工厂模式
1 public interface Computer { 2 3 void setComputerSystem(); 4 5 } 6 7 public class XiaoMiComputer implements Computer { 8 9 @Override 10 public void setComputerSystem() { 11 System.out.println("XiaoMiComputer"); 12 } 13 14 } 15 16 public class MacComputer implements Computer { 17 18 @Override 19 public void setComputerSystem() { 20 System.out.println("MacComputer"); 21 } 22 23 } 24 25 public class ComputerFactory { 26 27 public static Computer makeComputer(String brand) { 28 if (brand == null) { 29 return null; 30 } 31 32 Computer computer = null; 33 34 if (brand.equalsIgnoreCase("XiaoMi")) { 35 computer = new XiaoMiComputer(); 36 } else if (brand.equalsIgnoreCase("apple")) { 37 computer = new MacComputer(); 38 } 39 40 return computer; 41 } 42 43 } 44 45 public class UseSimpleFactory { 46 47 public static void main(String[] args) { 48 Computer computer = ComputerFactory.makeComputer("XiaoMi"); 49 computer.setComputerSystem(); 50 51 computer = ComputerFactory.makeComputer("apple"); 52 computer.setComputerSystem(); 53 } 54 55 }
优点: 1. 一个调用者想创建一个对象,只要知道其名称就可以了。 2. 屏蔽产品的具体实现,调用者只关心产品的接口。
缺点:违反了开闭原则。
工厂方法模式
1 public interface Computer { 2 3 void setComputerSystem(); 4 5 } 6 7 public class XiaoMiComputer implements Computer { 8 9 @Override 10 public void setComputerSystem() { 11 System.out.println("XiaoMiComputer"); 12 } 13 14 } 15 16 public class MacComputer implements Computer { 17 18 @Override 19 public void setComputerSystem() { 20 System.out.println("MacComputer"); 21 } 22 23 } 24 25 public class XiaoMiComputerFactory { 26 27 public static Computer makeComputer() { 28 return new XiaoMiComputer(); 29 } 30 31 } 32 33 public class MacComputerFactory { 34 35 public static Computer makeComputer() { 36 return new MacComputer(); 37 } 38 39 } 40 41 public class UseFactoryMethodPattern { 42 43 public static void main(String[] args) { 44 Computer computer = XiaoMiComputerFactory.makeComputer(); 45 computer.setComputerSystem(); 46 47 computer = MacComputerFactory.makeComputer(); 48 computer.setComputerSystem(); 49 } 50 51 }
抽象工厂模式
1 public interface Computer { 2 3 void setComputerSystem(); 4 5 } 6 7 public class XiaoMiComputer implements Computer { 8 9 @Override 10 public void setComputerSystem() { 11 System.out.println("XiaoMiComputerOS"); 12 } 13 14 } 15 16 public class MacComputer implements Computer { 17 18 @Override 19 public void setComputerSystem() { 20 System.out.println("MacComputerOS"); 21 } 22 23 } 24 25 public interface MobilePhone { 26 27 void setOperationSystem(); 28 29 } 30 31 public class XiaoMiPhone implements MobilePhone { 32 33 @Override 34 public void setOperationSystem() { 35 System.out.println("XiaoMiPhoneOS"); 36 } 37 38 } 39 40 public class iPhone implements MobilePhone { 41 42 @Override 43 public void setOperationSystem() { 44 System.out.println("iPhoneOS"); 45 } 46 47 } 48 49 public interface AbstractFactory { 50 51 Computer makeComputer(); 52 MobilePhone makeMobilePhone(); 53 54 } 55 56 57 public class XiaoMiFactory implements AbstractFactory { 58 59 @Override 60 public Computer makeComputer() { 61 return new XiaoMiComputer(); 62 } 63 64 @Override 65 public MobilePhone makeMobilePhone() { 66 return new XiaoMiPhone(); 67 } 68 69 } 70 71 public class AppleFactory implements AbstractFactory { 72 73 @Override 74 public Computer makeComputer() { 75 return new MacComputer(); 76 } 77 78 @Override 79 public MobilePhone makeMobilePhone() { 80 return new iPhone(); 81 } 82 83 } 84 85 86 public class FactoryProducer { 87 88 public static AbstractFactory getFactory(String brand) { 89 if (brand == null) { 90 return null; 91 } 92 93 if (brand.equalsIgnoreCase("Apple")) { 94 return new AppleFactory(); 95 } else if (brand.equalsIgnoreCase("XiaoMi")) { 96 return new XiaoMiFactory(); 97 } 98 99 return null; 100 } 101 102 } 103 104 public class UseAbstractFactoryPattern { 105 106 public static void main(String[] args) { 107 AbstractFactory appleFactory = FactoryProducer.getFactory("Apple"); 108 Computer computer = appleFactory.makeComputer(); 109 computer.setComputerSystem(); 110 MobilePhone mobilePhoneFactory = appleFactory.makeMobilePhone(); 111 mobilePhoneFactory.setOperationSystem(); 112 113 AbstractFactory xiaoMiFactory = FactoryProducer.getFactory("XiaoMi"); 114 computer = xiaoMiFactory.makeComputer(); 115 computer.setComputerSystem(); 116 mobilePhoneFactory = xiaoMiFactory.makeMobilePhone(); 117 mobilePhoneFactory.setOperationSystem(); 118 } 119 120 }