工厂模式
1、简单工厂模式:
首先我们有一个Car类,这个Car可以go:
public class Car {
public void go() {
System.out.println("car go ...");
}
}
还有一个调用者,这样就可以造出车来跑:
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.go();
}
}
此时如果我们想把car换成plane,那就需要新加一个plane类,并且修改main:
public class Plane {
public void go(){
System.out.println("plane fly ... ");
}
}
public class Main {
public static void main(String[] args) {
// Car car = new Car();
// car.go();
Plane plane = new Plane();
plane.go();
}
}
如果之后又要换成其他交通工具,那就得频繁修改main中代码,修改代码是必须的,但是怎么才能做到尽量少的修改代码呢?这时我们把交通工具抽象出来。新建Moveable接口:
public interface Moveable {
void go();
}
并且让交通工具实现这个接口:
public class Car implements Moveable{
public void go() {
System.out.println("car go ...");
}
}
public class Plane implements Moveable {
public void go(){
System.out.println("plane fly ... ");
}
}
此时我们的main,当以后我们需要变换其他的交通工具时,只需要修改new后面的就可以,其他地方不用动:
public class Main {
public static void main(String[] args) {
// Car car = new Car();
// car.go();
// Plane plane = new Plane();
// plane.go();
Moveable m = new Car();
m.go();
}
}
现在我们已经实现了自由切换交通工具了,但是再思考一个问题:如果现在我们想在new交通工具的时候,再加一些流程,比如日志,在每次造车前,记录一下车的生产日期等等,而如果这些代码都写在main中,会有大量的if判断,因为有汽车,有飞机,有火车…它们的制作方法都是不一样的,此时我们需要定义一个工厂:
public class SimpleFactory {
public Car createCar()
{
// Do something before create car....
return new Car();
}
public Plane createPlane()
{
// Do something before create plane....
return new Plane();
}
}
这个工厂负责每个交通工具生产前后的处理,而调用者一方只需要new一个工厂然后制造交通工具,其他的细节交给工厂来处理,这就是简单工厂模式:
public static void main(String[] args) {
Moveable m = new SimpleFactory().createCar();
m.go();
}
这个方式存在的缺陷是每当新增一个新的交通工具时,还是需要在SimpleFactory类中新增方法,还是需要修改源码,这还是不利于扩展,不符合开闭原则。所以我们现在考虑为每一个产品设计一个工厂:
2、工厂方法
为每一种交通工具设计工厂:
//造汽车的汽车工厂
public class CarFactory {
public Moveable createCar()
{
// Do something before create car ...
System.out.println("A car created!");
return new Car();
}
}
//造飞机的飞机工厂
public class PlaneFactory {
public Moveable createPlane()
{
// Do something before create plane ...
System.out.println("A plane created!");
return new Plane();
}
}
main:
public static void main(String[] args) {
Moveable m = new CarFactory().createCar();
m.go();
}
以后新增产品时,只需要新建一个产品类,然后新建一个对于该产品的工厂类(XXXFactory),这样就不必修改源码,即可完成拓展。
3、抽象工厂模式
考虑这样一种情形,描述一个人他开什么交通工具,穿什么衣服,用什么手机,这三类产品构成了产品簇
public class Car{
public void go() {
System.out.println("car go ...");
}
}
public class Jeans {
public void printName()
{
System.out.println("I'm wearing Jeans.");
}
}
public class CellPhone {
public void call()
{
System.out.println("Calling ...");
}
}
main
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.go();
Jeans jeans = new Jeans();
jeans.printName();
CellPhone cellPhone = new CellPhone();
cellPhone.call();
}
}
这样描述的是一个人开着车,穿的是牛仔裤,用的是大哥大手机。现在考虑另一个人他开着飞机,穿着休闲服,用的是智能手机,那我们就需要建立飞机类,休闲服类,智能手机类:
public class Plane {
public void go(){
System.out.println("plane fly ... ");
}
}
public class SmartPhone {
public void call()
{
System.out.println("Calling ...");
}
}
public class SportsWear {
public void printName()
{
System.out.println("I'm wearing sportswear.");
}
}
main:
public class Main {
public static void main(String[] args) {
// Car car = new Car();
// car.go();
//
// Jeans jeans = new Jeans();
// jeans.printName();
//
// CellPhone cellPhone = new CellPhone();
// cellPhone.call();
Plane plane = new Plane();
plane.go();
SportsWear s = new SportsWear();
s.printName();
SmartPhone smartPhone = new SmartPhone();
smartPhone.call();
}
}
当这个人是旧时代的,那他是驾马车,穿旧时代的衣服,用书信进行沟通
当这个人是现代的,那他是开汽车,穿现代的衣服,用智能手机进行沟通
这就构成了产品族,我们如何灵活的组合不同类型的产品呢?
当前我们的代码,如果我们想换产品族,就得修改全部的代码:
Plane plane = new Plane();
plane.go();
SportsWear s = new SportsWear();
s.printName();
SmartPhone smartPhone = new SmartPhone();
smartPhone.call();
有没有什么方法可以灵活扩展产品族呢?
抽象工厂可以实现。
我们将三种产品分别抽象出:
// 交通工具抽象类
public abstract class Vehicle {
public abstract void go();
}
// 衣着抽象类
public abstract class Clothes {
public abstract void printName();
}
// 通讯工具抽象类
public abstract class CommunicationTools {
public abstract void call();
}
在声明一个抽象工厂,这个工厂可以生产交通工具,衣服,通讯工具:
public abstract class AbstractFactory {
abstract Vehicle createVehicle();
abstract Clothes createClothe();
abstract CommunicationTools createCommunicationTool();
}
然后修改刚才的产品继承自抽象类:
public class Car extends Vehicle{
public void go() {
System.out.println("car go ...");
}
}
public class Jeans extends Clothes{
public void printName()
{
System.out.println("I'm wearing Jeans.");
}
}
public class CellPhone extends CommunicationTools{
public void call()
{
System.out.println("Calling ...");
}
}
//....
现在我们新建一个ModernFactory,这个工厂继承自AbstractFactory,它负责提供汽车,休闲装,智能手机:
public class ModernFactory extends AbstractFactory {
@Override
Vehicle createVehicle() {
return new Car();
}
@Override
Clothes createClothe() {
return new SportsWear();
}
@Override
CommunicationTools createCommunicationTool() {
return new SmartPhone();
}
}
修改我们的main
public class Main {
public static void main(String[] args) {
AbstractFactory factory = new ModernFactory();
Clothes clothe = factory.createClothe();
CommunicationTools tool = factory.createCommunicationTool();
Vehicle vehicle = factory.createVehicle();
}
}
这样做的好处就是当我们需要不同的产品族时,只需要新增对应产品的类,新增工厂类,然后直接修改AbstractFactory factory = new ModernFactory();
这一行代码即可,其他地方完全不用动。
工厂方法可以灵活的实现横向产品扩展,抽象工厂可以灵活的定制产品族,这两种模式一个有利于横向产品扩展,一个有利于纵向的产品族定制。