Design Pattern - Creational Patterns - Abstract Factory Pattern
2016-08-10 06:49
561 查看
2007 |
Section 2, Chapter 2 |
Concept
Since the Factory pattern is a class that creates other classes in a uniform way, the Abstract Factory pattern gives us a way to allow factories with similar methods and access points to be interchangeable.
A factory class is a way to compartmentalize code that creates other classes and types. It can initialize, pull in data, configure, set state, and perform nearly any other creational operation needed. An abstract factory then
is generally used when you may have multiple factories that do different things for a class type, and you wish to provide uniformity between these factories.
Use
Let's say you have two different factories. Each one has slightly different creational logic and renders a particular class type. If each of these factories rendered the same class type, but differed in how they create the class
type as an instance, we could tie both those factories together using an abstract factory class as a base. Now if we wanted to use the two factory classes interchangeably for different situations but still return the same class type, we could do so without
having to rewrite the code that calls the factory to explicitly call a particular factory class type.
Design
Illustration
class BaseClass { AbstractObjectFactory _factory; Hashtable _attributes; protected AbstractObjectFactory FactoryImpl { get{return _factory;} set{_factory = value;} } public Hashtable Attributes { get { if(_attributes == null) _attributes = FactoryImpl.GetAttributes(); return _attributes; } } } class ImageClass : BaseClass { public ImageClass() { FactoryImpl = new ImageFactory(); } } class TextClass : BaseClass { public TextClass() { FactoryImpl = new TextFactory(); } }
abstract class AbstractObjectFactory { abstract public Hashtable GetAttributes(); } class ImageFactory : AbstractObjectFactory { public override Hashtable GetAttributes() //changed from GetImageAttributes { Hashtable attributes = new Hashtable(); attributes["height"] = 52; attributes["width"] = 100; attributes["imageUrl"] = "http://imageserver/images/someimage.gif"; return attributes; } } class TextFactory : AbstractObjectFactory { public override Hashtable GetAttributes() //changed from GetTextAttributes { Hashtable attributes = new Hashtable(); attributes["initialText"] = "N/A"; attributes["maxTextLength"] = 100; attributes["textFormat"] = "_____@_____.__"; return attributes; } }
Abstract Factory + Builder
public abstract class HouseFactory { public abstract House CreateHouse(); public abstract Floor CreateFloor(); public abstract Wall CreateWall(); public abstract Roof CreateRoof(); } public class DuplexFactory : HouseFactory { public override House CreateHouse() { return new Duplex(); } public override Floor CreateFloor() { return new CementFloor(); } public override Wall CreateWall() { return new CementBlockWall(); } public override Roof CreateRoof() { return new ShingledRoof(); } } //new factory for different house type public class HUDFactory : HouseFactory { public override House CreateHouse() { return new HUDHouse(); } public override Floor CreateFloor() { return new SlabFloor(); } public override Wall CreateWall() { return new WoodenWall(); } public override Roof CreateRoof() { return new ShingledRoof(); } }
public class HouseBuilder { public House Build(HouseFactory factory) { House house = factory.CreateHouse(); house.Floor = factory.CreateFloor(); house.WestWall = factory.CreateWall(); house.EastWall = factory.CreateWall(); house.SouthWall = factory.CreateWall(); house.NorthWall = factory.CreateWall(); house.Roof = factory.CreateRoof(); return house; } } HouseBuilder builder = new HouseBuilder(); House duplex = builder.Build(new DuplexFactory()); House hud = builder.Build(new HUDFactory());
相关文章推荐
- Design Pattern 3: Factory Method && Abstract Factory Pattern
- Design Pattern----01.Creational.AbstractFactory.Pattern (Delphi Sample)
- Design Pattern - Abstract Factory Pattern
- 抽象工厂模式(Abstract Factory Pattern)
- 05.抽象工厂模式【ABSTRACT FACTORY PATTERN】
- Java Abstract Factory Pattern(抽象工厂模式)
- 设计模式学习笔记--抽象工厂模式(Abstract Factory Pattern)【创建型模式】
- 设计模式学习总结-抽象工厂模式(Abstract Factory Pattern)
- 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)
- 设计模式(三)抽象工厂模式(Abstract Factory Pattern)
- C#设计模式(6)-Abstract Factory Pattern
- 创建型模式——抽象工厂模式(Abstract Factory Pattern)
- Abstract Factory Pattern
- 设计模式-抽象工厂模式(AbstractFactoryPattern)
- 23种设计模式(3)_创建型_抽象工厂模式(Abstract Factory Pattern)
- 抽象工厂模式(Abstract Factory Pattern)
- 抽象工厂模式(Abstract Factory Pattern)
- Introducing the Abstract Factory Pattern
- Swift 02 抽象工厂模式 Abstract Factory Pattern
- C#设计模式(6)-Abstract Factory Pattern