Guess the origin of the Builder mode

xiaoxiao2021-03-06  74

I saw a lot of articles on the Internet. Many people also wrote some examples according to Builder mode. I believe that many beginners don't understand Builder, especially for Director, online examples, very few, have a polymorphism. It is usually in Builder that contains Parta, Partb, Partc. The construct in Director also includes Parta, Partb, Partc.

Thus, we look at it from solving problems: If a product must be composed of Parta, Partb, Partc. Then you don't use the Builder mode. Did not use it, Director does not have a manner at all. Use the Template mode to solve it. This is much simpler than builder.

// c # public abstract class abstractclass {public Abstract void buildparta (); public abstract void buildpartb (); public abstract void buildingpaartc ();

Public product getResult () {this.buildparta (); this.buildpartb (); this.buildpartc (); return product;}} Inheriting the various components of the Product can be diversified. But Product is always composed of these three components.

As demand changes, the Product composition principle has also changed, and some products are composed of two parts of AB, and some are part of C. Obviously bring the assembly explosion. So we need a construct () method to manage assembly.

Public Abstract Class AbstractClassClass {public Abstract void Buildparta (); Public Abstract void BuildPartB (); Public Abstract void BuildPartc ();

Public products getResult () {construct;} public abstract void construct ();} The problem is coming, the components are constructed and assembled in the same class, which is undoubtedly easier to couple. Then create a Director class to specifically manage assembly. Usually we can use an IDirector interface

Public interface idirector {void construct ();

This achieves the polymorphism of assembly, and finally evolved into the current Builder mode. Therefore, consider whether to use the Builder mode, considering whether it is composed of multiple parts, it is necessary to need a variety of assemblies.

转载请注明原文地址:https://www.9cbs.com/read-108713.html

New Post(0)