FAB-PS
Factory method, abstract factory, builder,
prototype, singleton
ABCD-FFP,
Adapter, Bridge, Composite, Decorator,
Facade, Flyweight, Proxy
CCI-MMO-SSTV,
Command, Chain of responsibility, Iterator
Mediator, Memento, Observer,
State, Strategy, Template method, Visitor
Factory method
- Have an interface IProduct
- Have concrete product classes that inherit this interface
- Have an abstract class ProductFactory that has an abstract method CreateProduct() that returns IProduct
- Have concrete factories that inherit this product factory, these classes also return IProduct but actually return their concrete objects
- The client code gets a concreteFactory inside a product factory type variable, and using that gets a concrete object inside a IProduct variable
Abstract Factory
- Have interfaces for products, IButton, ICheckbox
- Have concrete product classes, WindowsButton, MacButton, WindowsCheckbox, MacCheckbox
- Have an interface IUIFactory which has createButton and createCheckbox methods returning IButton and ICheckbox
- Implement concrete factories, WindowsFactory and MacFactory, which inherit IUIFactory and implement createButton and createCheckbox methods
- Client code creates. IUIFactory type variable, check at runtime what OS it is, then gets respective windows or mac factory into this variable,
rest of the code simply uses this factory to create button or checkbox without a worry since we will use IButton and ICheckbox
Builder
- a private constructor
- a public builder method inside our class that builds the object with various methods and has a build method to finally return the complete object
- the various methods in builder return builder, only the build method returns the actual object
- builder method has private readonly field of class type that creates the instance directly but this instance is returned only via build method
Prototype
- Clone method
- ICloneable interface
- shallow copy (this.MemberwiseClone() )
- deep copy
- Custom Clone method instead of ICloneable (better for deep copy)
Singleton
- simple impl
- lock impl
- what is lock (monitor.enter)
- double lock impl
- eager initialisation
- lazy initialisation
- why readonly (locking mutable object bad)
Adapter
- Class adapter (by inheritance) and object adapter (by composition)
- In first, we write an adapter class that inherits both the target interface and the adaptee class and likewise handles the logic inside
- In second, we inherit the target interface and have an instance (a field injected via constructor) of the adaptee class, and likewise handle the logic
Bridge
- Abstraction – Defines the interface and maintains a reference to an implementor.
- Refined Abstraction – Extends the abstraction while keeping the implementor reference.
- Implementor – Defines the interface for implementation classes.
- Concrete Implementor – Implements the Implementor interface.
When to Use Bridge Pattern?
When you need to separate abstraction from implementation.
When you expect future changes in both abstraction and implementation.
When extending a class in multiple dimensions (e.g., shape types & drawing APIs).
- Basically, one interface IDrawAPI which has method DrawCircle.
- Classes RedCircle and BlueCircle implement this interface.
- One abstract class Shape, which takes object of IDrawAPI type in constructor(and assign to internal field of IDrawAPI type), and an abstract method Draw.
- A class Circle inherits this Shape abstract class, and overrides Draw method in which it uses drawAPI.DrawCircle to implement this method.
- Now, the ‘bridge’ usage, is that we instantiate Circle class and assign it to Shape type variable. While creating Circle object, we pass in object
of redCircle or blueCircle in its constructor, as Circle expects something of type IDrawAPI. - So, now we simply call Draw methods on these.