C#生成器模式实现

来源:互联网 发布:数据评估方法 编辑:程序博客网 时间:2024/05/22 18:14

转自:http://blog.sina.com.cn/s/blog_9d35598f0100zob1.html

生成器模式:
 它将复杂对象的构建与表示分离,使得同样的构建过程可以创建不同的表示。需要注意如下几点。
(1)构建与表示分离:表明生成器模式的结构,构建过程被封装在导航器中,生成器则负责实现具体的表示。
(2)同样的构建过程:生成器模式关注的是构建过程,即构建过程是相同的。
(3)不同的表示:生成器模式并不在意产生对象的结果,其构造的产品不一定有相同的类型。

生成器模式中的角色:
(1)抽象接口:为创建产品对象的各个部件指定抽象接口。
(2)产品生成器: 实现抽象接口以构造和装配该产品的各个部件
(3)Director:构造一个使用抽象接口的对象。
(4)复杂产品对象: 表示被构造的复杂对象。


#region 产品接口

public interface IDoor
{
}

public interface IWall
{
}

public interface ICelling
{
}
#endregion


#region 子对象

public class Door : IDoor
{
    private string doorInfo;
    public Door(string info)
    {
        doorInfo = info;
    }
}

public class Wall : IWall
{
    private string wallInfo;
    public Wall(string info)
    {
        wallInfo = info;
    }
}

public class Celling : ICelling
{
    private string cellInfo;
    public Celling(string info)
    {
        cellInfo = info;
    }
}

#endregion


#region 复杂对象

/// <summary>
/// 复杂对象房子
/// 它由其他子对象门,天花板,墙组成
/// </summary>
public class House
{
    public IDoor door;
    public IWall wall;
    public ICelling celling;

    public House(IDoor door,IWall wall,ICelling celling)
    {
        door = door;
        wall = wall;
        celling = celling;
    }
}

#endregion


#region 复杂对象的构建过程(不变)

/// <summary>
/// 不管各个子对象如何改变
/// 将这些子对象 组合实现复杂对象的算法是不变的
/// 例如 不管是哥特式的还是中国式的
/// 建设一个房子都需要实现建门,墙,天花板等步骤
/// </summary>
public interface IBuilder
{
    IDoor BuilderDoor();
    IWall BuilderWall();
    ICelling BuilderCelling();
    House GetHouse(IDoor door, IWall wall, ICelling celling);
}

#endregion


#region 复杂对象的生成器(变)

/// <summary>
/// 构成复杂对象的各个子对象是变化的
/// 例如客户现在需要创建一个哥特式的房子
/// 也许过一会需要建设一个中国式的房子
/// 但这些变化并不会影响房子的实现步骤:
/// 房子都需要门,墙,天花板
/// </summary>
public class GeteHouse:IBuilder
{
   /// <summary>
   /// 创建一扇哥特式门
   /// </summary>
   /// <returns></returns>
    public IDoor BuilderDoor()
    {
        return new Door("哥特式门") as IDoor;
    }

    /// <summary>
    /// 创建哥特式天花板
    /// </summary>
    /// <returns></returns>
    public ICelling BuilderCelling()
    {
        return new Celling("哥特式天花板") as ICelling;
    }

    /// <summary>
    /// 创建一个哥特式墙壁
    /// </summary>
    /// <returns></returns>
    public IWall BuilderWall()
    {
        return new Wall("哥特式墙壁") as IWall;
    }

    /// <summary>
    ///  创建一个哥特式的房子
    /// </summary>
    /// <param name="door"></param>
    /// <param name="wall"></param>
    /// <param name="celling"></param>
    /// <returns></returns>
    public House GetHouse(IDoor door, IWall wall, ICelling celling)
    {
        return new House(door,wall,celling);
    }
}

#endregion

/// <summary>
/// 决定要建造什么风格的房子
/// 是哥特式的还是中国式的
/// </summary>
public class Create
{
    private IBuilder builder;
    public Create(IBuilder objBuilder)
    {
        builder = objBuilder;
    }

    public  House CreateHouse()
    {
        ICelling celling = builder.BuilderCelling();
        IDoor door = builder.BuilderDoor();
        IWall wall = builder.BuilderWall();
        return builder.GetHouse(door, wall, celling);
    }
}

原创粉丝点击