Reputation: 45
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.
}
}
i.e Please tell me a way in which i can initialise my interface class without revealing the class that implements the interface.
Thanks
Upvotes: 2
Views: 162
Reputation: 2027
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
}
}
Upvotes: 0
Reputation: 1110
I would suggest:
good luck!
Upvotes: 2
Reputation: 1500055
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.
Upvotes: 2