简体   繁体   中英

Interface implementation without revealing the class that implements the interface

Hi how do we implement interface in a real time scenario??

This is my situation

I have made an interface IPayPal which has 2 methods

void SaleTransaction();
void VoidTransaction();

now i have a class PayPal which implements this service.

class PayPal:IPayPal{

    public void SaleTransaction(){
    // Implementation happens here

    }

    public void VoidTransaction(){
    // Implementation happens here

    }



}

now i have a service that requests the services from PayPal

lets say

class Service{

IPayPal pp=null;


static void Main(){

    pp=new PayPal();
    //Now i do not want to expose all the methods in my class PayPal 
    // is there any other way to just show pp.SaleOneTransaction() method?? i donot want the //PayPal class to be present in this Program..

    //Please tell me how to acheive this.
    }

}

ie Please tell me a way in which i can initialise my interface class without revealing the class that implements the interface.

Thanks

Two options:

  • Don't expose public methods you don't want to be called from other assemblies, pretty simply. Don't expose even internal methods you don't want to be called from other classes in the assembly.

  • Create a wrapper which proxies all calls:

     public class PaymentProxy : IPayPal { private readonly IPayPal original; public PaymentProxy(IPayPal original) { this.original = original; } public void SaleTransaction() { original.SaleTransaction(); } public void VoidTransaction() { original.VoidTransaction(); } } 

    At this point, you can create a PaymentProxy with your original "secret" object, trust that not to leak the information about it, and hand the proxy to anything. Of course, this isn't secure against reflection etc - but it does hide the prevent the implementation details from being "accidentally" used in a quick and dirty, "Well I know it'll really be a PayPal , so let's just cast to that..." hack.

I would suggest:

  1. read about dependency injection and how it can help you resolve dependencies easily and in a loose coupled way.
  2. the interface name "IPayPal" is not very good name IMHO. It is very specific to one payment provider. Suppose tomorrow you want to implement another payment method that is not paypal, yet you want to use the same interface. I think the name should be generic like "IPaymentProvider", and the current implementation is PayPal (but no other class using that interface should care or know about this).

good luck!

You can separate 2 methods into 2 interfaces.

interface IPayPal1{
    void SaleTransaction();
}
interface IPayPal2{
    void VoidTransaction();
}

class PayPal:IPayPal1, IPayPal2{
    void SaleTransaction(){
        //
    }
    void VoidTransaction(){
        //
    }
}

class Service{
    IPayPal1 pp=null;

    static void Main(){
        pp=new PayPal(); //you cannot access VoidTransaction here
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM