1

I want to execute a function but before that , I want to verify the password.

I choosed to use the Proxy Design Pattern.


in words :

create the Proxy class,

send the password ,

and tell him WHAT FUNCTION TO EXECUTE if password is ok.

the problem is that According to Proxy Pattern the whole internals should/better be private.

so - I cant see from outside the Method1/2...

and i dont want to make Method1/2 public ( and I dont want If's) . I want to use delegates.

So , how ( from outside) can I send him a valid Action param ? ( the ac param)

Should I crate to each function a public Action ?

enter image description here

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

1 Answers1

2

Normally the method that you want to execute should not be part of your proxy class. It's a method that the caller of this class is passing:

public class ProxyClass
{
    public ProxyClass(string password, Action ac)
    {
        if (password == "111")
        {
            ac();
        }
    }
}

and then you could pass an Action as second argument:

var proxy = new ProxyClass("111", () => 
{
    ... some code that you want to execute in case of success
});

or if you already have some method defined:

public class Foo
{
    public void Success() 
    {
        ... some code that you want to execute in case of success
    }
}

you could:

var foo = new Foo();
var proxy = new ProxyClass("111", foo.Success);

of if Success was a static method you don't need an instance of Foo:

var proxy = new ProxyClass("111", Foo.Success);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • So method1/2() should be visible to the CALLER of the Proxy ? – Royi Namir Feb 19 '12 at 11:55
  • @RoyiNamir, well, if you define them inside the proxy as you did, then yes, they should be visible so that the caller can decide which one to call. And if those methods are not part of your proxy class, then the caller could pass them from somewhere else. But of course, since it's the caller that needs to pass an Action as second argument to the constructor, this Action should be known to the caller. – Darin Dimitrov Feb 19 '12 at 11:56
  • Seems weird to me that a design pattern which its usages is access control - enforce me to put the methods as public / visible to everyone..... – Royi Namir Feb 19 '12 at 11:58