patterncsharpMinor
Avoid explicitly writing base class state in a derived class. Suggestions for my method
Viewed 0 times
methodwritingavoidexplicitlystateforsuggestionsderivedclassbase
Problem
I have two classes one base Foo class and one derived Bar class.
Each class has its own associated settings class that can create a new instance from the Load method. This way all derived class settings can be stored in one list of FooSettings.
The classes themselves have their settings as an argument for the constructor and are created from their associated settings class (null = default settings class state)
The problem with this
Each class has its own associated settings class that can create a new instance from the Load method. This way all derived class settings can be stored in one list of FooSettings.
public abstract class FooSettings
{
public int a;
public int b;
public float c;
public abstract Foo Load();
}
public class BarSettings : FooSettings
{
public float d;
public override Foo Load()
{
return new Bar(this);
}
}The classes themselves have their settings as an argument for the constructor and are created from their associated settings class (null = default settings class state)
public abstract class Foo
{
private int a = 0;
private int b = 1;
private float c = 1.2f;
public Foo() { }
public abstract FooSettings WriteSettings();
protected void WriteBaseFooSettings(ref FooSettings settings)
{
settings.a = a;
settings.b = b;
settings.c = c;
}
public int A
{
get { return a; }
set { a = value; }
}
public int B
{
get { return b; }
set { c = value; }
}
public float C
{
get { return c; }
set { c = value; }
}
}
public class Bar : Foo
{
float d = 0.9f;
public Bar()
: base() { }
// METHOD A
public override BaseFooSettings WriteSettings()
{
DerivedBarSettings settings = new DerivedBarSettings();
settings.a = A;
settings.b = B;
settings.c = C;
settings.d = d;
return settings;
}
// METHOD B
public override FooSettings WriteSettings()
{
var settings = new BarSettings();
// Write base settings
var baseSettings = settings as FooSettings;
WriteBaseFooSettings(ref baseSettings);
// Write derived/local settings
settings.d = d;
return settings;
}
}The problem with this
Solution
I think this is the right way to do it. If you have some code that should be used from the base class and all its derived classes, it should be in the base class. Which is exactly what you're doing.
But I would make a modification in your code. You don't need to use
But I would make a modification in your code. You don't need to use
ref. In C#, classes are reference types, which means that the values in the object will change, even if you don't use ref. And if you remove the ref, you won't need the baseSettings variable anymore, making the code shorter:protected void WriteBaseFooSettings(FooSettings settings)
{
// set the properties, same as before
}
…
public override FooSettings WriteSettings()
{
var settings = new BarSettings();
WriteBaseFooSettings(settings);
// Write derived/local settings
settings.d = d;
return settings;
}Code Snippets
protected void WriteBaseFooSettings(FooSettings settings)
{
// set the properties, same as before
}
…
public override FooSettings WriteSettings()
{
var settings = new BarSettings();
WriteBaseFooSettings(settings);
// Write derived/local settings
settings.d = d;
return settings;
}Context
StackExchange Code Review Q#15764, answer score: 5
Revisions (0)
No revisions yet.