西维蜀黍

【Golang】反射(Reflection)

反射(Reflection)

Reflection is just a mechanism to examine the type and value pair stored inside an interface variable. To get started, there are two types we need to know about in package reflect: Type and Value. Those two types give access to the contents of an interface variable, and two simple functions, called reflect.TypeOf and reflect.ValueOf, retrieve reflect.Type and reflect.Value pieces out of an interface value.

  ...


【Hardware】CPU Cache

CPU Cache

A CPU cache is a hardware cache used by the central processing unit (CPU) of a computer to reduce the average cost (time or energy) to access data from the main memory.[1] A cache is a smaller, faster memory, located closer to a processor core, which stores copies of the data from frequently used main memory locations. Most CPUs have a hierarchy of multiple cache levels (L1, L2, often L3, and rarely even L4), with separate instruction-specific and data-specific caches at level 1.

  ...


【Programming】Memory Model

Memory Model

In computing, a memory model describes the interactions of threads through memory and their shared use of the data.

  ...


【Golang】设计模式 - Structural - Decorator

Decorator Pattern

Decorator design pattern is a structural design pattern. It lets you provide additional functionality or decorates an object without altering that object.

换言之,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰器模式可以在不需要创造更多子类的情况下,将对象的功能加以扩展。这就是装饰器模式的模式动机。

Refer to https://swsmile.info/post/design-pattern-decorator/ for Decorator Pattern.

  ...


【Golang】设计模式 - Behavioral - Chain of Responsibility

Chain of Responsibility is behavioral design pattern that allows passing request along the chain of potential handlers until one of them handles request.

The pattern allows multiple objects to handle the request without coupling sender class to the concrete classes of the receivers. The chain can be composed dynamically at runtime with any handler that follows a standard handler interface.

  ...


【Golang】设计模式 - Behavioral - Strategy

Intent

Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.

  ...


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

  ...