5

In my course, I have been taught that, in general, In general, a class B should be responsible for creating instances of class A if one, or preferably more, of the following apply:

  1. Instances of B contain or compositely aggregate instances of A
  2. Instances of B record instances of A
  3. Instances of B closely use instances of A
  4. Instances of B have the initializing information for instances of A and pass it on creation.

And in some more complex cases, the best place to create an object is within a factory class.

My course has never taught me who should be responsible for deleting an instance. Say you want to delete an instance of A that has been created in an instance of B. Who deletes A? Is it A itself? Is it B? Is it some other class C that has a sole responsibility of deleting A?

  • 7
    A lot of these choices are going to be heavily influenced by technical aspects of what platform you are programming against. General statements aren't going to be very useful. – whatsisname Mar 27 '17 at 22:43
  • 2
    Option 4: the language semantics are responsible for destruction – Caleth Mar 28 '17 at 10:30
  • Ideally, if B created A, than it's very likely it will be him that has to delete it. So the creation and deletion is not spread in different components of the application. – Walfrat Mar 28 '17 at 15:20
  • 1
    Usually, Toddlers. Sometimes pets. Angry Wife is also an option if the object in question is your Xbox. – T. Sar Mar 28 '17 at 19:05
  • 2
    Generally there is only 1 "owner" of the object. That is who is responsible for destroying the object. Ownership can be passed. The tricky part comes when there are users but not owners of the same object, which is why encapsulation is important. People who use garbage collect languages or reference counters tend to ignore the "ownership" concept because it doesn't crash their app. But ignoring the concept may cause a latent bug that is really hard to detect as the "owned" object may still be referenced and used for processing even though the "owner" has been deleted giving erroneous results. – Dunk Mar 28 '17 at 21:32
12

College professors tend to make these things more complicated than they have to be. Someone who knows the terminology in your question should already know when the right time for creating an instance is; they would probably respond with "when it is needed," rather than resorting to a complicated set of rules.

The destruction of an object, however, is complicated by the fact that there are two major strategies for accomplishing it. Direct strategies are things like Delete and RAII. Indirect strategies are things like Garbage Collection and cleanup of an app domain on exit.

Consequently, you can't point to a single principle for deletion responsibility. If it's a garbage-collected programming language, a given object instance is not generally disposed of immediately after its no longer needed. Instead, objects are cleaned up some time after they are freed (meaning there are no longer any object references holding them).

In contrast, with direct methods, disposal of the object should generally take place when it is no longer needed, and the best place to do that is within the context or scope of the object's use. In other words, you might create a method that creates an object to do it's work; it's that method itself that would be responsible for destruction of the object, unless the method's purpose is to create that object for use somewhere else.

  • 4
    I'm not sure the "academic eggheads" comment is helpful or relevant. College professors are trying to teach general concepts to beginners, not (necessarily) trying to make definitive statements applicable everywhere regardless of context. I think it's reasonable for them to make general statements in most cases, since they're not dealing w/ particular scenarios. Some students might be taking their words a bit too literally due to their relative inexperience. Pointing that out would've been more helpful, in general 😉 – code_dredd Mar 28 '17 at 1:15
  • Very good answer. I would mention reference counting as a third major strategy. – Pieter B Mar 28 '17 at 8:19
  • @PieterB: Reference counting is a garbage collection strategy. – Robert Harvey Mar 28 '17 at 16:31
  • @RobertHarvey: The difference with reference counting is that usually the object is destroyed as soon as the reference count becomes zero. (On the other hand, for an object like an array containing a million references the destruction might take a long time and therefore might be done on a background thread, but in either case it is guaranteed that destructor code will be run soon after the reference count becomes zero. Typically there is no guarantee for garbage collection). – gnasher729 Mar 28 '17 at 19:28
  • @gnasher as counter is hidden from the client, reference counting has same non-deterministic qualities. – Basilevs Mar 29 '17 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.