Constructor mode of design mode (Builder)

Introduction

Constructor mode: Use multiple simple objects to gradually build a complex object
Constructor mode belongs to the creation mode, which provides the best way to create objects

principle

The Builder pattern is based on: an object may have different components, and different creation objects of these components have different representations, but the assembly method between each part is consistent (such as the assembly of bicycles, wheels, chains, body, pedals plate).

Based on this, the construction process is the same, so an abstract interface can be used uniformly, and different components can be implemented by derived classes

effect

Separate the construction of a complex object from its representation so that the same construction process can create different representations

  • Advantages: The constructor is independent, easy to expand, easy to control the risk of details
  • Disadvantages: Products must have something in common, limited scope, complex internal changes, and many construction classes

scenes to be used

Applies to the following situations:

  • When constructing, it must allow the objects being constructed to have different representations
  • When creating an algorithm for building complex objects, it should be independent of the components of the object and how it is assembled

the difference

  • Compared with the factory pattern, the constructor pattern pays more attention to the order of assembly of parts

accomplish

UML diagrams

insert image description here
Parse:

  • Builder : the abstraction of the entire process of creating objects, providing interfaces for building different components, and providing concrete implementations in derived classes
  • Director.Construct : call the interface function to complete the construction of the object. The assembly process of different parts is the same, but different construction methods will have different representations (determined according to polymorphism)

in principle

The implementation of the Builder pattern is based on the following object-oriented design principles:

  1. The changed part abstracts a base class and the corresponding interface function, providing the same interface but different implementations
  2. Using the aggregation method, the aggregation will change the base class, for example: Directoi aggregates the pointer of the Builder class

the code

using System;

namespace ConsoleApp2
{
    
    
    class Class7
    {
    
    
        public static void Main(string[] args)
        {
    
    
            PersonDirector dir = new PersonDirector(new ThinPersonBuilder());
            Person person = dir.BuildPerson();
            Console.WriteLine(person.Head);
            Console.WriteLine(person.Body);
            Console.WriteLine(person.Arm);
            Console.WriteLine(person.Leg);

            Console.ReadLine();
        }
    }

    // 要建造的产品
    public class Person
    {
    
    
        public string Head {
    
     get; set; }
        public string Body {
    
     get; set; }
        public string Arm {
    
     get; set; }
        public string Leg {
    
     get; set; }
    }

    // 定义创建者接口,实现者必须实现该接口中定义的所有抽象方法,防止实现者疏忽而遗漏某个部件的创建
    public abstract class Builder
    {
    
    
        protected Person Person {
    
     get; set; }
        public Builder()
        {
    
    
            Person = new Person();
        }

        // 建造头
        public abstract void BuildHead();
        // 建造身体
        public abstract void BuildBody();
        // 建造胳膊
        public abstract void BuildArm();
        // 建造腿
        public abstract void BuildLeg();

        // 返回生成好的对象,这是一个具体方法,每个子类都可以使用它来返回一个已经创建成功的对象
        public Person GetPerson()
        {
    
    
            return Person;
        }
    }

    // 建造者的具体实现,这里是要建造出一个瘦子
    public class ThinPersonBuilder : Builder
    {
    
    
        public override void BuildHead()
        {
    
    
            Person.Head = "瘦子的脑袋";
        }

        public override void BuildBody()
        {
    
    
            Person.Body = "瘦子的身体";
        }

        public override void BuildArm()
        {
    
    
            Person.Arm = "瘦子的胳膊";
        }

        public override void BuildLeg()
        {
    
    
            Person.Leg = "瘦子的腿";
        }

    }

    // 建造者的具体实现,这里是要建造出一个胖子
    public class FatPersonBuilder : Builder
    {
    
    
        public override void BuildHead()
        {
    
    
            Person.Head = "胖子的脑袋";
        }

        public override void BuildBody()
        {
    
    
            Person.Body = "胖子的身体";
        }

        public override void BuildArm()
        {
    
    
            Person.Head = "胖子的胳膊";
        }

        public override void BuildLeg()
        {
    
    
            Person.Head = "胖子的腿";
        }
    }

    // 建造者模式中的指挥者
    public class PersonDirector
    {
    
    
        Builder builder;
        public PersonDirector(Builder personBuilder)
        {
    
    
            builder = personBuilder;
        }

        // 指挥创建一个人的过程,并返回创建成功的产品
        public Person BuildPerson()
        {
    
    
            builder.BuildHead();
            builder.BuildBody();
            builder.BuildArm();
            builder.BuildLeg();

            return builder.GetPerson();
        }
    }
}

Code reprint: Builder Pattern of C# Design Pattern Notes

Guess you like

Origin blog.csdn.net/qq_36804363/article/details/126040348