Behavioral patterns_Java design Patterns
2013-03-12 16:02
288 查看
Behavioral patterns prescribe the way objects interact with each other. They help make complex behavior manageable by specifying the responsibilities of objects
and the ways they communicate with each other.
The Observer Pattern:
Observer is a very common pattern. You typically use this pattern when you're implementing an application with a Model/View/Controller architecture. The Model/View part of this design is intended to decouple the presentation of data from the data itself.
Consider, for example, a case where data is kept in a database and can be displayed in multiple formats, as a table or a graph. The Observer pattern suggests that the display classes register themselves with the class responsible for maintaining the data, so
they can be notified when the data changes, and so they can update their displays.
The Java API uses this pattern in the event model of its AWT/Swing classes. It also provides direct support so this pattern can be implemented for other purposes.
The Java API provides an
To go along with this, an
registered itself with the
The following example demonstrates how an Observer pattern can be used to notify a display class for a sensor such as temperature has detected a change:
The Strategy and Template patterns:
Strategy and Template patterns are similar in that they allow different implementations for a fixed set of behaviors. Their intents are different, however.
Strategy is used to allow different implementations of an algorithm, or operation, to be selected dynamically at run time. Typically, any common behavior is implemented in an abstract class and concrete subclasses provide the behavior that differs. The client
is generally aware of the different strategies that are available and can choose between them.
For example, an abstract class,
instantaneous measurement, and yet another might hold a peak (or low) value for some period of time.
The intention of the Template pattern is not to allow behavior to be implemented in different ways, as in Strategy, but rather to ensure that certain behaviors are implemented. In other words, where the focus of Strategy is to allow variety, the focus of Template
is to enforce consistency.
The Template pattern is implemented as an abstract class and it is often used to provide a blueprint or an outline for concrete subclasses. Sometimes this is used to implement hooks in a system, such as an application framework.
and the ways they communicate with each other.
The Observer Pattern:
Observer is a very common pattern. You typically use this pattern when you're implementing an application with a Model/View/Controller architecture. The Model/View part of this design is intended to decouple the presentation of data from the data itself.
Consider, for example, a case where data is kept in a database and can be displayed in multiple formats, as a table or a graph. The Observer pattern suggests that the display classes register themselves with the class responsible for maintaining the data, so
they can be notified when the data changes, and so they can update their displays.
The Java API uses this pattern in the event model of its AWT/Swing classes. It also provides direct support so this pattern can be implemented for other purposes.
The Java API provides an
Observableclass that can be subclassed by objects that want to be observed. Among the methods
Observableprovides are:
addObserver(Observer o)is called by
Observableobjects to register themselves.
setChanged()marks the
Observableobject as having changed.
hasChanged()tests if the
Observableobject has changed.
notifyObservers()notifies all observers if the
Observableobject has changed, according to
hasChanged().
To go along with this, an
Observerinterface is provided, containing a single method that is called by an
Observableobject when it changes (providing the
Observerhas
registered itself with the
Observableclass, of course):
public void update(Observable o, Object arg) |
import java.util.*; |
Strategy and Template patterns are similar in that they allow different implementations for a fixed set of behaviors. Their intents are different, however.
Strategy is used to allow different implementations of an algorithm, or operation, to be selected dynamically at run time. Typically, any common behavior is implemented in an abstract class and concrete subclasses provide the behavior that differs. The client
is generally aware of the different strategies that are available and can choose between them.
For example, an abstract class,
Sensor, could define taking measurements and concrete subclasses would be required to implement different techniques: one might provide a running average, another might provide an
instantaneous measurement, and yet another might hold a peak (or low) value for some period of time.
The intention of the Template pattern is not to allow behavior to be implemented in different ways, as in Strategy, but rather to ensure that certain behaviors are implemented. In other words, where the focus of Strategy is to allow variety, the focus of Template
is to enforce consistency.
The Template pattern is implemented as an abstract class and it is often used to provide a blueprint or an outline for concrete subclasses. Sometimes this is used to implement hooks in a system, such as an application framework.
相关文章推荐
- Software Architecture Design Patterns in Java
- Design Patterns in Java: Creational
- Java Enterprise Design Patterns: Patterns in Java Volume 3
- Design Patterns in Java
- Java设计模式(Design Patterns In Java)读书摘要 第1部分接口型模式——第2章 接口型模式介绍
- Design Patterns in Java: Structural
- [java-design-patterns]Enum和Factory模式的替换
- Java Design Patterns Abstract Factory
- Examples of GoF Design Patterns(GOF提及的设计模式在java中的例子)
- Design Patterns in Java: Behavioral
- Design Patterns in Java - Singleton
- The Genesis of Java Design Patterns
- Structural patterns_Java design patterns
- 说Hand-over-hand lock: 通读Concurrent Programming in Java: Design Principles and Patterns (1)
- JAVA设计模式(DESIGN PATTERNS IN JAVA)读书摘要 第1部分接口型模式——第4章 外观(Facade)模式
- Java设计模式(Design Patterns In Java)读书摘要——第1章 绪论
- Examples of GoF Design Patterns in Java's core ...
- Examples of GoF Design Patterns(GOF提及的设计模式在java中的例子)
- JAVA设计模式(DESIGN PATTERNS IN JAVA)读书摘要 第1部分接口型模式——第3章 适配器(Adapter)模式
- Design Patterns in Java