mardi 4 juillet 2017

Is this some kind of wrapper pattern?

Is this some kind of wrapper pattern? Does this pattern exist and do you think it has sense? The idea is that you make generic wrapper class, which implements generic wrapping methods and than use it via interface in wrappable class (the class that uses IWrappable to wrap all it's methods in generic manner)

public interface IWrapper
{
    void Wrap(Action action);

    T Wrap<T>(Func<T> func);
}


public class SomeWrapper : IWrapper
{
    public void Wrap(Action action)
    {
        Console.WriteLine("Wrapped before");
        action();
        Console.WriteLine("Wrapped after");
    }

    public T Wrap<T>(Func<T> func)
    {
        Console.WriteLine("Wrapped before");
        var ret = func();
        Console.WriteLine("Wrapped after");
        return ret;
    }        
}



public class WrappableClass
{
    protected IWrapper _wrapper;

    public Aclass(IWrapper wrapper)
    {
        _wrapper = wrapper;
    }


    public void DoSomething()
    {
        _wrapper.Wrap(() => Console.WriteLine("Doing something"));
    }


    public int CalcSomething()
    {            
        return _wrapper.Wrap(() => 
        {
            Console.WriteLine("Calculating");
            return 5;
        });
    }



}

Aucun commentaire:

Enregistrer un commentaire