Cancellation Token Pattern in Python

Hello! 👋 The Cancellation Token Pattern article is a pattern inspired by C#’s CancellationToken struct and Golang’s context package. The main idea of the pattern is to allow the user of an API to cancel its operations, but in order for this pattern to work, the API must be written with the cancellation token pattern in mind. To use the pattern, you need a class that represents a cancellation token. Here’s my simple version:...

March 9, 2024 Â· 3 min Â· Denis Nutiu

Object Pool Pattern

Hi 👋 In this article we’ll talk about the Object Pool pattern in Golang. The Object Pool pattern is a design pattern used in situations when constructing objects is a costly operation, for example building an HTTPClient or DatabaseClient object can take some time. By having a pool of resources, the resources are requested from the pool when needed and then returned when not needed so they can be reused later....

August 21, 2022 Â· 5 min Â· Denis NuČ›iu

Context Managers and Cross Cutting concerns in Python

Hello, In this short article I would like to talk about context managers. I personally consider that at the core they are just a form of decorators. If you don’t know what a decorator is check the Decorator Pattern Wikipedia article. Decorators can be used to implement cross-cutting concerns. We have componentA and we need logging and security, we could write the logic for logging and security handling in componentA but some people consider component a should be componentA not componentAthatAlsoKnowsAboutSecurityAndOtherStuff....

March 7, 2021 Â· 4 min Â· Denis NuČ›iu

Method Injection and Property Injection Design Patterns

Hello, In this article we’re going to explore the Method Injection and Property Injection design patterns. To demonstrate the patterns I’m going to add a new interface named Encoder to the printer.py file and a concrete implementation for two encoders: Rot13Encoder and NullEncoder. class Encoder(metaclass=abc.ABCMeta): def encode(self, message: Message) -> Message: raise NotImplementedError("encode must be implemented!") class Rot13Encoder(metaclass=abc.ABCMeta): def encode(self, message: Message) -> Message: return Message(codecs.encode(str(message), 'rot_13')) class NullEncoder(metaclass=abc.ABCMeta): def encode(self, message: Message) -> Message: return message The Encoder will be used by the printer in order to encode the messages before printing them....

December 28, 2020 Â· 2 min Â· Denis NuČ›iu

Constructor Injection and Null Object Design Patterns

The Constructor Injection design pattern is a pattern that helps you declare all the required dependencies of a class in it’s constructor. This is useful because it helps you decouple the code, you can specify an interface instead of a concrete type, remember, program to an interface. Also, in the constructor it is easier to guard against null objects. The calling code doesn’t have to worry about null exceptions every time it uses a dependency....

November 7, 2020 Â· 2 min Â· Denis NuČ›iu

Composition Root Pattern: How to Write Modular Software

The composition root is a design pattern which helps you structure a software application by implementing a class that builds all the other classes. In this example we will examine this pattern in Python. Here’s the object graph of the classes that we’re going to implement: I have designed a sample application that we’re going to use. It contains three components: ConsoleInputListener, ConsolePrinter and RomanianTranslator and a value object class: Message....

November 6, 2020 Â· 3 min Â· Denis NuČ›iu