- 浏览: 661590 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
sztime:
可以在文本框上绑定事件来禁用回车键, 我就是这样做的.在IE中 ...
form 回车自动提交问题 -
damoqiongqiu:
非常好的文章,很透彻不过有一句话小僧腆着脸补充一下:“1111 ...
为什么要用补码来做存储 -
wuyizhong:
原来如此啊。
form 回车自动提交问题 -
luliangy:
谢楼主~!
用C语言扩展Python的功能 -
kwong:
很有用,谢谢
火狐和IE 对css 样式解释的差异
Yesterday, when I was on my way home, I suddenly met an old friend. I haven’t met him since I began to write these articles. We stop at a little cafe, and began to talk.
“I’ve just changed my job”, he said, “and now I join the Orange”.
“WOW, Orange, you mean the biggest MP3 manufacturer company,” I answered.
“Yeah, and I join the new product team, we want to surprise the whole world by our new product.” He said proudly.
……. (The rest of our conversation is very boring, so, let’s stop here)
When I back home, I can’t help to imagine the new product, which may surprise the whole world. Actually, I don’t think this product can give a surprise to me. From my old friend’s description, I don’t think this product can defeat another fruit company’s product.
“This product will have nothing but just one button”, it was the description of the new product. It sounds Orange goes further more than other fruit company. Just one button, it confuses me. How to control it just by one button? I don’t have the answer. But it remains me something that I don’t want to tell you now. Let’s write some code to mimic this product now.
The code maybe look like this, we can give some rules to this product. When you press the button the first time, It plays music, and it will be paused by your second touch, the third touch will cause it to stop, and then, the first. So, we can get the code like below.
- if ( currentState == play )
- currentState = pause ;
- else if ( currentState == pause )
- currentState = stop ;
- else if ( currentState == stop )
- currentState = play ;
Let’s check our code carefully, eh, it works well. But, how can I start this machine or stop it? Aha, we have a new mp3 player now, but we can’t even start it. I admire that it surprises me.
Eh, maybe for this kind of product, you can start the product just by press the button down with a fixed interval, and stop it in the same way. So, we can add these two rules to our code.
Another more question, how to add these two rules to our code, just by add another two more if-else? Of course, it makes the code looks ugly. Genius programmers, what will you do to refactor the code?
Aha, this is what I want to talk today. Firstly, let me show you a new pattern, named State. The intent is as follows.
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
–By GOF BOOK
Now, let’s begin.
Firstly, we need a super class to define some basic operation. Then, let all the other states inherit from the super class.
And the class diagram is as follows.
Does it look more clearly? If you want to add other rules, you just need to add the concrete state class; you don’t have to maintain so much if-else statements.
Let’s take a look at the super class, and a concrete state class.
- public interface State
- {
- function press ( player : Player ) : String
- }
- public class PlayState implements State
- {
- public function press ( player : Player ) : String
- {
- player . setState ( new PauseState ()) ;
- return “ Playing :)”;
- }
- }
With this pattern, we leave the state-related operation to the concrete state class. And every concrete state class corresponds to a concrete state.
This pattern is very helpful when you modeling the objects that
have state-related operations. You don’t need to write so much if-else
statements, and maintain the variable to record the current state. In
fact, if-else block maybe causes some trouble, especially when the
block becomes larger. So, you’d better use this pattern to construct
you code.
Download Full Project
Much for today, 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 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 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 ... -
Design Patterns in Action Script-Bridge
2009-02-23 13:44 554I think many of you are the fan ...
相关推荐
It introduces state-of-the art objectoriented design principles, patterns, and techniques. Then it weds these to two different partners. The first partner is PHP, the programming language. The second...
### Ajax in Action: Key Insights and Concepts "Ajax in Action" is an authoritative guide for programmers with some experience in web development. The book is structured into four parts, each ...
### ASP.NET MVC in Action: Key Insights and Knowledge Points #### Overview The book "ASP.NET MVC in Action," authored by Jeffrey Palermo, Ben Scheirman, and Jimmy Bogard, provides an in-depth ...
"I found Real Time UML, Third Edition, to provide an informative and practical application of UML 2.0 to the development of real-time systems, and well worth the read by ...Mechanistic design patterns
State state_a = {handle_state_a}; State state_b = {handle_state_b}; ``` **2.6 命令模式** 命令模式将一个请求封装为一个对象,从而使用户可以用不同的请求来参数化对象。在C中,可以通过定义命令结构体和执行...
《Wrox Professional ASP.NET Design Patterns Sep.2010》这本书是ASP.NET开发领域的一本经典之作,专注于探讨设计模式在ASP.NET中的应用。设计模式是软件工程中经过时间验证的解决方案,它可以帮助开发者在遇到常见...
the process of discovering and communicating patterns in data towards evaluating and driving action, improving performance and solving problems in game development and game research. Written by over ...
This guide enables developers to create state-of-the-art applications using this technology. Written by a team of Microsoft MVPs and WCF experts, this book explains how the pieces of WCF 4.0 build ...
Adapter lets classes work together that couldn_t otherwise because of incompatibility interface" (from Design Patterns - Elements of Reusable OO Software by Gof). The client in this instance is the ...
- **Strategy:** Use patterns like the builder pattern to avoid duplication. - **Sample:** ```csharp public class PersonBuilder { private string _name; private int _age; public PersonBuilder ...
14.2.1 Outside-In Approach 14.2.2 Inside-Out Approach 14.3 Technical Implementation 14.3.1 Development Environment 14.3.2 Developing a Server Java Proxy 14.3.3 Developing a Client Java Proxy ...
设计模式是软件工程中的一种最佳实践,用于解决...例如,WCF服务中的服务代理就是代理模式的应用,ASP.NET MVC中的Controller和Action则是策略模式的体现。对于.NET开发者来说,掌握这些设计模式是非常重要的技能提升。