← back to blog

Design pattern

Overview design pattern - OOP design.

Design pattern

Design patterns are like handling templates when working with object-oriented design. They provide a long-term vision and help us save time in organizing and designing objects in systems.

This not only usable for senior developers but also for any developers who want to have the vision of object-oriented design.

Creational Pattern

Creational patterns are design templates that provide methods for creating objects in many different ways. When you learn them, you develop a mindset to organize code for maintainable and reusable design.

Here are some examples of these patterns:

  • Singleton: Create only one instance on the application lifecycle.
  • Factory Method: Builds a factory to create objects.
  • Abstract Factory: Builds a factory to create a family of objects.
  • Builder: Organizes the creation of objects to create multiple variants, allowing flexibility in object composition.
  • Prototype: Clones objects from existing ones instead of copying to a new class.

Structural Pattern

Structural patterns are design patterns that facilitate the composition of classes or objects into larger structures while keeping these structures flexible and efficient. They help ensure that if one part of a system changes, the entire system doesn’t need to be modified. These patterns focus on how classes and objects can be combined to form larger, more complex structures.

Here are some examples of these patterns:

  • Adapter pattern: Create a connection between an old class/instance with a new class/instance.
  • Bridge pattern: A bridge between a seperate objects.
  • Composite: Create a structural from a single object or group of objects. Tree is a data structure used to storage and access data.
  • Decorator: A design allow add feature for an existed instance without effect to a object structural.
  • Facade: A wrapping way to hide the complexity of handling
  • Flyweight: Manage factory to only create one instance in the lifecycle - reduce a duplicate instance
  • Proxy: Like a protection mechanism of a class.

Behavior Pattern

Behavioral patterns focus on how objects interact and communicate with each other. Behavioral patterns abstract complex control flows and ensure that objects can cooperate effectively without tightly coupling the classes.

  • Chain of Responsibility: Passes a request along a chain of handlers. Each handler decides either to process the request or to pass it to the next handler in the chain.
  • Command: Encapsulates a request as an object, thereby allowing for parameterization of clients with different requests, queuing of requests, and logging of request operations.
  • Interpreter: Implements a specialized language or expression and interprets sentences in that language by defining a grammar and an interpreter.
  • Iterator: Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
  • Mediator: Defines an object that encapsulates how a set of objects interact, promoting loose coupling by preventing objects from referring to each other explicitly.
  • Memento: Captures and externalizes an object’s internal state without violating encapsulation, so that the object can be restored to this state later.
  • Observer: Defines a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically.
  • State: Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
  • Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. The strategy pattern allows the algorithm to vary independently from the clients that use it.
  • Template Method: Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
  • Visitor: Represents an operation to be performed on the elements of an object structure. The Visitor pattern lets you define a new operation without changing the classes of the elements on which it operates.

References

Related posts

Design Patterns ·
Singleton
Design Patterns ·
[Structural] Bridge
Design Patterns ·
[Structural] Composite
← back to blog