Design Patterns in ActionScript–Decorator

When we build some flash applications, we spend most of the time on two things. One is image, the other is text. Eh, I don’t want to talk about the image. Because in most cases, I don’t need to deal with it, it belongs to others. So, I want to talk something about the text.

Sometimes, we want to capitalize all the text, while sometimes we want all of them be lowercased. Sometimes, we want the text color is red, while sometimes is blue. We need to care the case, the color, and more over, the font, or something else.


 

I’m tried of this thing. I don’t know whether you guys have any idea to solve this trouble. If you have any, please tell to me via the email.

Now, I’ll introduce my solution. Firstly, we need a basic class to implement the basic function.

The basic class’s code is as follows.

  1. class OutputText {
  2. public   function write ( s : String ) : void {
  3. trace ( s ) ;
  4. }
  5. }

After we finish the basic class, let’s consider the other classes. We need to deal with the format in the other classes. So, we can write some classes to wrapper the basic operation. The concrete format class will inherited from the basic class, and override the basic operation. The override function will do some other thing to format the text.

Here is a concrete format class code.

  1. class OutputAppendText extends OutputText {
  2.  
  3. private   var output : OutputText ;
  4.  
  5. public   function OutputAppendText ( output : OutputText ){
  6. this . output = output ;
  7. }
  8.  
  9. public   override function write ( s : String ) : void {
  10. s += " {APPEND TEXT} " ;
  11. output . write ( s ) ;
  12. }
  13. }

Here is another concrete format class code.

  1. class OutputLowercaseText extends OutputText {
  2.  
  3. private   var output : OutputText ;
  4.  
  5. public   function OutputLowercaseText ( output : OutputText ){
  6. this . output = output ;
  7. }
  8.  
  9. public   override function write ( s : String ) : void {
  10. output . write ( s . toLowerCase ()) ;
  11. }
  12. }

As you see, two concrete classes both inherited from the basic class. So, you can use one concrete class to wrap another concrete class without considering the sequence.

Here is the test code.

  1. var output : OutputAppendText = new OutputAppendText (
  2. new   OutputLowercaseText (
  3. new   OutputText ())) ;
  4. output . write ( " skfjaslf " ) ;

If you want to add some other format, you just need to write some other class just like the classes mentioned.

And the UML diagram is as follows.

clip_image002

In this class hierarchy, there is only one basic class, which defines the basic operations. All the other classes will be inherited from the basic class, and override the basic operations. Every sub class will do something in the override function to implement the concrete operation.

This pattern is called decorator. The GoF’s definition is as follows.

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

–By GOF BOOK

This pattern is very useful when you deal with the input/output or formatting the text. The java I/O system is a famous example of this pattern.

Search-256x256Demo | DownloadDownload Full Project

Enjoy!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值