Question

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

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top