- 浏览: 664699 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
sztime:
可以在文本框上绑定事件来禁用回车键, 我就是这样做的.在IE中 ...
form 回车自动提交问题 -
damoqiongqiu:
非常好的文章,很透彻不过有一句话小僧腆着脸补充一下:“1111 ...
为什么要用补码来做存储 -
wuyizhong:
原来如此啊。
form 回车自动提交问题 -
luliangy:
谢楼主~!
用C语言扩展Python的功能 -
kwong:
很有用,谢谢
火狐和IE 对css 样式解释的差异
In our last topic, we talk about the strategy pattern . And it helps us to encapsulate the change of algorithm. Today, we go on talking about the birds. We will consider how to generate the birds, eh, I mean the classes.
Now, we have the classes of eagle and penguin. We can use them anywhere we want. Further more, we can write down the following code to decide which class should be initialized.
- public class birdFactory {
- public function birdFactory (){
- }
- public function getBird ( type : String ) : bird {
- if ( type == " eagle " )
- return new eagle () ;
- else
- return new penguin () ;
- }
- }
Now, we just need to look at the type, and we can decide which kind of birds to be initialized. This is a pattern called simple factory , it seems that everything goes all right.
I don’t want to talk too much about this pattern, because it is not our topic today. And I want to introduce you a principle of OOD, called Open-Close-Principle, which means we should open for extension, and closed for modification.
Let’s take a sight at the UML diagram of the above code. Then discussing where the problem is.
Eh, Here comes the problem. Look at the code I just write down, what’ll happen if I want to add a new kind of bird. It seems I need to change the method of getBird(). That’s right, this is the problem of this pattern. It doesn’t fit the O-C-P. So, we need another solution.
Remember the Open-Close-Principle. We will follow this principle to get the result.
We want a factory to produce the class, and when we also want the factory doesn’t need to be modified, if we want to add a new kind of bird.
Then we’ll need another two classes of factories, named eagleFactory and penguinFactory, paired to the birds.
The birdFactory will change into interface.
- public interface birdFactory {
- function getBird () : bird ;
- }
And the concrete factory will implement the birdFactory. Here is the code of eagleFactory.
- public class eagleFactory implements birdFactory {
- public function getBird () : bird {
- return new eagle () ;
- }
- }
It looks like very simple and clean, isn’t it? Another concrete factory, penguinFactory is much the same.
Now the UML diagram will change into:
One factory, one kind of birds. If you want to add a new kind of birds, you need to add two more classes. It sounds a little red-tape, but you’ll get the rewards. You don’t need to change the code, and the code is easy to extension. It means when the requirement is changed, you can easily fit for it. And that’s why we need design patterns, it saves our time.
That’s all about this pattern, and we will talk about another related pattern next time.
Enjoy!.
发表评论
-
设计模式 图析
2011-09-24 14:27 734【observer】 【adapter】 【comm ... -
一个设计模式的图片
2009-05-03 02:14 539... -
Design Patterns in ActionScript-Strategy
2009-02-24 13:34 571Today, we’re going to talk abou ... -
Design Patterns in ActionScript-Mediator
2009-02-24 13:29 507Ok, the last pattern now. Let’s ... -
Design Patterns in ActionScript-Command
2009-02-24 13:21 619A few months ago, I was an inte ... -
Design Patterns in ActionScript-Chain of Responsib
2009-02-24 13:12 600When you need some help in a ho ... -
Design Patterns in ActionScript-Flyweight
2009-02-24 13:07 638In Action Script 3.0 we have th ... -
Design Patterns in ActionScript-Visitor
2009-02-24 11:26 511It’s our winter holiday now, an ... -
Design Patterns in ActionScript-Memento
2009-02-23 23:38 518Now, I’m using Microsoft word t ... -
Design Patterns in Action Script-Composite
2009-02-23 14:29 514Still remember the Interpreter ... -
Design Patterns in ActionScript-Interpreter
2009-02-23 14:26 664In web programming, we often us ... -
Design Patterns in ActionScript-Proxy
2009-02-23 14:19 630Have you ever use HTTP-proxy or ... -
Design Patterns in Action Script-State
2009-02-23 14:17 958Yesterday, when I was on my way ... -
Design Patterns in ActionScript-Builder
2009-02-23 14:16 520Have you ever buy a computer on ... -
Design Patterns in ActionScript-Prototype
2009-02-23 14:12 665When I want to write the Protot ... -
Design Patterns in ActionScript-Iterator
2009-02-23 14:07 575There is a famous saying in com ... -
Design Patterns in Action Script-Template Method
2009-02-23 14:04 497Do you like playing cards? If y ... -
Design Patterns in ActionScript-Observer
2009-02-23 14:00 592In GUI programming, event-drive ... -
Design Patterns in ActionScript-Singleton
2009-02-23 13:55 526In our real world, many things ... -
Design Patterns in Action Script-Bridge
2009-02-23 13:44 556I think many of you are the fan ...
相关推荐
《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....
Alan Ezust在其著作《An Introduction to Design Patterns in C++ with Qt™, 2nd Edition》中探讨了设计模式的这一概念,并且在Qt框架的基础上讲解了如何在C++项目中应用这些设计模式。 C++是一种高性能、多范式的...
App Architecture: iOS Application Design Patterns in Swift 包含Source code 有钱请支持正版 没钱请默默学习 原书地址: https://www.objc.io/books/app-architecture 中文原书地址: ...
Data Structures And Algorithms With Object-oriented Design Patterns In Java.chm
Design Patterns in Java(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
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删除 查看此书详细信息请在美国亚马逊官网搜索此书
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环境下,如何通过教程学习设计模式。设计模式是软件开发中的一个重要概念,它是一套被反复使用、多数...
Data Structures and Algorithms with Object-Oriented Design Patterns in C++.rar Data Structures and Algorithms with Object-Oriented Design Patterns in C++.rar