Re: OO Design question



Today your base class *only* has common data. That may change tomorrow,
when you realize you need methods x y and z in your base class.

Pure data-only objects are well-expressed using structs. I suggest
instantiating your structs as private members of the base class, and
provide protected accessors to the structs' members so that the
subclasses can use them. This is sometimes known as
multiple-inheritance by delegation, and it helps keep things neat. It
also happens to be far more flexible than using multiple levels of
inheritance (base->subclass level1->subclass level 2 ...).

For example:

namespace Example
{
public class BaseClass
{
private PureData _pd;

public BaseClass() : this(int.MinValue, string.Empty) {}

public BaseClass(int id, string name)
{
_pd = new PureData(id, name);
}

protected string Name
{
get
{
return _pd.Name;
}
set
{
_pd.Name = value;
}
}

protected int ID
{
get
{
return _pd.ID;
}
set
{
_pd.ID = value;
}
}
}

public struct PureData
{
private int _id;
private string _name;

public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

public int ID
{
get
{
return _id;
}
set
{
_id = value;
}
}

public PureData(int id, string name)
{
_id = id;
_name = name;
}
}
}

.



Relevant Pages

  • Re: struct ToString() not automatically invoked
    ... Because a method with the same signature is defined in a base class ... Ok, so, structs DO derive from object. ... Strange. ...
    (microsoft.public.dotnet.languages.csharp)
  • private field is never assigned ; force comiler to stop complaining?
    ... know if there is a more relevant group. ... I have a series of private members in my class, ... reflection in a base class. ... basis for supressing or eliminating the compiler warning. ...
    (microsoft.public.dotnet.general)
  • Re: Having problem getting private members using reflection.
    ... Private members from the concrete class or the base class? ... there's one last bit of detail where private base class members are inaccessible via reflection on the derived type. ... I did some digging and the MSDN documentation states that the caller must have ReflectionPermission in order to get the private members of a class. ... Could someone please clarify on what I need to do in order for my code to be able to parse private members of my Page using reflection? ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: private instance variable of derived class not always returned by base class getter?
    ... combination of private instance variables than can be accessed through ... getters declared in the base class. ... Private members can only be seen inside the ... The getters and setters of the base-class can only ...
    (comp.lang.java.help)
  • RE: When should we use the single inheritance v.s private members
    ... In case of single inheritance u can call functions of base class ... I am sending u the code so u can understand the logic when use single inheritance or when use private members as containers.. ... int main(int argc, char* argv[]) ...
    (microsoft.public.dotnet.languages.vc)