Reputation: 883
For the past few weeks, I have been thinking of a way to make rather unrelated objects to communicate with each other. While I have been able to find several ways to make it work, they all seem to be somewhat unelegant or overly complicated.
One of the working solutions was to create some sort of a super-class which serves the role of a "mediator", checking the state of its member objects and responding accordingly.
struct SuperClass {
Car playerCar;
Police police;
void update() { if(playerCar.exceedsSpeed()) { police.call(); } }
};
While this could work well in simple programs, this design tends to get messy with complexity. As more different object types are created, more super-classes or mid-super-classes are required. Eventually you end up with a strict hierarchy of classes which are extremely difficult to maintain.
Another solution is to pass the reference of an object as an argument.
struct Car {
Police* pPolice;
Car(Police* obj) { pPolice = obj; }
};
Police police;
Car playerCar(&police);
Every call to the Police::call()
could be made through pPolice
, but there are two issues with this solution.
First, the Car
struct now doesn't make any sense. If every car has various characteristics such as color
, manufacturer
, maxSpeed
, and so on, why the hell does every car have a police
? Since objects are structures containing variables specific to the type of that object, including a completely unrelated variable among those variables breaks the aesthetics of OOP.
Second, in case Car
needs to call methods of more objects than just police.call()
, I would now need to pass more references as arguments.
Car playerCar(&police, &anotherObj, &yetAnotherObj...)
... And this is just silly.
Instead, I would like to be able to call the appropriate methods locally.
void Car::accelerate() {
speed++;
if (speed > maxSpeed) { Mediator::callPolice(); }
};
How do I do that? Is that possible? Is that even the correct way?
Upvotes: 0
Views: 351
Reputation: 490148
Seems like you normally have two cases.
One is that the police catch somebody speeding. They don't call the police to report themselves--rather, an individual police officer has some area in which he can observe, and if a car exceeds the limit while traveling through that area (and the police officer isn't otherwise occupied), he's caught and gets issued a ticket (or whatever).
The second, is that the driver of the car decides to call the police (e.g., there's been an accident). In this case, there is a "mediator"-like class. Specifically, there's a class to represent the phone system, and the driver (or another driver who sees) uses their phone to call the police. If nobody with a phone sees the incident, then the police might just not get called--or there might at least be a delay while the driver finds somebody with a phone.
Upvotes: 2
Reputation: 337
Create a public static method "callPolice" inside your "Police" struct. Then you could call it from a method inside Car struct. This would work for you if body of "callPolice" method does not need to execute on an instance of a specific Police object.
Upvotes: 0