Solved

Manager/Managed patterns

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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.

813 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

13 Experts available now in Live!

Get 1:1 Help Now