K
K
KPIshnik2014-11-01 23:21:03
.NET
KPIshnik, 2014-11-01 23:21:03

Collection of generic plugins. How to?

I have the following task:
there is a certain class

abstract class Plugin<T>
    {
        public abstract T Modify(T param);
    }

You need to write a class that stores a collection of plugins and is itself a plugin. The modify method must execute all collection plugins to param.
Please help with ideas on how to do this.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aush, 2014-11-02
@aush

Composer (design pattern)
www.dofactory.com/net/composite-design-pattern

I
Ilya Glebov, 2014-11-03
@IljaGlebov

sealed class CompoundPlugin<T> : Plugin<T> {
  private readonly IReadOnlyList<Plugin<T>> plugins;

  public CompoundPlugin(IReadOnlyList<Plugin<T>> plugins) {
    Contract.Requires<ArgumentNullException>(plugins != null, "arg");
    Contract.Requires<ArgumentException>(plugins.Count > 0);

      this.plugins = plugins;
  }

  public override T Modify(T param) {
    return plugins.Aggregate(param, (arg1, plugin) => plugin.Modify(arg1));
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question