How to make a "static overloaded const" in C #?

I am creating a game in C #. Each level consists of several fragments. Each tile has a specific type, for example. grass floor, wooden wall, etc.

Ideally, I would like to have one base class "Tile" and inherit it to create classes for each type of tile. I would like to have the tile properties of a somehow static / const / etc subclass member, since each type of tile will only ever have the same properties. That is, I do not want 100 Tiles to have a property that has the same value, which seems rather inefficient.

The problem is that you cannot do this in C #. Are there any ways to achieve what I want?

My alternative idea was to split all of this into trees, only with the "Tile" class, which represents an instance and another "TileType", from which I created one object for each type and possibly accessed them through some kind of "TileTypeCollection". This seems strange, although I would prefer to do it first.

Are there any general guidelines for dealing with this situation?

+7
source share
5 answers

Are you looking for a FlyWeight design template:

http://en.wikipedia.org/wiki/Flyweight_pattern

Flyweight is a software design template. Flies - this is an object that minimizes the use of memory, as much data as possible with other similar objects; this is a way to use objects in large quantities when a simple re-presentation will use an unacceptable amount of memory.

enter image description here

You have C # examples here: http://www.dofactory.com/Patterns/PatternFlyweight.aspx#_self2

+4
source

You can create a class structure as follows:

public abstract class Tile { public abstract string BaseType; } public class Floor : Tile { public override string BaseType { get { return "floor"; } } } public class Grass : Tile { public override string BaseType { get { return "grass"; } } } public class Wooden : Tile { public override string BaseType { get { return "wooden"; } } } 
+2
source

Can you use a static field to return a property to the Tile base class?

 public abstract class Tile { private static string _commonProperty public static string CommonProperty { get { return _commonProperty; } set { _commonProperty = value; } } } 
+1
source

One way is to have static properties for child classes and a non-stationary virtual assistant in the base class, which when redefined returns static values.

0
source

Depending on the exact requirements, there are several options.

IF there is a common property that differs in value for each type, then declare it in the base class, make it a read-only value and set it in the constructor of each type.

If you have properties that exist only for a particular type, then they should be declared in a subclass and likewise are read-only.

0
source

All Articles