-3

I want to use generics to be able to use a Proxy with any kind of RealClient.

public interface IClient
{
    string GetData();
}

public class RealClient : IClient
{
    string Data;
    public RealClient()
    {
        Console.WriteLine("Real Client: Initialized");
        Data = "success";
    }

    public string GetData()
    {
        return Data;
    }
}


public class Proxy : IClient
{
    RealClient _classobject = new RealClient();
    public Proxy()
    {
        Console.WriteLine("ProxyClient: Initialized");
    }

    public string GetData()
    {
        return _classobject.GetData();
    }
}


class Program
{
    static void Main(string[] args)
    {
        Proxy proxy = new Proxy();
        Console.WriteLine("Data from Proxy Client = {0}", proxy.GetData());

        Console.ReadKey();
    }
}
dcastro
  • 66,540
  • 21
  • 145
  • 155
Rahul Kumar
  • 528
  • 1
  • 3
  • 19
  • 3
    It is a bit unclear what the question is. – Davin Tryon Mar 06 '14 at 12:09
  • why negative.. i also post one question this is the secound part of this http://stackoverflow.com/questions/22223136/how-to-initiate-t-type-object-of-generic-class/22223202#22223202 – Rahul Kumar Mar 06 '14 at 12:11
  • 2
    This is very ambiguous, but according to your [previous question](http://stackoverflow.com/questions/22223136/how-to-initiate-t-type-object-of-generic-class/22223202?noredirect=1#comment33743948_22223202), I assume you want to turn `Proxy` into `Proxy` where `T` could be any class that derives from `RealClient`. Is that true? – dcastro Mar 06 '14 at 12:11
  • @RahulKumar Since you did not link or mention the other question, I had no idea that it existed. – Davin Tryon Mar 06 '14 at 12:11
  • 2
    Your question is being downvoted because it's not clear.. to be honest, I don't even see the point of using generics here. – dcastro Mar 06 '14 at 12:11
  • right dcastro please do modification and put the answer – Rahul Kumar Mar 06 '14 at 12:12
  • dcastro you are right but i confused how to do this in generic as i asked before in previous question – Rahul Kumar Mar 06 '14 at 12:14

1 Answers1

2

It seems you're trying to implement the Proxy design pattern, and you want your proxy to work with any kind of IClient.

If so, you don't need generics for this. This is what you'd normally do to implement a proxy:

public class Proxy : IClient
{
    private readonly IClient _client;
    public Proxy(IClient client)
    {
        _client = client;
        Console.WriteLine("ProxyClient: Initialized");
    }

    public string GetData()
    {
        return _client.GetData();
    }
}

Keep it simple.

dcastro
  • 66,540
  • 21
  • 145
  • 155
  • you are right but i want the in Generic , bcz the Realclient class are many around 10 so that's why i want it in generic. – Rahul Kumar Mar 06 '14 at 12:16
  • This code already works with *any* `RealClient`. If you have `class ReallyRealClient : RealClient`, this will still work. There's no need for generics, from what you're telling us. – dcastro Mar 06 '14 at 12:17
  • @RahulKumar If I'm misunderstanding you, please post some code that uses a `Proxy` *without* generics that shows us the problem you're having. The code doesn't have to compile, just show us what you're trying to do. – dcastro Mar 06 '14 at 12:20