Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma,Richard Helm, Ralph Johnson, and John Vlissides.
Gang of Four.
GoF.
TheFour Elements of a Pattern:
The pattern name
The problem
The solution
The consequences
address做动词的用法
address vt.
1.(写信封,包裹)写姓名地址
例:address a parcel to sb 把包裹寄给某人
2.向....致意
例:address one's thanks to sb.向某人表示感谢
3.向....正式演说
例:address the crowd 向人群演讲
4.称呼
例:address sb. as Dr.称呼某人为博士
5.对付,处理
例:addressthe problem of prollution 处理污染问题
Design Patterns defines the MVC components in the following manner:
The Model is the application object, the View is the screen presentation,and the Controller defines the way the user interface reacts to user input.
The authors ofthe book divided the patterns into three categories:
l Creational patterns
l Structural patterns
l Behavioral patterns
l Abstract factory
l Builder
l Factory method
l Prototype
l Singleton
Figure15.3 SingletonUML diagram.
public classClassicSingleton {
private static ClassicSingleton instance =null;
protected ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static ClassicSingletongetInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
}
Two References to a Single Counter
Be awarethat in this example, there are two separate references pointing to thecounter.
l Adapter
l Bridge
l Composite
l Decorate
l Façade
l Flyweight
l Proxy
adapter pattern…This patternis a good example of how the implementation and interface are separated.
package MailTool;
public class MailTool {
public MailTool () {
}
public int retrieveMail() {
System.out.println (“You’ve Got Mail”);
return 0;
}
}
let’s supposeyou want to change the interface in all your company’s clients from retrieveMail() to getMail().
package MailTool;
interface MailInterface {
int getMail();
}
package MailTool;
class MyMailTool implements MailInterface {
private MailTool yourMailTool;
public MyMailTool () {
yourMailTool= new MailTool();
setYourMailTool(yourMailTool);
}
public int getMail() {
returngetYourMailTool().retrieveMail();
}
public MailTool getYourMailTool() {
return yourMailTool ;
}
public void setYourMailTool(MailTool newYourMailTool) {
yourMailTool = newYourMailTool;
}
}
package MailTool;
public class Adapter
{
public static void main(String[] args)
{
MyMailTool myMailTool = new MyMailTool();
myMailTool.getMail();
}
}
When you doinvoke the getMail()method, you are using this newinterface to actually invoke the retrieveMail() method from the original tool. …by creating this wrapper, you can actuallyenhance the interface and add your own functionality to the original class.
l n Chain of response
l n Command
l n Interpreter
l n Iterator
l n Mediator
l n Memento
l n Observer
l n State
l n Strategy
l n Template method
l n Visitor
The followingcode creates a vector and then inserts a number of strings into it.
package Iterator;
import java.util.*;
public class Iterator {
public static void main(String args[]) {
// Instantiate an ArrayList.
ArrayList
// Add values to the ArrayList
names.add(new String(“Joe”));
names.add(new String(“Mary”));
names.add(new String(“Bob”));
names.add(new String(“Sue”));
//Now Iterate through the names
System.out.println(“Names:”);
iterate(names );
}
private static void iterate(ArrayList
for(String listItem : arl) {
System.out.println(listItem.toString());
}
}
}
反面教材
In the November1995 C++ Report,Andrew Koenig described two facets of antipatterns:
n Thosethat describe a bad solution to a problem, which result in a bad situation
n Thosethat describe how to get out of a bad situation and how to proceed from thereto a good solution
Thus,antipatterns lead to the revision of existing designs, and the continuousrefactoring of those designs until a workable solution is found.
In this chapter,we exploredthe concept of design patterns.