- Design Principles
- Open-close, open for extension, close for modification
- Liskov substitution, any subclass can be in the place where base class is
- Demeter, least known principle
- interface segregation, prefer small interface than big one
- dependency reversion, depends on interface, not implementation
- composite over inheritance
- Design Patterns
=======================Creation=============================
- Factory Method
- simple factory method, create different implementations of one interface in one method
- multiple factory methods, each implementation has one method
- static factory methods, no need to create factory instance. methods are static.
- Abstract Factory,
- to fix problem of Factory Method: need to change the Factory class when want to add more implementations.
- add one more layer for Factories, Abstract Factory.
- when want to add more impls, just create concrete factory of Abstract Factory and the concrete factory creates product. no modification to other factories.
- Single Instance
- the double-check way to get single instance has potential issue of uninitialized instance due to non-order between instance initialization and set reference to variable. The typical issue is, one thread gets lock and a = new A(). JVM could assign memory to the instance and set reference to a. then do instance initialization. In between, other threads could see the a without being initialized yet.
- Notes:
private constructor,
private Object readResolve() {return _instance;} to prevent deserialization
- Builder, different from Factory Method that builder creates complex object is composed of many parts which are created one by one in builder itself
- Prototype, Cloneable, private Object clone() throws CloneNotSupportedException
=========================Structure==================================
- Adapter
- class adapter, Ad extends Source implements Targetable
- object adapter, Wrapper(source obj) implements Targetable
- interface adapter, Ad1 extends AbstractSource, which implements target interface; Ad2 extends AbstractSource
- Decorator, Dc(source obj) implements Targetable, source implements the same interface Targetable
- Proxy, ClassLoader, Interfaces and InvocationHandler
- Facade,
- Bridge, JDBC DriverManager and JDBC Driver (mysql driver, oracle driver)
- Composite, part and whole
- Flyweight
==========================Behavior====================================
- Strategy, a series of algorithms implement same interface.
- Template Method
- Observer, listener
- Iterator
- Chain of Responsibility, Servlet Filter
- Command, invoker, command and receiver
- Memento, original, memento and storage
- State, operation gets changed when state changes
- Visitor, decouple data structure and data algorithm
- Mediator, least known principle
- Interpreter
- Miscs
- aggregation/composition, composition has stranger relation than aggregation. parts alone are meaningless in composition
- dependency/relation, dependency is reflected as method params, static invocation in Java. relation is reflected as fields of class in Java.