- 浏览: 661578 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
sztime:
可以在文本框上绑定事件来禁用回车键, 我就是这样做的.在IE中 ...
form 回车自动提交问题 -
damoqiongqiu:
非常好的文章,很透彻不过有一句话小僧腆着脸补充一下:“1111 ...
为什么要用补码来做存储 -
wuyizhong:
原来如此啊。
form 回车自动提交问题 -
luliangy:
谢楼主~!
用C语言扩展Python的功能 -
kwong:
很有用,谢谢
火狐和IE 对css 样式解释的差异
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.
You can see the sample source code first. Download Full Project
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.
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.
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!
发表评论
-
设计模式 图析
2011-09-24 14:27 711【observer】 【adapter】 【comm ... -
一个设计模式的图片
2009-05-03 02:14 533... -
Design Patterns in ActionScript–Factory Method
2009-02-24 13:37 764In our last topic, we talk abou ... -
Design Patterns in ActionScript-Strategy
2009-02-24 13:34 568Today, we’re going to talk abou ... -
Design Patterns in ActionScript-Mediator
2009-02-24 13:29 503Ok, the last pattern now. Let’s ... -
Design Patterns in ActionScript-Command
2009-02-24 13:21 616A few months ago, I was an inte ... -
Design Patterns in ActionScript-Chain of Responsib
2009-02-24 13:12 596When you need some help in a ho ... -
Design Patterns in ActionScript-Flyweight
2009-02-24 13:07 633In Action Script 3.0 we have th ... -
Design Patterns in ActionScript-Visitor
2009-02-24 11:26 509It’s our winter holiday now, an ... -
Design Patterns in ActionScript-Memento
2009-02-23 23:38 513Now, I’m using Microsoft word t ... -
Design Patterns in Action Script-Composite
2009-02-23 14:29 511Still remember the Interpreter ... -
Design Patterns in ActionScript-Interpreter
2009-02-23 14:26 660In web programming, we often us ... -
Design Patterns in ActionScript-Proxy
2009-02-23 14:19 625Have you ever use HTTP-proxy or ... -
Design Patterns in Action Script-State
2009-02-23 14:17 951Yesterday, when I was on my way ... -
Design Patterns in ActionScript-Builder
2009-02-23 14:16 517Have you ever buy a computer on ... -
Design Patterns in ActionScript-Prototype
2009-02-23 14:12 662When I want to write the Protot ... -
Design Patterns in ActionScript-Iterator
2009-02-23 14:07 570There is a famous saying in com ... -
Design Patterns in Action Script-Template Method
2009-02-23 14:04 495Do you like playing cards? If y ... -
Design Patterns in ActionScript-Observer
2009-02-23 14:00 590In GUI programming, event-drive ... -
Design Patterns in ActionScript-Singleton
2009-02-23 13:55 523In our real world, many things ...
相关推荐
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 ...
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 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.torrent"则可能是一个BT种子文件,用于通过BitTorrent协议下载整个课程的大型数据包,这通常包括所有视频讲座和其他相关文件。 在课程"设计模式在PHP中"中,你可能会学到以下...
Joshi - C++ Design Patterns and Derivatives Pricing - CUP[2008]2Ed.pdf》,是关于C++设计模式和衍生品定价的第二版教科书。本书着重讨论在C++中实现金融模型的背景下,设计模式这一面向对象编程的先进范式。...
**设计模式**(Design Patterns)是软件工程中的一个重要概念,它是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。本章节将从多个角度对设计模式进行深入探讨。 #### 二、设计模式的基本定义 ...
design pattern in C# language
https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码
《Kotlin设计模式实战解析》 在编程领域,设计模式是一种通用、可重用的...开源项目"Design-Patterns-In-Kotlin"则是一个很好的学习资源,它提供了各种设计模式在Kotlin中的具体实现,有助于开发者深入学习和实践。
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...
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 ...
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....