使用Java语言编写设计模式-7.创建型模式-抽象工厂模式
原创2022年9月8日大约 8 分钟约 2367 字
7.创建型模式-抽象工厂模式
7.1.章节内容概述
本章节涉及主要内容有:
7.1.章节内容概述
7.2.章节内容大纲
7.3.简介
7.4.应用场景
7.5.优缺点
7.6.角色及其职责
7.7.模型
7.8.示例
7.9.在开源框架中的应用
具体每个小节中包含的内容可使通过下面的章节内容大纲进行查看。
7.2.章节内容大纲
7.3.简介
抽象工厂模式(Abstract Factory Pattern)是一种创建型设计模式,是工厂方法模式的升级版本,解决了简单工厂模式中工厂类职责太重的问题,抽象工厂中提供一个创建一系列相关或相互依赖对象的接口,这个接口中定义了创建多个不同的对象方法,但是让该接口的子类去实例化具体的类,把实例化具体对象的过程延迟到了子类中去实现。与工厂方法模式不同的是,抽象工厂模式中的具体工厂不再是只能创建一种产品,一个具体的工厂可以创建一个产品族的产品。
7.4.应用场景
a.客户端(应用层)不依赖于产品类实例如何被创建、实现等细节
b.强调一系列相关的产品对象(属于同一产品族)一起使用创建对象需要大量重复的代码
c.提供一个产品类的库,所有的产品以同样的接口出现,从而使得客户端不依赖于具体的实现
7.5.优缺点
7.5.1.优点
a.具体产品在应用层的代码隔离,无需关心创建的细节
b.将一个系列的产品统一到一起创建
7.5.2.缺点
a.具体产品在应用层的代码隔离,无需关系创建的细节
b.将一个系列的产品统一到一起创建
7.6.角色及其职责
AbstractFactory
提供了创建产品的接口,它包含多个创建产品的方法,可以创建多个不同等级的产品
ConcreteFactory
主要是实现抽象工厂中的多个抽象方法,完成具体产品的创建
AbstractProduct
定义了产品的规范,描述了产品的主要特性和功能,抽象工厂模式有多个抽象产品
ConcreteProduct
实现了抽象产品角色所定义的接口,由具体工厂来创建,它同具体工厂之间是多对一的关系
7.7.模型
7.7.1.模型类图
模型类图(有Client)
模型类图(无Client)
7.7.2.模型代码
ProductPart1.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* 产品的Part1部分接口
*/
public interface ProductPart1 {
}
ProductPart1LevelA.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* 产品的Part1部分 A级别
*/
public class ProductPart1LevelA implements ProductPart1{
@Override
public String toString() {
return "ProductPart1-LevelA";
}
}
ProductPart1LevelB.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* 产品的Part1部分 B级别
*/
public class ProductPart1LevelB implements ProductPart1{
@Override
public String toString() {
return "ProductPart1-LevelB";
}
}
ProductPart1LevelC.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* 产品的Part1部分 C级别
*/
public class ProductPart1LevelC implements ProductPart1{
@Override
public String toString() {
return "ProductPart1-LevelC";
}
}
ProductPart2.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* 产品的Part2部分接口
*/
public interface ProductPart2 {
}
ProductPart2LevelA.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* 产品的Part2部分 A级别
*/
public class ProductPart2LevelA implements ProductPart2{
@Override
public String toString() {
return "ProductPart2-LevelA";
}
}
ProductPart2LevelB.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* 产品的Part2部分 B级别
*/
public class ProductPart2LevelB implements ProductPart2{
@Override
public String toString() {
return "ProductPart2-LevelB";
}
}
ProductPart2LevelC.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* 产品的Part2部分 C级别
*/
public class ProductPart2LevelC implements ProductPart2{
@Override
public String toString() {
return "ProductPart2-LevelC";
}
}
ProductPart3.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* 产品的Part3部分接口
*/
public interface ProductPart3 {
}
ProductPart3LevelA.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* 产品的Part3部分 A级别
*/
public class ProductPart3LevelA implements ProductPart3{
@Override
public String toString() {
return "ProductPart3-LevelA";
}
}
ProductPart3LevelB.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* 产品的Part3部分 B级别
*/
public class ProductPart3LevelB implements ProductPart3{
@Override
public String toString() {
return "ProductPart3-LevelB";
}
}
ProductPart3LevelC.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* 产品的Part3部分 C级别
*/
public class ProductPart3LevelC implements ProductPart3{
@Override
public String toString() {
return "ProductPart3-LevelC";
}
}
AbstractFactory.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* AbstractFactory(抽象工厂:用于创建一个产品族的产品)
* ProductPart1,ProductPart2,ProductPart3是一个产品族中的不同产品,可以理解为如下情况
* a.汽车案例
* Product : 汽车
* 产品族: 轮胎、座椅、方向盘
* ProductPart1 : 轮胎
* ProductPart1LevelA : 特斯拉轮胎
* ProductPart1LevelB : 丰田轮胎
* ProductPart1LevelC : 五菱轮胎
* ProductPart2 : 座椅
* ProductPart2LevelA : 特斯拉座椅
* ProductPart2LevelB : 丰田座椅
* ProductPart2LevelC : 五菱座椅
* ProductPart3 : 方向盘
* ProductPart3LevelA : 特斯拉方向盘
* ProductPart3LevelB : 丰田方向盘
* ProductPart3LevelC : 五菱方向盘
* b.运动服装案例
* Product : 一整套运动服装
* 产品族: 上衣、裤子、鞋子
* ProductPart1 : 上衣
* ProductPart1LevelA : 耐克上衣
* ProductPart1LevelB : 安踏上衣
* ProductPart1LevelC : 特价上衣
* ProductPart2 : 裤子
* ProductPart2LevelA : 耐克裤子
* ProductPart2LevelB : 安踏裤子
* ProductPart2LevelC : 特价裤子
* ProductPart3 : 鞋子
* ProductPart3LevelA : 耐克鞋子
* ProductPart3LevelB : 耐克鞋子
* ProductPart3LevelC : 耐克鞋子
* c.工厂案例
* Product : 工厂
* 产品族: 汽车、自行车、摩托车
* ProductPart1 : 汽车
* ProductPart1LevelA : 特斯拉汽车
* ProductPart1LevelB : 长安汽车
* ProductPart1LevelC : 奇瑞汽车
* ProductPart2 : 自行车
* ProductPart2LevelA : 特斯自行车
* ProductPart2LevelB : 长安自行车
* ProductPart2LevelC : 奇瑞自行车
* ProductPart3 : 摩托车
* ProductPart3LevelA : 特斯摩托车
* ProductPart3LevelB : 长安摩托车
* ProductPart3LevelC : 奇瑞摩托车
*/
public abstract class AbstractFactory {
/**
* 通过工厂方法生产产品的Part1部分
* @return
*/
public abstract ProductPart1 factoryMethod4ProductPart1();
/**
* 通过工厂方法生产产品的Part2部分
* @return
*/
public abstract ProductPart2 factoryMethod4ProductPart2();
/**
* 通过工厂方法生产产品的Part3部分
* @return
*/
public abstract ProductPart3 factoryMethod4ProductPart3();
}
ConcreteFactoryLevelA.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* ConcreteFactoryLevelA(生产A级别Product的工厂)
*/
public class ConcreteFactoryLevelA extends AbstractFactory {
@Override
public ProductPart1 factoryMethod4ProductPart1() {
return new ProductPart1LevelA();
}
@Override
public ProductPart2 factoryMethod4ProductPart2() {
return new ProductPart2LevelA();
}
@Override
public ProductPart3 factoryMethod4ProductPart3() {
return new ProductPart3LevelA();
}
}
ConcreteFactoryLevelB.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* ConcreteFactoryLevelB(生产B级别Product的工厂)
*/
public class ConcreteFactoryLevelB extends AbstractFactory {
@Override
public ProductPart1 factoryMethod4ProductPart1() {
return new ProductPart1LevelB();
}
@Override
public ProductPart2 factoryMethod4ProductPart2() {
return new ProductPart2LevelB();
}
@Override
public ProductPart3 factoryMethod4ProductPart3() {
return new ProductPart3LevelB();
}
}
ConcreteFactoryLevelC.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
/**
* ConcreteFactoryLevelC(生产C级别Product的工厂)
*/
public class ConcreteFactoryLevelC extends AbstractFactory {
@Override
public ProductPart1 factoryMethod4ProductPart1() {
return new ProductPart1LevelC();
}
@Override
public ProductPart2 factoryMethod4ProductPart2() {
return new ProductPart2LevelC();
}
@Override
public ProductPart3 factoryMethod4ProductPart3() {
return new ProductPart3LevelC();
}
}
Client.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.model;
import org.junit.Test;
/**
* 客户端
*/
public class Client {
@Test
public void fun() {
AbstractFactory concreteFactoryLevelA = new ConcreteFactoryLevelA();
ProductPart1 productPart1LevelA = concreteFactoryLevelA.factoryMethod4ProductPart1();
ProductPart2 productPart2LevelA = concreteFactoryLevelA.factoryMethod4ProductPart2();
ProductPart3 productPart3LevelA = concreteFactoryLevelA.factoryMethod4ProductPart3();
System.out.println("productLevelA: " + productPart1LevelA +"," + productPart2LevelA + "," + productPart3LevelA);
System.out.println("--------------------");
AbstractFactory concreteFactoryLevelB = new ConcreteFactoryLevelB();
ProductPart1 productPart1LevelB = concreteFactoryLevelB.factoryMethod4ProductPart1();
ProductPart2 productPart2LevelB = concreteFactoryLevelB.factoryMethod4ProductPart2();
ProductPart3 productPart3LevelB = concreteFactoryLevelB.factoryMethod4ProductPart3();
System.out.println("productLevelB: " + productPart1LevelB +"," + productPart2LevelB + "," + productPart3LevelB);
System.out.println("--------------------");
AbstractFactory concreteFactoryLevelC = new ConcreteFactoryLevelC();
ProductPart1 productPart1LevelC = concreteFactoryLevelC.factoryMethod4ProductPart1();
ProductPart2 productPart2LevelC = concreteFactoryLevelC.factoryMethod4ProductPart2();
ProductPart3 productPart3LevelC = concreteFactoryLevelC.factoryMethod4ProductPart3();
System.out.println("productLevelC: " + productPart1LevelC +"," + productPart2LevelC + "," + productPart3LevelC);
}
}
7.8.示例
7.8.1.不使用抽象工厂模式
7.8.2.使用抽象工厂模式
类图(有Client)
类图(无Client)
代码
AbstractClothesProduceFactory.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
/**
* 服装工厂
* @author lingwh
*
*/
public abstract class AbstractClothesProduceFactory {
//生产帽子
public abstract AbstractHat produceHat();
//生产夹克衫
public abstract AbstractJacket produceJacket();
//生产裤子
public abstract AbstractTrousers produceTrousers();
//生产鞋子
public abstract AbstractShoes produceShoes();
}
AdidasClothesProduceFactory.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class AdidasClothesProduceFactory extends AbstractClothesProduceFactory {
@Override
public AbstractHat produceHat() {
return new AdidasHat();
}
@Override
public AbstractJacket produceJacket() {
return new AdidasJacket();
}
@Override
public AbstractTrousers produceTrousers() {
return new AdidasTrousers();
}
@Override
public AbstractShoes produceShoes() {
return new AdidasShoes();
}
}
NikeClothesProduceFactory.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class NikeClothesProduceFactory extends AbstractClothesProduceFactory {
@Override
public AbstractHat produceHat() {
return new NikeHat();
}
@Override
public AbstractJacket produceJacket() {
return new NikeJacket();
}
@Override
public AbstractTrousers produceTrousers() {
return new NikeTrousers();
}
@Override
public AbstractShoes produceShoes() {
return new NikeShoes();
}
}
PumaClothesProduceFactory.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class PumaClothesProduceFactory extends AbstractClothesProduceFactory {
@Override
public AbstractHat produceHat() {
return new PumaHat();
}
@Override
public AbstractJacket produceJacket() {
return new PumaJacket();
}
@Override
public AbstractTrousers produceTrousers() {
return new PumaTrousers();
}
@Override
public AbstractShoes produceShoes() {
return new PumaShoes();
}
}
AbstractHat.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public abstract class AbstractHat {
public abstract void produce();
}
AdidasHat.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class AdidasHat extends AbstractHat {
@Override
public void produce() {
System.out.println("Adidas Hat produce ok...");
}
}
NikeHat.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class NikeHat extends AbstractHat {
@Override
public void produce() {
System.out.println("Nike Hat produce ok...");
}
}
PumaHat.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class PumaHat extends AbstractHat {
@Override
public void produce() {
System.out.println("Puma Hat produce ok...");
}
}
AbstractJacket.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public abstract class AbstractJacket {
public abstract void produce();
}
AdidasJacket.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class AdidasJacket extends AbstractJacket {
@Override
public void produce() {
System.out.println("Adidas Jacket produce ok...");
}
}
NikeJacket.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class NikeJacket extends AbstractJacket {
@Override
public void produce() {
System.out.println("Nike Jacket produce ok...");
}
}
PumaJacket.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class PumaJacket extends AbstractJacket {
@Override
public void produce() {
System.out.println("Puma Jacket produce ok...");
}
}
AbstractShoes.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public abstract class AbstractShoes {
public abstract void produce();
}
AdidasShoes.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class AdidasShoes extends AbstractShoes {
@Override
public void produce() {
System.out.println("Adidas Shoes produce ok...");
}
}
NikeShoes.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class NikeShoes extends AbstractShoes {
@Override
public void produce() {
System.out.println("Nike Shoes produce ok...");
}
}
PumaShoes.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class PumaShoes extends AbstractShoes {
@Override
public void produce() {
System.out.println("Puma Shoes produce ok...");
}
}
AbstractTrousers.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public abstract class AbstractTrousers {
public abstract void produce();
}
AdidasTrousers.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class AdidasTrousers extends AbstractTrousers {
@Override
public void produce() {
System.out.println("Adidas Trousers produce ok...");
}
}
NikeTrousers.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class NikeTrousers extends AbstractTrousers {
@Override
public void produce() {
System.out.println("Nike Trousers produce ok...");
}
}
PumaTrousers.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
public class PumaTrousers extends AbstractTrousers {
@Override
public void produce() {
System.out.println("Puma Trousers produce ok...");
}
}
Client.java
package com.dragonsoft.designpattern.create.factory.abstractfactory.use;
import org.junit.Test;
public class Client {
@Test
public void fun() {
//Adidas工厂
AbstractClothesProduceFactory adidasClothesFactory = new AdidasClothesProduceFactory();
AbstractHat adidasHat = adidasClothesFactory.produceHat();
AbstractJacket adidasJacket = adidasClothesFactory.produceJacket();
AbstractTrousers adidasTrousers = adidasClothesFactory.produceTrousers();
AbstractShoes adidasShoes = adidasClothesFactory.produceShoes();
adidasHat.produce();
adidasJacket.produce();
adidasTrousers.produce();
adidasShoes.produce();
System.out.println("-------------------------------");
//Nike工厂
AbstractClothesProduceFactory nikeClothesFactory = new NikeClothesProduceFactory();
AbstractHat nikeHat = nikeClothesFactory.produceHat();
AbstractJacket nikeJacket = nikeClothesFactory.produceJacket();
AbstractTrousers nikeTrousers = nikeClothesFactory.produceTrousers();
AbstractShoes nikeShoes = nikeClothesFactory.produceShoes();
nikeHat.produce();
nikeJacket.produce();
nikeTrousers.produce();
nikeShoes.produce();
System.out.println("-------------------------------");
//Puma工厂
AbstractClothesProduceFactory pumaClothesFactory = new PumaClothesProduceFactory();
AbstractHat pumaHat = pumaClothesFactory.produceHat();
AbstractJacket pumaJacket = pumaClothesFactory.produceJacket();
AbstractTrousers pumaTrousers = pumaClothesFactory.produceTrousers();
AbstractShoes pumaShoes = pumaClothesFactory.produceShoes();
pumaHat.produce();
pumaJacket.produce();
pumaTrousers.produce();
pumaShoes.produce();
}
}
7.9.在开源框架中的应用
评论