I’m trying to figure out the best design pattern to insulate a child object from knowing too much about the parent object it is contained in.
For instance, with a parent class like this…
class Airplane {
var seats:Array
...
function removeSeat(seat:Seat) {
// find seat object in seats array and remove it
}
}
child class…
class Seat {
var rowNumber:int
...
}
If I’m working in the context of the Seat object and I want to remove myself from my parent Airplane object, what is the best way of separating the Seat from knowing about where it is in the Airplane.seats array?
I know I can pass the Airplane parent object into the constructor of the Seat, then call the removeSeat method on Airplane to remove that Seat, but I want to the Seat knowing as little about the Airplane if possible. Any ideas?
You could use the event handler pattern – essentially the airplane passes a ‘removeMe’ callback into the Seat on construction. The Seat then executes this callback when it wants to be removed. The seat has no idea who passed in the callback – it just needs to know the signature of the callback, in this example that the first parameter is a reference to a Seat object.
E.g. in pseudo-code