工厂设计模式之:简单工厂、工厂方法、抽象工厂
简单工厂模式(Simple Factory):简单工厂模式又叫静态工厂模式,顾名思义,通过一个具体的工厂类,在该工厂类中定义返回值为不同实例对象的静态方法,来获得各种具体的实例对象。
工厂方法模式(Factory Method):建立一个抽象工厂,其不同的工厂实例用来创建不同的产品实例对象(单个产品),如果要增加新的产品,只需增加新的产品工厂即可。符合开闭原则。
抽象工厂模式(Abstract Factory):建立一个抽象工厂,用来实例化工厂子类,这些工厂子类用来生产对应的产品族(即一组对象实例)。
代码:
1 package com.yan.factory; 2 3 /** 4 * 接口Car的一个实现类CarA 5 * 6 * @author Yan 7 * 8 */ 9 public class CarA implements Car { 10 11 public CarA() { 12 } 13 14 public void run() { 15 System.out.println("CarA is running..."); 16 } 17 }
1 package com.yan.factory; 2 3 /** 4 * 接口Car的一个实现类CarB 5 * 6 * @author Yan 7 * 8 */ 9 public class CarB implements Car { 10 11 public CarB() { 12 } 13 14 public void run() { 15 System.out.println("CarB is running..."); 16 } 17 }
1 package com.yan.factory; 2 3 /** 4 * 接口Car的一个实现类CarC 5 * 6 * @author Yan 7 * 8 */ 9 public class CarC implements Car { 10 11 public CarC() { 12 } 13 14 public void run() { 15 System.out.println("CarC is running..."); 16 } 17 }
1 package com.yan.factory; 2 3 /** 4 * 静态工厂类CarFactory,用来创建Car实例 5 * 6 * @author Yan 7 * 8 */ 9 public class CarFactory { 10 11 public CarFactory() { 12 } 13 14 public static Car getCar(String car) { 15 switch (car) { 16 case "CarA": 17 return new CarA(); 18 case "CarB": 19 return new CarB(); 20 case "CarC": 21 return new CarC(); 22 23 default: 24 System.out.println("Illeagle input..."); 25 return null; 26 } 27 } 28 29 }
1 package com.yan.factory; 2 3 /** 4 * 用于测试静态工厂的客户端Client 5 * 6 * @author Yan 7 * 8 */ 9 public class Client { 10 11 public Client() { 12 } 13 14 public static void main(String[] args) { 15 Car carA = CarFactory.getCar("CarA"); 16 Car carB = CarFactory.getCar("CarC"); 17 Car carC = CarFactory.getCar("CarB"); 18 carA.run(); 19 carB.run(); 20 carC.run(); 21 } 22 }
工厂方法模式:
创建工厂接口
1 package com.yan.factoryMethod; 2 3 public interface FactoryInterface { 4 Car createCar(); 5 }
分别创建CarA、CarB、CarC的工厂类
1 package com.yan.factoryMethod; 2 3 /** 4 * 工厂接口FactoryInterface的实现工厂类CarAFactory,用来创建CarA实例 5 * 6 * @author Yan 7 * 8 */ 9 public class CarAFactory implements FactoryInterface { 10 11 public CarAFactory() { 12 } 13 14 @Override 15 public Car createCar() { 16 return new CarA(); 17 } 18 19 }
1 package com.yan.factoryMethod; 2 3 /** 4 * 工厂接口FactoryInterface的实现工厂类CarBFactory,用来创建CarB实例 5 * 6 * @author Yan 7 * 8 */ 9 public class CarBFactory implements FactoryInterface { 10 11 public CarBFactory() { 12 } 13 14 @Override 15 public Car createCar() { 16 return new CarB(); 17 } 18 19 }
1 package com.yan.factoryMethod; 2 3 /** 4 * 工厂接口FactoryInterface的实现工厂类CarBFactory,用来创建CarB实例 5 * 6 * @author Yan 7 * 8 */ 9 public class CarCFactory implements FactoryInterface { 10 11 public CarCFactory() { 12 } 13 14 @Override 15 public Car createCar() { 16 return new CarC(); 17 } 18 19 }
测试客户端
1 package com.yan.factoryMethod; 2 3 /** 4 * 用于测试的客户端Client 5 * 6 * @author Yan 7 * 8 */ 9 public class Client { 10 11 public Client() { 12 } 13 14 public static void main(String[] args) { 15 Car carA = new CarAFactory().createCar(); 16 Car carB = new CarBFactory().createCar(); 17 Car carC = new CarCFactory().createCar(); 18 carA.run(); 19 carB.run(); 20 carC.run(); 21 } 22 }
抽象工厂模式:需要用到产品族的概念,实现工厂接口的各个工厂分别负责一个产品族的生产。
1.引擎(高端、低端)
2.座椅(高端、低端)
3.工厂(高端(高端引擎、高端座椅)、低端(低端引擎、低端座椅))
1 package com.yan.abstractFactory; 2 3 /** 4 * 引擎接口 5 * 6 * @author Yan 7 * 8 */ 9 public interface Engine { 10 11 void engineMethod(); 12 }
1 package com.yan.abstractFactory; 2 3 /** 4 * 座椅接口 5 * 6 * @author Yan 7 * 8 */ 9 public interface Seat { 10 void seatmethod(); 11 12 }
1 package com.yan.abstractFactory; 2 3 /** 4 * 定义工厂接口,用来定义生产产品要用到的方法 5 * 6 * @author Yan 7 * 8 */ 9 public interface FactoryInterface { 10 Engine createEngine(); 11 12 Seat createSeat(); 13 }
1 package com.yan.abstractFactory; 2 3 /** 4 * 定义一个低端引擎类,实现引擎接口 5 * 6 * @author Yan 7 * 8 */ 9 public class LowEngine implements Engine { 10 11 public LowEngine() { 12 } 13 14 @Override 15 public void engineMethod() { 16 System.out.println("This is a low engine..."); 17 } 18 19 }
1 package com.yan.abstractFactory; 2 3 /** 4 * 定义一个低端座椅类,实现座椅接口 5 * 6 * @author Yan 7 * 8 */ 9 public class LowSeat implements Seat { 10 11 public LowSeat() { 12 } 13 14 @Override 15 public void seatmethod() { 16 System.out.println("This is a low seat..."); 17 } 18 19 }
1 package com.yan.abstractFactory; 2 3 /** 4 * 定义一个低端工厂类,实现工厂接口,用于生产低端产品族 5 * 6 * @author Yan 7 * 8 */ 9 public class LowFactory implements FactoryInterface { 10 11 public LowFactory() { 12 } 13 14 @Override 15 public Engine createEngine() { 16 return new LowEngine(); 17 } 18 19 @Override 20 public Seat createSeat() { 21 return new LowSeat(); 22 } 23 24 }
1 package com.yan.abstractFactory; 2 3 /** 4 * 定义高端引擎类,实现引擎接口 5 * 6 * @author Yan 7 * 8 */ 9 public class LuxuryEngine implements Engine { 10 11 public LuxuryEngine() { 12 } 13 14 @Override 15 public void engineMethod() { 16 System.out.println("This is a Luxury Engine..."); 17 } 18 19 }
1 package com.yan.abstractFactory; 2 3 /** 4 * 定义一个高端座椅类,实现座椅接口 5 * 6 * @author Yan 7 * 8 */ 9 public class LuxurySeat implements Seat { 10 11 public LuxurySeat() { 12 } 13 14 @Override 15 public void seatmethod() { 16 System.out.println("This is a luxury seat..."); 17 } 18 19 }
1 package com.yan.abstractFactory; 2 3 /** 4 * 定义一个高端工厂,实现工厂接口,用来生产高端的产品族 5 * 6 * @author Yan 7 * 8 */ 9 public class LuxuryFactory implements FactoryInterface { 10 11 public LuxuryFactory() { 12 } 13 14 @Override 15 public Engine createEngine() { 16 return new LuxuryEngine(); 17 } 18 19 @Override 20 public Seat createSeat() { 21 return new LuxurySeat(); 22 } 23 24 }
1 package com.yan.abstractFactory; 2 3 /** 4 * 用于测试抽象工厂模式的测试客户端 5 * 6 * @author Yan 7 * 8 */ 9 public class Client { 10 11 public Client() { 12 } 13 14 public static void main(String[] args) { 15 /* 生成高端工厂的实例,生产高端产品 */ 16 FactoryInterface factory = new LuxuryFactory(); 17 Engine engine = factory.createEngine(); 18 Seat seat = factory.createSeat(); 19 engine.engineMethod(); 20 seat.seatmethod(); 21 /* 成产低端工厂的实例,生产低端产品 */ 22 FactoryInterface factory2 = new LowFactory(); 23 Engine engine2 = factory2.createEngine(); 24 Seat seat2 = factory2.createSeat(); 25 engine2.engineMethod(); 26 seat2.seatmethod(); 27 } 28 29 }
posted on 2016-04-29 19:52 Yanspecial 阅读(183) 评论(0) 编辑 收藏 举报