Master the Software Craftsmanship with Design Patterns

CHAIRI Chaimae
3 min readJan 30, 2024

--

You might have heard about design patterns, encountered them somewhere, or even studied them in college. Perhaps you’ve felt frustrated by their multitude and intricate implementations. But have you ever wondered why we use them and when we should?

Before diving deep into the topic, let’s first take a look at the definition. A design pattern is a general reusable solution to a common problem encountered in software design. It represents a best practice for solving a certain type of problem and provides a template for creating a well-structured, efficient, and scalable software architecture. Design patterns help developers communicate efficiently, make code more understandable, and promote code reuse across different projects.

Categories

Design patterns are categorized into three main groups:

  • Problem related to creating objects ==> Creational Patterns
  • Problem related to assembling object ==> Structural Patterns
  • Problem related to managing their interactions ==> Behavioral Patterns
Design patterns illustrations

Creational Patterns: Architects of Instances

  • Singleton: Ensures a single instance of a class used logging or database connection. In this senario scenarios, Singleton guarantees only one instance of the database connection class, streamlining control and preventing unnecessary duplication. This is essential for consistent and centralized management, especially in tasks like logging or critical data operations.
  • Factory Method: Decouples object creation from usage in creating different database connections dynamically.
  • Builder: Assembles complex objects step by step. For example, it is needed when crafting complex domain objects.
  • Prototype: Reuses cached objects to reduce database calls.
  • Abstract Factory: Creates families of related objects. For instance, it serves for building parsers for file formats like JSON, XML, or CSV which allows you to define interfaces for these parsers, and ensures that the created parser objects adhere to a consistent structure.

Structural Patterns: Building Bridges and Adapting Interfaces

  • Adapter: Makes incompatible interfaces compatible. It might be used in integrating a new logging library into an existing system.
  • Composite: Represents part-whole hierarchies. For instance, we can group graphic objects in a drawing application.
  • Proxy: Controls access to objects as in lazy loading of a high-resolution images in a web application.
  • Decorator: Dynamically adds or removes behavior . It might be used in implementing compression or encryption on file streams.
  • Bridge: Decouples essence from the platform. It might be used in separating platform-specific code from core logic.

Behavioral Patterns: Orchestrating Interactions

  • Strategy: Defines a family of algorithms like in enabling users to choose different sorting or compression techniques.
  • Observer: Maintains consistent states by being notified of changes as in a messaging system.
  • Command: Encapsulates requests. It is might be implemented undo/redo functionality in a text or image editor.
  • State: Encapsulates state-specific behavior. For instance, managing different states of a user interface element: enabled, disabled...
  • Template Method: Defines the skeleton of an algorithm, with steps delegated to subclasses. It is crucial for unit testing with customizable setup and teardown steps.

To get more about the subject, please visit this:

Design Patterns (refactoring.guru)

To use the multiple design patterns effectively, developers should thoroughly understand the specific challenges they face and choose patterns intentionally based on the problem at hand, considering factors like project size and complexity. Documenting design choices and regularly reviewing patterns within the codebase are essential practices. Critically, design patterns should not introduce unnecessary rigidity; instead, they should align with the project’s evolution.

In essence, design patterns are most effective when used as thoughtful solutions to identified problems.

Note: The subject of this post is inspired by a linkedin post and the mentioned examples are exactly the same mentioned in the post.

--

--

CHAIRI Chaimae
CHAIRI Chaimae

Written by CHAIRI Chaimae

This space's to document my learning and track my progress. Hope it helps someone somewhere. You're welcomed!

No responses yet