Solved

Manager/Managed patterns

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now