Design Pattern - Creational Patterns - Abstract Factory Pattern

2007
Design Pattern - Creational Patterns - Abstract Factory Pattern_第1张图片
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


Design Pattern - Creational Patterns - Abstract Factory Pattern_第2张图片


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


Design Pattern - Creational Patterns - Abstract Factory Pattern_第3张图片


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,&,Enginerring,Reference,Books,C#,Java)