这篇讲解工厂模式。
工厂模式:工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式。
工厂模式可划分为:简单工厂模式、工厂方法模式、抽象工厂模式。
一、简单工厂模式:又成为静态工厂模式,通常是由工厂实例根据传入参数来决定需要创建哪种产品类实例。
代码实现(根据条件不同创建不同车型的车辆):
首先是我们的工厂类:CarFactory.java
package com.interview.design.factory.simpleFactory;
/**
* @Author: gtd
* @Description:
* @Date: Create in 21:01 2019/1/11
*/
public class CarFactory {
public static Car createCar(String carName){
switch (carName){
case "bus":
return new Bus("bus");
case "trucks":
return new Trucks("trucks");
default:
return new Sedan("sedan");
}
}
}
然后抽象类Car.java
package com.interview.design.factory.simpleFactory;
/**
* @Author: gtd
* @Description:
* @Date: Create in 21:01 2019/1/11
*/
public abstract class Car {
public String name;
public abstract void drive();
}
其中一个Car的子类,Bus.java
package com.interview.design.factory.simpleFactory;
/**
* @Author: gtd
* @Description:
* @Date: Create in 21:06 2019/1/11
*/
public class Bus extends Car {
public Bus(String name){
this.name = name;
}
@Override
public void drive() {
System.out.println("我驾驶这一辆"+this.name+"出发了");
}
}
测试类TestFactory.java
package com.interview.design.factory.simpleFactory;
/**
* @Author: gtd
* @Description:
* @Date: Create in 21:12 2019/1/11
*/
public class TestFactory {
public static void main(String[] args) {
Car car = CarFactory.createCar("bus");
car.drive();
}
}
二、工厂方法模式:提供一个工厂接口,让该工厂接口的实现类来决定去实例化哪一个产品类,并且该实现类去创建对应产品类的实例
代码实现(不同工厂生产不同的车辆):
抽象工厂类CarFactory.java
package com.interview.design.factory.factoryMethod.factory;
import com.interview.design.factory.factoryMethod.product.Car;
/**
* @Author: gtd
* @Description:
* @Date: Create in 20:32 2019/1/12
*/
public interface CarFactory {
public Car createCar();
}
其中一个抽象工厂接口实现类BusFactory.java
package com.interview.design.factory.factoryMethod.factory.impl;
import com.interview.design.factory.factoryMethod.factory.CarFactory;
import com.interview.design.factory.factoryMethod.product.Bus;
import com.interview.design.factory.factoryMethod.product.Car;
/**
* @Author: gtd
* @Description:
* @Date: Create in 20:34 2019/1/12
*/
public class BusFactory implements CarFactory {
@Override
public Car createCar() {
return new Bus("bus");
}
}
另外一个抽象工厂接口实现类TrucksFactory.java
package com.interview.design.factory.factoryMethod.factory.impl;
import com.interview.design.factory.factoryMethod.factory.CarFactory;
import com.interview.design.factory.factoryMethod.product.Car;
import com.interview.design.factory.factoryMethod.product.Trucks;
/**
* @Author: gtd
* @Description:
* @Date: Create in 20:35 2019/1/12
*/
public class TrucksFactory implements CarFactory {
@Override
public Car createCar() {
return new Trucks("trucks");
}
}
具体的产品汽车抽象类Car.java
package com.interview.design.factory.factoryMethod.product;
/**
* @Author: gtd
* @Description:
* @Date: Create in 20:36 2019/1/12
*/
public abstract class Car {
public String name;
public abstract void drive();
}
具体产品类Bus.java
package com.interview.design.factory.factoryMethod.product;
/**
* @Author: gtd
* @Description:
* @Date: Create in 21:06 2019/1/11
*/
public class Bus extends Car {
public Bus(String name){
this.name = name;
}
@Override
public void drive() {
System.out.println("我驾驶这一辆"+this.name+"出发了");
}
}
具体产品类Trucks.java
package com.interview.design.factory.factoryMethod.product;
/**
* @Author: gtd
* @Description:
* @Date: Create in 21:04 2019/1/11
*/
public class Trucks extends Car {
public Trucks(String name){
super.name = name;
}
@Override
public void drive() {
System.out.println("我驾驶这一辆"+this.name+"出发了");
}
}
测试类TestFactoryMethod.java
package com.interview.design.factory.factoryMethod;
import com.interview.design.factory.factoryMethod.factory.CarFactory;
import com.interview.design.factory.factoryMethod.factory.impl.BusFactory;
import com.interview.design.factory.factoryMethod.factory.impl.TrucksFactory;
import com.interview.design.factory.factoryMethod.product.Car;
import com.interview.design.factory.factoryMethod.product.Trucks;
/**
* @Author: gtd
* @Description:
* @Date: Create in 21:56 2019/1/12
*/
public class TestFactoryMethod {
public static void main(String[] args) {
CarFactory factory;
factory = new BusFactory();
Car bus = factory.createCar();
bus.drive();
factory = new TrucksFactory();
Car trucks = factory.createCar();
trucks.drive();
}
}
三、抽象工厂模式:抽象工厂模式是工厂方法模式的升级版本,抽象工厂模式中我们可以定义实现不止一个接口,而是创建一组相关或者相互依赖的对象。
代码实现(生产汽车并附带保险)
抽象工厂接口 Factory.java
package com.interview.design.factory.abstractFactory.factory;
import com.interview.design.factory.abstractFactory.product.car.Car;
import com.interview.design.factory.abstractFactory.product.insurance.Insurance;
/**
* @Author: gtd
* @Description:
* @Date: Create in 0:26 2019/1/13
*/
public interface Factory {
//创建汽车产品
public Car createCar();
//为汽车绑定保险
public Insurance attachInsurance();
}
具体工厂实现类BusFactory.java
package com.interview.design.factory.abstractFactory.factory.impl;
import com.interview.design.factory.abstractFactory.factory.Factory;
import com.interview.design.factory.abstractFactory.product.car.Bus;
import com.interview.design.factory.abstractFactory.product.car.Car;
import com.interview.design.factory.abstractFactory.product.insurance.Insurance;
import com.interview.design.factory.abstractFactory.product.insurance.LifeInsurance;
/**
* @Author: gtd
* @Description:
* @Date: Create in 0:28 2019/1/13
*/
public class BusFactory implements Factory {
@Override
public Car createCar() {
return new Bus(); //Bus
}
@Override
public Insurance attachInsurance() {
return new LifeInsurance(); //人寿
}
}
具体抽象工厂实现类TrucksFactory.java
package com.interview.design.factory.abstractFactory.factory.impl;
import com.interview.design.factory.abstractFactory.factory.Factory;
import com.interview.design.factory.abstractFactory.product.car.Car;
import com.interview.design.factory.abstractFactory.product.car.Trucks;
import com.interview.design.factory.abstractFactory.product.insurance.Insurance;
import com.interview.design.factory.abstractFactory.product.insurance.PingAnInsurance;
/**
* @Author: gtd
* @Description:
* @Date: Create in 0:41 2019/1/13
*/
public class TrucksFactory implements Factory {
@Override
public Car createCar() {
return new Trucks(); //Truck
}
@Override
public Insurance attachInsurance() {
return new PingAnInsurance(); //平安
}
}
我们的产品(汽车)抽象类Car.java
package com.interview.design.factory.abstractFactory.product.car;
/**
* @Author: gtd
* @Description:
* @Date: Create in 0:30 2019/1/13
*/
public abstract class Car {
public String name;
public abstract void drive();
}
抽象产品(汽车)类的具体实现类Bus.java
package com.interview.design.factory.abstractFactory.product.car;
/**
* @Author: gtd
* @Description:
* @Date: Create in 0:32 2019/1/13
*/
public class Bus extends Car {
@Override
public void drive() {
System.out.println("欢迎购买一辆Bus");
}
}
另一个汽车抽象类的具体实现Trucks.java
package com.interview.design.factory.abstractFactory.product.car;
/**
* @Author: gtd
* @Description:
* @Date: Create in 0:35 2019/1/13
*/
public class Trucks extends Car {
@Override
public void drive() {
System.out.println("欢迎购买一辆Trucks");
}
}
我们的另外一种产品(保险)的抽线类Insurance.java
package com.interview.design.factory.abstractFactory.product.insurance;
/**
* @Author: gtd
* @Description:
* @Date: Create in 0:30 2019/1/13
*/
public abstract class Insurance {
//保险名称
public String name;
//打印信息
public abstract void print();
}
抽象产品(保险)类的具体实现类LifeInsurance.java
package com.interview.design.factory.abstractFactory.product.insurance;
/**
* @Author: gtd
* @Description:
* @Date: Create in 0:37 2019/1/13
*/
public class LifeInsurance extends Insurance {
@Override
public void print() {
System.out.println("您的爱车已经投保人寿保险");
}
}
抽象产品(保险)类的其他具体实现类PingAnInsurance.java
package com.interview.design.factory.abstractFactory.product.insurance;
/**
* @Author: gtd
* @Description:
* @Date: Create in 0:39 2019/1/13
*/
public class PingAnInsurance extends Insurance {
@Override
public void print() {
System.out.println("您的爱车已经投保平安保险");
}
}
下面是我们的测试类TestAbstractFactory.java
package com.interview.design.factory.abstractFactory;
import com.interview.design.factory.abstractFactory.factory.Factory;
import com.interview.design.factory.abstractFactory.factory.impl.BusFactory;
import com.interview.design.factory.abstractFactory.factory.impl.TrucksFactory;
import com.interview.design.factory.abstractFactory.product.car.Car;
import com.interview.design.factory.abstractFactory.product.insurance.Insurance;
/**
* @Author: gtd
* @Description:
* @Date: Create in 0:42 2019/1/13
*/
public class TestAbstractFactory {
public static void main(String[] args) {
Factory factory;
Car car;
Insurance insurance;
factory = new BusFactory();
car = factory.createCar();
car.drive();
insurance = factory.attachInsurance();
insurance.print();
System.out.println();
factory = new TrucksFactory();
car = factory.createCar();
car.drive();
insurance = factory.attachInsurance();
insurance.print();
}
}
测试类运行结果如下:
欢迎购买一辆Bus
您的爱车已经投保人寿保险
欢迎购买一辆Trucks
您的爱车已经投保平安保险
Process finished with exit code 0
抽象工厂模式的优点 (引用 https://www.cnblogs.com/zailushang1996/p/8601808.html)
抽象工厂模式除了具有工厂方法模式的优点外,最主要的优点就是可以在类的内部对产品族进行约束。所谓的产品族,一般或多或少的都存在一定的关联,抽象工厂模式就可以在类内部对产品族的关联关系进行定义和描述,而不必专门引入一个新的类来进行管理。
抽象工厂模式的缺点
产品族的扩展将是一件十分费力的事情,假如产品族中需要增加一个新的产品,则几乎所有的工厂类都需要进行修改。所以使用抽象工厂模式时,对产品等级结构的划分是非常重要的。
至此我们的工厂模式是代码简单讲解和代码实现已经完成了,代码上传地址为:https://github.com/tiedungao/interviewDesignModelProject
-----------------------------------------------------------------------------
优缺点及使用场景:
简单工厂模式:
① 它是一个具体的类,非接口 抽象类。有一个重要的create()方法,利用if或者 switch创建产品并返回。
② create()方法通常是静态的,所以也称之为静态工厂。
优点:
①单工厂模式实现了生成产品类的代码跟客户端代码分离,有一定的解耦作用
②我们可以在工厂类中对象实例生成前后的一些业务逻辑代码。
缺点:
①扩展性差,当需要新增产品类的时候需要修改工厂代码。
② 不同的产品需要不同额外参数的时候 不支持。
-----------------------------------------------------------------------------------------------------
工厂方法模式:
工厂方法模式是简单工厂模式的衍生,解决了许多简单工厂模式的问题。首先完全实现‘开-闭 原则’,实现了可扩展。其次更复杂的层次结构,可以应用于产品结果复杂的场合
优点:
①可以一定程度上解耦。
②可以一定程度上增加扩展性,满足"开-闭原则"。
③屏蔽产品类。产品类的实现如何变化,调用者都不需要关心,只需关心产品的接口,只要接口保持不变,系统中的上层模块就不会发生变化
缺点:
①如果产品类过多,我们就要生成很多的工厂类,需要在横向上增加很多工厂类,不能把有关系的业务 逻辑进行很好的封装。
------------------------------------------------------------------------------------------------------------
抽象工厂模式:
抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。抽象工厂模式可以向客户端提供一个接口,使客户端在不必指定产品的具体的情况下,创建多个产品族中的产品对象。工厂类负责创建抽象产品的具体子类的实例。
优点:
①拥有上面两种工厂模式的优点
②当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
缺点:
当发现使用抽象工厂模式有缺点的时候那就是不适合用工厂模式的时候。嘻嘻…^_^!