In GUI programming, event-driven is an important concept. Such as,
when you programming in Flex, you may put a button on the stage, then
you’ll add a callback function to the button. The function will be
called when there is some action play on the button, such as click.
This is almost the same with the observer pattern.
Make it clearer. Firstly, we care the state of the button; we
want know when the button was clicked. Secondly, when the button state
was changed, we want to do something, that’s the callback function.
Actually, you can consider as the function cares the state of the
button. When the button state change, the function will do something.
The button is observable, and the function maybe the observer.
Now, let’s come to the intent of this pattern.
Define a one-to-many dependency between objects so that when
one object changes state, all its dependents are notified and updated
automatically.
– By THE GOF BOOK
I’ll give you a new example to illustrate this pattern.
The class diagram is as follows.
As you see, there are only two parts in our example. One is Observable, and the other is Observer.
The role of Observable is holding the observers in a list, and
notifies all the observers when the state was change. Here, we don’t
have the state variable, and when you click the Notify All button in
the demo the notifyAll() operation will be execute.
The Observer’s role is much simpler, just execute or update. Of
course, you need to figure out which subject you’d like to focus on,
and you may need to call the Observable.addObserver(yourself) to let
you appear in the observer list. Then when the state is changed,
execute or update method will be called automatically.
And this is pattern is also called Publish/Subscribe pattern.
It means if you subscribe something, such as newspaper, you’ll get it
sooner after the newspaper was published.
This pattern is very useful in dealing with GUI programming. Take it with you!
You can download the full source code from
here
.
分享到:
相关推荐
Implement the design patterns in real-world applications Select an alternative to these patterns by comparing their pros and cons Use Visual Studio Community Edition 2017 to write code and generate...
Go: Design Patterns for Real-World Projects What You Will Learn: Install and configure the Go development environment to quickly get started with your first program Use the basic elements of the ...
Go Design Patterns for Real-World Projects 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Go Design Patterns for Real-World Projects 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Go Design Patterns for Real-World Projects 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权...
Laracasts - design -patterns -in- php 课程资源
Design Patterns in ActionScript
Data Structures and Algorithms with Object-Oriented Design Patterns in CSharp - Bruno R. Preiss
Design Patterns in Modern C++.pdf
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...
The Go programming language has firmly ... With these skills in hand, you will be able to conquer all your fears of application development and go on to build large, robust and succinct apps in Go.
Pro-Objective-C-Design-Patterns-for-iOS.pdf
https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码
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 ...
C# 设计模式 design-patterns-c-sharp
Practical-Python-Design-Patterns-Pythonic-Solutions-to-Common-Problems.pdf
to apply the 23 Gang of Four (GoF) design patterns in Go and also learn about CSP concurrency patterns, the "killer feature" in Go that has helped Google develop software to maintain thousands of ...
Go: Design Patterns for Real-World Projects What You Will Learn: Install and configure the Go development environment to quickly get started with your first program Use the basic elements of the ...
Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3
关于测试的新文章:mockito 2的单元测试