Golang Design Patterns – Prototype. Instantiate Objects

Tram Ho

The last pattern belongs to the Creational Pattern in the Design Patterns series that I want to introduce. Also revolves around object initialization, but not as complicated as other design patterns of the same type, we make use of existing objects for initialization. The details of this design I will talk about below.

I. Prototype – Creational Patterns

Prototype’s goal is to target objects that have been initialized before (like at compile time) and allow us to clone that object an unlimited number of times at runtime, for example. For example, when building a website with 3 header-body-footer components, there will be many pages that only need to change the content in the body, and the header and footer are the same. Then the prototype will save us the cost and time of re-initializing the header and footer.

II. What does Prototype bring to developers?

Avoid repetitive initialization, that’s what this design pattern is all about. Imagine the header of the page contains a lot of information taken from the API, moving the page means having to fetch all the information again, which is redundant. In a nutshell, Prototype is about:

  • Objects that are often reused have a high initialization cost and (probably), provide a default value for them
  • Reduce resource consumption for complex initialization (CPU, resources…)

III. Practical examples

Let’s take the example above to implement. The problem is a website that includes 3 routes:

  • /login (no header and footer)
  • /home (with header and footer)
  • /profile (with header and footer) It is easy to see that there are 2 types of layout Blank and Main (I often name it). Blank is simply a layout containing only content (body). Main is a layout containing 3 sections header – body (dynamic) – footer.

The Prototype template applied in this case should adhere to the following rules:

  • There are 2 layouts (Blank and Main), the layout always requires a body parameter (content of the page).
  • When requesting to switch pages in the same layout (home -> profile), new page objects are created but header and footer are reused instances.
  • A Page Info will include the information of the page as route, header, body, footer (default is nil).

IV. Implementation

Initialize struct Layout as follows, layout includes 3 components (header, body, footer). A Layout includes methods SetBody, GetInfo and 2 layouts that can be cloned, Blank and Main

  • Blank Layout includes elements from Layout and a default instance (header and footer nil)

  • With Main Layout, it is more complicated, to initialize the Main Layout header must go through a few stages such as fetching data from the API, processing logic … and initialization takes about 1s:

  • Finally, define the struct for the Page, including the route, layout, and methods NewPage and GetInfo. Here NewPage takes route and pageLayout as parameters, here I put the layout clone job in:

  • Now I will run the code according to the requirements of the problem, including 3 routes: /home , /profile and /login . Make sure the layout instance is not affected during the clone process:

  • Result:

image.png

V. Conclusion

Prototype design pattern is considered as a powerful support tool for quick instantiation of objects which saves a lot of time and resources, can also consider prototype as a build caches. There can be some overlap in implementation between creational design patterns, but there are still small differences that make these patterns powerful if used correctly.

I also introduce through all 5 design patterns of the initialization group most often used by developers. Hope to see you at other chaps

Thank you for reading this post

References

Go Design Patterns (Mario Castro Contreras)

Share the news now

Source : Viblo