views:

160

answers:

3

I'm looking for advice on the best way to structure my program. Here is what I have now, and the problems I see with the design:

I have a class with a private constructor and a submit() method, to carefully control the objects created, and every object created is added to a global list. The program has several functions that perform "big" operations with the global list. Right now, they're inside the object class as static functions. This seemed to make sense, as they should be in a class, and they are obviously related to the objects. But it seems weird that whenever I access a property of an object, I'm also greeted by Intellisense with a list of these "big" functions, that while are associated, really shouldn't be called by an individual object: they act on the list of all the objects! But I can't think of another way to do it. Should I have a class called Operations and put them all in that? That seems too dissociative. Also, this way I'd still see the submit() method, which I can't remove from the class without declaring the constructor as public, which I don't want to do.

Any ideas? Thanks!

(sorry I wasn't sure exactly what to tag this, and I know there are many questions like it, but i've been puzzled for a while. Feel free to re-tag, edit, w/e! thanks again!)

A: 

Hmm. Could you do something like this instead (from a coupling point of view)?

class Object { };
void ProcessObjects (list<Object *> &objects) { }
list<Object *> &GetGlobalObjects (void);

int main (void)
{
 ProcessObjects(GetGlobalObjects());
 ProcessObjects(somePossibleTemporaryList);
}
Nick Bedford
+1  A: 

I suggest you have 2 classes:

The data class 'Precious', which is used to hold the properties, accessors, and operations that affect a single member of the class.

  class Treasure; // let's pre-declare this.

  class Precious {
  private:
    BOOL invisible;
    Precious() : invisible(FALSE) {}
    friend class Treasure. // so it can call the private constructor
  public:
    BOOL isMine() { return TRUE; }
    BOOL iWantsIt() { return TRUE; }
    void don() { invisible = TRUE; }
    ~Precious { printf("need to find mnt/doom here"); exit(1); }
  }

A collection and management class 'Treasure' which contains methods to create new members and add them to the list, and methods to manage and do operations across these lists.

  class Treasure {
    void forgeOne { rings.add( new Precious ); };
    void findThemAll {
      for (Precious* myPrecious;
      {
        // implement remote ring call
      }
    }
    void inTheDarknessBindThem {
      // need to add check for the one ring
      for (Precious* myPrecious;
      {
        // filter on dark rings
        // add binding code.
      }
    }
    Map<Precious*> rings;
  }

You can then construct an instance of Treasure as a global or singleton and access it as appropriate.

Alex Brown
ok so I just went to see district 9, and I'm feeling all hobbity.
Alex Brown
OK, I like this solution, except that it doesn't stop me from seeing the submit() method, which is still strange for an individual object to be able to call, but I can't take it out of the method without making the constructor public, which I don't want to do.
Nona Urbiz
I don't quite understand what submit does, but you can easily make the Precious constructor private and then make it a friend of the Treasure class so it can call it. I'll update the example.
Alex Brown
Friend classes let you declare private functions which can be called from specific other classes only.
Alex Brown
cool! thanks!, I'd never heard of friend functions, I'm gonna read up on that. I'm holding off on accepting before I have it all working, but I think that's exactly what I was looking for.
Nona Urbiz
A: 
Jerry Coffin