写在前面
1:介绍
1.1:什么时候用工厂方法
当我们有若干个种类的对象需要创建,并且每个种类的对象需要由多个对象
协调工作才能完成任务,此时可以考虑使用抽象工厂设计模式。
1.2:UML类图
工厂方法设计模式,包含如下元素:
1:抽象工厂类工厂类的抽象,定义多个用于生产对象的方法(这里不同于工厂方法只有一个生产对象的方法)。
2:具体工厂类继承抽象工厂类,用于生产具体的某项类型的对象(产品)。
3:产品类需要被创建的类,一般由多个类构成,这多个类,统一协作,才能完成工作(这里不同于工厂方法只需要创建一个对象)。
4:客户端客户端根据需要使用对应的具体工厂类来创建具体的产品(对象)。
2:实例
源码 。
2.1:场景
分别需要生产自行车,汽车,飞机,这三类产品,但是每种产品都需要刹车类,轮子类,二者组合才能正常工作。
2.2:程序
- 定义三个刹车类(包括接口)
public interface Brake {void brake();
}public class BicycleBrake implements Brake {@Overridepublic void brake() {System.out.println(this.getClass().getSimpleName() + "刹车了");}
}public class CarBrake implements Brake {@Overridepublic void brake() {System.out.println(this.getClass().getSimpleName() + "刹车了");}
}public class PlaneBrake implements Brake {@Overridepublic void brake() {System.out.println(this.getClass().getSimpleName() + "刹车了");}
}
- 定义三个轮子类(包括接口)
public interface Wheel {void run();
}public class BicycleWheel implements Wheel {@Overridepublic void run() {System.out.println(this.getClass().getSimpleName() + " 跑了");}
}public class CarWheel implements Wheel {@Overridepublic void run() {System.out.println(this.getClass().getSimpleName() + " 跑了");}
}public class PlaneWheel implements Wheel {@Overridepublic void run() {System.out.println(this.getClass().getSimpleName() + " 跑了");}
}
- 定义3个具体的产品类(包括接口)
public interface Vehicle {void sayHi();
}@Getter
@Setter
//@NoArgsConstructor
@AllArgsConstructor
public class Bicycle implements Vehicle {private Brake brake;private Wheel wheel;@Overridepublic void sayHi() {System.out.println(this.getClass().getSimpleName() + "组装完毕!");}
}@Getter
@Setter
//@NoArgsConstructor
@AllArgsConstructor
public class Car implements Vehicle {private Brake brake;private Wheel wheel;@Overridepublic void sayHi() {System.out.println(this.getClass().getSimpleName() + "组装完毕!");}
}@Getter
@Setter
//@NoArgsConstructor
@AllArgsConstructor
public class Plane implements Vehicle {private Brake brake;private Wheel wheel;@Overridepublic void sayHi() {System.out.println(this.getClass().getSimpleName() + "组装完毕!");}
}
- 定义抽象工厂类以及3个具体工厂类
// A B C 分别代表 刹车 轮子 成品
public abstract class AbstractFactory<A, B, C> {// 创建刹车抽象方法protected abstract A createBrake();// 创建轮子抽象方法protected abstract B createWheel();// 创建产品public abstract C createProduct();
}public class BicycleFactory extends AbstractFactory<BicycleBrake, BicycleWheel, Bicycle> {@Overrideprotected BicycleBrake createBrake() {return new BicycleBrake();}@Overrideprotected BicycleWheel createWheel() {return new BicycleWheel();}@Overridepublic Bicycle createProduct() {Brake brake = createBrake();brake.brake();Wheel wheel = createWheel();wheel.run();return new Bicycle(brake, wheel);}
}public class CarFactory extends AbstractFactory<CarBrake, CarWheel, Car> {@Overrideprotected CarBrake createBrake() {return new CarBrake();}@Overrideprotected CarWheel createWheel() {return new CarWheel();}@Overridepublic Car createProduct() {Brake brake = createBrake();brake.brake();Wheel wheel = createWheel();wheel.run();return new Car(brake, wheel);}
}public class PlaneFactory extends AbstractFactory<PlaneBrake, PlaneWheel, Plane> {@Overrideprotected PlaneBrake createBrake() {return new PlaneBrake();}@Overrideprotected PlaneWheel createWheel() {return new PlaneWheel();}@Overridepublic Plane createProduct() {Brake brake = createBrake();brake.brake();Wheel wheel = createWheel();wheel.run();return new Plane(brake, wheel);}
}
- 测试
public class AbstractfactoryTest {public static void main(String[] args) {CarFactory carFactory = new CarFactory();carFactory.createProduct().sayHi();System.out.println("----------");BicycleFactory bicycleFactory = new BicycleFactory();bicycleFactory.createProduct().sayHi();System.out.println("----------");PlaneFactory planeFactory = new PlaneFactory();planeFactory.createProduct().sayHi();}
}
运行:
CarBrake刹车了
CarWheel 跑了
Car组装完毕!
----------
BicycleBrake刹车了
BicycleWheel 跑了
Bicycle组装完毕!
----------
PlaneBrake刹车了
PlaneWheel 跑了
Plane组装完毕!Process finished with exit code 0