01.抽象工厂模式——对象创建型模式

01.抽象工厂模式——对象创建型模式

意图

提供一个接口以创建一系列相关或相互依赖的对象,而无需指定他们具体的类

适用性

  • 一个系统要独立于它的产品的创建、组合和表示
  • 一个系统要由多个产品系列中的一个来配置
  • 要强调一系列相关的产品对象的设计以便进行联合使用
  • 提供一个产品类库,但只想显示他们的接口而不是实现

结构

参与者

  • Abstract Factory

    ​ 声明一个创建抽象产品对象的操作接口

  • Concrete Factory

    ​ 实现创建具体产品对象的操作

  • Abstract Product

    ​ 为一类产品对象声明一个接口

  • Concrete Product

    ​ 定义一个将要被相应的具体工厂创建的产品对象并实现Abstract Product接口

  • Client

    ​ 仅适用由Abstract Factory和Abstract Product类声明的接口

效果

  • 它分离了具体的类(pro)

  • 它使得易于交换产品系列(pro)

  • 它有利于产品的一致性(pro)

  • 难以支持新种类的产品(con)

实现

基本思路

​ 我们考虑每个人穿衣服和穿裤子两项最基本的操作对抽象工厂模式进行一个具体的实现。

基本结构

在这里插入图片描述

具体实现

Abstract Factory类
public abstract class AbstractFactory {
	public abstract Clothes getClothes(String order);
	public abstract Trousers getTrousers(String order);
}

Clothes Factory类
public class ClothesFactory extends AbstractFactory{

	@Override
	public Clothes getClothes(String order) {
		// TODO Auto-generated method stub
		if (order.equalsIgnoreCase("JACKET")) {
			return new Jacket();
		}
		else if (order.equalsIgnoreCase("SWEATER")) {
			return new Sweater();
		}
		else if (order.equalsIgnoreCase("SHIRT")) {
			return new Shirt();
		}
		else {
			return null;
		}
	}

	@Override
	public Trousers getTrousers(String order) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
Trousers Factory类
public class TrousersFactory extends AbstractFactory{

	@Override
	public Clothes getClothes(String order) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Trousers getTrousers(String order) {
		// TODO Auto-generated method stub
		if (order.equalsIgnoreCase("JEAN")) {
			return new Jean();
		}
		else if (order.equalsIgnoreCase("OVERALL")) {
			return new Overall();
		}
		else if (order.equalsIgnoreCase("SLACK")) {
			return new Slacks();
		}
		else {
			return null;
		}
	}

}
Clothes接口
public interface Clothes {
	public void dressClothes();
}

Jacket类对Clothes接口进行具体实现
public class Jacket implements Clothes{
	@Override
	public void dressClothes() {
		// TODO Auto-generated method stub
		System.out.println("Jacket Dressed!");
	}
}
程序主入口
public class AbstractFactoryPatternDemo {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		AbstractFactory clothesFactory = new ClothesFactory();
		AbstractFactory trousersFactory = new TrousersFactory();
		Clothes clothes = clothesFactory.getClothes("jacket");
		Trousers trousers = trousersFactory.getTrousers("overall");
		clothes.dressClothes();
		trousers.dressTrousers();
	}
}
程序主入口
public class AbstractFactoryPatternDemo {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		AbstractFactory clothesFactory = new ClothesFactory();
		AbstractFactory trousersFactory = new TrousersFactory();
		Clothes clothes = clothesFactory.getClothes("jacket");
		Trousers trousers = trousersFactory.getTrousers("overall");
		clothes.dressClothes();
		trousers.dressTrousers();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值