(C++) State (Design Pattern)

January 25, 2018 · View on GitHub

 

 

 

 

 

(C++) State (Design Pattern)

 

The State is a Design Pattern to 'allow an object to alter its behavior when its internal state changes. The object will appear to change its class' [1]

 

Below is an example from [2].

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • Boost Boost: version 1.42
  • STL STL: GNU ISO C++ Library, version 4.4.5

 

 

 

 

 

Qt project file: CppDesignPatternState.pro

 


#------------------------------------------------- # # Project created by QtCreator 2011-04-14T15:44:05 # #------------------------------------------------- QT       += core QT       -= gui TARGET = CppDesignPatternState CONFIG   += console CONFIG   -= app_bundle TEMPLATE = app SOURCES += main.cpp \     gumballmachine.cpp \     hasquarterstate.cpp \     noquarterstate.cpp \     soldoutstate.cpp \     soldstate.cpp HEADERS += \     gumballmachine.h \     hasquarterstate.h \     noquarterstate.h \     state.h \     soldoutstate.h \     soldstate.h

 

 

 

 

 

gumballmachine.cpp

 


 

 

 

 

 

gumballmachine.h

 


 

 

 

 

 

hasquarterstate.cpp

 


 

 

 

 

 

hasquarterstate.h

 


 

 

 

 

 

main.cpp

 


 

 

 

 

 

noquarterstate.cpp

 


 

 

 

 

 

noquarterstate.h

 


 

 

 

 

 

soldoutstate.cpp

 


 

 

 

 

 

soldoutstate.h

 


 

 

 

 

 

soldstate.cpp

 


 

 

 

 

 

soldstate.h

 


 

 

 

 

 

state.h

 


 

 

 

 

 

References

 

  1. Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides. Design Patterns. 1995. ISBN: 0201633612.
  2. Eric Freeman, Elisabeth Freeman. Head First Design Patterns. 2004. ISBN: 978-0-596-00712-6.