【转】如何应用设计模式设计你的足球引擎

 
如何应用设计模式设计你的足球引擎(第一、二部分) 收藏
 


原文地址:

http://www.codeproject.com/KB/architecture/applyingpatterns.aspx

作者:An 'OOP' Madhusudanan

译者:赖勇浩(http://blog.csdn.net/lanphaday

译者说:这是一篇非常好的文章,有非常棒的例子,非常棒的文笔,非常棒的代码(VB.net编写的,但你肯定读得懂),如果你还不懂设计模式,那它肯定是最适合你的 DPs 文章之一。


第一部分

解决方案架构师:你可以尝试使用模式

愚蠢的开发者:好的,它像 ActiveX 控件那样用吗?"

介绍

关于本文

本文希望能够做到

  •  以简单、可读的方式向你介绍模式
  • 教你如何真正“应用”模式(模式易学,但必须有过硬的设计本领才能应用它们解决问题)
  • 让你认清应用 Builder、Observer、Strategy和 Decorator(这几个可是少数极常用的模式)模式的时机。
  • 展示如何用 Observer 模式解决设计难题

全文通过如下内容依次推进

  1. 为一个简单足球游戏引擎建立模型
  2. 确定足球游戏引擎中的设计问题
  3. 决定用哪些模式来解决设计问题
  4.  然后真正地利用 observer 模式来解决其中一个设计问题。

先决条件

  • 你需要懂得一些阅读和理解 UML 图的知识。

代码使用指南

  • 相应的 zip 文件包含了代码、UML设计图(visio 格式)等,你可以使用 Winzip 等压缩软件解压。

简说设计模式

即使对设计模式知之甚少,设计师和开发者也会倾向于重用类和对象间来简化设计过程。简言之就是“设计模式考虑了多种对象(类、关系等)间的协作”,为常见的设计问题提供解决方案。最为重要的是他们为设计师和程序员提供一些“行话”来谈论他们的设计。例如你可以告诉你的朋友你使用了 Builder 模式来解决你项目中的一些问题。
Erich Gamma、Richard Helm、Ralph Johnson和John Vlissides[即知名的四人帮(GOF)]为常见的设计问题提供了一致的分类模式。GOF 模式被认为是其它所有模式的基础。
使用模式的基本原则是可重用性。如果你正确理解了以模式为中心的软件工程概念,当遇到问题时你就不会重复发明轮子。这里有一些关于设计模式的重要观点:

  • 设计模式不是代码,实际上它是一种解决问题的方法或模型。
  • 设计模式是关于设计和对象间互动的,为它们提供解决常见设计问题的可重用的解决方案。
  • 设计模式通常可以用 UML 图来表示。

真正的动手的经验可以给你更好的理念。

架构(简单)足球引擎

假设你在一家游戏开发公司供职,上头决定让你为公司的重要项目——足球游戏引擎做一套解决方案架构(很棒,哈哈)。现在由你领导设计整个足球游戏引擎,突然你就多了许多要考虑的事情,比如:

  • 在游戏系统中如何标识实体,
  • 如何确定设计问题所在,
  • 如何应用模式来搞定你的设计说明书?

标识实体

首先,需要标识游戏引擎中的所有对象。因此你要想像一下终端用户将如何使用这个系统,现在假设终端用户将用以下序列来操作游戏(先简单化):

  • 打开游戏
  • 选择两支球队
  • 配置球员
  • 选择球场
  • 开始

系统是可能有若干个球场(PlayGrounds)和球队(Teams)。系统中实际上起码有这些对象:

  • 球员(Player),踢球的人。
  • 球队(Team),包含若干球员。
  • 球(Ball),球员所持有的物体。
  • 球场(PlayGround),比赛进行的地方。
  • 裁判(Referee),球场上控制比赛的人。

另外,游戏引擎中还有一些逻辑对象,如:

  • 游戏(Game),定义了足球比赛,制定球队、球、裁判、球场等。
  • 同时模拟一个或多个比赛。
  • 球队策略(TeamStragy),比赛时决定球队的策略

这只是对系统的一个抽象形式,下图表示了系统中的类的多样性和它们之间的接连关系(“has”)。其中箭头表示了阅读的方向次序。游戏引擎(GameEngine)拥有若干比赛(Game);比赛(Game)有三个裁判、一个球、两支球队和一个球场;而球队又有多个球员和一个策略产生器。

 

Fig 1 - High level view

确定设计问题

现在你要决定

  • 这些对象如何组织
  • 如何创建
  • 如何在设计说明书中确切地阐述当他们彼此影响时的行为。

首先,你得写下对足球引擎的最小描述来确定设计问题,例如下面是是对我们之前讨论的一对象的设计问题

  • 足球(Ball)
    • 当球的位置变化,所有的球员和裁判应当能够立即感知。
  • 球队与球队策略(Team and TeamStrategy)
    • 在比赛中,终端用户可以改变球队的策略(如从进攻改为防守)
  • 球员(Player)
    • 球队中的球员还得有一些额外的职责,如前锋、后卫等,应该可以在运行进指派这些职责。
  • 球场(球场)
    • 每一个球场要有座位、草皮、观众等,而且每一个球场都应该有不同的外观。

现在让我们想想该怎么确定模式以解决这些设计问题

确定要用的模式

再仔细看看(是的,最好多看几次)上面确定的设计问题,现在让我们想想怎么用设计模式来解决它们。

1: 解决与球(Ball)相关的设计问题

首先来看看关于球的说明,需要设计一个框架使得当球的状态(位置)变化时能够通知所有球员和裁判,以得到球的新状态(位置),实际上就是:

特定的设计问题:当球的位置变态,马上通知所有球员和裁判。

问题泛化:当主题(这里是指球)改变,所有的依赖物(在这里是指球员等)能够自动获得通知并更新。

当你遇到这样的设计问题,应当马上想起 GOF 模式,甚至立马认识到可以用Observer 模式来解决问题。

观察者模式(Observer Pattern):定义了对象间一对多的依赖关系,当一个对象的状态改变,自动通知所有依赖对象并更新。

在这里我们使用这个模式是因为当球的位置变化时需要通知所有的球员。

2: 解决与球队(Team)和球队策略(TeamStrategy)相关的设计问题

然后,我们来解决球队和球队策略的问题。像之前讨论的那样,当比赛进行时,终端用户能够改变他的球队的策略(如从进攻改为防守)。无疑地,这意味着我们需要把球队策略从球队中分离出来。

特定的设计问题:在比赛进行中终端用户能够改变它的球队的策略(例如从进攻改为防守)

问题泛化:使客户(在这里是球队)能够独立地改变算法(球队策略)

你可以选择 Strategy 模式来解决上面这个设计问题。

策略模式(Strategy Pattern):定义一系列算法,通过封装使它们可以互相替换,Strategy模式使用户能够独立地改变算法。

3: 解决与球员(Player)相关的设计问题

现在让我们来完成与球员相关的设计说明书。从我们的问题定义可以确定我们需要在运行时为每一个球员指派不同的职责(如前锋、后卫等)。这时候我们可以考虑子类化(也就是继承),通过创建一个球员类,然后从这个基类派生一些类,如前锋、后卫等。但它的不足是当你子类化的时候,你不能从对象的实现中分离职责。

换言之,在我们的案例中子类化并非恰当的方法,因为我们需要从球员的实现中分离类似前锋、中锋、后卫等职责。原因在于球员在某一时刻是前锋,而另一个时刻同一个球员又可以是中锋。

特定的设计问题:球队中的球员有额外的职责,如前锋、后卫等,而且要能够在运行时指派。

问题泛化:需要在对象(在这里是指球员)上动态附加额外职责(如前锋、中锋等),而且不可使用子类化。

那么你可以选择 Decorator 模式来解决这个设计问题。

装饰者模式(Decorator Pattern):在对象上动态地额外附加职责,Decorator 提供了子类化之外的灵活的扩展功能。

4: 解决球场(PlayGround)相关的设计问题

如果看去看看球场的说明,可以发现球场的外观由多个子单元(如座位、草皮和观众等)决定。球场的外观根据这些子单元的不同而不同,因此,我们需要特别的构建方式,它可以创建不同的球场。也就是说一个意大利球场应该有与英格兰球场不同的座位结构和草皮,但游戏引擎却可以通过调用相同的函数族来创建这些球场。

特定的设计问题:每个球场都由座位、草皮和观众等构成,但它们又有互不相同的外观。

问题泛化:需要从对象(球场)的表示(球场的外观)分离它的构建,还需要使用同样的构建过程来创建不同的表示。

创建者模式(Builder Pattern):从复杂对象的表示中分离它的构建,从而使相同的构建过程能够创建不同的表示。

现在,你可以选择 Builder 模式来解决上面的设计问题。


第二部分

解决方案架构师:我叫你去学学模式

愚蠢的开发者:是的,现在我可以用模式开发一个足球引擎了

解决方案架构师:啊?你的意思是?!@@#

应用 Observer 模式

在这一节,我们先深入学习 Observer 模式,然后应用模式来解决第一个设计问题。不知道你还记不记得第一个设计问题:

  • 当球的位置变化,马上通知所有的球员。

理解 Observer 模式

下面是 Observer 模式的是 UML 类图:

 

Fig 2 - Observer Pattern

下面介绍一下这个模式的成员:

  • 主题(Subject

Subject类提供了挂上和拆卸观察者的接口,并且持有一序列的观察者,还有如下函数:

    • Attach - 增加一个新的观察者到观察者序列
    • Detach - 从观察者序列中删除一个观察者
    • Notify- 当发生变化时,调用每一个观察者的 Update 函数来通知它们。
  • 具体的主题(ConcreteSubject

这个类提供了观察者感兴趣的状态,它通过父类的 Notify 函数通知所有的观察者。ConcreteSubject的函数有:

    • GetState - 返回主题的状态
  • 观察者(Observer

Observer类为所有的观察者定义了一个更新接口,用以接收来自主题的更新通知,它是一个抽象类,可以派生具体的观察者:

    • Update - 这是一个抽象函数,具体的观察者会重载这个函数。
  • 具体的观察者(ConcreteObserver

这个类维护了一个主题的引用,用来在收到通知的时候接收主题的状态。

    • Update - 这是具体类重载的函数,当主题调用它时,ConcreteObserver 调用主题的 GetState 函数来更新与主题状态相关的信息。

应用 Observer 模式

现在让我们来看看怎么用这个模式解决我们的特定问题,下图或许能给你一点启发:

 

Fig 3 - Solving Our First Design Problem

当调用球的 SetBallPosition 函数设置一个新的位置时,它马上调用类 Ball 中定义的 Notify 函数。Notify 函数迭代观察者序列,并调用它们的 Update 函数。当 Update 函数被调用,观察者就可以通过调用 FootBall 类的 GetBallPosition 函数来得到球的新的状态位置。

各部分详述如下:

Ball (Subject)

下面是类 Ball 的实现。

  1. ' Subject : The Ball Class 
  2. Public Class Ball 
  3. 'A private list of observers 
  4. Private observers As new System.Collections.ArrayList 
  5. 'Routine to attach an observer 
  6. Public Sub AttachObserver(ByVal obj As IObserver) 
  7. observers.Add(obj) 
  8. End Sub 
  9. 'Routine to remove an observer 
  10. Public Sub DetachObserver(ByVal obj As IObserver) 
  11. observers.Remove(obj) 
  12. End Sub 
  13. 'Routine to notify all observers 
  14. Public Sub NotifyObservers() 
  15. Dim o As IObserver 
  16. For Each o In observers 
  17. o.Update() 
  18. Next 
  19. End Sub 
  20. End Class ' END CLASS DEFINITION Ball 

FootBall (ConcreteSubject)

下面是类 FootBall 的实现。

  1. ' ConcreteSubject : The FootBall Class 
  2. Public Class FootBall 
  3. Inherits Ball 
  4. 'State: The position of the ball 
  5. Private myPosition As Position 
  6. 'This function will be called by observers to get current position 
  7. Public Function GetBallPosition() As Position 
  8. Return myPosition 
  9. End Function 
  10. 'Some external client will call this to set the ball's position 
  11. Public Function SetBallPosition(ByVal p As Position) 
  12. myPosition = p 
  13. 'Once the position is updated, we have to notify observers 
  14. NotifyObservers() 
  15. End Function 
  16. 'Remarks: This can also be implemented as a get/set property 
  17. End Class ' END CLASS DEFINITION FootBall 

IObserver (Observer)

下面是类 IObserver的实现,它提供了具体的观察者(Concrete Observers)的接口。

  1. ' Observer: The IObserver Class 
  2. 'This class is an abstract (MustInherit) class 
  3. Public MustInherit Class IObserver 
  4. 'This method is a mustoverride method 
  5. Public MustOverride Sub Update() 
  6. End Class ' END CLASS DEFINITION IObserver 

Player (ConcreteObserver)

下面是类 Player 的实现,它继承自 IObserver:

  1. ' ConcreteObserver: The Player Class 
  2. 'Player inherits from IObserver, and overrides Update method 
  3. Public Class Player 
  4. Inherits IObserver 
  5. 'This variable holds the current state(position) of the ball 
  6. Private ballPosition As Position 
  7. 'A variable to store the name of the player 
  8. Private myName As String 
  9. 'This is a pointer to the ball in the system 
  10. Private ball As FootBall 
  11. 'Update() is called from Notify function, in Ball class 
  12. Public Overrides Sub Update () 
  13. ballPosition = ball.GetBallPosition() 
  14. System.Console.WriteLine("Player {0} say that the ball is at {1},{2},{3} ", _ 
  15. myName, ballPosition.X, ballPosition.Y, ballPosition.Z) 
  16. End Sub 
  17. 'A constructor which allows creating a reference to a ball 
  18. Public Sub New(ByRef b As FootBall, ByVal playerName As String) 
  19. ball = b 
  20. myName = playerName 
  21. End Sub 
  22. End Class ' END CLASS DEFINITION Player 

Referee (ConcreteObserver)

下面是类 Referee 的实现,它也继承自 IObserver

  1. ' ConcreteObserver : The Referee Clas 
  2. Public Class Referee 
  3. Inherits IObserver 
  4. 'This variable holds the current state(position) of the ball 
  5. Private ballPosition As Position 
  6. 'This is a pointer to the ball in the system 
  7. Private ball As FootBall 
  8. 'A variable to store the name of the referee 
  9. Private myName As String 
  10. 'Update() is called from Notify function in Ball class 
  11. Public Overrides Sub Update() 
  12. ballPosition = ball.GetBallPosition() 
  13. System.Console.WriteLine("Referee {0} say that the ball is at {1},{2},{3} ", _ 
  14. myName, ballPosition.X, ballPosition.Y, ballPosition.Z) 
  15. End Sub 
  16. 'A constructor which allows creating a reference to a ball 
  17. Public Sub New(ByRef b As FootBall, ByVal refereeName As String) 
  18. myName = refereeName 
  19. ball = b 
  20. End Sub 
  21. End Class ' END CLASS DEFINITION Referee 

Position

同样的,我们需要一个位置类来表示球的位置

  1. 'Position: This is a data structure to hold the position of the ball 
  2. Public Class Position 
  3. Public X As Integer 
  4. Public Y As Integer 
  5. Public Z As Integer 
  6. 'This is the constructor 
  7. Public Sub New(Optional ByVal x As Integer = 0, _ 
  8. Optional ByVal y As Integer = 0, _ 
  9. Optional ByVal z As Integer = 0) 
  10. Me.X = x 
  11. Me.Y = y 
  12. Me.Z = Z 
  13. End Sub 
  14. End Class ' END CLASS DEFINITION Position 

组装起来

现在我们创建一个球和一些观察者,然后把观察者挂接到球上,这样在球的位置变化的时候就可以自动地通知它们。

 

  1. 'Let us create a ball and few observers 
  2. Public Class GameEngine 
  3. Public Shared Sub Main() 
  4. 'Create our ball (i.e, the ConcreteSubject) 
  5. Dim ball As New FootBall() 
  6. 'Create few players (i.e, ConcreteObservers) 
  7. Dim Owen As New Player(ball, "Owen") 
  8. Dim Ronaldo As New Player(ball, "Ronaldo") 
  9. Dim Rivaldo As New Player(ball, "Rivaldo") 
  10. 'Create few referees (i.e, ConcreteObservers) 
  11. Dim Mike As New Referee(ball, "Mike") 
  12. Dim John As New Referee(ball, "John") 
  13. 'Attach the observers with the ball 
  14. ball.AttachObserver(Owen) 
  15. ball.AttachObserver(Ronaldo) 
  16. ball.AttachObserver(Rivaldo) 
  17. ball.AttachObserver(Mike) 
  18. ball.AttachObserver(John) 
  19. System.Console.WriteLine("After attaching the observers...") 
  20. 'Update the position of the ball. 
  21. 'At this point, all the observers should be notified automatically 
  22. ball.SetBallPosition(New Position()) 
  23. 'Just write a blank line 
  24. System.Console.WriteLine() 
  25. 'Remove some observers 
  26. ball.DetachObserver(Owen) 
  27. ball.DetachObserver(John) 
  28. System.Console.WriteLine("After detaching Owen and John...") 
  29. 'Updating the position of ball again 
  30. 'At this point, all the observers should be notified automatically 
  31. ball.SetBallPosition(New Position(10, 10, 30)) 
  32. 'Press any key to continue.. 
  33. System.Console.Read() 
  34. End Sub 
  35. End Class 

运行

下面是运行程序的输出

 

结论

模式可以分为两类

  • 关于目的
  • 关于范围

其中关于目的又可以分为创建、结构和行为等三种,例如

  • 我们刚才学习的 Observer 模式是一种行为模式(因为它有助于对行为建模和对象间的交互)
  • 创建者模式则是一种创建型模式(因为它封装了如何以特别的方式创建对象)

下图是完整的分类图表

 

我希望这篇文章

  • 可以让你理解设计模式
  • 可以帮助你在项目中应用模式
  • 在你跟朋友谈起模式的时候对你有所帮助

最后,如果你已经跃跃欲试(杰出程序员的特征之一),那么我向你推荐 Art Of Living 专题的第一部分(参考http://www.artofliving.org/courses.html)。这个交互式专题讨论分为 6 天,共 18 小时,希望它能够帮你找到工作与生活的平衡——既可以理清自己的思考,又可以增进生活质量。你可以从这里开始:http://www.artofliving.org/centers/main.htm。 blog原文地址http://blog.csdn.net/lanphaday/archive/2008/09/11/2915518.aspx

 

原文地址:http://www.codeproject.com/KB/cpp/applyingpatterns2.aspx

作者:An 'OOP' Madhusudanan

译者:赖勇浩(http://blog.csdn.net/lanphaday


解决方案架构师:你的进度怎么样?

愚蠢的开发者:是的,我觉得我学会了如何应用 Observer 模式去解决所有问题

解决方案架构师:一个模式解决所有问题?

愚蠢的开发者:啊,还不够么?

介绍

关于本文

本文是这个系列的第二篇文章,在阅读本文之前,你应该已经读过并且理解这个系列的第一篇,题为

  • 学习如何应用设计模式设计你的足球引擎(第一、二部分)

在第一篇文章中,我们讨论了

  • 什么是模式,如何使用它们
  • 如何支持方案应用模式
  • 如何用观察者模式来解决我们的足球引擎的设计问题

本文是前一篇文章的延续,在这里我们将讨论

  • 第三部分:应用 Strategy 模式解决“球队(Team)”和“球队策略(TeamStrategy)”相关的设计问题
  • 第四部分:应用 Decorator 模式来解决“球员(Player)”相关的设计问题

如果你已经不记得这些设计问题,最好翻回第一篇文章去看看再回来继续。


第三部分

应用 Strategy 模式

在这一节我们将深入策略模式,然后我们应用这个模式来解决第二个设计问题。像前一篇文章那样,在这里我们再来回顾一下我们的第二个设计问题。

如何你能够记起,我们的第二个设计问题是:

  • 特定的设计问题:在比赛进行时,终端用户能够改变它的球队的策略(如从进攻改为防守)
  • 问题泛化:需要客户端(在这里就是球队)使用的算法(球队策略)能够独立改变

如前文所言,当比赛进行时,我们需要改变球队的策略(如从进攻改为防守)。确切来说就是需要从球队分享球队的策略。

我们已知可以利用策略模式来解决上述设计问题,因为它可使客户端(例如球队)使用的算法(例如球队策略)能够独立改变;那么我们来看看如何利用 Strategy 模式来解决这个设计问题。

理解 Strategy 模式

Strategy 模式非常简单,下面是 Strategy 模式的 UML 图:

clip_image002

Fig - Strategy Pattern

它包括如何几个部分

  • 策略(Strategy

这是一个算法(策略)的抽象类,所有的具体的算法都派生自它。简单地说,它为所有的具体算法(或具体的策略)提供了一个通用的接口。换言之,如果类 Strategy 有个抽象函数叫 foo(),那么所有具体的策略类都应该重载foo() 函数。

  • 具体的策略(ConcreteStrategy

这个类是我们真正实现算法的地方,或者说,它是类 Strategy 的具体实现。例如 Sort 是实现算法的策略类,而具体的策略可以是 MergeSort 或 QuickSort 等等。

  • 上下文(Context

Context可由一个或多个策略类配置,它通常策略接口访问具体策略对象。

应用 Strategy 模式

现在让我们想想如何用 Strategy 模式来解决问题。

clip_image003

Fig - Solving Our Second Design Problem

类 TeamStrategy 包含 Play 函数,AttackStrategy 和 DefendStrategy 是它的具体实现。Team 包含策略,策略可以根据比赛的形势而改变(例如,在领先了若干个进球后从进攻策略改为防守策略)。当用 Team 的PlayGame 函数时,它会调用当前策略的 Play 函数,这一切马上生效,干净利索。

通过策略模式,就可以从类 Team 中分离算法(例如团队的策略)。

Strategy 模式实现

TeamStrategy (Strategy)

下面是类 TeamStrategy 的代码:

  1. 'Strategy: The TeamStrategy class
  2. 'This class provides an abstract interface
  3. 'to implement concrete strategy algorithms
  4. Public MustInherit Class TeamStrategy
  5. 'AlgorithmInterface : This is the interface provided
  6. Public MustOverride Sub Play ()
  7. End Class ' END CLASS DEFINITION TeamStrategy

AttackStrategy (ConcreteStrategy)

下面是类 AttackStrategy 的代码,它派生自 TeamStrategy:

  1. 'ConcreteStrategy: The AttackStrategy class
  2. 'This class is a concrete implementation of the
  3. 'strategy class.
  4. Public Class AttackStrategy
  5. Inherits TeamStrategy
  6. 'Overrides the Play function.
  7. 'Let us play some attacking game
  8. Public Overrides Sub Play()
  9. 'Algorithm to attack
  10. System.Console.WriteLine(" Playing in attacking mode")
  11. End Sub
  12. End Class ' END CLASS DEFINITION AttackStrategy

DefendStrategy (ConcreteStrategy)

下面是类 DefendStrategy 的代码,它也派生自 TeamStrategy:

  1. 'ConcreteStrategy: The DefendStrategy class
  2. 'This class is a concrete implementation of the
  3. 'strategy class.
  4. Public Class DefendStrategy
  5. Inherits TeamStrategy
  6. 'Overrides the Play function.
  7. 'Let us go defensive
  8. Public Overrides Sub Play()
  9. 'Algorithm to defend
  10. System.Console.WriteLine(" Playing in defensive mode")
  11. End Sub
  12. End Class ' END CLASS DEFINITION DefendStrategy

Team (Context)

下面是类 Team 的代码,根据我们的设计,一个球队在同一时间只能有一种策略:

  1. 'Context: The Team class
  2. 'This class encapsulates the algorithm
  3. Public Class Team
  4. 'Just a variable to keep the name of team
  5. Private teamName As String
  6. 'A reference to the strategy algorithm to use
  7. Private strategy As TeamStrategy
  8. 'ContextInterface to set the strategy
  9. Public Sub SetStrategy(ByVal s As TeamStrategy)
  10. 'Set the strategy
  11. strategy = s
  12. End Sub
  13. 'Function to play
  14. Public Sub PlayGame()
  15. 'Print the team's name
  16. System.Console.WriteLine(teamName)
  17. 'Play according to the strategy
  18. strategy.Play()
  19. End Sub
  20. 'Constructor to create this class, by passing the team's
  21. 'name
  22. Public Sub New(ByVal teamName As String)
  23. 'Set the team name to use later
  24. Me.teamName = teamName
  25. End Sub
  26. End Class ' END CLASS DEFINITION Team

组合起来

创建球队,然后设置它们的策略,并开始比赛。下面的代码颇为简单而且有详细的注释。

  1. 'GameEngine class for demonstration
  2. Public Class GameEngine
  3. Public Shared Sub Main()
  4. 'Let us create a team and set its strategy,
  5. 'and make the teams play the game
  6. 'Create few strategies
  7. Dim attack As New AttackStrategy()
  8. Dim defend As New DefendStrategy()
  9. 'Create our teams
  10. Dim france As New Team("France")
  11. Dim italy As New Team("Italy")
  12. System.Console.WriteLine("Setting the strategies..")
  13. 'Now let us set the strategies
  14. france.SetStrategy(attack)
  15. italy.SetStrategy(defend)
  16. 'Make the teams start the play
  17. france.PlayGame()
  18. italy.PlayGame()
  19. System.Console.WriteLine()
  20. System.Console.WriteLine("Changing the strategies..")
  21. 'Let us change the strategies
  22. france.SetStrategy(defend)
  23. italy.SetStrategy(attack)
  24. 'Make them play again
  25. france.PlayGame()
  26. italy.PlayGame()
  27. 'Wait for a key press
  28. System.Console.Read()
  29. End Sub
  30. End Class

运行

程序运行后的输出如下:

clip_image004


第四部分

应用 Decorator 模式

在这一节我们来讲讲如何应用 Decorator 模式来解决第三个设计问题(如有必要可参考前一篇文章)。这个问题是与球员的运行时职责指派相关的(如前锋、后卫等)。

你可以考虑创建一个球员类,然后基于它派生出类似前锋、中锋和后卫等多个子类。但它并非最好的解决方案,正如我们之前讨论的——一个球员可以在某个时刻是前锋,另一个时刻又变成了中锋。至少,在我们的足球引擎中是这样的,所以这是我们的设计问题。

特定的设计问题:球员拥有额外的职责,如前锋、后卫等,而且可以在运行时切换。

问题泛化:在不使用子类化的情况下,需要能够动态地为对象(在这里是指球员)挂接额外的职责(如前锋、中锋等)。

理解 Decorator 模式

Decorator 模式可以动态地为对象增加职责,是子类化之外的完美之选。下面是 Decorator 模式的 UML 图:

clip_image005

Fig - Decorator Pattern

这个模式由以下部分组成

  • 组件(Component

类 Component 为组件声明了一个抽象接口,我们能够在这些组件上挂接额外的职责。

  • 具体的组件(ConcreteComponent

类 ConcreteComponent 是类 Component 的具体实现,它真正定义了一个能够挂接的额外职责。

  • 装饰者(Decorator

类 Decorator 从类 Component 继承而来,这意味着它继承了组件的所有接口(函数、属性等),而且持有一个从组件类继承下来的对象的引用。其实一个具体的装饰者甚至能够持有其它装饰者的引用(因为类 Decorator 本来就是由类 Component 派生而来)。

  • 具体的装饰者(Concrete Decorator

这个类是真正为组件挂接职责的地方。

应用 Decorator 模式

现在看看如何应用 Decorator 模式来解决与球员相关的设计问题。

clip_image006

Fig - Solving Our Third Design Problem

可以看到从类 Player 继承两个具体的组件——GoalKeeper 和FieldPlayer,另外还有三个具体的装饰者——Forward、MidFielder和Defender。一个球队有 11 个全场球员和一个守门员(译注:作者写错了,应该是 10个全场球员,后文相同的错误不再指出)。我们的设计问题是需要在运行时指派类似前锋、后卫之类的职责给球员。虽然我们只有 11 全场球员,但可能同时有 11 个前锋和 11 个中锋,因为一个球员可以同时既是前锋又是中锋。通过给球员指派多个角色,和交换它们的角色等,使我们能够规划极佳的比赛策略。

例如可以在比赛的某一时刻通过暂时给一个球员指派 Forward 装饰者,使他可以冲刺和射门。

为具体的组件增加额外的职责,首先可以创建一个具体的组件,然后以引用的形式把它指派给装饰者。比如你可以创建一个全场球员,和一个中锋装饰者,将全场球员指派给中锋装饰者可以为它增加中锋的职责。最后,如果你想,你也可以把同一个球员指派给前锋装饰者。在示例代码中的 GameEngine 模块很好地解释了 Decorator 模式。

下面来看看它的实现,代码都加上了很多注释。

Decorator 模式实现

Player (Component)

类 Player 的实现如下:

  1. ' Component: The Player class
  2. Public MustInherit Class Player
  3. 'Just give a name for this player
  4. Private myName As String
  5. 'The property to get/set the name
  6. Public Property Name() As String
  7. Get
  8. Return myName
  9. End Get
  10. Set(ByVal Value As String)
  11. myName = Value
  12. End Set
  13. End Property
  14. 'This is the Operation in the component
  15. 'and this will be overrided by concrete components
  16. Public MustOverride Sub PassBall()
  17. End Class ' END CLASS DEFINITION Player

FieldPlayer (ConcreteComponent)

下面是类 FieldPlayer 的实现:

  1. ' ConcreteComponent : Field Player class
  2. 'This is a concrete component. Later, we will add additional responsibilities
  3. 'like Forward, Defender etc to a field player.
  4. Public Class FieldPlayer
  5. Inherits Player
  6. 'Operation: Overrides PassBall operation
  7. Public Overrides Sub PassBall ()
  8. System.Console.WriteLine(" Fieldplayer ({0}) - passed the ball", _
  9. MyBase.Name)
  10. End Sub
  11. 'A constructor to accept the name of the player
  12. Public Sub New(ByVal playerName As String)
  13. MyBase.Name = playerName
  14. End Sub
  15. End Class ' END CLASS DEFINITION FieldPlayer

GoalKeeper (ConcreteComponent)

下面是类 GoalKeeper 的实现:

  1. ' ConcreteComponent : GaolKeeper class
  2. 'This is a concrete component. Later, we can add additional responsibilities
  3. 'to this class if required.
  4. Public Class GoalKeeper
  5. Inherits Player
  6. 'Operation: Overriding the base class operation
  7. Public Overrides Sub PassBall ()
  8. System.Console.WriteLine(" GoalKeeper ({0}) - passed the ball", MyBase.Name)
  9. End Sub
  10. 'A constructor to accept the name of the player
  11. Public Sub New(ByVal playerName As String)
  12. MyBase.Name = playerName
  13. End Sub
  14. End Class ' END CLASS DEFINITION GoalKeeper

PlayerRole (Decorator)

下面是类 PlayerRole 的实现:

  1. 'Decorator: PlayerRole is the decorator
  2. Public Class PlayerRole
  3. Inherits player
  4. 'The reference to the player
  5. Protected player As player
  6. 'Call the base component's function
  7. Public Overrides Sub PassBall()
  8. player.PassBall()
  9. End Sub
  10. 'This function is used to assign a player to this role
  11. Public Sub AssignPlayer(ByVal p As player)
  12. 'Keep a reference to the player, to whom this
  13. 'role is given
  14. player = p
  15. End Sub
  16. End Class ' END CLASS DEFINITION PlayerRole

Forward (ConcreteDecorator)

下面是类 Forward 的实现:

  1. 'ConcreteDecorator: Forward class is a Concrete implementation
  2. 'of the PlayerRole (Decorator) class
  3. Public Class Forward
  4. Inherits PlayerRole
  5. 'Added Behavior: This is a responsibility exclusively for the Forward
  6. Public Sub ShootGoal()
  7. System.Console.WriteLine(" Forward ({0}) - Shooted the ball to goalpost", _
  8. MyBase.player.Name)
  9. End Sub
  10. End Class ' END CLASS DEFINITION Forward

MidFielder (ConcreteDecorator)

下面是类 MidFielder 的实现:

  1. 'ConcreteDecorator: MidFielder class is a Concrete implementation
  2. 'of the PlayerRole (Decorator) class
  3. Public Class MidFielder
  4. Inherits PlayerRole
  5. 'AddedBehavior: This is a responsibility exclusively for the Midfielder
  6. '(Don't ask me whether only mid filders can dribble the ball - atleast
  7. 'it is so in our engine)
  8. Public Sub Dribble()
  9. System.Console.WriteLine(" Midfielder ({0}) - dribbled the ball", _
  10. MyBase.player.Name)
  11. End Sub
  12. End Class ' END CLASS DEFINITION Midfielder

Defender (ConcreteDecorator)

下面是类 Defender 的实现:

  1. 'ConcreteDecorator: Defender class is a Concrete implementation
  2. 'of the PlayerRole (Decorator) class
  3. Public Class Defender
  4. Inherits PlayerRole
  5. 'Added Behavior: This is a responsibility exclusively for the Defender
  6. Public Sub Defend()
  7. System.Console.WriteLine(" Defender ({0}) - defended the ball", _
  8. MyBase.player.Name)
  9. End Sub
  10. End Class ' END CLASS DEFINITION Defender

组合起来

  1. 'Let us put it together
  2. Public Class GameEngine
  3. Public Shared Sub Main()
  4. '-- Step 1:
  5. 'Create few players (concrete components)
  6. 'Create few field Players
  7. Dim owen As New FieldPlayer("Owen")
  8. Dim beck As New FieldPlayer("Beckham")
  9. 'Create a goal keeper
  10. Dim khan As New GoalKeeper("Khan")
  11. '-- Step 2:
  12. 'Just make them pass the ball
  13. '(during a warm up session ;))
  14. System.Console.WriteLine()
  15. System.Console.WriteLine(" > Warm up Session... ")
  16. owen.PassBall()
  17. beck.PassBall()
  18. khan.PassBall()
  19. '-- Step 3: Create and assign the responsibilities
  20. '(when the match starts)
  21. System.Console.WriteLine()
  22. System.Console.WriteLine(" > Match is starting.. ")
  23. 'Set owen as our first forward
  24. Dim forward1 As New Forward()
  25. forward1.AssignPlayer(owen)
  26. 'Set Beckham as our midfielder
  27. Dim midfielder1 As New MidFielder()
  28. midfielder1.AssignPlayer(beck)
  29. 'Now, use these players to do actions
  30. 'specific to their roles
  31. 'Owen can pass the ball
  32. forward1.PassBall()
  33. 'And owen can shoot as well
  34. forward1.ShootGoal()
  35. 'Beckham can pass ball
  36. midfielder1.PassBall()
  37. 'Beckham can dribble too
  38. midfielder1.Dribble()
  39. ' [ Arrange the above operations to some meaningfull sequence, like
  40. ' "Beckham dribbled and passed the ball to owen and owen shooted the
  41. ' goal ;) - just for some fun ]"
  42. '-- Step 4: Now, changing responsibilities
  43. '(during a substitution)
  44. 'Assume that owen got injured, and we need a new player
  45. 'to play as our forward1
  46. System.Console.WriteLine()
  47. System.Console.WriteLine(" > OOps, Owen got injured. " & _
  48. "Jerrard replaced Owen.. ")
  49. 'Create a new player
  50. Dim jerrard As New FieldPlayer("Jerrard")
  51. 'Ask Jerrard to play in position of owen
  52. forward1.AssignPlayer(jerrard)
  53. forward1.ShootGoal()
  54. '-- Step 5: Adding multiple responsibilities
  55. '(When a player need to handle multiple roles)
  56. 'We already have Beckham as our midfielder.
  57. 'Let us ask him to play as an additional forward
  58. Dim onemoreForward As New Forward()
  59. onemoreForward.AssignPlayer(beck)
  60. System.Console.WriteLine()
  61. System.Console.WriteLine(" > Beckham has multiple responsibilities.. ")
  62. 'Now Beckham can shoot
  63. onemoreForward.ShootGoal()
  64. 'And use his earlier responsibility to dribble too
  65. midfielder1.Dribble()
  66. 'According to our design, you can attach the responsibility of
  67. 'a forward to a goal keeper too, but when you actually
  68. 'play football, remember that it is dangerous ;)
  69. 'Wait for key press
  70. System.Console.Read()
  71. End Sub
  72. End Class

运行

下面是运行程序的输出

clip_image007

结论

在本文中我们讨论了

  • 模式及其实现
  • 模式及其实现

暂时就这么多了。事实上,正是 code project 社区对我前一篇文章的支持才鼓气我的勇气来发表这一篇。谢谢所有人的支持和鼓励!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值