File tree Expand file tree Collapse file tree 11 files changed +148
-0
lines changed
Expand file tree Collapse file tree 11 files changed +148
-0
lines changed Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .ChainOfResponsibility ;
2+
3+ public class Main {
4+ public static void main (String [] args ) {
5+ //configure Chain of Responsibility
6+ Receiver c1 = new NegativeProcessor ();
7+ Receiver c2 = new ZeroProcessor ();
8+ Receiver c3 = new PositiveProcessor ();
9+ c1 .setNext (c2 );
10+ c2 .setNext (c3 );
11+ //calling chain of responsibility
12+ c1 .process (99 ); // -> "PositiveProcessor : 99"
13+ c1 .process (-30 ); // -> "NegativeProcessor : -30"
14+ c1 .process (0 ); // -> "ZeroProcessor : 0"
15+ c1 .process (100 ); // -> "PositiveProcessor : 100"
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .ChainOfResponsibility ;
2+
3+ public class NegativeProcessor implements Receiver {
4+ private Receiver nextInChain ;
5+ public void setNext (Receiver c ) {
6+ nextInChain = c ;
7+ }
8+ public void process (int request ) {
9+ if (request < 0 ) {
10+ System .out .println ("NegativeProcessor : " + request );
11+ }
12+ else { nextInChain .process (request ); }
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .ChainOfResponsibility ;
2+
3+ public class PositiveProcessor implements Receiver {
4+ private Receiver nextInChain ;
5+ public void setNext (Receiver c ) {
6+ nextInChain = c ;
7+ }
8+ public void process (int request ) {
9+ if (request > 0 ) {
10+ System .out .println ("PositiveProcessor : " + request );
11+ }
12+ else { nextInChain .process (request ); }
13+ }
14+ }
15+
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .ChainOfResponsibility ;
2+
3+ public interface Receiver {
4+ void setNext (Receiver nextInChain );
5+ void process (int request );
6+ }
7+
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .ChainOfResponsibility ;
2+
3+ public class ZeroProcessor implements Receiver {
4+ private Receiver nextInChain ;
5+ public void setNext (Receiver c ) {
6+ nextInChain = c ;
7+ }
8+ public void process (int request ) {
9+ if (request == 0 ) {
10+ System .out .println ("ZeroProcessor : " + request );
11+ }
12+ else { nextInChain .process (request ); }
13+ }
14+ }
15+
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .Mediator ;
2+
3+ import java .util .ArrayList ;
4+
5+ public class ApplicationMediator implements Mediator {
6+ private ArrayList <Colleague > colleagues ;
7+ public ApplicationMediator () {
8+ colleagues = new ArrayList <Colleague >();
9+ }
10+ public void addColleague (Colleague colleague ) {
11+ colleagues .add (colleague );
12+ }
13+ public void send (String message , Colleague originator ) {
14+ //let all other screens know that this screen has changed
15+ for (Colleague colleague : colleagues ) {
16+ //don't tell ourselves
17+ if (colleague != originator ) {
18+ colleague .receive (message );}
19+ }
20+ }
21+ }
22+
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .Mediator ;
2+
3+ public abstract class Colleague {
4+ private Mediator mediator ;
5+ public Colleague (Mediator m ) {
6+ mediator = m ;
7+ }
8+ //send a message via the mediator
9+ public void send (String message ) {
10+ mediator .send (message , this );
11+ }
12+ //get access to the mediator
13+ public Mediator getMediator () {return mediator ;}
14+ public abstract void receive (String message );
15+ }
16+
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .Mediator ;
2+
3+ public class ConcreteColleague extends Colleague {
4+ public ConcreteColleague (Mediator m ) {
5+ super (m );
6+ }
7+
8+ public void receive (String message ) {
9+ System .out .println ("Colleague Received: " + message );
10+ }
11+ }
12+
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .Mediator ;
2+
3+ public class Main {
4+ public static void main (String [] args ) {
5+ ApplicationMediator mediator = new ApplicationMediator ();
6+ Colleague desktop = new ConcreteColleague (mediator );
7+ Colleague mobile = new MobileColleague (mediator );
8+ mediator .addColleague (desktop );
9+ mediator .addColleague (mobile );
10+ desktop .send ("Hello World" );
11+ mobile .send ("Hello" );
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .Mediator ;
2+
3+ public interface Mediator {
4+ void send (String message , Colleague colleague );
5+ }
You can’t perform that action at this time.
0 commit comments