😴 🧙🌈 ʕ•ᴥ•ʔ

The following is a recap on some design patterns we use everyday, but don’t know we are using them or don’t know them by their name.

https://dev.to/documatic/from-problems-to-solutions-understanding-design-patterns-3b7i


An interesting fact in software development is that you are almost always not the first person to face a specific problem. The problem may have been occurring since the dawn of software development. In this blog post, we are going to take a look at design patterns, why we need them, and an overview of some of the design patterns.

Design patterns are a systematic approach for addressing recurring problems that programmers face. They describe a universal standard to solve those problems. The provided solution is in such a way that it can be reused despite the specific domain of the problem at hand. Therefore, next time you face a programming problem, beware that you don’t have to rediscover the solution from scratch.

Why Design Patterns?

Elements of a Design Pattern

  1. Name - is a handle used to describe the design issue. It is necessary because it gives a common ground of understanding among programmers.
  2. Problem - is used to describe when the pattern can be used.
  3. Solution - is a template that describes elements and their relationships without providing implementation detail.
  4. Consequence - is used to describe the trade-offs of the pattern. Examples: time and space trade-offs, flexibility, extensibility, etc.

Types of Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software, commonly known as the Gang of Four, is a cornerstone in software engineering that brought design patterns into the mainstream. The book breaks down 23 different design patterns that fall into the following 3 categories:

  1. Creational - They are concerned with object-creation techniques.
  2. Structural - They are concerned with the combination of objects and classes in a flexible and effective way.
  3. Behavioral - They are concerned with the interaction between classes and objects and their responsibility.

[

23 design patterns gang of four](https://raw.githubusercontent.com/freitzzz/cinderela/master/blog/general/from-problems-to-solutions-understanding-design-patterns/w1tmfrb1zt6bqgzu8bvv.webp)

Introduction to Six Design Patterns

The following section is going to provide an overview of 6 software design patterns (2 from each category) and how they can be used to solve common programming challenges.

1. Singleton

The Singleton pattern is a type of creational pattern that ensures the creation of a single instance of a class and provides a global point of access to that instance.

The Why:

Suppose you are building a Database connection class:

The How:

There are various ways of implementing a Singleton pattern. However, all of the implementations share the following basic concepts:

2. Factory Method

The factory method is also another type of creational pattern that provides an interface for creating objects but lets the subclasses decide which class to instantiate. In other words, the subclasses can change the type of objects that will be created.

The Why:

Suppose you are creating a logging framework by using the factory method design pattern to log different kinds of messages including, but not limited to, error messages, warning messages, and informational messages. In this use case:

The How:

3. Adapter

Adapter is a type of structural design pattern that allows objects with incompatible interfaces to work together. It lies at an intermediary position between the client and the interface that the client wants to communicate with.

The Why:

Suppose you are building a video format conversion tool by following the Adapter design pattern:

The How:

4. Decorator

Decorator is another type of structural design pattern that lets you attach new behaviors to objects without affecting the behavior of other objects from the same class. Even though inheritance can be used to alter the behavior of an object statically, a decorator helps you alter it dynamically as well.

The Why:

Suppose you are building a text editor:

The How:

Note - Adapter provides a different interface to its subject. Decorator provides an enhanced interface.

5. Observer

Observer is a behavioral design pattern that defines a one-to-many dependency between objects so that when one object, named the subject, changes its state, all of its dependents, named observers, are notified and updated automatically.

The Why

Suppose you are building a stock market website:

The How

6. Strategy

Strategy is another type of behavioral design pattern that allows you to define a family of algorithms, encapsulate each one, and make them interchangeable. It enables the client to select the algorithm’s behavior at run time.

The Why

Suppose you are creating software that has sorting functionality:

The How

Conclusion

In conclusion, having a solid grasp of design patterns is crucial, especially as you advance your career in software engineering.

#reads #hana belay #design pattern #software engineering #gof