java - How to create a composite that uses components from a provider where it should itself be added to? -
java - How to create a composite that uses components from a provider where it should itself be added to? -
the title maybe bit complicated didn't see other way of verbalizing problem.
i'm working on project components initialized loading several files , transforming them java components, others loaded serviceloader alreaddy existing java classes. of these objects mapped provider can retrieved there.
public class provider { private map<string, component> map; public provider() { /* * load files, transform, , add together them map * keys beingness names */ /* * load java components , add together them map * keys beingness names */ } public component getcomponent(string name) { homecoming this.map.get(name); } } public interface component { public number execute(); }
i add together more components straight in java more of composite meaning utilize components loaded provider. hence, must gather required components provider, add together them composite , add together composite provider.
public class composite implements component { private component component1, component2; public composite() { provider provider = new provider(); this.component1 = provider.getcomponent("comp1"); this.component2 = provider.getcomponent("comp2"); } public number execute() { homecoming this.component1.execute() + this.component2.execute(); } }
the composite can added provider adding specific meta-inf file. handled serviceloader.
for me, somehow cyclic dependency. have define, in meta-inf file, java classes should gathered serviceloader. include newly implemented composite well. 1 cannot created long other metrics have been loaded provider.
is there pattern or different approach can utilize solve problem?
more should have chain. instead of creating new instance of provider
, in composite
, should create interface allow called compositecomponent
, extend component
, have 1 more method init(provider provider)
. or may improve show in code:
public interface compositecomponent extends component { void init(provider provider); }
so , in provider
:
public component getcomponent(string name) { component component = this.map.get(name); if (component instanceof compositecomponent) { ((compositecomponent)component).init(this); } }
java design-patterns serviceloader
Comments
Post a Comment