State pattern

State pattern

[
LePUS3 ( [http://lepus.org.uk/ref/legend/legend.xml legend] ) ]

The state pattern is a behavioral software design pattern, also known as the objects for states pattern. This pattern is used in computer programming to represent the state of an object. This is a clean way for an object to partially change its type at runtimecite book | author=Gamma, Erich | coauthors = Richard Helm, Ralph Johnson, John M. Vlissides
title = Design Patterns: Elements of Reusable Object-Oriented Software
publisher = Addison-Wesley
date = 1995
pages = 395
isbn = 0201633612
] .

Take for example, a drawing program, in which there could be an abstract interface representing a tool, then concrete instances of that class could each represent a kind of tool. When the user selects a different tool, the appropriate tool would be instantiated.

Interface Example

For example, an interface to a drawing tool could be class AbstractTool { public: virtual void MoveTo(const Point& inP) = 0; virtual void MouseDown(const Point& inP) = 0; virtual void MouseUp(const Point& inP) = 0; }; Then a simple pen tool could be class PenTool : public AbstractTool { public: PenTool() : mMouseIsDown(false) {} virtual void MoveTo(const Point& inP) { if(mMouseIsDown) { DrawLine(mLastP, inP); } mLastP = inP; } virtual void MouseDown(const Point& inP) { mMouseIsDown = true; mLastP = inP; } virtual void MouseUp(const Point& inP) { mMouseIsDown = false; } private: bool mMouseIsDown; Point mLastP; }; class SelectionTool : public AbstractTool { public: SelectionTool() : mMouseIsDown(false) {} virtual void MoveTo(const Point& inP) { if(mMouseIsDown) { mSelection.Set(mLastP, inP); } } virtual void MouseDown(const Point& inP) { mMouseIsDown = true; mLastP = inP; mSelection.Set(mLastP, inP); } virtual void MouseUp(const Point& inP) { mMouseIsDown = false; } private: bool mMouseIsDown; Point mLastP; Rectangle mSelection; };

A client using the state pattern above could look like this: class DrawingController { public: DrawingController() { selectPenTool(); } // Start with some tool. void MoveTo(const Point& inP) {currentTool->MoveTo(inP); } void MouseDown(const Point& inP) {currentTool->MouseDown(inP); } void MouseUp(const Point& inP) {currentTool->MouseUp(inP); } void selectPenTool() { currentTool.reset(new PenTool); } void selectSelectionTool() { currentTool.reset(new SelectionTool); } private: std::auto_ptr currentTool; };The state of the drawing tool is thus represented entirely by an instance of AbstractTool. This makes it easy to add more tools and to keep their behavior localized to that subclass of AbstractTool.

As opposed to using switch

The state pattern can be used to replace switch() statements and if {} statements which can be difficult to maintain and are less type-safe. For example, the following is similar to the above but adding a new tool type to this version would be much more difficult. class Tool { public: Tool() : mMouseIsDown(false) {} virtual void MoveTo(const Point& inP); virtual void MouseDown(const Point& inP); virtual void MouseUp(const Point& inP); private: enum Mode { Pen, Selection }; Mode mMode; Point mLastP; bool mMouseIsDown; Rectangle mSelection; }; void Tool::MoveTo(const Point& inP) { switch(mMode) { case Pen: if(mMouseIsDown) { DrawLine(mLastP, inP); } mLastP = inP; break; case Selection: if(mMouseIsDown) { mSelection.Set(mLastP, inP); } break; default: throw std::exception(); } } void Tool::MouseDown(const Point& inP) { switch(mMode) { case Pen: mMouseIsDown = true; mLastP = inP; break; case Selection: mMouseIsDown = true; mLastP = inP; mSelection.Set(mLastP, inP); break; default: throw std::exception(); } } void Tool::MouseUp(const Point& inP) { mMouseIsDown = false; }

ee also

* Finite State Machine
* Strategy pattern
* Dynamic classification

External links

* [http://www.lepus.org.uk/ref/companion/State.xml State in UML and in LePUS3] (a formal modelling language)
* [http://www.fsw.com/Jt/Jt.htm Jt] J2EE Pattern Oriented Framework
* [http://www.lepus.org.uk/ref/companion/State.xml State pattern in UML and in LePUS3] (a formal modelling language)

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • State (disambiguation) — State or The State may refer to:Government* A sovereign political entity ** State ** Unitary state ** Nation state ** State (law), a well defined jurisdiction, with its own set of laws and courts. *State (country subdivision), a non sovereign… …   Wikipedia

  • Pattern theory — Pattern theory, formulated by Ulf Grenander, is a mathematical formalism to describe knowledge of the world as patterns. It differs from other approaches to artificial intelligence in that it does not begin by prescribing algorithms and machinery …   Wikipedia

  • State Defense Forces — (SDF) (also known as State Guards, State Military Reserves, or State Militias) in the United States are military units that operate under the sole authority of a state government, although they are regulated by the National Guard Bureau through… …   Wikipedia

  • state — state, the state The state is a distinct set of institutions that has the authority to make the rules which govern society . It has, in the words of Max Weber, a ‘monopoly on legitimate violence’ within a specific territory. Hence, the state… …   Dictionary of sociology

  • Pattern Recognition (novel) — infobox Book | name = Pattern Recognition image caption = Original 1st edition cover author = William Gibson cover artist = country = United States language = English series = genre = Science fiction novel publisher = G. P. Putnam s Sons release… …   Wikipedia

  • State highways in New Jersey — Standard route signage in New Jersey Highway names Interstates: Interstate X (I X) …   Wikipedia

  • State of matter — A state of matter (or physical state, or form of matter) has physical properties which are qualitatively different from other states of matter.Traditionally, three states of matter were recognized: solid, which maintains a fixed volume and shape; …   Wikipedia

  • State religion — Religions by country North America Canada · United States · Mexico …   Wikipedia

  • State highways in Oregon — Sign for Oregon Route 99 The state highway system of the U.S. state of Oregon is a network of highways that are owned and maintained by the Highway Division of the Oregon Department of Transportation (ODOT). Compared to other states, Oregon s… …   Wikipedia

  • State funeral of John F. Kennedy — Robert Kennedy and Jean Kennedy seen following Jacqueline Kennedy as she leaves the United States Capitol with John Kennedy Jr. and Caroline Kennedy, after viewing John F. Kennedy lying in state, 1963. The state funeral of John F. Kennedy took… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”