I have a class:
class Cube
{
public string Name;
public string TexturePath;
}
And another class that inherits Cube
class DrawableCube : Cube
{
public Texture2D Texture;
public Shader Shader;
}
Suppose I have a collection of Cube
that I want to turn into DrawableCube
. Without just creating a constructor that takes a Cube
as an paramter and copies all of the attributes across, how can I make a copy of a Cube
with the resulting type DrawableCube
?
-
5This code is evil, you declare two public members with the same name except for capitalization that are different types...alternative– alternative2011年04月24日 21:12:43 +00:00Commented Apr 24, 2011 at 21:12
-
2I think what you are talking about is the removal of boilerplate. To my knowledge, its not possible to do it without copying all the values over; Why are you doing this in the first place?alternative– alternative2011年04月24日 21:14:00 +00:00Commented Apr 24, 2011 at 21:14
-
@mathepic Sorry about that, I typed it in to hastily. FixedHannesh– Hannesh2011年04月24日 21:18:48 +00:00Commented Apr 24, 2011 at 21:18
-
@mathepic because I have an array of cubes, and I want to turn them into drawable cubes. Maybe my design is flawed?Hannesh– Hannesh2011年04月24日 21:22:44 +00:00Commented Apr 24, 2011 at 21:22
-
can you not create DrawableCube's in the first place? Or just add the drawable cube stuff to the cube class.alternative– alternative2011年04月24日 21:24:09 +00:00Commented Apr 24, 2011 at 21:24
4 Answers 4
IMHO the best solution here is not to inherit DrawableCube
from Cube
at all, but let the UI class DrawableCube
keep an internal reference to the data class Cube
. Cube
, however, should probably be designed as an immutable class. This way DrawableCube
will need a constructor taking a Cube
, but you don't have to copy all of it's attributes, you can just store the given reference:
class DrawableCube :
{
private myCube;
public DrawableCube(Cube c)
{
myCube=c;
}
}
3 Comments
DrawableCube
is almost certainly a Cube
, so this breaks OOP polymorphism.Cube
and DrawableCube
. If that's the case, they should have a common base class or interface ICube
. This will result in the Proxy design pattern (en.wikipedia.org/wiki/Proxy_pattern), where DrawableCube is a proxy for Cube.Warning: The following solution is ugly but it does what you want.
Make Cube
take a single parameter of type CubeInformation
in its constructor, and that is its only member (it is private). It simply sets the CubeInformation
. It then provides properties for the stuff in that struct.
Let CubeInformation
be a struct that contains the data for a Cube
.
Let DrawableCube
have a constructor that takes a Cube
and copies over the CubeInformation
struct from the Cube
.
Wallah. If someone has to add more data to the Cube
, they add it to the CubeInformation
struct and implement the properties in Cube
. No maintenance is necessary in DrawableCube
.
Comments
If you want to turn collections of Cube
into a collections of DrawableCube
, then maybe inheritance is not the right tool.
Give aggregation a try:
class DrawableCube //: Cube
{
public Cube TheCube { get; private set; }
public DrawableCube (Cube c) { TheCube = c; }
public Texture2D Texture;
public Shader Shader;
}
Comments
Since you don't want a constructor, I guess the only other option is with a factory pattern.