Reputation: 1646
My goal is to keep static Bitmaps in child classes and use logic in base class with minimal amount of code in child classes.
At the moment I've got 2 classes: LEDa
, LEDb
, that derive from LED
. Each of the child classes needs it's own set of static
objects (huge bitmaps) to prevent high ram usage. I'm passing child bitmaps to base methods as arguments, but I want to reduce that "linking" code to minimum.
I've come to a possible solution, however it seems quite hacky so i decided to ask for a second opinion. I am thinking of passing child's static bitmap references into an instance variable of a base class.
class LED{
Bitmap bmp;
Bitmap[] StateBMP;
int state;
public LED(Bitmap bmp){
this.bmp = bmp;
createStateBMPs();
}
}
class LEDa : LED{ //code is identical in LEDb
static Bitmap bmp = .......;
static Bitmap[] StateBMP;
public LEDa():base(bmp){
}
}
If I got it correctly, initializing multiple instances of LEDa
and LEDb
would only load 2 bitmaps into memory and all of bitmaps in LED
would just be references linking to 1 of the 2 bitmaps.
I am wondering if there is better way to deal with this problem.
Upvotes: 0
Views: 907
Reputation: 1646
I've spent quite a few hours trying different things to achieve the goal, and static bmp passed to base class instance object DOES work. My only concern is having an unecessary duplicate object reference in each instance of LED
.
Following Stefan's answer I have found an approach (that would not lead to the same class objects having multiple identical references). Basically, a static Bitmap in child classes, which can be accessed by base class with overridden properties:
class LED{
/*
I used NULL instead of Global.DefaultBitmap, which worked just fine
if you don't leave lose ends, but these classes are actually
Controls and the UserControl test container kept giving me errors
when I scrolled through base control, so I decided to add a static
10x10 image/icon to all base controls to have that instead of errors.
*/
public virtual Bitmap bmp {get {return Global.DefaultBitmap;} set{}}
public LED(){}
}
class LEDa : LED{ //code is identical in LEDb
static Bitmap _bmp;
public override Bitmap bmp{get{return _bmp;}set{_bmp=value}}
public LEDa(){}
}
Upvotes: 1
Reputation: 10219
I think what you try to achieve is a Singleton, there are many variations about this, but most easy solution can look like this.
public class LED{
Bitmap bmp;
public LED(Bitmap bmp){
this.bmp = bmp;
}
}
public static class LEDSingleton
{
private static Dictionary<string,LED> LEDs = new Dictionary<string, LED>();
public static LED Instance(string name, Bitmap bmp)
{
if (!LEDs.ContainsKey(name))
{
LEDs.Add(name, new LED(bmp));
}
return LEDs[name];
}
}
And you use it like this
var LEDa1 = LEDSingleton.Instance("LEDa", new Bitmap(..));
var LEDb = LEDSingleton.Instance("LEDb", new Bitmap(..));
var LEDa2 = LEDSingleton.Instance("LEDa", new Bitmap(..)); // same instance as LEDa1
Note:
Instead of string
name can be an enum
or something else.
Any IoC (Inversion of Control) container can work very well in this case because you can configure the class to be in singleton scope and every time you get and instance of that type, will be the same.
Upvotes: 1