创建者模式
是一种复杂对象创建模式,它将客户端与包含多个组成部分的复杂对象的创建过程分离.客户无需知道复杂对象的内部组成部分与装配方式,只需要知道所需要建造者的类型即可.
建造模式关注如何逐步创建 一个复杂对象,不同的具体建造者定义了不同的创建过程,且具体建造者相互独立,更换建造者或增加新的建造非常方便,系统具有较好的扩展性.
//创建者模式
//是一种复杂对象创建模式,它将客户端与包含多个组成部分的复杂对象的创建过程分离.客户无需知道复杂对象的内部组成部分与装配方式,只需要知道所需要建造者的类型即可.
//建造模式关注如何逐步创建 一个复杂对象,不同的具体建造者定义了不同的创建过程,且具体建造者相互独立,更换建造者或增加新的建造非常方便,系统具有较好的扩展性.
//下面有两个盒子,一个涉及到对象的建造,后面一个是涉及对象建造的。
var builder = new Elephent();
builder.Close();
class Elephent : Builder
{
protected override void Step1()
{
Console.WriteLine("把冰箱门打开");
}
protected override void Step2()
{
Console.WriteLine("把大象装进冰箱");
}
protected override void Step3()
{
Console.WriteLine("把冰箱门关上");
}
}
abstract class Builder
{
protected abstract void Step1();
protected abstract void Step2();
protected abstract void Step3();
public void Close()
{
Step1();
Step2();
Step3();
}
}
下面涉及到对象的建造
//顶级语句
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
//IHost host = Host.CreateDefaultBuilder()
// .ConfigureServices(services =>
// {
// services.AddSingleton<ActorController>();
// services.AddSingleton<ActorBuilder, HeroBuilder>();
// // services.AddSingleton<ActorBuilder, AngeBuilder>();
// services.AddSingleton<Actor>();
// })
// .Build();
//ActorController actorController= host.Services.GetRequiredService<ActorController>();
//var actor=actorController.Bluid();
IServiceCollection services = new ServiceCollection();
services.AddSingleton<ActorBuilder, AngeBuilder>();
services.AddSingleton<ActorController>();
services.AddSingleton<Actor>();
ServiceProvider serviceProvider = services.BuildServiceProvider();
ActorController actorController = serviceProvider.GetRequiredService<ActorController>();
var actor = actorController.Bluid();
Console.WriteLine($"角色:{actor.type}");
Console.WriteLine($"性别:{actor.Sex}");
Console.WriteLine($"面容:{actor.Face}");
Console.WriteLine($"服装:{actor.Costume}");
Console.WriteLine($"发型:{actor.Hairstyle}");
/// <summary>
/// 英雄角色建造者
/// </summary>
class HeroBuilder : ActorBuilder
{
public HeroBuilder(Actor actor) : base(actor)
{
}
public override void BuilderCostume()
{
_actor.type = "英雄";
}
public override void BuilderFace()
{
_actor.Sex = "男";
}
public override void BuilderHairstyle()
{
_actor.Face = "英俊";
}
public override void BuilderSex()
{
_actor.Costume = "盔甲";
}
public override void BuilderType()
{
_actor.Hairstyle = "飘逸";
}
}
/// <summary>
/// 天使角色建造者
/// </summary>
class AngeBuilder : ActorBuilder
{
public AngeBuilder(Actor actor) : base(actor)
{
}
public override void BuilderCostume()
{
_actor.type = "天使";
}
public override void BuilderFace()
{
_actor.Sex = "女";
}
public override void BuilderHairstyle()
{
_actor.Face = "漂亮";
}
public override void BuilderSex()
{
_actor.Costume = "白袍";
}
public override void BuilderType()
{
_actor.Hairstyle = "披肩长发";
}
}
public class Actor
{
/// <summary>
/// 角色类型
/// </summary>
public string type { get; set; }
/// <summary>
/// 性别
/// </summary>
public string Sex { get; set; }
/// <summary>
/// 面容
/// </summary>
public string Face { get; set; }
//服装
public string Costume { get; set; }
/// <summary>
/// 发型
/// </summary>
public string Hairstyle { get; set; }
}
abstract class ActorBuilder
{
protected Actor _actor;
protected ActorBuilder(Actor actor)
{
_actor = actor;
}
abstract public void BuilderType();
abstract public void BuilderSex();
abstract public void BuilderFace();
abstract public void BuilderCostume();
abstract public void BuilderHairstyle();
public Actor CreateActor()
{
return _actor;
}
}
class ActorController
{
private readonly ActorBuilder _builder;
public ActorController(ActorBuilder actorBuilder)
{
_builder = actorBuilder;
}
public Actor Bluid()
{
_builder.BuilderType();
_builder.BuilderSex();
_builder.BuilderFace();
_builder.BuilderCostume();
_builder.BuilderHairstyle();
return _builder.CreateActor();
}
}
标签:创建者,void,actor,abstract,services,模式,override,public
From: https://www.cnblogs.com/bcd589/p/17817180.html