Design Patterns
Last updated
Last updated
Interface -> implementation
Pattern: A pattern is a solution to a problem in a context.
Context: A context is the situation in which the pattern applies. This should be a recurring situation.
Problem: The problem refers to the goal you are trying to achieve in this context, but it also refers to any constraints that occur in the context.
Solution: The solution is what you're after: a general design that anyone can apply that resolves the goal and set of constraints.
Anti-pattern: An anti-pattern tells you how to go from a problem to a BAD solution.
Inheritance can make the rubber ducks fly
Interfaces affects code reusability
Encapsulate behaviors
Publishers (subject) + subscribers (observers) = Observer Pattern
Publisher notifies the subscribers
Subscribers upon being notified pulls the required data from the publisher
Inheritance by composition and inheritance by delegation
A thing or two about decorators:
Decorators have the same supertype as the objects they decorate
An object can be wrapped by one or more decorators
Pros:
Provides an alternative to subclassing or extending behavior
Can extend behavior without modifying existing code
Cons:
Overuse can add complexity
Factory pattern encapsulate object creation by letting sub-classes decide what objects to create.
Dependency inversion: Depend upon abstractions. Do not depend upon concrete classes.
Dependency inversion principles:
No variable should hold a reference to a concrete class.
No class should derive from a concrete class.
No method should override an implemented method of any of its base classes.
The abstract factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete objects.
Relies on inheritance: object creation is delegated to subclasses, which implement the factory method to create objects.
Relies on object composition: object creation is implemented in methods exposed in the factory interface.
The singleton pattern ensures a class has only one instance, and provides a global point of access to it.
The command pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.
In simple words it is like event listeners attached to different UI buttons which is fired on common onClick
event.
The adapter pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Object adapter: normally the calls made by the client to the adapter ends of invoking corresponding call(s) at the adaptee.
Class adapter: Not possible in Java as it requires multiple inheritance, but ideally it gives the adaptee respond to client calls as well.
The facade pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
The template method pattern 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.
The iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
The composite pattern allows you to compose objects into tree structures to represent part-whole heirarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
The state pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
The proxy pattern provides a surrogate or placeholder for another object to control access to it.
Use the proxy pattern to create a representative object that controls access to another object, which may be remote, expensive to create, or in need of securing.
Bridge
Builder
Chain of responsibility
Flyweight
Interpreter
Mediator
Memento
Prototype
Visitor
Pinciple of least knowledge: talk only to your immediate friends.
The hollywood principle: Don't call us, we'll call you.
A class should have only one reason to change.
Single responsibility principle:
DRY (Don't repeat yourself):
Pure function: