《The Object-Oriented Thought Process》讀書筆記10

15 Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma,Richard Helm, Ralph Johnson, and John Vlissides.

Gang of Four.

GoF.

Why Design Patterns?

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 處理污染問題

Smalltalk’s Model/View/Controller

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.

Types of Design Patterns

The authors ofthe book divided the patterns into three categories:

l Creational patterns

l Structural patterns

l Behavioral patterns

Creational Patterns

l  Abstract factory

l  Builder

l  Factory method

l  Prototype

l  Singleton

    The SingletonDesign Pattern

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.

Structural Patterns

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.

    The AdapterDesign Pattern

 

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.

Behavioral Patterns

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 Iterator Design Pattern

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<String> names = new 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<String> arl) {

       for(String listItem : arl) {

           System.out.println(listItem.toString());

       }

    }

}

Antipatterns

反面教材

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.

Conclusion

In this chapter,we exploredthe concept of design patterns.

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