Design patterns every programmer should know

design-patterns


Design patterns are optimized, reusable solutions to the programming problems that we encounter every day. A design pattern is a description, or template, for how to solve a problem that can be used in many different situations, it's not language-specific. A good design pattern should be implementable in most if not all languages, depending on the capabilities of the language.

Singleton

The singleton pattern is used to limit the creation of a class to only one object. This is beneficial when one and only one object is needed to coordinate actions across the system. There are several examples of where only a single instance of a class should exist, including caches, thread pools, and registries.

Adapter

The adapter pattern is a design pattern that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.

Factory method

A factory is a creator of objects that have a common interface. Factory method is a pattern that defines an interface for creating an object but lets subclasses decide which class to instantiate. To accomplish this, objects are created by calling a factory method instead of fo calling a constructor.

Object pool

This pattern is useful when it is necessary to work with a large number of objects that are particularly expensive to instantiate especially if each object is only needed for a short period of time. instead of creating and destroying the expensive objects, the object pool pattern suggests reusing the already created objects.

Builder

The builder design pattern is a pattern that separates the construction of a complex object from its representation. By doing so the same construction process can create different representation. Simply said, it builds a complex object using simple objects and a step by step approach

Composite

The composite pattern describes a group of objects that are treated the same way as a single instance of the same type of object. Composite should be used when clients ignore the differences between compositions of objects and individual objects.

Prototype

This pattern uses a prototype to create new objects by copying the prototype. In other words, instead of creating new objects, we clone them from the prototype. This pattern used when object creation is resourced expensive. By cloning new objects, we avoid the inherent cost of creating a new object in a standard way.





Labels: ,