All Downloads are FREE. Search and download functionalities are using the official Maven repository.

poem.boundary.Boundary Maven / Gradle / Ivy

There is a newer version: 0.6.6
Show newest version
package poem.boundary;

import java.util.function.Consumer;

import org.requirementsascode.Model;
import org.requirementsascode.ModelRunner;

import poem.boundary.driven_port.IObtainPoems;
import poem.boundary.driver_port.IReactToCommands;
import poem.boundary.internal.command_handler.PickRandomPoem;

/**
 * The boundary class is the only point of communication with left-side driver
 * adapters. It accepts commands, and calls the appropriate command handler.
 * 
 * On creation, this class wires up the dependencies between command types and
 * command handlers, by injecting the command handlers into a use case model.
 * 
 * After creation, this class sends each command it receives to the runner of
 * the use case model. The model runner then dispatches the command to the
 * appropriate command handler, which in turn calls the driven adapters.
 * 
 * @author b_muth
 *
 */
public class Boundary implements IReactToCommands {

	private ModelRunner modelRunner;

	public Boundary(IObtainPoems poemObtainer, Consumer eventPublisher) {
		Model model = buildModel(poemObtainer);
		modelRunner = new ModelRunner().publishWith(eventPublisher);
		modelRunner.run(model);
	}

	private Model buildModel(IObtainPoems poemObtainer) {
		// Create the command handler(s)
		PickRandomPoem pickRandomPoem = new PickRandomPoem(poemObtainer);

		// Inject command handler(s) into use case model, to tie them to command
		// types.
		Model model = UseCaseModel.build(pickRandomPoem);
		return model;
	}

	@Override
	public void reactTo(Object commandObject) {
		modelRunner.reactTo(commandObject);
	}
}