- 浏览: 661456 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
sztime:
可以在文本框上绑定事件来禁用回车键, 我就是这样做的.在IE中 ...
form 回车自动提交问题 -
damoqiongqiu:
非常好的文章,很透彻不过有一句话小僧腆着脸补充一下:“1111 ...
为什么要用补码来做存储 -
wuyizhong:
原来如此啊。
form 回车自动提交问题 -
luliangy:
谢楼主~!
用C语言扩展Python的功能 -
kwong:
很有用,谢谢
火狐和IE 对css 样式解释的差异
Do you like playing cards? If your had ever played, you may noticed that everyone has their own way arranging the cards. And in most cases, people will put the cards in order, maybe from the biggest one to the smallest one. Eh, this is a way of sorting.
We can write down the following code to mimic this action.
- public function sort ( array : Array ) : void
- {
- for ( var i : int = 0 ; i < array . length ; i ++ )
- {
- for ( var j : int = 0 ; j < array . length - i ; j ++ )
- if ( ( int )( array [ j ]) < ( int )( array [ j + 1 ]) )
- swap ( array , j , j + 1 ) ;
- }
- }
Here, I use the bubble sort, not insertion sort. Because I think the bubble sort is easy to understand. Using this sort will make you cards arrange from the biggest to the smallest, and the sequence is from left to right. Eh, maybe you don’t like to this sequence, you’d like to hold the cards from the smallest to the biggest, and also it’s from left to right.
Of course, you can change the code with your sorting logic. But, you’ll find that you just need to change the compare logic. Maybe, we can extract the compare logic to another function. And the code will be as follows.
- for ( var i : int = 0 ; i < array . length ; i ++ )
- {
- for ( var j : int = 0 ; j < array . length - i ; j ++ )
- if ( compare ( array , j , j + 1 ) )
- swap ( array , j , j + 1 ) ;
- }
Now, we can rewrite the compare function to get a different result. Let’s go further, we don’t need to implement the compare method now, we can delay it to the subclass. Then, each subclass can get its own way of sorting by override the compare function.
And the class diagram will be.
Aha, a new pattern! And it’s called template method.
The intent is here.
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
– By THE GOF BOOK
As you see the skeleton of our sorting algorithm is defined in the template class, and the compare function is use for the subclass to redefine.
This pattern is very useful especially when your high level design is stable, but the detail needs to change frequency.
Enjoy!
发表评论
-
设计模式 图析
2011-09-24 14:27 711【observer】 【adapter】 【comm ... -
一个设计模式的图片
2009-05-03 02:14 532... -
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 567Today, 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 595When you need some help in a ho ... -
Design Patterns in ActionScript-Flyweight
2009-02-24 13:07 631In Action Script 3.0 we have th ... -
Design Patterns in ActionScript-Visitor
2009-02-24 11:26 508It’s our winter holiday now, an ... -
Design Patterns in ActionScript-Memento
2009-02-23 23:38 512Now, I’m using Microsoft word t ... -
Design Patterns in Action Script-Composite
2009-02-23 14:29 510Still remember the Interpreter ... -
Design Patterns in ActionScript-Interpreter
2009-02-23 14:26 659In web programming, we often us ... -
Design Patterns in ActionScript-Proxy
2009-02-23 14:19 624Have you ever use HTTP-proxy or ... -
Design Patterns in Action Script-State
2009-02-23 14:17 950Yesterday, when I was on my way ... -
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 661When I want to write the Protot ... -
Design Patterns in ActionScript-Iterator
2009-02-23 14:07 569There is a famous saying in com ... -
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 522In our real world, many things ... -
Design Patterns in Action Script-Bridge
2009-02-23 13:44 552I think many of you are the fan ...
相关推荐
Apply design patterns to modern C++ programming Use creational patterns of builder, factories, prototype and singleton Implement structural patterns such as adapter, bridge, decorator, facade and ...
The topic of Design Patterns sounds dry, academically constipated and, in all honesty, done to death in almost every programming language imaginable—including programming languages such as JavaScript...
Design Patterns are also a fun investigation of how a problem can be solved in many different ways, with varying degrees of technical sophistication and different sorts of trade-offs. Some patterns ...
"Laracasts - design-patterns-in-php.torrent"则可能是一个BT种子文件,用于通过BitTorrent协议下载整个课程的大型数据包,这通常包括所有视频讲座和其他相关文件。 在课程"设计模式在PHP中"中,你可能会学到以下...
**设计模式**(Design Patterns)是软件工程中的一个重要概念,它是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。本章节将从多个角度对设计模式进行深入探讨。 #### 二、设计模式的基本定义 ...
Joshi - C++ Design Patterns and Derivatives Pricing - CUP[2008]2Ed.pdf》,是关于C++设计模式和衍生品定价的第二版教科书。本书着重讨论在C++中实现金融模型的背景下,设计模式这一面向对象编程的先进范式。...
design pattern in C# language
https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码
《Kotlin设计模式实战解析》 在编程领域,设计模式是一种通用、可重用的...开源项目"Design-Patterns-In-Kotlin"则是一个很好的学习资源,它提供了各种设计模式在Kotlin中的具体实现,有助于开发者深入学习和实践。
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 ...
这个压缩包“java-design-patterns-master”显然是一个专注于Java设计模式的学习资源,旨在帮助开发者深入理解和应用这些模式。下面我们将详细探讨Java设计模式及其在实际开发中的应用。 1. **单例模式(Singleton...