设计模式-简单工厂 --工厂模式--抽象模式
Posted luzhouxiaoshuai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式-简单工厂 --工厂模式--抽象模式相关的知识,希望对你有一定的参考价值。
1 、简单工厂模式
package com.bjsxt.factory.simplefactory; public interface Car { void run(); }
package com.bjsxt.factory.simplefactory; public class Audi implements Car { @Override public void run() { System.out.println("奥迪再跑!"); } }
package com.bjsxt.factory.simplefactory; public class Byd implements Car { @Override public void run() { System.out.println("比亚迪再跑!"); } }
package com.bjsxt.factory.simplefactory; public class CarFactory { public static Car createCar(String type){ if("奥迪".equals(type)){ return new Audi(); }else if("比亚迪".equals(type)){ return new Byd(); }else{ return null; } } }
调用者:如何没有使用工厂模式
package com.bjsxt.factory.simplefactory; /** * 测试在没有工厂模式的情况下 * @author 尚学堂高淇 www.sxt.cn * */ public class Client01 { //调用者 public static void main(String[] args) { Car c1 = new Audi(); Car c2 = new Byd(); c1.run(); c2.run(); } }
如果使用简单工厂模式:
package com.bjsxt.factory.simplefactory; /** * 简单工厂情况下 * @author 尚学堂高淇 www.sxt.cn * */ public class Client02 { //调用者 public static void main(String[] args) { Car c1 =CarFactory.createCar("奥迪"); Car c2 = CarFactory.createCar("比亚迪"); c1.run(); c2.run(); } }
简单工厂的缺点:
下面我们来看工厂模式:
我们首先定义了工厂抽象类的接口:
package com.bjsxt.factory.factorymethod; public interface CarFactory { Car createCar(); }
package com.bjsxt.factory.factorymethod; public class AudiFactory implements CarFactory { @Override public Car createCar() { return new Audi(); } }
package com.bjsxt.factory.factorymethod; public class BenzFactory implements CarFactory { @Override public Car createCar() { return new Benz(); } }
package com.bjsxt.factory.factorymethod; public class BydFactory implements CarFactory { @Override public Car createCar() { return new Byd(); } }
package com.bjsxt.factory.factorymethod; public class Client { public static void main(String[] args) { Car c1 = new AudiFactory().createCar(); Car c2 = new BydFactory().createCar(); c1.run(); c2.run(); } }
抽象工厂模式:
比如一个汽车的一个产品族:包括汽车发动机、汽车座椅 汽车 轮胎
package com.bjsxt.factory.abstractFactory; public interface CarFactory { Engine createEngine(); Seat createSeat(); Tyre createTyre(); }
package com.bjsxt.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("启动慢!"); } }
package com.bjsxt.factory.abstractFactory; 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("不能按摩!"); } }
package com.bjsxt.factory.abstractFactory; 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("旋转磨损快!"); } }
package com.bjsxt.factory.abstractFactory; 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(); } }
package com.bjsxt.factory.abstractFactory; 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(); } }
package com.bjsxt.factory.abstractFactory; public class Client { public static void main(String[] args) { CarFactory factory = new LuxuryCarFactory(); Engine e = factory.createEngine(); e.run(); e.start(); } }
一个高端的汽车由高端发动机、高端座椅、高端轮胎构成
以上是关于设计模式-简单工厂 --工厂模式--抽象模式的主要内容,如果未能解决你的问题,请参考以下文章
设计模式从青铜到王者第五篇:创建型模式之简单工厂模式( Simple Factory Pattern )