java-design-patterns icon indicating copy to clipboard operation
java-design-patterns copied to clipboard

Chain Of Responsibilities Pattern Implementation using Java Interface/Implementation and Streams

Open velmurugan0411 opened this issue 2 years ago • 10 comments

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); });

velmurugan0411 avatar Jan 13 '22 03:01 velmurugan0411

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.

iluwatar avatar Jan 25 '22 19:01 iluwatar

Thanks for reviewing. I can pickup and provide Pull Request.

velmurugan0411 avatar Jan 25 '22 23:01 velmurugan0411

Ok @velmurugan0411

iluwatar avatar Feb 06 '22 08:02 iluwatar

where Contact class?

TreeFireMen avatar Jun 20 '22 03:06 TreeFireMen

@velmurugan0411 are you still working on this?

iluwatar avatar Sep 21 '22 17:09 iluwatar

@iluwatar could I pickup this issue?

JanFidor avatar Oct 05 '22 18:10 JanFidor

Ok @JanFidor, assigned to you

iluwatar avatar Oct 06 '22 05:10 iluwatar

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.

stale[bot] avatar Nov 28 '22 12:11 stale[bot]

I'm assuming that @JanFidor is still working on this

iluwatar avatar Dec 03 '22 16:12 iluwatar

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.

stale[bot] avatar Feb 01 '23 20:02 stale[bot]