lundi 8 juin 2015

State design pattern - Passing Objects

I have a state design pattern implementation with 3 states:

State1::DoTask(){...}

State2::DoTask(){...}

State3::DoTask(){...}

These all 3 states need to use 2 classes already instanced

e.g.

State1::DoTask()
{ 
    sensor->GetData(...)
    server->Send(buf,size)
}

How is the best way to pass these 2 class pointer objects to the State1, State2 and State3?

I thought in two options:

1) The Sensor and Server class could be a singleton:

State1::DoTask()
{
    sensor = Sensor::GetInstance();
    server = Server::GetInstance();
    ....
}

2) Passing by using other object to hold the class pointer:

class ComClasses
{
    Sensor *sensor;
    Server *server;
}

State1::DoTask(ComClasses *c)
{
    ....
}

In your opinion, how is the best method? Is there a better solution?

Best regards,

Aucun commentaire:

Enregistrer un commentaire