JAVA设计模式之工厂模式
一、问题引入:
在面向对象编程中,最通常的方法是new一个实例化对象,但在一些情况下,这时模式在不需要了解细节过程的前提下,就能解决构造对象实例的问题。
工厂模式概述(以宝马车为例):
1.还没有工厂时代:如果一个客户要一款宝马车,一般的做法是客户自己创建一款宝马车,然后拿来用
2.简单工厂模式:客户不需要自己创建宝马车,有一个工厂会帮他创建。客户想要什么系列的车,工厂就创建这个系列的车,即工厂可以创建产品。
3.工厂方法模式:为了满足客户的需求,宝马车的系列越来越多,(工厂抽象的)于是分出来很多工厂,每个工厂具体生产一个系列的车,即一个工厂类只能创建一个具体的产品。需要哪个系列的车就指定哪个工厂生产这个系列的车。
4.抽象工厂模式:例:宝马320系列配置A型号空调和A型号发动机,宝马230系列配置B型号空调和B型号发动机。
最终客户只要对宝马车的工厂提出需求即可,而不需要自己创建宝马车。
二、分类
工厂模式可分为三类:
简单工厂模式
工厂方法模式:
一个抽象产品类,可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类只能创建一个具体产品类的实例。
抽象工厂模式:
多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类可以创建多个具体产品类的实例。
三、区别(工厂方法模式和抽象工厂模式):
工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。
工厂方法模式的具体工厂类只能创建一个具体的产品类的实例,而抽象工厂模式可以创建多个
四、优点:可不修改源代码,进行扩展
五、代码实现:
无工厂时代:客户自己创建宝马车
public class BMW320 { public BMW320(){ System.out.println("制造-->BMW320"); } } public class BMW523 { public BMW523(){ System.out.println("制造-->BMW523"); } } public class Customer { public static void main(String[] args) { BMW320 bmw320 = new BMW320(); BMW523 bmw523 = new BMW523(); } }
简单工厂模式:一个抽象产品类,有多个具体的具体产品类;只有一个具体的工厂类,一个工厂类可以创建多个具体的产品类
abstract class BMW {
public BMW(){
}
}
public class BMW320 extends BMW {
public BMW320() {
System.out.println("制造-->BMW320");
}
}
public class BMW523 extends BMW{
public BMW523(){
System.out.println("制造-->BMW523");
}
}
工厂类:
public class Factory {
public BMW createBMW(int type) {
switch (type) {
case 320:
return new BMW320();
case 523:
return new BMW523();
default:
break;
}
return null;
}
}
客户类:
public class Customer { public static void main(String[] args) { Factory factory = new Factory(); BMW bmw320 = factory.createBMW(320); BMW bmw523 = factory.createBMW(523); }
}
工厂方法模式:一个抽象工厂类,有多个具体工厂类;一个抽象产品类,有多个具体产品类;每个具体工厂类类只能创建一种具体产品
产品类:
产品类:
- abstract class BMW {
- public BMW(){
- }
- }
- public class BMW320 extends BMW {
- public BMW320() {
- System.out.println("制造-->BMW320");
- }
- }
- public class BMW523 extends BMW{
- public BMW523(){
- System.out.println("制造-->BMW523");
- }
- }
创建工厂类:
- interface FactoryBMW {
- BMW createBMW();
- }
- public class FactoryBMW320 implements FactoryBMW{
- @Override
- public BMW320 createBMW() {
- return new BMW320();
- }
- }
- public class FactoryBMW523 implements FactoryBMW {
- @Override
- public BMW523 createBMW() {
- return new BMW523();
- }
- }
客户类:
- public class Customer {
- public static void main(String[] args) {
- FactoryBMW320 factoryBMW320 = new FactoryBMW320();
- BMW320 bmw320 = factoryBMW320.createBMW();
- FactoryBMW523 factoryBMW523 = new FactoryBMW523();
- BMW523 bmw523 = factoryBMW523.createBMW();
- }
- }
抽象工厂模式:多个抽象产品类,每个抽象产品类可以派生出多个具体产品类;一个抽象工厂类,每个抽象工厂类可以派生出多个具体工厂类,每个具体工厂类可以创建多个产品类。
产品类:
- //发动机以及型号
- public interface Engine {
- }
- public class EngineA extends Engine{
- public EngineA(){
- System.out.println("制造-->EngineA");
- }
- }
- public class EngineBextends Engine{
- public EngineB(){
- System.out.println("制造-->EngineB");
- }
- }
- //空调以及型号
- public interface Aircondition {
- }
- public class AirconditionA extends Aircondition{
- public AirconditionA(){
- System.out.println("制造-->AirconditionA");
- }
- }
- public class AirconditionB extends Aircondition{
- public AirconditionB(){
- System.out.println("制造-->AirconditionB");
- }
- }
创建工厂类:
- //创建工厂的接口
- public interface AbstractFactory {
- //制造发动机
- public Engine createEngine();
- //制造空调
- public Aircondition createAircondition();
- }
- //为宝马320系列生产配件
- public class FactoryBMW320 implements AbstractFactory{
- @Override
- public Engine createEngine() {
- return new EngineA();
- }
- @Override
- public Aircondition createAircondition() {
- return new AirconditionA();
- }
- }
- //宝马523系列
- public class FactoryBMW523 implements AbstractFactory {
- @Override
- public Engine createEngine() {
- return new EngineB();
- }
- @Override
- public Aircondition createAircondition() {
- return new AirconditionB();
- }
- }
客户:
- public class Customer {
- public static void main(String[] args){
- //生产宝马320系列配件
- FactoryBMW320 factoryBMW320 = new FactoryBMW320();
- factoryBMW320.createEngine();
- factoryBMW320.createAircondition();
- //生产宝马523系列配件
- FactoryBMW523 factoryBMW523 = new FactoryBMW523();
- factoryBMW320.createEngine();
- factoryBMW320.createAircondition();
- }
- }