西维蜀黍

【Golang】设计模式 - Structural - Template Method

Template Method Design Pattern

Template Method Design Pattern is a behavioral design pattern that lets you define a template or algorithm for a particular operation. Let’s understand the template design pattern with an example.

Consider the example of One Time Password or OTP. There are different types of OTP that can be triggered for eg. OTP can be SMS OTP or EMAIL OTP. But irrespective it is an SMS OTP or EMAIL OTP, the entire steps of the OTP process are the same. The steps are

  • Generate a random n digit number.

  • Save this number in the cache for later verification.

  • Prepare the content

  • Send the notification

  • Publish the metrics

Even in the future let’s say a push notification OTP is introduced but still it will go through the above steps.

In such scenarios when the steps of a particular operation are the same but the steps of the operations can be implemented in a different way by different implementors , then that becomes a candidate for the Template Method Design Pattern. We define a template or algorithm which comprises of a fixed number of methods. The implementer of the operation overrides the methods of the template.

Now check out the below code example.

  • iOtp represents an interface that defines the set of methods that any otp type should implement

  • sms and email are the implementors of iOtp interface

  • otp is the struct which defines the template method genAndSendOTP(). otp embeds iOtp interface.

  ...


【Golang】设计模式 - Structural - Observer

Observer Pattern

Observer Design Pattern is a behavioral design pattern. This pattern allows an instance (called subject) to publish events to other multiple instances (called observers). These observers subscribe to the subject and hence get notified by events in case of any change happening in the subject.

Refer to https://swsmile.info/post/design-pattern-observer-pattern/ for Observer Pattern

  ...


【Golang】设计模式 - Structural - Proxy/Wrappper

Proxy/Wrapper Pattern

Proxy Design Pattern is a structural design pattern. This pattern suggests providing an extra layer of indirection for controlled and intelligent access to the main object.

In this pattern, a new proxy class is created that implements the same interface as the main object. This lets you execute some behavior before the actual logic of the main object. Let’s understand it more with an example:

A web server such as Nginx can act as a proxy for your application server. It provides

  • Controlled access to your application server – For example, it can do rate limiting
  • Additional behavior – For example, it can do some caching

See https://swsmile.info/post/design-pattern-proxy/ for Proxy Pattern.

  ...


【Golang】设计模式 - Structural - Composite

Composite Pattern

Let’s try to understand the Composite pattern with an example of an operating system’s file system. In the file system, there are two types of objects: files and folders. There are cases when files and folders should be treated to be the same way. This is where the Composite pattern comes in handy.

Imagine that you need to run a search for a particular keyword in your file system. This search operation applies to both files and folders. For a file, it will just look into the contents of the file; for a folder, it will go through all files of that folder to find that keyword.

When to Use

  • Composite Design pattern makes sense to use in cases when the composite and individual object needs to be treated in the same way from a client perspective.

​ – In our example above of the file system, let’s say search operation of a particular keyword needs to be executed. Now, this search operation applies to both File and Folder. For a File, it will just look into the contents of the file and for a Folder, it will go through all files in the hierarchy in that folder to find that keyword

  • Use this pattern when the composite and individual object form a tree-like structure

​ – In our example, File and Folder do form a tree structure

Refer to https://swsmile.info/post/design-pattern-composite-pattern/ for Composite pattern.

  ...


【Golang】设计模式 - idioms - Functional Options

Functional Options

Functional options are a method of implementing clean/eloquent APIs in Go. Options implemented as a function set the state of that option.

  ...