“This is the first day of the month. For me, this is a challenge. Although I have read design mode for a long time, I have returned it to the book.

  • 📢 welcome to like: 👍 collect ⭐ message 📝 If there is any mistake please correct it, send roses, hand left fragrance!
  • 📢 This article is written by Webmote.
  • 📢 author’s motto: life lies in tossing about, when you do not toss about life, life will start tossing you, let us work together! 💪 💪 💪

Preface 🎏

Pattern: The word pattern can be understood as routine, much like the three batting axe of Cheng Biting gold, according to this routine can get something done. Design patterns: Software/architecture design routines, the most popular ones.

The new guy knows what to do, and the team quickly understands what the other guy is trying to do.

In this way, the ancient ancestor of the Chinese nation was very powerful. He put forward the art of War, a common pattern in the world of war, very early on. MMMM, beauty like Diao Chan, give me a dozen…

Of course, in order to successfully implement the honey trap, is it necessary to think about the problem you want to solve? To whom? The final result?

Yes, the application of patterns depends on the problem domain, the solution, and the performance evaluation.

🎏 01. Explanation of generator mode

Intent: The generation of a complex object is broken down into different parts of the build, and these builds are combined to produce different representations of object instances.

Problem domain: It is generally used to solve the following problems.

  • When algorithms for creating complex objects need to be independent
  • The construction process allows for different behaviors when

Solution: We use UML diagrams to describe this.

As you can see in the figure, participants have Director and DestObject classes, which have no special constraints.

The key Builder can be an interface or abstract base class, which can be customized to your needs.

Of course, if the generator design pattern is adopted, then we generally define the interface or implementation class in accordance with XXXBuilder, so that other children’s shoes to see these classes, can quickly Get to XXX point.

Effect:

  • Benefits:
  1. The client does not need to know the details of the internal composition of the target object. The target object itself is decoupled from the creation process of the target object, so that different target objects can be created in the same creation process.
  2. The specific creator can be extended;
  3. More refined control of the target object generation process, according to the steps provided by the generator step by step, can be refined control into the product interior.
  • Limit:
  1. The target objects have many common specificities, the composition of different target objects is similar, the difference is not very much;

🎏 02. Dotnet Core source code appreciation

The builder pattern is used at WebHost build time.

CreateHostBuilder(args).Build().Run();
Copy the code

The generator interface is defined as follows. The Configure series of configuration methods all return the builder interface class so that the configuration can be easily configured continuously at build time. This is one of the classic scenarios of chained calls. Such as the build time can use CreateHostBuilder (args). ConfigureAppConfiguration (a = > a. uilder ()). ConfigureServices ((builder, s) = > s.r egister ()). Build(); “, which feels like a pipeline-based machine, gradually building the parts to produce the prefab.

    public interface IHostBuilder
    {      
        IDictionary<object.object> Properties
        {
            get;
        }     
        IHost Build();
        IHostBuilder ConfigureAppConfiguration(Action<HostBuilderContext, IConfigurationBuilder> configureDelegate);
        IHostBuilder ConfigureContainer<TContainerBuilder> (Action<HostBuilderContext, TContainerBuilder> configureDelegate);
        IHostBuilder ConfigureHostConfiguration(Action<IConfigurationBuilder> configureDelegate);
        IHostBuilder ConfigureServices(Action<HostBuilderContext, IServiceCollection> configureDelegate);   
    }
Copy the code

🎏 03.Dotnet generator implementation

This is an example of a WebRequest Builder. The interface is defined as follows:

 IHttpRequestBuilder BuildUrl(string url);
IHttpRequestBuilder BuildRequestMethod(string method);
WebRequest Build();
Copy the code

The implementation is relatively simple, so I won’t list it here, and will put it on Github later.

The caller can be directly used as a chained call.

 var builder = new HttpRequestBuilder();
var request = builder.BuildUrl("http://webmote.csdn.net")
             .BuildRequestMethod("GET")
             .Build();

Console.WriteLine($"request url: {request.RequestUri}");
Copy the code

🎏 04. Summary

Yes, it feels like another article. Are gifts really important?

Important, no gift, no power!

To develop a good habit requires constant encouragement and encouragement. Writing ability may be improved through continuous writing, and of course, my own capacity. In the process of continuous output, I find my own deficiencies and consolidate my knowledge.

Today is the first day. Come on, guys!

Routine summary, rational view!

Knot is what, knot is I want you to like but can not get the loneliness. 😳 😳 😳

👓 all see this, still care to point a like?

👓 all points like, still care about a collection?

Do you care about a comment when you have collected 👓?