A software-based interface that combines some instances of the same interface in various ways

What is the best way to implement an interface that combines some instances of the same interface in different ways? I need to do this for multiple interfaces, and I want to minimize the pattern and still achieve good performance, because I need it for a critical production system.

Here is a sketch of the problem.

So I have a generic combiner class that accepts instances and defines various combinators:

class Combiner<I> {
   I[] instances;

   <T> T combineSomeWay(InstanceMethod<I,T> method) {
     // ... method.call(instances[i]) ... combined in some way ...
   }

   // more combinators
}

Now, let's say I want to implement the following interface among many others:

Interface Foo {
  String bar(int baz);
}

I want to finish the code as follows:

class FooCombiner implements Foo {
  Combiner<Foo> combiner;

  @Override 
  public String bar(final int baz) {
    return combiner.combineSomeWay(new InstanceMethod<Foo, String> {
      @Override public call(Foo instance) { return instance.bar(baz); } 
    });
  }
}

, . , API Java , . , ?

+3
2

- - , . , , . ( 100 , , ? , - !)

: Command . .

, , combiner cglib, javassist, orther -. .

, aspectJ , . , .

+2

:

@Override
public String bar(int baz)
{
    //for (Foo f:combiner.combineSomeWay())// returns Iterator<Foo>
    for (Foo f:combiner) //combiner must implement Iterable<Foo> or Iterator<Foo>
    {
        // In case of several ways to combine
        // add() method should call some temp object
        // in combiner created (or installed) by 
        // combineSomeWay.
        // The best temp object to use is Iterator
        // returned by combiner.combineSomeWay();
        combiner.add(f.bar(baz));// or addResult, or addCallResult
    }
    // clear (or uninstall) temp object and result
    // thats why get* method 
    // name here is bad.
    return combiner.done(); 
}

, . , . try/catch addException.

+1

Source: https://habr.com/ru/post/1746645/


All Articles