WebCast听课录(4)

课程名:C#面向对象设计模式纵横谈(3)Abstract Factory 抽象工厂模式(创建型模式)

创建型模式要解决的问题就是如何创建一个对象的问题。使用new的问题在于: 实现依赖,不能应对具体实例化类型的变化。这个问题的解决思路是: 封装变化点—— 哪里变化,封装哪里。

WebCast听课录(4)//创建一个Road对象
WebCast听课录(4)
Roadroad=
WebCast听课录(4)roadFactory.CreateRoad();
WebCast听课录(4)

如果以后Road类改变了,例如从泥土路变成了水泥路,那么代码就必须要进行大量修改。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

关键就是如何封装变化点,将变化与系统其他稳定的部分隔离开,使其的更改不会对稳定部分产生影响

既然变化点在对象创建,因此就封装对象创建,原则是:面向接口编程——依赖接口,而非依赖实现。

最简单的解决方法:


WebCast听课录(4)classRoadFactory
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
publicstaticRoadCreateRoad()
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
returnnewRoad();
WebCast听课录(4)}

WebCast听课录(4)}

WebCast听课录(4)Roadroad
=RoadFactory.CreateRoad();
WebCast听课录(4)
WebCast听课录(4)

假设一个场景:需要构造道路房屋地道丛林”……等等对象.

WebCast听课录(4)classRoadFactory
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
publicstaticRoadCreateRoad()
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
returnnewRoad();
WebCast听课录(4)}

WebCast听课录(4)
publicstaticBuildingCreateBuilding()
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
returnnewBuilding();
WebCast听课录(4)}

WebCast听课录(4)
publicstaticTunnelCreateTunnel()
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
returnnewTunnel();
WebCast听课录(4)}

WebCast听课录(4)
publicstaticJungleCreateJungle()
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
returnnewJungle();
WebCast听课录(4)}

WebCast听课录(4)
WebCast听课录(4)

简单工厂的问题:不能应对不同系列对象的变化。比如有不同风格的游戏场景——对应不同风格的道路、房屋、地道等。解决办法:使用面向对象的技术来封装变化点。

在软件系统中,经常面临着一系列相互依赖的对象的创建工作;同时,由于需求的变化,往往存在更多系列对象的创建工作。如何应对这种变化?如何绕过常规的对象创建方法(new),提供一种封装机制来避免客户程序和这种多系列具体对象创建工作的紧耦合?

意图:提供一个接口,让该接口负责创建一系列相关或者相互依赖的对象,无需指定它们具体的类。——《设计模式》GoF


WebCast听课录(4)

WebCast听课录(4)publicabstractclassRoad
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
WebCast听课录(4)}

WebCast听课录(4)
publicabstractclassBuilding
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
WebCast听课录(4)}

WebCast听课录(4)
WebCast听课录(4)
WebCast听课录(4)
publicabstractclassFacilitiesFactory
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
publicabstractRoadCreateRoad();
WebCast听课录(4)
WebCast听课录(4)
publicabstractBuildingCreateBuilding();
WebCast听课录(4)
WebCast听课录(4)}

WebCast听课录(4)
WebCast听课录(4)
WebCast听课录(4)
publicclassModernRoad:Road
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
publicModernRoad()
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)}

WebCast听课录(4)}

WebCast听课录(4)
publicclassModernBuilding:Building
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
publicModernBuilding()
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)}

WebCast听课录(4)}

WebCast听课录(4)
WebCast听课录(4)
WebCast听课录(4)
publicclassModernFacilitiesFactory:FacilitiesFactory
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
publicModernFacilitiesFactory()
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)}

WebCast听课录(4)
publicoverrideRoadCreateRoad()
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
returnnewModernRoad();
WebCast听课录(4)}

WebCast听课录(4)
WebCast听课录(4)
publicoverrideBuildingCreateBuilding()
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
returnnewModernBuilding();
WebCast听课录(4)}

WebCast听课录(4)}

WebCast听课录(4)
WebCast听课录(4)
classGameManager
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)FacilitiesFactoryfacilitiesFactory;
WebCast听课录(4)Roadroad;
WebCast听课录(4)Buildingbuilding;
WebCast听课录(4)
publicGameManager(FacilitiesFactoryfacilitiesFactory)
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
this.facilitiesFactory=facilitiesFactory;
WebCast听课录(4)}

WebCast听课录(4)
WebCast听课录(4)
publicvoidBuildGameFacilities()
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
this.road=this.facilitiesFactory.CreateRoad();
WebCast听课录(4)
this.building=this.facilitiesFactory.CreateBuilding();
WebCast听课录(4)
WebCast听课录(4)}

WebCast听课录(4)
WebCast听课录(4)
publicvoidPlay()
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
//WebCast听课录(4).
WebCast听课录(4)
}

WebCast听课录(4)
WebCast听课录(4)
WebCast听课录(4)}

WebCast听课录(4)
WebCast听课录(4)
classTest
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)
WebCast听课录(4)
publicstaticvoidMain(String[]args)
WebCast听课录(4)WebCast听课录(4)
WebCast听课录(4){
WebCast听课录(4)GameManagerg
=newGameManager(newModernFacilitiesFactory());
WebCast听课录(4)g.BuildGameFacilities();
WebCast听课录(4)g.Play();
WebCast听课录(4)}

WebCast听课录(4)
WebCast听课录(4)}

WebCast听课录(4)

Abstract Factory模式的几个要点

1,如果没有应对多系列对象构建的需求变化,则没有必要使用Abstract Factory模式,这时候使用简单的静态工厂完全可以。

2系列对象指的是这些对象之间有相互依赖、或作用的关系,例如游戏开发场景中的道路房屋的依赖,道路地道的依赖。

3 Abstract Factory 模式主要在于应对新系列的需求变动。其缺点在于难以应对新对象的需求变动。

4 Abstract Factory 模式经常和Factory Method模式共同组合来应对对象创建的需求变化。