Design Pattern #
Creational and Structural Design Patterns #
Singleton Pattern #
- One object of a class.
- Globally accessible within the program.
Public class ExampleSingleton { // lazy construction
// the class variable is null if no instance is
// instantiated
Private static ExampleSingleton uniqueInstance = null;
Private ExampleSingleton() {
…
}
// lazy construction of the instance
Public static ExampleSingleton getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new ExampleSingleton();
}
Return uniqueInstance;
}
…
}
Factory Method Pattern #
public class KnifeFactory {
public Knife createKnife(String knifeType) {
Knife knife = null;
// create Knife object
If (knifeType.equals("steak")) {
knife = new SteakKnife();
} else if (knifeType.equals("chefs")) {
knife = new ChefsKnife();
}
return knife;
}
}
Facade /fə’sɑ:d/ Design Pattern #
- A facade simply acts as a point of entry into the subsystem
- Wrapper class that encapsulates a subsystem in order to hide the subsystem’s complexity
- A facade class can be used to wrap all the interfaces and classes for a subsystem
Adapter Pattern #
- Facilitates communication between two existing systems by providing a compatible interface.
Composite Pattern (Polymorphism) #
Enforcing polymorphism across each class through implementing an interface (or inheriting from a superclass).
Proxy Pattern #
- In this example, class
OderFulfillment
is the proxy class, which implement interfaceIOrder
, and send the requests toWarehouse
.
Decorator Pattern #
- Add additional functions to the original classes
- Serves as the abstract superclass of concrete decorator classes that will each provide an increment of behaviour
Behavioural Design Patterns #
- Focus on how independent objects work towards a common goal
Template Method Pattern #
Chain of Responsibility Pattern #
- Like
switch
andtry/catch
block
State Pattern #
- Change the behaviour of an object based on the state that it’s in at run-time.
Command Pattern #
Observer Pattern #
Design Principles #
MVC Pattern #
- Basic Model-View-Control Pattern.
Open/Closed Principle #
- Consider a class as being
closed
to editing once it has been:- Tested to be functioning properly
- All the attributes and behaviours are encapsulated
- Proven to be stable within your system
Dependency Inversion Principle #
- The principle states that high level modules should be depend on high level generalizations, and not on low level details.
- The client classes should depend on an interface or abstract class instead of referring to concrete resources.
Composing Objects Principle #
- This principle states that classes should achieve code reuse through aggregation rather that inheritance to reduce coupling.
- Inheritance should be only used to extend classes.
Interface Segregation Principle #
- The Interface Segregation Principle states that a class should bot be forced to depend on methods it does not use. This means that any classes that implement an interface, should not have “dummy” implementations of any methods defined in the interface. Instead, you should split large interfaces into smaller generalizations.
Principle of Least Knowledge #
- Classes should know about and interact with as few other classes as possible.
- A method, M, of an object should only call other methods if they are:
- Encapsulated within the same object.
- Encapsulated within an object that is in the parameters of M.
- Encapsulated within an object that is instantiated inside the M.
- Encapsulated within an object that is referenced in an instance variable of the class for M.
UML #
Words #
- encapsulate vt. 压缩;将…装入胶囊;将…封进内部;概述 vi. 形成胶囊
- delegate [‘deliɡət, -ɡeit, ‘deliɡeit] vt. 委派…为代表 n. 代表
- stimulate [‘stimjuleit] vt. 刺激;鼓舞,激励 vi. 起刺激作用;起促进作用
- generalization [,dʒenərəlai’zeiʃən, -li’z-] n. 概括;普遍化;一般化 接口化
- abstraction 抽象化
- polymorphism [,pɔli’mɔ:fizm] n. 多态性;多形性;同质多晶
- segregation [,seɡri’ɡeiʃən] n. 隔离,分离;种族隔离