I am facing design issues with my current task at hand.
I am implementing a service in C++ which has basically two main aims to acheive:
- Control LEDs and set a specific pattern based on incoming request to service
- Decide next state of system based on current state and incoming request to service
I have thought of Application, LEDController & State classes for the implementation. I am facing difficulty in communicating among different objects. I am unable to have seamless flow of data from one object to another.
Kindly refer to code snippet below:
/* Deals with the LED driver code */
class LEDController
{
public:
turnONLed(int pattern);
turnOFFLed();
};
/* Base class for different states of the system */
class State
{
protected:
std::string stateName;
public:
void transitionTo(State* newState);
virtual void HandleSystemOnEvent() = 0;
virtual void HandleSystemOnEvent() = 0;
virtual void HandleSetLEDPatternRequest() = 0;
virtual void HandleCancelLEDPatternRequest() = 0;
};
/* Some derived states here and concrete implementations for their member functions */
class DefaultState: public State;
class SystemONState: public State;
class SystemServingLEDRequest : public State;
/* Service object implementation */
class Application
{
LEDController* ledController;
State* currentState;
public:
Application();
void waitForEvents();
void serveEvent( EventData data);
};
Application::serveEvent(EventData data)
{
switch( data.eventType)
{
case SystemON:
currentState->HandleSystemOnEvent( ??? );
/* Help needed here!
Inside these polymorphic functions {HandleSystemOnEvent, HandleSystemOnEvent, ...} , I want to control the LEDs and make decision for new state.
How to access Application::ledController from inside of State ?
Also how to manipulate Application::currentState from these functions.
*/
newState = new SystemOnState();
currentState->transitionTo(newState);
break;
case SystemOFF:
currentState->HandleSystemOffEvent();
break;
case SetPatternRequest:
currentState->HandleSetLEDPatternRequest();
break;
case CancelPatternRequest:
currentState->HandleCancelLEDPatternRequest();
break;
}
}
I want to achieve LED control and state related decisions inside the polymorphic functions of State object. Can you kindly suggest on how to achieve this or other design suggestions to fix this issue.
Aucun commentaire:
Enregistrer un commentaire