Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is this a passable software design?

I'm currently working on a game in c++. since there's no garbage collector one has always to carefully delete the objects and also make sure that such objects are not accessed anymore once they got deleted.
Now as a project grows some objects may get referenced from more and more places. For example my units in the game may get referenced from the renderer, from the scene hierarchy, from the selection mechanism, from the HUD and so on. now - if a object gets deleted one has to make sure that all other classes that reference this object will be notified about it.
Or let's say it the other way arround - if i create a new class that may reference one of my units, i'll also have to change the code of the unit (or of the unit manager or whatever module delets the unit if it gets destroyed) to make sure this new module knows when the particular unit it currently references gets deleted.

Now I thoght there could be a simple event driven general purpose aproach to solve this problem by creating a baseclass to which one another object can subscribe. Something like this:

class DeletableBase;//forward declaration

class ISubscriber{
public:
    virtual someObjectGotDeleted(DeletableBase* deletedObject)=0;
};

class DeletableBase{
private:
    vector<ISubscriber*> subscribers;
public:
    virtual ~DeletableBase(){
        for(int i=0; i<subscribers.size(); i++)
            subscribers[i]->someObjectGotDeleted(this);
    }
    subscribeForDeleteEvent(ISubscriber* subscriber){
        subscribers.push_back(subscriber);
    }
};

with that - if i reference any object that inherits from this class from a new class i can simply add myself as a subscriber and if the object will be deleted from any other place I will get notifed about it.

is this a "clean" way of coding?

like image 389
Mat Avatar asked Feb 02 '11 14:02

Mat


People also ask

What is a good software design?

Good design relies on a combination of high-level systems thinking and low-level component knowledge. In modern software design, best practice revolves around creating modular components that you can call and deploy as needed. In doing this, you build software that is reusable, extensible, and easy to test.

What is meant by software design?

Software design is the process of envisioning and defining software solutions to one or more sets of problems. One of the main components of software design is the software requirements analysis (SRA). SRA is a part of the software development process that lists specifications used in software engineering.

Why are we designing a software?

As software design is performed by creating modules, it makes the task easier to maintain. Tasks like finding bugs, debugging, restructuring, and changing the functionality of specific elements in the software application become quite easy due to software design.

What are the types of software design?

The software design process can be divided into the following three levels of phases of design: Interface Design. Architectural Design. Detailed Design.


2 Answers

If this is purely about memory management (rather than state change), use a smart pointer instead. Start with shared_ptr, then optimize using make_shared/allocate_shared or boost::intrusive_ptr if it's too slow.

like image 197
Fred Foo Avatar answered Sep 28 '22 10:09

Fred Foo


One thing you'll have to consider (especially if you're writing a game) is that when one of your subscribed objects gets deleted on the main thread, your game will most likely block until each of its subscribers is done doing whatever it's going to do upon deletion of the object. That may affect game performance if you're not careful.

like image 45
Adam Maras Avatar answered Sep 28 '22 11:09

Adam Maras