当前位置:网站首页>Builder pattern

Builder pattern

2022-06-25 11:25:00 tlqwanttolearnit

Builder pattern (Builder Pattern) Also called generator mode , It's a bit like the factory model , Both use special classes to create objects .

  • The factory pattern is used to create a series of related but different objects .
  • The builder pattern is used to create a type of complex object , By setting different optional parameters , Customize to create different objects .

The main difference is that the builder pattern focuses on building a complex object step by step , And the abstract factory pattern focuses on multiple series of product objects .

Why use builder mode

  • A class has many properties , Constructor and arguments are too long .
  • There are certain dependencies and constraints between attributes .
  • The creation of objects needs to follow a certain sequence or steps

There is a sentence that sums up the builder model very well :

When we want to create an object composed of multiple parts , And their construction needs to be done step by step , Only when all parts are created , This object is complete .

That's exactly what it is. Where the builder's design pattern can be used .

The advantages and disadvantages of the builder model

advantage

  1. Independence of builders , Easy to expand .
  2. Easy to control detail risk .
  3. Decouple the construction and presentation of objects

shortcoming

  1. Products must have something in common , The scope is limited .
  2. If the internal changes are complex , There will be a lot of construction classes .

The composition of the builder model

  1. The actual object : The actual object to return
  2. Builder abstract class : Define the functions that the builder entity class needs to implement .
  3. Builder entity class : Inherit builder abstract class , Concrete implementation of abstract class methods .
  4. Director class :
原网站

版权声明
本文为[tlqwanttolearnit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251108331218.html