Solved

Manager/Managed patterns

Posted on 1997-05-04
1
314 Views
Last Modified: 2012-08-13
Hi,
A question about the philosophy of C++.

I have a class of robots.  These robots sometime bump into each other.  So each robot would like to know where the other is. Also, each robot has a laser that can "shoot" the other robots.

The robot class is defined like this:

class robot
{
      public:
            //stuff
      protected
            laser * m_plaser;
};

So this allows the standard laser and all of its child variations to be
attached.

Now, the laser needs to know where the robot is so it can shoot it.

Now here is my problem, the robot has a laser as part of it, and the laser needs to take a robot as an input to find out if it can hit it. And a cicular reference thing happens.

So how can I get around this.

Somebody mentioned to me that I need to do a Manager/Managed item pattern.  ie give each robot a reference to an enviornment,   When a robot wants to shoot its laser, it asks the envionrment and the enviornment works out if it hit.

I've tried nutting out the environment thing as well, but keep on running into similar problem.

So any suggestions would be appreciated.  If you can think of a completely different way to atack this, that would be cool as well.

Malcolm

P.S. this seems like a fairly basic thing in C++, and I think i've missed something easy.  So I think it is an easy quesion, but feel free to change its value
0
Comment
Question by:trevena
1 Comment
 
LVL 5

Accepted Solution

by:
yonat earned 50 total points
ID: 1163265
The key to solving circular dependency problems is usually
abstraction.
In your case, I think that the laser does not shoot a robot; it
shoots a *target*. Now, there are several possibilities:

1. A robot can inherit from target. While this may seem OK at
   first look, I wouldn't do that. It means that if you want to
   reuse the robot in another context, you will have to carry
   the target with you. It may also mean multiple-inheritance,
   and some people dislike that.

2. A robot can have a conversion operation to a target. Here you
   do not have MI, but the reusability issue still hold.

3. You can have a function (yes, that under-respected thing)
   that takes a robot argument and returns a target argument.
   You can make this a bit more general: A target is probably
   just coordinates. You can either ask each robot for its
   coordinates, or, if you use a map to manage the robots'
   locations, the map can be queried about the location of a
   robot.

The use of an environment or map seems appropriate. The map does
not know about robots - it knows only about targets, or
coordinates or some other abstract class.

I highly recommend Robert Martin's book "Designing Object
Oriented C++ Applications using the Booch Method" (ISBN 0-13-203837-4).
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question