Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Manager/Managed patterns

Posted on 1997-05-04
1
311 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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