[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Manager/Managed patterns

Posted on 1997-05-04
1
Medium Priority
?
322 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 100 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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

591 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