设计模式——工厂模式
工厂模式用来实例化对象并且实现实现与调用分离,用工厂模式来替代new。
来看看这位老兄总结的吧,不想废话了。http://www.cnblogs.com/poissonnotes/archive/2010/12/01/1893871.html
工厂模式有三种:
1. 简单工厂模式
2. 工厂方法模式
3. 抽象工厂模式
借用高淇老师的Demo
简单工厂模式
简单工厂模式也叫静态工厂模式,工厂类一般使用静态方法
通过接收的参数不同来返回不同的对象实例
但是对增加新产品无能为力,不增加代码无法扩展
public class CarFactory { public static Car creatCar(String name) { Car car = null; if(name.equals("benz")) { car = new Benz(); }else if(name.equals("bmw")) { car = new BMW(); }else if(name.equals("audi")) { car = new Audi(); } return car; } } public interface Car { void run(); } public class BMW implements Car { @Override public void run() { System.out.println("BMW running"); } } public class Benz implements Car { @Override public void run() { System.out.println("Benz running"); } } public class Audi implements Car { @Override public void run() { System.out.println("Audi running"); } } public class Client { public static void main(String[] args) { //不用简单工厂模式的创建 Car benz = new Benz(); benz.run(); Car bmw = new BMW(); bmw.run(); Car audi = new Audi(); audi.run(); System.out.println("+++++++++++++++++++++"); //简单工厂 Car benz2 = CarFactory.creatCar("benz"); benz.run(); Car bmw2 = CarFactory.creatCar("bmw"); bmw.run(); Car audi2 = CarFactory.creatCar("audi"); audi.run(); } }
工厂方法模式:
工厂方法模式,避免了简单工厂的缺点,满足了OCP(开闭原则,对扩展开放,对修改关闭)原则。
简单工厂只有一个工厂类,而工厂方法有一组实现了相同接口的工厂方法。
工厂方法模式的缺点:结构和代码复杂度高,但是可扩展性好,客户端编程难度小。
综合考虑,简单工厂模式,简单有一定的可扩展性。实际上简单工厂模式用的多。
public interface Car { void run(); } public class Audi implements Car { @Override public void run() { System.out.println("Audi running"); } } public class Benz implements Car { @Override public void run() { System.out.println("Benz running"); } } public class BMW implements Car { @Override public void run() { System.out.println("BMW running"); } } public interface CarFactory { Car creatCar(); } 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 BMWFactory implements CarFactory { @Override public Car creatCar() { return new BMW(); } } public class Client { public static void main(String[] args) { //不用简单工厂模式的创建 Car benz = new Benz(); benz.run(); Car bmw = new BMW(); bmw.run(); Car audi = new Audi(); audi.run(); System.out.println("+++++++++++++++++++++"); //简单工厂 Car benz2 = new BenzFactory().creatCar(); benz.run(); Car bmw2 = new BMWFactory().creatCar(); bmw.run(); Car audi2 = new AudiFactory().creatCar(); audi.run(); } }
抽象工厂模式:抽象工厂模式可以增加产品族,但是不可以增加新产品。纵向扩展。
public interface Seat { void massage(); } class LuxurySeat implements Seat { @Override public void massage() { System.out.println("可以自动按摩!"); } } class LowSeat implements Seat { @Override public void massage() { System.out.println("不可以自动按摩!"); } } public interface Tyre { void revolve(); } class LuxuryTyre implements Tyre { @Override public void revolve() { System.out.println("旋转不磨损!"); } } class LowTyre implements Tyre { @Override public void revolve() { System.out.println("旋转磨损!"); } } 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("启动慢"); } } public interface CarFactory { Engine createEngine(); Seat createSeat(); Tyre createTyre(); } public class LowCarFactory implements CarFactory { @Override public Engine createEngine() { return new LowEngine(); } @Override public Seat createSeat() { return new LowSeat(); } @Override public Tyre createTyre() { return new LowTyre(); } } public class LuxuryCarFactory implements CarFactory { @Override public Engine createEngine() { return new LuxuryEngine(); } @Override public Seat createSeat() { return new LuxurySeat(); } @Override public Tyre createTyre() { return new LuxuryTyre(); } } public class Client { public static void main(String[] args) { CarFactory factory = new LuxuryCarFactory(); Engine e = factory.createEngine(); e.run(); e.start(); } }
作者:Pickle
声明:对于转载分享我是没有意见的,出于对博客园社区和作者的尊重一定要保留原文地址哈。
致读者:坚持写博客不容易,写高质量博客更难,我也在不断的学习和进步,希望和所有同路人一道用技术来改变生活。觉得有点用就点个赞哈。