《The Object-Oriented Thought Process》读书笔记10

本文探讨了设计模式的概念,包括创建型、结构型和行为型三种类型,并通过具体实例介绍了单例模式、适配器模式及迭代器模式等的设计与应用。此外,还讨论了反模式及其解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值