- 浏览: 661701 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
sztime:
可以在文本框上绑定事件来禁用回车键, 我就是这样做的.在IE中 ...
form 回车自动提交问题 -
damoqiongqiu:
非常好的文章,很透彻不过有一句话小僧腆着脸补充一下:“1111 ...
为什么要用补码来做存储 -
wuyizhong:
原来如此啊。
form 回车自动提交问题 -
luliangy:
谢楼主~!
用C语言扩展Python的功能 -
kwong:
很有用,谢谢
火狐和IE 对css 样式解释的差异
Our company is building a new project recently. This project is about simulated flight. The total solution will include the software module and hardware module. Unfortunately, our company just a software company, we don’t have the ability to design the hardware. So, our company wants to buy the hardware solution from other company, we only need to work out the software.
The software part looks like just a piece of cake to us. So, we quickly build a team to handle this task.
After many and many overtime, the team has finished the software module. And all the operation with hardware is reserved as an interface.
At the same time, our boss has met some hardware suppliers and decided to make a deal with one of them.
Sooner after the deal, we receive the hardware driver library. Things not always go that easy. This time, we found out that the hardware driver library is not fit for the interfaces reserved by our software.
This is a big problem. If we can’t fix it, we’ll only have two ways to go, one is rewriting our code to fit for the hardware driver library; the other is buying a new hardware solution. Eh, maybe we have the third choice, that’s we quit our jobs.
We were very depressed by this situation. But life still goes on. We check the driver library carefully, and we found that many functions can provide the function we need, but it has a different name. We can’t change the driver library. Does it mean that we need to change our code?
Obviously, we don’t want to do that, it’s not an easy job, further more, we’re all lazy guys. So, we need a tricky solution.
Genius programmers, what will you do if you’re in this situation?
Finally, a genius guy in our team gives a suggestion. This solution is much like building a bridge from the interfaces reserved in the software module and the functions provided by the driver library.
The solution is that, we build a new class to wrap driver library, and the new class will derived from the interfaces reserved by the software.
The original code is showing below.
- public class Rocket {
- private var rocketLib : RocketLib ;
- public function Rocket (){
- rocketLib = new RocketLib () ;
- }
- public function run () : void {
- rocketLib . run () ;
- }
- }
- public class RocketDriver {
- public function run () : void {
- trace ( " The Rocket Runs! " ) ;
- }
- }
As you see, the Rocket class is in our software module, and the RocketDriver class is in the library they provided. And the solution is as follows.
- public class RocketLib {
- private var rocketDriver : RocketDriver ;
- public function RocketLib (){
- rocketDriver = new RocketDriver () ;
- }
- public function run () : void {
- rocketDriver . run () ;
- }
- }
Now, the software module doesn’t need to be changed. And everything goes well.
The UML diagram is as below.
Do you like this solution? Of course, this is a pattern named adapter pattern. The intent of this pattern is as follows.
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
–By GOF BOOK
Hope you have a sensitive feeling about this pattern now.
Enjoy!
发表评论
-
设计模式 图析
2011-09-24 14:27 712【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 504Ok, 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 952Yesterday, when I was on my way ... -
Design Patterns in ActionScript-Builder
2009-02-23 14:16 518Have you ever buy a computer on ... -
Design Patterns in ActionScript-Prototype
2009-02-23 14:12 663When 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 591In GUI programming, event-drive ... -
Design Patterns in ActionScript-Singleton
2009-02-23 13:55 523In our real world, many things ...
相关推荐
《ActionScript设计模式》是软件开发领域中针对ActionScript编程语言的一种实践指南,它深入探讨了如何在ActionScript项目中应用经典的设计模式。设计模式是软件工程中的宝贵经验总结,它们是解决常见问题的可复用...
design pattern in C# language
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....
Alan Ezust在其著作《An Introduction to Design Patterns in C++ with Qt™, 2nd Edition》中探讨了设计模式的这一概念,并且在Qt框架的基础上讲解了如何在C++项目中应用这些设计模式。 C++是一种高性能、多范式的...
App Architecture: iOS Application Design Patterns in Swift 包含Source code 有钱请支持正版 没钱请默默学习 原书地址: https://www.objc.io/books/app-architecture 中文原书地址: ...
Data Structures And Algorithms With Object-oriented Design Patterns In Java.chm
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 ...
and applications that run on various software and hardware platforms with little or no change in the underlying codebase, while still being a native application with native capabilities and speed....
Design Patterns in Java(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Design Patterns in Modern C++ Reusable Approaches for Object-Oriented Software Design 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
Pro Design Patterns in Swift 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...
本书《Design Patterns by Tutorials Learning design patterns in Swift 4.2, 2nd Edition》主要介绍在Swift 4.2环境下,如何通过教程学习设计模式。设计模式是软件开发中的一个重要概念,它是一套被反复使用、多数...