Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can parallel inheritance be avoided?

Status
Not open for further replies.

Audriusa83

Programmer
Jul 5, 2010
4
GB
I have to develop a recording system. Say, there are multiple records instructing what to check and then checks are performed over the time.
Now in my case records can be two types - Value record (with min and max values specified and check contains the actual value) and Conform record (meaning that check meets the criteria). I was thinking to make abstract record and then inherit Value and Conform records. Then I should have abstract check and inherited Value and Conform checks.

abstract class Record
{
public string Task { get; set; }
public List<Check> Checks { get; set; }
}

class ValueRecord : Record
{
public float MinValue { get; set; }
public float MaxValue { get; set; }
public void AddCheck(ValueCheck item) {
base.Checks.Add(item);
}
}

class ConfromRecord : Record
{
public void AddCheck(ConformCheck item) {
base.Checks.Add(item);
}
}

abstract class Check
{
public DateTime CreatedOn { get; set; }
}

class ValueCheck : Check
{
public float Value { get; set; }
}

class ConformCheck : Check
{
public bool IsConform { get; set; }
}


But this means I have parallel inheritance and if I'll have to change anything, I must change in two places. Is there a better, more loosely way to do it?
 
When you 'check conformance' does it use the values in the ValueRecord? Should you have composition or possibly the Template pattern here?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks Steve, I looked up the composite and I think it should fit here well. Something like this perhaps:

class Record
{
public void Record(IRecordType type)
{
this.Type = type;
}
public string Task { get; set; }
public List<Check> Checks { get; set; }
public IRecordType Type { get; private set; }
}

interface IRecordType
{
Check CreateCheck(Record record);
}

class ValueRecord : IRecordType
{
public float MinValue { get; set; }
public float MaxValue { get; set; }
Check CreateCheck(Record record)
{
ValueCheck check=new ValueCheck();
record.Checks.Add(check);
return check;
}
}

class ConfromRecord : IRecordType
{
Check CreateCheck(Record record)
{
ConformCheck check = new ConformCheck();
record.Checks.Add(check);
return check;
}
}

abstract class Check
{
public DateTime CreatedOn { get; set; }
}

class ValueCheck : Check
{
public float Value { get; set; }
}

class ConformCheck : Check
{
public bool IsConform { get; set; }
}


Does it look OK? I'm still trying to nail OOP so feedback would be appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top