- 浏览: 661671 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
sztime:
可以在文本框上绑定事件来禁用回车键, 我就是这样做的.在IE中 ...
form 回车自动提交问题 -
damoqiongqiu:
非常好的文章,很透彻不过有一句话小僧腆着脸补充一下:“1111 ...
为什么要用补码来做存储 -
wuyizhong:
原来如此啊。
form 回车自动提交问题 -
luliangy:
谢楼主~!
用C语言扩展Python的功能 -
kwong:
很有用,谢谢
火狐和IE 对css 样式解释的差异
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.
- class OutputText {
- public function write ( s : String ) : void {
- trace ( s ) ;
- }
- }
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.
- class OutputAppendText extends OutputText {
- private var output : OutputText ;
- public function OutputAppendText ( output : OutputText ){
- this . output = output ;
- }
- public override function write ( s : String ) : void {
- s += " {APPEND TEXT} " ;
- output . write ( s ) ;
- }
- }
Here is another concrete format class code.
- class OutputLowercaseText extends OutputText {
- private var output : OutputText ;
- public function OutputLowercaseText ( output : OutputText ){
- this . output = output ;
- }
- public override function write ( s : String ) : void {
- output . write ( s . toLowerCase ()) ;
- }
- }
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.
- var output : OutputAppendText = new OutputAppendText (
- new OutputLowercaseText (
- new OutputText ())) ;
- 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.
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.
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....
App Architecture: iOS Application Design Patterns in Swift 包含Source code 有钱请支持正版 没钱请默默学习 原书地址: https://www.objc.io/books/app-architecture 中文原书地址: ...
Alan Ezust在其著作《An Introduction to Design Patterns in C++ with Qt™, 2nd Edition》中探讨了设计模式的这一概念,并且在Qt框架的基础上讲解了如何在C++项目中应用这些设计模式。 C++是一种高性能、多范式的...
Data Structures And Algorithms With Object-oriented Design Patterns In Java.chm
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 Modern C++ Reusable Approaches for Object-Oriented Software Design 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
Design Patterns in Java(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
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 ...
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环境下,如何通过教程学习设计模式。设计模式是软件开发中的一个重要概念,它是一套被反复使用、多数...