- 浏览: 661554 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
sztime:
可以在文本框上绑定事件来禁用回车键, 我就是这样做的.在IE中 ...
form 回车自动提交问题 -
damoqiongqiu:
非常好的文章,很透彻不过有一句话小僧腆着脸补充一下:“1111 ...
为什么要用补码来做存储 -
wuyizhong:
原来如此啊。
form 回车自动提交问题 -
luliangy:
谢楼主~!
用C语言扩展Python的功能 -
kwong:
很有用,谢谢
火狐和IE 对css 样式解释的差异
Do you still remember the factory method pattern we talked about last time? If you’re already forgot it. May be you can take a look at the following intent, which was defined by the GOF book.
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
— By THE GOF BOOK
In out last example, we use eagleFactory and penguinFactory to decide which bird should be instantiated. And we can easily extend this program only by adding some new classes; we don’t need to change the main framework.
OK, now let’s do something more about the last example.
First, let’s add a new class named littleEagle, and the little eagle doesn’t have the ability to fly, and not migration. So, we can write the code as follows.
- class LittleEagle extends Bird {
- public function LittleEagle (){
- super ( new neverFly () , new neverMigration ()) ;
- }
- }
Then, we need to add a bird related to penguin, eh, I wonder that whether there is some kind of penguin flies and not migration. It seems that god doesn’t make that kind of creature. So, we need to make it, and we can call it littlePenguin. Of course, it was faked The code is as below.
- class LittlePenguin extends Bird {
- public function LittlePenguin (){
- super ( new fly () , new neverMigration ()) ;
- }
- }
According to the factory method pattern, we need to add two more factories to produce these two new products. And the UML diagram becomes like this.
How’s your feeling about this diagram?
Four factories correspond to four birds. Looking deep inside, we can found that littleEagle is much the same as eagle, and the same to the other two classes. If we want to add bigEagle and oldEagle, we’ll need another two more factories, and it sounds unreasonable. The eagles can be looked upon as series products. Maybe they should be produced by the same factory.
Now, I should show you the new pattern, named abstract factory pattern. Its intent was defined as follows.
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
– By THE GOF BOOK
Using this pattern to change our existing code, we can get the following UML diagram.
Of course, the code has to be changed. Let me show you the code of eagleFactory.
- public class EagleFactory implements BirdFactory {
- public function getBird () : bird {
- return new eagle () ;
- }
- public function getLittleBird () : bird {
- return new littleEagle () ;
- }
- }
Abstract Factory and Factory Method are both about generating the new object, while abstract factory cares more about a family of object.
Enjoy!
发表评论
-
设计模式 图析
2011-09-24 14:27 711【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 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 633In 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 951Yesterday, 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 662When 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 Action Script-Template Method
2009-02-23 14:04 494Do you like playing cards? If y ... -
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 ...
相关推荐
《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