您的位置:首页 > 其它

Design Pattern - Creational Patterns - Abstract Factory Pattern

2016-08-10 06:49 561 查看
2007



Section 2, Chapter 2

Abstract Factory Pattern

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