简体   繁体   中英

Builder pattern protected constructor

Going through this exercise/design-pattern for Builder pattern:

https://github.com/Apress/design-patterns-in-.net-core-3/blob/master/Creational/Builder/Builder.cs

Making the HtmlElement constructor "protected" breaks everything, making it internal seems to work and at the same time limit access a bit

Is there a way to make protected HtmlElement work ?

source:

void Main()
{
    var builder = HtmlElement.Create("div")
        .AddChild("li", "hello")
        .AddChild("li", "world")
        ;
    builder.ToString().Dump();
}

public class HtmlElement
{
    internal string Name { get; set; }
    internal string Text { get; set; }
    internal List<HtmlElement> Elements = new();
    
    internal HtmlElement() { }
    internal HtmlElement(string name, string text)
    {
        this.Name = name;
        this.Text = text;
    }
    
    public override string ToString()
    {
        var stringBuilder = new System.Text.StringBuilder();
        stringBuilder
            .Append($"<{Name}>")
            .Append($"{Text}");
            
        foreach (var elem in Elements)
        {
            stringBuilder
                .Append($"<{elem.Name}>")
                .Append($"{elem.Text}")
                //.Append($"{elem.Elements.ToString()}")
                .Append($"</{elem.Name}>");
        }
        stringBuilder.Append($"</{Name}>");
        return stringBuilder.ToString();
    }
    
    public static HtmlBuilder Create(string name) => new HtmlBuilder(name);   

}

public class HtmlBuilder  {
    protected readonly string rootName;
    protected HtmlElement root = new();
    
    internal HtmlBuilder(string rootName) {
        this.rootName = rootName;
        this.root.Name = rootName;
    }
    
    public HtmlBuilder AddChild(string childName, string childText) {
        var elem = new HtmlElement(childName, childText);
        root.Elements.Add(elem);
        return this;
    }
 
    public override string ToString() => root.ToString();   
    
}

练习生成器模式

All types and type members have an accessibility level. The accessibility level controls whether they can be used from other code in your assembly or other assemblies. Use the following access modifiers to specify the accessibility of a type or member when you declare it:

public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private: The type or member can be accessed only by code in the same class or struct.

protected: The type or member can be accessed only by code in the same class, or in a class that is derived from that class.

internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal: The type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly.

private protected: The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.

you cannot call protected constructor from a different class that does not derive from HtmlElement. Is there any use for creating that access modifier?

if so, what you are looking for is probably internal \\ private protected, depend on the useage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM