C#设计模式之建造者模式

    建造者模式可以将部件本身和它们的组装过程分开,关注如何一步步创建一个包含多个组成部分的复杂对象,用户只需要指定复杂对象的类型即可得到该对象,而无须知道其内部的具体构造细节。

建造者模式:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

建造者模式的定义:

将客户端与包含多个部件的复杂对象的创建过程分离,客户端无须知道复杂对象的内部组成部分与装配方式,只需要知道所需建造者的类型即可;

关注如何逐步创建一个复杂的对象,不同的建造者定义了不同的创建过程;

建造者模式的结构:

建造者模式包含以下4个角色:

Builder(抽象建造者)

ConcreteBuilder(具体建造者)

Product(产品)

Director(指挥者)

例子:

/// <summary>
/// 抽象建造者
/// </summary>
public abstract class Builder
{
    public abstract void BuilderPartA();
    public abstract void BuilderPartB();
    public abstract void BuilderPartC();

    /// <summary>
    /// //创建产品对象
    /// </summary>
    protected Product product = new Product();
    /// <summary>
    /// 返回产品对象
    /// </summary>
    /// <returns></returns>
    public Product GetResult()
    {
        return product;
    }
}
/// <summary>
/// 产品类
/// </summary>
public class Product
{
    //生产的产品
    public string PartA { get; set; }
    public string PartB { get; set; }
    public string PartC { get; set; }
}
/// <summary>
/// 具体建造者
/// </summary>
public class ConcreteBuilder:Builder
{
    /// <summary>
    /// 产品A
    /// </summary>
    public override void BuilderPartA()
    {
        product.PartA = "A1";
        Console.WriteLine("生产了A1产品");
    }
    /// <summary>
    /// 产品B
    /// </summary>
    public override void BuilderPartB()
    {
        product.PartB = "B1";
        Console.WriteLine("生产了B1产品");
    }
    /// <summary>
    /// 产品C
    /// </summary>
    public override void BuilderPartC()
    {
        product.PartC = "C1";
        Console.WriteLine("生产了C1产品");
    }
}
/// <summary>
/// 指挥者
/// </summary>
public class Director
{
    private Builder _builder;
    public Director(Builder builder)
    {
        this._builder = builder;
    }
    public void SetBuilder(Builder builder)
    {
        this._builder = builder;
    }
    /// <summary>
    /// 产品构建与组装方法
    /// </summary>
    /// <returns></returns>
    public Product Construct()
    {
        _builder.BuilderPartA();
        _builder.BuilderPartB();
        _builder.BuilderPartC();
        return _builder.GetResult();
    }
}

 

//客户端调用
Builder builder = new ConcreteBuilder();
Director director = new Director(builder);
Product product = director.Construct();

 

游戏不同角色案例:

/// <summary>
/// 游戏角色建造者,充当抽象建造者
/// </summary>
public abstract class ActorBuilder
{
    public abstract void BuildType();
    public abstract void BuildSex();
    public abstract void BuildFace();
    public abstract void BuildCostume();
    public abstract void BuildHairstyle();

    protected Actor actor = new Actor();
    /// <summary>
    /// 工厂方法,返回一个完整的游戏角色对象
    /// </summary>
    /// <returns></returns>
    public Actor CreateActor()
    {
        return actor;
    }
}
/// <summary>
/// 游戏角色类,充当复杂产品对象
/// </summary>
public class Actor
{
    /// <summary>
    /// 角色类型
    /// </summary>
    public string Type { get; set; }
    /// <summary>
    /// 性别
    /// </summary>
    public string Sex { get; set; }
    /// <summary>
    /// 面容
    /// </summary>
    public string Face { get; set; }
    /// <summary>
    /// 服装
    /// </summary>
    public string Costume { get; set; }
    /// <summary>
    /// 发型
    /// </summary>
    public string Hairstyle { get; set; }
}
/// <summary>
/// 英雄角色建造者,充当具体建造者
/// </summary>
public class Hero : ActorBuilder
{
    public override void BuildType()
    {
        actor.Type = "英雄";
    }

    public override void BuildSex()
    {
        actor.Sex = "";
    }

    public override void BuildFace()
    {
        actor.Face = "英俊";
    }

    public override void BuildCostume()
    {
        actor.Costume = "盔甲";
    }

    public override void BuildHairstyle()
    {
        actor.Hairstyle = "飘逸";
    }    
}
/// <summary>
/// 天使角色建造者,充当具体建造者
/// </summary>
public class Angel : ActorBuilder
{
    public override void BuildType()
    {
        actor.Type = "天使";
    }

    public override void BuildSex()
    {
        actor.Sex = "";
    }

    public override void BuildFace()
    {
        actor.Face = "漂亮";
    }

    public override void BuildCostume()
    {
        actor.Costume = "白裙";
    }

    public override void BuildHairstyle()
    {
        actor.Hairstyle = "披肩长发";
    }    
}
/// <summary>
/// 角色控制器,充当指挥者
/// </summary>
public class ActorController
{
    /// <summary>
    /// 逐步构建复杂产品对象
    /// </summary>
    /// <param name="ab"></param>
    /// <returns></returns>
    public Actor Construct(ActorBuilder ab)
    {
        ab.BuildType();
        ab.BuildSex();
        ab.BuildFace();
        ab.BuildCostume();
        ab.BuildHairstyle();
        Actor actor = ab.CreateActor();
        return actor;
    }
}
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <appSettings>
    <add key="builder" value="ConsoleApplication1.Angel"/>
  </appSettings>
</configuration>

客户端调用:

//通过读取配置文件创建对象
string builderType = ConfigurationManager.AppSettings["builder"];
ActorBuilder _actor = (ActorBuilder)Assembly.Load("ConsoleApplication1").CreateInstance(builderType);
//直接创建对象
//ActorBuilder _actor = new Angel();// new Hero();
ActorController actorController = new ActorController();
//生产后返回对象实体
Actor actor = actorController.Construct(_actor);
Console.WriteLine(actor.Type);
Console.WriteLine(actor.Sex);
Console.WriteLine(actor.Face);
Console.WriteLine(actor.Costume);
Console.WriteLine(actor.Hairstyle);
Console.ReadKey();

 

posted @ 2016-11-20 23:27  黄者之风  阅读(465)  评论(0编辑  收藏  举报