设计模式(二) 工厂模式
核心作用:
-- 实例化对象,用工厂方法替代new操作
-- 将选择实现类,创键对象统一管理和控制,从而将调用者跟我们的实现类解耦
常见应用场景:
简单工厂模式:
-- 简单工厂模式也叫静态工厂模式,就是工厂类一般是使用静态方法,通过接受的参数不同来返回不同的对象实例
-- 对于增加新产品无能为力! 不修改工厂类代码是不行,无法扩展 违反了开闭原则
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.simplefactory; public interface Car { void run(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.simplefactory; public class Audi implements Car{ @Override public void run() { System.out.println("奥迪在跑"); } } public class Byd implements Car { @Override public void run() { System.out.println("比亚迪在跑"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.simplefactory; import com.lp.factory.factorymethod.Audi; import com.lp.factory.factorymethod.Byd; import com.lp.factory.factorymethod.Car; //这是一个简单工厂 当修改汽车子类时必须修改这个类 不符合开闭原则 public class CarFactory { public static Car creatCar(String type){ if("Audi".equals(type)){ return new Audi(); }else if("Byd".equals(type)){ return new Byd(); }else{ return null; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.simplefactory; import com.lp.factory.factorymethod.Car; //调用简单工厂类 public class Client02 { public static void main(String[] args){ Car c1 = CarFactory.creatCar("Audi"); Car c2 = CarFactory.creatCar("Byd"); c1.run(); c2.run(); } }
工厂方法模式:
-- 为避免简单工厂模式的缺点,不完全满足OCP
-- 工厂方法模式和简单工厂最大的不同在于,简单工厂模式只有一个(对于一个项目或一个独立模块而言)工厂类,而工厂方法模式一组实现了相同接口的工 厂类
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.factorymethod; public interface Car { void run(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.factorymethod; public class Audi implements Car{ @Override public void run() { System.out.println("奥迪在跑"); } } package com.lp.factory.factorymethod; public class Benz implements Car { @Override public void run() { System.out.println("奔驰在跑"); } } package com.lp.factory.factorymethod; public class Byd implements Car { @Override public void run() { System.out.println("比亚迪在跑"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.factorymethod; public interface CarFactory { Car creatCar(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.factorymethod; public class AudiFactory implements CarFactory { @Override public Car creatCar() { return new Audi(); } } public class BenzFactory implements CarFactory{ @Override public Car creatCar() { return new Benz(); } } public class BydFactory implements CarFactory { @Override public Car creatCar() { return new Byd(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.factorymethod; public class Client { public static void main(String[] args){ Car c1 = new AudiFactory().creatCar(); Car c2 = new BydFactory().creatCar(); Car c3 = new BenzFactory().creatCar(); c1.run(); c2.run(); c3.run(); } }
抽象工厂模式:
-- 用来生产不同产品族的全部产品.(对于新增加的产品无能为力,支持增加产品族)
-- 抽象工厂模式是工厂方法模式的升级版本,在有多个业务品种,业务分类是,用过抽象工厂模式产生需要的对象是一种非常好的解决方式
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.abstractfactory; public interface Engine { void run(); void start(); } class LuxuryEngine implements Engine{ @Override public void run() { System.out.println("转得快"); } @Override public void start() { System.out.println("启动得快!可以自动启停"); } } class LowEngine implements Engine{ @Override public void run() { System.out.println("转得慢!"); } @Override public void start() { System.out.println("启动得慢"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.abstractfactory; public interface Seat { void message(); } class LuxurySeat implements Seat { @Override public void message() { System.out.println("可以按摩"); } } class LowSeat implements Seat { @Override public void message() { System.out.println("不可以按摩"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.abstractfactory; public interface Type { void revolve(); } class LuxuryType implements Type{ @Override public void revolve() { System.out.println("磨损小"); } } class LowType implements Type{ @Override public void revolve() { System.out.println("磨损大"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.abstractfactory; public interface CarFactory { Engine creatEngine(); Seat creatSeat(); Type creatType(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.abstractfactory; public class LowCarFactory implements CarFactory { @Override public Engine creatEngine() { // TODO 自动生成的方法存根 return new LowEngine(); } @Override public Seat creatSeat() { // TODO 自动生成的方法存根 return new LowSeat(); } @Override public Type creatType() { // TODO 自动生成的方法存根 return new LowType(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.abstractfactory; public class LuxuryCarFactory implements CarFactory { @Override public Engine creatEngine() { return new LuxuryEngine(); } @Override public Seat creatSeat() { return new LuxurySeat(); } @Override public Type creatType() { return new LuxuryType(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lp.factory.abstractfactory; public class Client { public static void main(String[] args){ CarFactory cf = new LuxuryCarFactory(); Engine e = cf.creatEngine(); e.run(); e.start(); } }
工厂模式要点:
-- 简单工厂模式(静态工厂)
虽然某种程度不符合设计原则,但实际使用最多。
-- 工厂方法模式
不修改已有类的前提下,通过增加新的工厂类实现扩展
-- 抽象工厂模式
不可以增加产品,可以增加产品族