`
shake863
  • 浏览: 661578 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Design Patterns in Action Script-Bridge

 
阅读更多

I think many of you are the fans of Diablo. I’m one of you :) In Diablo series, you can choose different characters to begin your Diablo journey. As a programmer, maybe you’ll write a base class, named Character. Suppose there are only two characters, named FatCharacter and ThinCharacter.

And we will define some common operations in the base class Character, define special skills in the derived classes.

Search-256x256 You can see the sample source code first. Download Download Full Project

 

clip_image001

Now, you can use the characters to walk around. However,, it’s dangerous to walk around in Diablo, because the monsters are everywhere, so the characters need to protect themselves. The FatCharacter can push the monsters, and the ThinCharacter can escape from the monsters’ attack. Maybe it’s not enough; we’d better give the characters some weapons, like sword or blade.

Now, our implementation is not suitable, so we need to change. A solution is, add more classes, such as FatCharacterWithSword, FatCharacterWithBlade and so on, and let these classes inherited from the FatCharacter and the ThinCharacter.

The class hierarchy will be as follows.

clip_image003

Now, the characters can use the weapon to beat the monsters.

Do you like this implementation? In this class hierarchy, you can see some classes are exactly the same, especially in level 3. If you want to add some other weapons, you may need to write the classes twice, one for the FatCharacter, and another for ThinCharacter. If you want to add a normal stature character, it would be worse and worse. Class explosion!

Watch the implementation carefully. There are two varying points, one is the character’s stature, and the other is the weapon. These two should be varying independently, not together. So, we need a new pattern to solve this, it is bridge pattern.

Decouple an abstraction from its implementation so that the two can vary independently.

– By THE GOF BOOK

In order to let the two vary independently, we should use composite instead of inheritance. Let the weapon be a part of the character, and the sword and the blade implement the weapon interface. Now the class hierarchy will be as follows.

clip_image004

Now, if you want to add a normal stature character, you just need to write a new class, not two or more, also, if you want to add an axe, you just need to write a new class and implement the weapon interface. You character can use the new weapon to attack the monsters.

In general, composite brings more flexibility than inheritance. Another important thing is separate the varying point, let them vary independently.

For more information you can take a look at the GoF book!

Enjoy!

分享到:
评论

相关推荐

    Design Patterns in Modern C++--2018

    Implement structural patterns such as adapter, bridge, decorator, facade and more Work with the behavioral patterns such as chain of responsibility, command, iterator, mediator and more Apply ...

    Design-Patterns-in-Modern-C++

    The topic of Design Patterns sounds dry, academically constipated and, in all honesty, done to death in almost every programming language imaginable—including programming languages such as JavaScript...

    Design Patterns in Modern C++-Apress(2018)

    Design Patterns are also a fun investigation of how a problem can be solved in many different ways, with varying degrees of technical sophistication and different sorts of trade-offs. Some patterns ...

    Laracasts - design-patterns-in-php

    "Laracasts - design-patterns-in-php.torrent"则可能是一个BT种子文件,用于通过BitTorrent协议下载整个课程的大型数据包,这通常包括所有视频讲座和其他相关文件。 在课程"设计模式在PHP中"中,你可能会学到以下...

    M. S. Joshi - C++ Design Patterns and Derivatives Pricing - CUP[2008]2Ed.pdf

    Joshi - C++ Design Patterns and Derivatives Pricing - CUP[2008]2Ed.pdf》,是关于C++设计模式和衍生品定价的第二版教科书。本书着重讨论在C++中实现金融模型的背景下,设计模式这一面向对象编程的先进范式。...

    GOF-设计模式-Design Patterns-英文原版-高清-有目录-有页码

    **设计模式**(Design Patterns)是软件工程中的一个重要概念,它是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。本章节将从多个角度对设计模式进行深入探讨。 #### 二、设计模式的基本定义 ...

    Design Patterns in C#

    design pattern in C# language

    design-patterns-for-humans设计模式中文翻译版

    https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码

    Design-Patterns-In-Kotlin,在kotlin中实现的设计模式.zip

    《Kotlin设计模式实战解析》 在编程领域,设计模式是一种通用、可重用的...开源项目"Design-Patterns-In-Kotlin"则是一个很好的学习资源,它提供了各种设计模式在Kotlin中的具体实现,有助于开发者深入学习和实践。

    Design Patterns in Modern C++

    Design Patterns in Modern C++: Reusable Approaches for Object-Oriented Software Design English | PDF| 2018 | 312 Pages | ISBN : 1484236025Design Patterns in Modern C++: Reusable Approaches for Object...

    Design Patterns in PHP and Laravel

    Too often design patterns are explained using tricky concepts, when in fact they are easy to use and can enrich your everyday development. Design Patterns in ...

    《Java Design Patterns》高清完整PDF版

    Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....

Global site tag (gtag.js) - Google Analytics