Design Pattern - Creational Patterns - Abstract Factory Pattern

50 篇文章 0 订阅
37 篇文章 0 订阅

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());



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值