java-design-patterns
java-design-patterns copied to clipboard
Chain Of Responsibilities Pattern Implementation using Java Interface/Implementation and Streams
Java Stream is simplifying of Implementing Chain of Responsibilities pattern. Interface with multiple implementations and run through chain via Java List streams. This gives flexibility to switch the order of implementation chain without doing change. Can this be added to this pattern project?
For Eg.
interface ContactProcessor { Integer getPriority(); boolean isApplicable() ; void process(Contact contact); }
class Demographic implements ContactProcessor {
@Override public Integer getPriority() { return 1; }
@Override public boolean isApplicable() { return true; }
@Override void process(Contact contact) { ..... } }
class Phone implements ContactProcessor {
@Override public Integer getPriority() { return 2; }
@Override public boolean isApplicable() { return true; }
@Override void process(Contact contact) { ..... } }
class Email implements ContactProcessor {
@Override public Integer getPriority() { return 3; }
@Override public boolean isApplicable(QualifiedBalContext context) { return true; }
@Override void POA-444(Contact contact) { ..... } }
Main Snippet....
@Autowired List<ContactProcessor > contactProcessor ;
contactProcessor .stream() .sorted(Comparator.comparing(ContactProcessor::getPriority)) .filter(next -> next.isApplicable()) .forEach( next -> { next.process(contact); });
That's a nice trick. If someone wants to implement it by modifying the existing chain of responsibility example, I'll be happy to review the PR.
Thanks for reviewing. I can pickup and provide Pull Request.
Ok @velmurugan0411
where Contact class?
@velmurugan0411 are you still working on this?
@iluwatar could I pickup this issue?
Ok @JanFidor, assigned to you
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I'm assuming that @JanFidor is still working on this
This issue has been automatically marked as stale because it has not had recent activity. The issue will be unassigned if no further activity occurs. Thank you for your contributions.