Software Design

- 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
  1. simple factory method, create different implementations of one interface in one method
  2. multiple factory methods, each implementation has one method
  3. static factory methods, no need to create factory instance. methods are static.
  • Abstract Factory, 
  1. to fix problem of Factory Method: need to change the Factory class when want to add more implementations.
  2. add one more layer for Factories, Abstract Factory. 
  3. 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
  1. 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.
  2. 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
  1. class adapter,  Ad extends Source implements Targetable
  2. object adapter, Wrapper(source obj) implements Targetable
  3. 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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章