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

Design Patterns in Action Script-State

阅读更多

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.

  1. if ( currentState == play )
  2. currentState = pause ;
  3. else   if ( currentState == pause )
  4. currentState = stop ;
  5. else   if ( currentState == stop )
  6. 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.

clip_image001

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.

  1. public interface State
  2. {
  3. function   press ( player : Player ) : String
  4. }
  5.  
  6. public   class PlayState implements State
  7. {
  8. public   function press ( player : Player ) : String
  9. {
  10. player . setState ( new   PauseState ()) ;
  11. return Playing :)”;
  12. }
  13. }

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 Download Full Project

Much for today, Enjoy!

分享到:
评论

相关推荐

    PHP in Action.pdf

    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

    ### 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

    ### 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 ...

    Addison.Wesley.Real.Time.UML.Advances.in.The.UML.for.Real.Time.Systems.Third.Edition.eBook-DDU.chm

    "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

    C语言设计模式 PDF《C Design Pattern》

    State state_a = {handle_state_a}; State state_b = {handle_state_b}; ``` **2.6 命令模式** 命令模式将一个请求封装为一个对象,从而使用户可以用不同的请求来参数化对象。在C中,可以通过定义命令结构体和执行...

    Wrox.Professional.ASP.NET.Design.Patterns.Sep.2010.rar

    《Wrox Professional ASP.NET Design Patterns Sep.2010》这本书是ASP.NET开发领域的一本经典之作,专注于探讨设计模式在ASP.NET中的应用。设计模式是软件工程中经过时间验证的解决方案,它可以帮助开发者在遇到常见...

    Game Analytics 无水印pdf

    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 ...

    Wrox.Professional.WCF.4

    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 ...

    外文翻译 stus MVC

    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 ...

    Effective C#

    - **Strategy:** Use patterns like the builder pattern to avoid duplication. - **Sample:** ```csharp public class PersonBuilder { private string _name; private int _age; public PersonBuilder ...

    SAP PO/PI教程 Process Orchestration The Comprehensive Guide

    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 ...

    25种设计模式样例.zip

    设计模式是软件工程中的一种最佳实践,用于解决...例如,WCF服务中的服务代理就是代理模式的应用,ASP.NET MVC中的Controller和Action则是策略模式的体现。对于.NET开发者来说,掌握这些设计模式是非常重要的技能提升。

Global site tag (gtag.js) - Google Analytics