Design Patterns
Introduction
Gang of Four (GoF)is a term referring to a group of 4 authors (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) who published the classic bookDesign Patterns: Elements of Reusable Object-Oriented Softwarein1994. The book summarizes23 design patternscommonly used to solve recurring object-oriented programming (OOP) design problems.- The main benefit of
GoF Design Patternsis providing standardized solutions to common problems, making source code easier to maintain, extend and creating a common language for developers when discussing software architecture.
These 23 design patterns are divided into 3 main groups:
Creational Patterns
Factory Method: Defines aninterfacefor creating an object, but lets subclasses decide which class to instantiate.Abstract Factory: Provides aninterfacefor creating families of related or dependent objects without specifying their concrete classes.Builder: Separates the construction of a complex object from its representation, allowing the same construction process to create different representations.Prototype: Creates new objects by cloning an existing prototype object instead of creating them from scratch.Singleton: Ensures a class has only one instance throughout the application lifecycle and provides a global point of access to it.
Structural Patterns
Adapter: Converts theinterfaceof a class into anotherinterfaceexpected by the client, allowing classes with incompatibleinterfacesto work together.Bridge: Decouples an abstraction from its implementation so that the two can vary independently.Composite: Composes objects into tree structures to represent part-whole hierarchies, allowing clients to treat individual objects and compositions of objects uniformly.Decorator: Attaches additional responsibilities to an object dynamically at runtime without altering the structure of the original class.Facade: Provides a simplifiedinterfaceto a set of complexinterfacesin a subsystem.Flyweight: Optimizes memory usage by sharing as much common data as possible among multiple similar objects instead of storing data individually.Proxy: Provides a surrogate or placeholder object to control access to another object.
Behavioral Patterns
Chain of Responsibility: Passes a request along a chain of handler objects, where each handler decides whether to process the request or pass it to the next handler.Command: Encapsulates a request as an object, allowing parameterization of operations, queuing of requests and support for undo operations.Interpreter: Defines a grammatical representation for a specific language and provides an interpreter to process sentences in that language.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, reducing direct dependencies between them.Memento: Captures and externalizes an object's internal state so that it can be restored later without violating encapsulation.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.Strategy: Defines a family of algorithms, encapsulates each one and makes them interchangeable at runtime.Template Method: Defines the skeleton of an algorithm in a base class, allowing subclasses to override specific steps without changing the algorithm structure.Visitor: Separates an algorithm from an object structure on which it operates, allowing new operations to be added to existing object structures without modifying them.
Happy coding!
Comments
Post a Comment