Posts
Factory Method Pattern
Factory method is a creational design pattern which solves the problem of creating product objects without specifying their concrete classes.
The Factory Method defines a method, which should be used for creating objects instead of using a direct constructor call (new operator). Subclasses can override this method to change the class of objects that will be created.
Golang Implementation Example: Simple Factory It’s impossible to implement the classic Factory Method pattern in Go due to lack of OOP features such as classes and inheritance.
read morePosts
Abstract Factory Pattern
Abstract Factory is a creational design pattern, which solves the problem of creating entire product families without specifying their concrete classes.
Abstract Factory defines an interface for creating all distinct products but leaves the actual product creation to concrete factory classes. Each factory type corresponds to a certain product variety.
The client code calls the creation methods of a factory object instead of creating products directly with a constructor call (new operator).
read morePosts
Builder Pattern
Builder is a creational design pattern, which allows constructing complex objects step by step.
Unlike other creational patterns, Builder doesn’t require products to have a common interface. That makes it possible to produce different products using the same construction process/steps.
Golang Implementation Example: Build houses 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.
read morePosts
Flyweight Pattern
Flyweight is a structural design pattern that allows programs to support vast quantities of objects by keeping their memory consumption low.
The pattern achieves it by sharing parts of object state between multiple objects. In other words, the Flyweight saves RAM by caching the same data used by different objects.
Golang Implementation Example: The dress object in Counter-Strike game In a game of Counter-Strike, Terrorist and Counter-Terrorist have a different type of dress.
read morePosts
Facade Pattern
Facade is a structural design pattern that provides a simplified (but limited) interface to a complex system of classes, library or framework.
While Facade decreases the overall complexity of the application, it also helps to move unwanted dependencies to one place.
Golang Implementation It’s easy to underestimate the complexities that happen behind the scenes when you order a pizza using your credit card. There are dozens of subsystems that are acting in this process.
read morePosts
Time Travel
If you encounter a difficulty that you dare not face directly, you can close your eyes and imagine yourself as an elderly person in their 80s who is nearing the end of their life, full of regret for all the hardships they once gave up on and avoided.
You will tell yourself, “If I could be young again, how wonderful it would be to return to that past!” Then you can open your eyes, and boom!
read morePosts
Observer Pattern
Observer is a behavioral design pattern that allows some objects to notify other objects about changes in their state.
The Observer pattern provides a way to subscribe and unsubscribe to and from these events for any object that implements a subscriber interface.
Golang Implementation Example: E-Commerce Website pushes notification In the e-commerce website, items go out of stock from time to time. There can be customers who are interested in a particular item that went out of stock.
read morePosts
Decorator Pattern
Decorator is a structural pattern that allows adding new behaviors to objects dynamically by placing them inside special wrapper objects, called decorators.
Using decorators you can wrap objects countless number of times since both target objects and decorators follow the same interface. The resulting object will get a stacking behavior of all wrappers.
Golang Implementation Example: Pizza Toppings pizza.go package main type IPizza interface { getPrice() int } veggieMania.go package main type VeggieMania struct { } func (p *VeggieMania) getPrice() int { return 15 } tomatoTopping.
read morePosts
Composite Pattern
Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.
Composite became a pretty popular solution for the most problems that require building a tree structure. Composite’s great feature is the ability to run methods recursively over the whole tree structure and sum up the results.
Golang Implementation Example: Folders and Files Let’s try to understand the Composite pattern with an example of an operating system’s file system.
read morePosts
Visitor Pattern
Visitor is a behavioral design pattern that allows adding new behaviors to existing class hierarchy without altering any existing code.
Golang Implementation Example: Opearte on different shapes The Visitor pattern lets you add behavior to a struct without actually modifying the struct. Let’s say you are the maintainer of a lib which has different shape structs such as:
Square Circle Triangle Each of the above shape structs implements the common shape interface.
read morePosts
Bridge Pattern
Bridge is a structural design pattern that divides business logic or huge class into separate class hierarchies that can be developed independently.
One of these hierarchies (often called the Abstraction) will get a reference to an object of the second hierarchy (Implementation). The abstraction will be able to delegate some (sometimes, most) of its calls to the implementations object. Since all implementations will have a common interface, they’d be interchangeable inside the abstraction.
read morePosts
Adapter Pattern
Adapter is a structural design pattern, which allows incompatible objects to collaborate.
The Adapter acts as a wrapper between two objects. It catches calls for one object and transforms them to format and interface recognizable by the second object.
In this example, the trait SpecificTarget is incompatible with a call function which accepts trait Target only.
fn call(target: impl Target); The adapter helps to pass the incompatible interface to the call function.
read more