JavaScript Design Patterns builder mode

Pattern builder constructs may be represented by its phase separation, so that the same build process can create different representations of a complex object. That is, if we use the builder mode, users only need to specify the need to build the type you can get them, and the process of construction and specific details do not need to know. The builder pattern is actually a commander, a builder, a customer using a specific command invokes the outcome of the work of the builders.

Builder mode is mainly used for "step by step to build a complex object" in which "step by step" is a stable algorithm, and the various parts of a complex object is constantly changing. For example, we need to buy a house, then we just need to find your favorite real estate developers on it, then directed by the developers and contractors construction workers build a house, as to how the house is covered up, we do not know, You do not need to know.

Action builder mode:

  • Step by step to create a complex object
  • Assembly and packaging process particular decoupling created
  • Without concern for how to assemble components
< Script > 
        // we want house 
       function Fanzi () {
            the this .woshi =  '' ;
            the this .keting =  '' ;
            the this .chufang =  '' ;
       }
       // contractors workers started to call, tell the house of the specific needs of the workers 
       function Baogongtou () {
            the this .gaifangzi =  function (gongren) {
               gongren.jian_woshi ();
               gongren.jian_keting ();
               gongren.jian_chufang ();
           }
       }
      // workers responsible for building a house 
       function Gongren () {
            the this .jian_woshi =  function () {
               the console.log ( ' bedroom completed a ' );
           }
           this.jian_keting = function(){
               the console.log ( ' living completed a ' );
           }
           this.jian_chufang = function(){
               the console.log ( ' kitchen completed a ' );
           }
           the this .jiaogong =  function () {
                var _fangzi =  new new Fanzi ();
               _fangzi.woshi =  ' OK ' ;
               _fangzi.keting =  ' OK ' ;
               _fangzi.chufang =  ' OK ' ;
                return _fangzi
           }
       }
       var gongren =  new new Gongren ();
        var baogongtou =  new new Baogongtou ();
       baogongtou.gaifangzi (gongren);
       var myHouse for Windows = gongren.jiaogong ();
       console.log(myHouse);
    </script>

Builders mode Caution:

  • Be sure to support a stable algorithm
  • Process is exposed

Guess you like

Origin www.cnblogs.com/yuyujuan/p/12128990.html