西维蜀黍

【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.

  ...


【Golang】设计模式 - Structural - Adapter

Adaptor Pattern

We have a client code that expects some features of an object (Lightning port), but we have another object called adaptee (Windows laptop) which offers the same functionality but through a different interface (USB port)

This is where the Adapter pattern comes into the picture. We create a struct type known as adapter that will:

  • Adhere to the same interface which the client expects (Lightning port).
  • Translate the request from the client to the adaptee in the form that the adaptee expects. The adapter accepts a Lightning connector and then translates its signals into a USB format and passes them to the USB port in windows laptop.

Refer to https://swsmile.info/post/design-pattern-adapter-pattern/ for Adapter pattern.

  ...


【Golang】设计模式 - Creational - Object Pool

Object Pool Pattern

The object pool creational design pattern is used to prepare and keep multiple instances according to the demand expectation.

As and when needed, a client can request an object from the pool, use it, and return it to the pool. The object in the pool is never destroyed.

Refer to https://swsmile.info/post/design-pattern-flyweight-pattern/#%E4%BA%AB%E5%85%83%E6%A8%A1%E5%BC%8F%E7%9A%84%E5%B9%BF%E6%B3%9B%E4%BD%BF%E7%94%A8 for Flyweight pattern.

  ...


【Golang】设计模式 - Creational - Prototype

Prototype Pattern

Prototype is a creational design pattern that allows cloning objects, even complex ones, without coupling to their specific classes.

All prototype classes should have a common interface that makes it possible to copy objects even if their concrete classes are unknown. Prototype objects can produce full copies since objects of the same class can access each other’s private fields.

  ...


【Golang】设计模式 - Creational - Abstract Factory

Analysis

Abstract Factory Design Pattern is a creational design pattern that lets you create a family of related objects. It is an abstraction over the factory pattern.

Refer to https://swsmile.info/post/design-pattern-factory-pattern/ for Factory pattern.

  ...


【Golang】设计模式 - Creational - Builder

Analysis

The Builder pattern is used when the desired product is complex and requires multiple steps to complete. In this case, several construction methods would be simpler than a single monstrous constructor. The potential problem with the multistage building process is that a partially built and unstable product may be exposed to the client. The Builder pattern keeps the product private until it’s fully built.

In the below code, we see different types of houses (igloo and normalHouse) being constructed by iglooBuilder and normalBuilder. Each house type has the same construction steps. The optional director struct helps to organize the building process.

  ...