设计模式——抽象工厂(Abstract Factory Pattern)

抽象工厂:

当你的汽车公司做到一定程度后,公司开始为土豪们做私人订制一些系列车型!比如说、中国土豪私人订制的要求是一家三口都要不同样式的汽车,孩子要酷炫式、母亲要青春式、父亲要高贵式!当方案一推出英国和日本的土豪们都很欣赏很想要类似的私人定制,公司响应他们的要求作出了整体的汽车设计方案,汽车的设计方案如下(图中为了方便省略了日本):


汽车设计方案UML类图


为了生产该方案的汽车,公司对各地的汽车工厂生产模式做了对应的调研,提出两种修改方案:

一、为每个产品方案创建一个Factory层次类(及工厂模型),如ChildCarFactory生产ChineseChildCar和EnglishChildCar。这种方法可行但为每个地域创建私人订制需要多个Factory层次类,而且工厂类太多(不考虑某地到某地的运费等),比较麻烦!

二、以上3个层次类图具有相同的结构,可以改造工厂方式模式,让一个工厂层次类就可以负担的起创建3组产品对象的责任,如ChineseFactory生产ChineseChildCar、ChineseMatherCar和ChineseFatherCar。这样不但减少了Factory层次类的个数,而且每个地域的私人订制只需要一个Factory层次类(即本地工厂)就能满足需求,比较方便。

第二种方案就是抽象工厂!上述对应的工厂方案设计如下:


工厂的UML类图

然后,将工厂和汽车关联起来的类图如下:



工厂模式的UML类图

抽象工厂类代码:

public interface CarFactory {//工厂接口
	public ChildCar getChildCar();
	public MatherCar getMatherCar();
	public FatherCar getFatherCar();
}

public interface ChildCar {//ChildCar接口
	public String getCarType();
}

public interface FatherCar {//FatherCar接口
	public String getCarType();
}

public interface MatherCar {//MatherCar接口
	public String getCarType();
}

public class ChineseCarFactory implements CarFactory {//CarFactory的实例工厂ChineseCarFactory
	public ChildCar getChildCar() {
		return new ChineseChildCar();
	}
	
	public MatherCar getMatherCar() {
		return new ChineseMatherCar();
	}
	
	public FatherCar getFatherCar() {
		return new ChineseFatherCar();
	}
}

public class ChineseChildCar implements ChildCar {//中国式ChildCar的实现 下同
	public String getCarType() {
		return "ChineseChildCar";
	}
}

public class ChineseFatherCar implements FatherCar {
	public String getCarType() {
		return "ChineseFatherCar";
	}
}

public class ChineseMatherCar implements MatherCar {
	public String getCarType() {
		return "ChineseMatherCar";
	}
}


public class EnglishCarFactory implements CarFactory {//CarFactory的实例工厂EnglishCarFactory
	public ChildCar getChildCar() {
		return new EnglishChildCar();
	}
	
	public MatherCar getMatherCar() {
		return new EnglishMatherCar();
	}
	
	public FatherCar getFatherCar() {
		return new EnglishFatherCar();
	}
}

public class EnglishChildCar implements ChildCar {//英国式ChildCar的实现 下同
	public String getCarType() {
		return "EnglishChildCar";
	}
}

public class EnglishFatherCar implements FatherCar {
	public String getCarType() {
		return "EnglishFatherCar";
	}
}

public class EnglishMatherCar implements MatherCar {
	public String getCarType() {
		return "EnglishMatherCar";
	}
}
测试类:

public class Client {

	public static void main(String[] args) {
		CarFactory fa;
		ChildCar childCar;
		MatherCar matherCar;
		FatherCar fatherCar;
		
		fa = new ChineseCarFactory();
		childCar = fa.getChildCar();
		matherCar = fa.getMatherCar();
		fatherCar = fa.getFatherCar();
		System.out.println(childCar.getCarType() + "\n" +
				matherCar.getCarType() + "\n" +
				fatherCar.getCarType() + "\n");
		
		fa = new EnglishCarFactory();
		childCar = fa.getChildCar();
		matherCar = fa.getMatherCar();
		fatherCar = fa.getFatherCar();
		System.out.println(childCar.getCarType() + "\n" +
				matherCar.getCarType() + "\n" +
				fatherCar.getCarType() + "\n");
	}

}
运行结果:

ChineseChildCar
ChineseMatherCar
ChineseFatherCar

EnglishChildCar
EnglishMatherCar
EnglishFatherCar

抽象工厂的运用场景:

       当客户对象要从一个相关的产品组中创建一个对象,而没有必要知道到底创建哪个对象时,可以应用抽象工厂模式

抽象工厂的优缺点:

优点:

1、可以产生一系列的产品实例

2、产生新系列产品很方便扩展性很好,只需要实现对应的产品接口和工厂接口就能轻松实现系列扩充,符合开闭原则

缺点:

    添加新产品改动较大,如添加一个新产品XXCar就要改动CarFactory接口不符合开闭原则


















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值