设计模式之:建造者模式BuilderPattern的实现

Posted 张德长

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式之:建造者模式BuilderPattern的实现相关的知识,希望对你有一定的参考价值。

建造者模式相较于工厂模式,用于创建复杂的对象;

使用的技术:继承、抽象类、多态、配置文件、反射;

建造者模式的设计原则:

  • 面向抽象类编程,符合依赖倒置原则
  • 通过配置文件生成具体建造者,替代抽象建造者,符合里氏替换原则
  • 不同的建造者建造不同的角色,符合单一职责原则
  • 客户端只需调用建造者生成角色对象,而不需要之道具体的实现细节,符合迪米特法则
  • 生成不同角色,只需更改配置文件,无须更改源代码,符合开闭原则
  • 扩展新的角色,只需增加新的角色类和新的建造者类,无须更改源代码,符合开闭原则

类图 with StarUML

角色类

    internal class Actor
    
        private string type;
        private string sex;
        private string face;
        private string costume;
        private string hairstyle;
        public string Type  get => type; set => type = value; 
        public string Sex  get => sex; set => sex = value; 
        public string Face  get => face; set => face = value; 
        public string Costume  get => costume; set => costume = value; 
        public string Hairstyle  get => hairstyle; set => hairstyle = value; 
    

建造者抽象类及3个实现类

    internal abstract class ActorBuilder
    
        protected Actor actor = new Actor();
        public abstract  void  BuildType();
        public abstract void BuildSex();
        public abstract void BuildFace();
        public abstract void BuildCostume();
        public abstract void BuildHairstyle();        
        public Actor CreateActor()  return actor; 
    
    internal class DevilBuilder : ActorBuilder
    
        public override void BuildCostume()  actor.Costume = "黑衣"; 
        public override void BuildFace()  actor.Face = "丑陋"; 
        public override void BuildHairstyle()  actor.Hairstyle = "光头"; 
        public override void BuildSex()  actor.Sex = "男"; 
        public override void BuildType()  actor.Type = "恶魔"; 
    
    internal class AngelBuilder : ActorBuilder
    
        public override void BuildCostume()  actor.Costume = "白裙"; 
        public override void BuildFace()  actor.Face = "漂亮"; 
        public override void BuildHairstyle()  actor.Hairstyle = "披肩长发"; 
        public override void BuildSex()  actor.Sex = "女"; 
        public override void BuildType()  actor.Type = "天使"; 
    
    internal class HeroBuilder : ActorBuilder
    
        public override void BuildCostume()  actor.Costume = "盔甲"; 
        public override void BuildFace()  actor.Face = "英俊"; 
        public override void BuildHairstyle()  actor.Hairstyle = "飘逸"; 
        public override void BuildSex()  actor.Sex = "男"; 
        public override void BuildType()  actor.Type = "英雄"; 
    

角色控制器类,充当指挥者

    internal class ActorController
    
        public Actor Construct(ActorBuilder builder)
        
            Actor actor;
            builder.BuildCostume();
            builder.BuildFace();
            builder.BuildHairstyle();
            builder.BuildSex();
            builder.BuildType();
            actor = builder.CreateActor();
            return actor;
        
    

 

配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<appSettings>
		<add key="builderType" value="BuilderPattern.AngelBuilder"/>
	</appSettings>
</configuration>

客户端

    internal class Program
    
        static void Main(string[] args)
        
            Actor actor;
            //面向抽象编程
            ActorBuilder builder;
            //加载配置文件
            string builderType = ConfigurationManager.AppSettings["builderType"];
            //通过反射创建对象
            builder = Assembly.Load("BuilderPattern").CreateInstance(builderType) as ActorBuilder;
            
            ActorController controller = new ActorController();
            actor = controller.Construct(builder);

            Console.WriteLine($"类型:actor.Type");
            Console.WriteLine($"性别:actor.Sex");
            Console.WriteLine($"面容:actor.Face");
            Console.WriteLine($"服装:actor.Costume");
            Console.WriteLine($"发型:actor.Hairstyle");
            Console.Read();
        
    

运行结果

 

以上是关于设计模式之:建造者模式BuilderPattern的实现的主要内容,如果未能解决你的问题,请参考以下文章

BuilderPattern建造者模式

设计模式之建造者模式

创建型模式之建造者模式

创建型模式:建造者模式

创建型模式:建造者模式

设计模式之建造者模式