Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to come up with a solution for below problem and not sure how to approach it without making client depended on concrete classes. Problem statement is as below

"I have a list of a product say A,B,C,D. A has 3 property named x,y and z and 1 function F1. product B has 4 property named x,y,z and p and 2 function named F1 and F2 and so on for product C also has 4 property named x,y,z and p and 2 function named F1 and F3. and product D has has 4 property named x,y,z and p and 2 function named F1 and F4.



Now because of this i can not have a common interface or abstract classes because class B have one additional function and C and D have one function which is different than B and which is common.


Is there any possible solution using which i can avoid this ? Any suggestion would be highly appreciated.

What I have tried:

I thought of Having conman code in one abstract class and than creating concrete class with additional function which is contain the abstract class (as a dependency). But in that case my client will be forced to be depended on concrete class.
Posted
Updated 2-Jul-17 8:06am

If I'm reading the pattern in the classes correctly, wouldn't something as simple as this do?
Or you could even pub p into a separate interface & inherit it in B,C,D...
C#
public interface IA
{
    int x { get; set; }
    int y { get; set; }
    int z { get; set; }
    int F1();
}

public interface IB
{
    int p { get; set; }
    int F2();
}

public interface IC
{
    int p { get; set; }
    int F3();
}

public interface ID
{
    int p { get; set; }
    int F4();
}

public class A : IA
{
    public int x { get; set; }
    public int y { get; set; }
    public int z { get; set; }

    public int F1()
    {
        return 0;
    }
}
public class B : IA, IB
{
    public int x { get; set; }
    public int y { get; set; }
    public int z { get; set; }
    public int p { get; set; }

    public int F1()
    {
        return 0;
    }
    public int F2()
    {
        return 0;
    }
}

public class C : IA, IC
{
    public int x { get; set; }
    public int y { get; set; }
    public int z { get; set; }
    public int p { get; set; }

    public int F1()
    {
        return 0;
    }
    public int F3()
    {
        return 0;
    }
}

public class D : IA, ID
{
    public int x { get; set; }
    public int y { get; set; }
    public int z { get; set; }
    public int p { get; set; }

    public int F1()
    {
        return 0;
    }
    public int F4()
    {
        return 0;
    }
}
 
Share this answer
 
Comments
CS2011 2-Jul-17 13:02pm    
how would you create the instance in this case? assume you need to create an instance of D. what would you use ? if your client uses IA than F4 will not be available and in case of ID F1,F2 and F3 will not be available.
pt1401 2-Jul-17 16:37pm    
Depends on whether you need to inject it.
If not, this would work (and has the advantage of being mind-numbingly simple).
Sometimes the simple way is best.

var myD = new D();
CS2011 5-Jul-17 1:28am    
Well i wanted to avoid using the concrete class when making the object and this will defeat the whole purpose of asking the question.
pt1401 5-Jul-17 3:33am    
If you really need to create from an interface, perhaps because you want to inject it, you could create composite interfaces, then create a D using interface ID

public interface ID : IA
{
int p { get; set; }
int F4();

}
public class D : ID
{
}
Maybe you can use a Dictionary similar to that in the answer to this StackOverflow question: reflection - C# Using Activator.CreateInstance - Stack Overflow[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900