Click here to Skip to main content
14,300,872 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have two classes A & B and both class implements the interface ISomeInterface. But some properties are not required for both class A & B. But in the client app I am calling the same ISomeInterface to invoke both the classes. The problem which I have is I don't want Dictionary<string,> & TypedDataSet, IList<record> properties in the same interface. But the clients needs to use this IsomeInterface.

Actually DataValues() property is only applicable to class A. Similarly MetaData() and RecordCollection() properties are applicable to class B. Also, if I introduce a new class C in future and that needs a separate property like this then my code will look ugly which I don't want. So, Is there any way I can still use the same IsomeInterface in my client app and have the appropriate properties in the corresponding classes? I think I need to use Strategy Design pattern but got confused on how to implement the same. correct me if I am wrong?

See below:

interface ISomeInterface
{
 string Id{get; set;}
 void Display();
 Dictionary<string, string> DataValues{get;};
 TypedDataSet MetaData{get; }
 IList<Record> RecordCollection{get; }
}

public class A: ISomeInterface
{
public string Id
{
        return "A1";
}

void Display()
{
    Console.Writeline("class A");
}

public Dictionary<string, string> DataValues()
{
    return new Dictionary<string, string>();
}


public TypedDataSet MetaData()
{
    //I dont want this method for class A
    throw new NotImplementedException();
}

public IList<Record> RecordCollection()
{
    //I dont want this method for class A
    throw new NotImplementedException();
}
}


public class B: ISomeInterface
{
public string Id
{
        return "B1";
}

void Display()
{
    Console.Writeline("class B");
}

public Dictionary<string, string> DataValues()
{
    //I dont want this method for class B
    throw new NotImplementedException();
}

public TypedDataSet MetaData()
{
    return new TypedDataSet();
}

public IList<Record> RecordCollection()
{
    IList<Record> rc = null;

    //do something
    return rc;
}
 }


Client App -

Main()
{
ISomeInterface a = new A();
a.Display();
Dictionary<string, string> data = a.DataValues();

ISomeInterface b = new B();
b.Display();
TypedDataSet data = b.MetaData();
IList<Record> rc = b.RecordCollection();
}
Posted
Updated 29-Sep-11 17:07pm
v3

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You are going in the right direction.
Even though it's not the ideal scenario\example which can be called as an implementation of strategy pattern but based on the limitation on the usage of the interface ISomeInterface, this would do the job without messing up the code.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100