This isn't strictly C#, but since that's what I'm using , I'll post it here. OO-ing stuff confuses me.
I have a thing, it's say a location.Now, I actually want a whole pile of locations, say a grid:Code:class Location { ... coords; }Now I want different things to use my grid. I want actually the same grid to be used in different ways. I want something to show my grid, but not be able to mutate it. I also want something to mutate my grid, but not show it. So I make two more classes?Code:class Grid { ... dimensions Location[,] gridinfo; }Now I could inherit a Grid:Code:class GridPainter { } class GridMutator { }Or I could make its own private grid as a member of it, and write like a copy constructor, taking a Grid as an argument to the initializer:Code:class GridPainter : Grid { }(Except that's not actually a copy constructor, and it wouldn't even be worth using one here, since on the mutator I actually do want to mangle what I'm being passed, not just a copy of it).Code:class GridMutator { Grid mutate; GridMutator( Grid toMutate ) { mutate = toMutate; } }
I don't understand how to decide what is best. OO has always confused me, because I'm used to straight up C. I kinda get the is-a / has-a thing, but I'm still hung up on this. I'm not even sure there isn't another better way to do either of these.
See the Mutator has-a grid, actually it should be that it receives-a grid - or can I inherit a grid and have it be operating on the original? Ok, I make a base instance:Now I want to make a Mutator that works off of this one. Does this work? Does it even make sense?Code:Grid mine = new Grid( 100, 100 );Is that even right? I don't get the inheritance thing. I mean I see how you can do it, if you just make a new instance, but what if I want to use an existing one? Do you not use inheritance there?Code:GridMutator mut = new GridMutator( mine );
I'm too old to learn new tricks I think.
Quzah.



5Likes
LinkBack URL
About LinkBacks


