A factory can provide the right juicers, but then your juicer processing logic will be pushed to your controller.
A combination of composite and visitor templates may be useful to you.
- The composite template allows you to create a MasterJuicer that knows about other juicers and can initiate a “juice process” by delegating a juicer. This refers to the “structural” aspect of your problem.
- The visitor template allows each juicer to have a single method of interacting with them without the caller (compound MasterJuicer) taking care of how they work together. This handles the "behavioral" aspect.
You can pass a list of ingredients for processing between each visitor so that they can interact with the fruits with which they are associated.
If you do not want to register your juicers with MasterJuicer manually, you may need the skill with some kind of service opening. A common technique in Java is to use annotations and class scans to find classes at runtime and automatically register them at startup. There are several libraries that do this, or if you are already using the Spring Framework, you can use the built-in scanning tools .
source share