Design mode-builder mode (Go language description)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In the previous blog design pattern-Decorator mode we introduced how to implement the decorator pattern in Golang, today we continue to design the model series of articles on the blog, today still look at a simple design pattern- builder mode .

Introduced

I believe that the students who have done Android are more familiar with the builder model, and must have used the open source picture loading frame imageloader, in the Imageloader configuration we used the builder mode, what is the builder mode? Consider the definition:

Separates the construction of a complex object from its representation so that the same build process can create different representations.

There are several characters in the builder model that need to be familiar to us:

  1. Product This is the complex object that we are creating (typically complex objects that need to use Builder mode).
  2. One of the things that Builder abstracts is mainly used to regulate our builders.
  3. ConcreteBuilder specific builder implementations, which are the focus of today, are used primarily to create the build of the object to be created based on the unused business.
  4. The role of Director is to standardize the process of creating complex objects.

Take a look at the structure diagram of the Create pattern:

Well, knowing what the builders and the builders are involved with, let's implement the builder pattern in code, and we'll use the code to implement it, depending on the characters mentioned above. Today we use a game character to create a case to show the code, our game character is very simple, contains only the name and the weapon used two fields.

Creating Complex objects

First, let's create a specific game character class.

typestruct {  string  string}funcstring) {  p.Name = name}funcstring) {  p.Arms = arms}funcstring {  return p.Name}funcstring {  return p.Arms}

This class is very simple, except that we have mentioned the name and the use of the weapon two fields, the rest of the code is only for these two fields to provide getter and setter method, here is not much to say, below we are based on this class to create a builder interface.

Implementing an abstract Builder interface

typeinterface {  string) Builder  string) Builder  Build() *Character}

The first two methods are designed according to the character class of the game, and the last Build method is used to return the game character object we created.

Create a specific creator class

Let's follow the interface defined above to implement a specific creator class,

type  Characterbuilder struct  {character *character}func  (P *characterbuilder) SetName (name string ) Builder {if  p.character = = nil  {p.character = &character{}} p.ch Aracter. SetName (name) return  p}func  (P * Characterbuilder) setarms (arms string ) Builder {if  P.character = = nil  {p.character = &character{}} p.character.setarms ( Arms) return  p}func  (P *characterbuilder) Build () *character {return  p.character}  

This struct implements all the methods of the interface defined above Builder , so it implements the Builder interface, and it also has a Character variable that we are using to create an instance of this type. Buildthe method finally returns the game role object we created.

Director's implementation

As stated above, Director the main function is to standardize the object's component creation process, to see its implementation:

typestruct {  builder Builder}funcstringstring) *Character {  return p.builder.SetName(name).SetArms(arms).Build()}

He needs an Builder example, so we Director need to define a specific implementation when we create it, and there is Builder Director only one method- Create method that we call Create to create a specific game character based on the parameters.
Finally, let's take a look at how to use:

func main() {  var builder Builder = &CharacterBuilder {}  var director *Director = &Director {builder: builder}  var character *Character = director.Create("loader""AK47")  "," + character.GetArms())}

First we create a Builder concrete implementation of the instance, then the example he gave Director , and finally we call Director the Create method to complete the creation of a specific game role. Finally, let's emphasize that when to use creator mode? When an object is not very complex, the creator pattern is not very suitable, and when we need to create an object that is particularly complex, consider using the creator pattern implementation.

Code on GitHub, welcome Star:https://github.com/qibin0506/go-designpattern.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.