Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mediator design pattern #16

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Mediator Design Pattern class diagram.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Mediator pattern sequence diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
# DesignPatternsJava9
This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns.
# What is Mediator Design Pattern
The Mediator design pattern enables the centralised communication between related objects (Colleagues). All communication is handled by a Mediator Object and the Colleagues don't need to know anything about each other to work together.

## Diagram
![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/mediator-pattern/diagrams/Mediator%20Design%20Pattern%20communication%20diagram.png "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/mediator-pattern/diagrams/Mediator-Design-Pattern-general.png "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/mediator-pattern/diagrams/Mediator%20pattern%20sequence%20diagram.png "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/mediator-pattern/diagrams/aircraft.jpg "Diagram")

# When to use Mediator Design Pattern
* when the communication logic between objects is complex, we can have a central point of communication using Mediator object.
* JMS- Java Message Service uses mediator pattern underneath.

### Learn Design Patterns with Java by Aseem Jain
This repository contains working project code used in video Course by Packt Publication with title "Learn Design Patterns with Java " authored by "Aseem Jain".

### Course link:
https://www.packtpub.com/application-development/learn-design-patterns-java-9-video

### ![ http://in.linkedin.com/in/premaseem](https://github.com/premaseem/DesignPatternsJava9/blob/master/linkedin.png "http://in.linkedin.com/in/premaseem") Profile: http://in.linkedin.com/in/premaseem

### Authors blog on design patterns:
https://premaseem.wordpress.com/category/computers/design-patterns/

### Software Design pattern community face book page:
https://www.facebook.com/DesignPatternGuru/

### Note:
* This code base will work on Java 9 and above versions.
* `diagrams` folders carry UML diagrams.
* `pattern` folder has code of primary example.
* `patternBonus` folder has code of secondary or bonus example.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/Mediator pattern sequence diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/Mediator-Design-Pattern-general.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/aircraft.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 0 additions & 13 deletions pattern/src/com/premaseem/Client.java

This file was deleted.

45 changes: 45 additions & 0 deletions pattern/src/com/premaseem/atc/AtcMediator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.premaseem.atc;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/

public class AtcMediator {

Flight approachingFligh;

enum FlightStatus {
IN_Q, IN_APPROACH, LANDED
}

public void grantLandingPermission(Flight flight) {
if (approachingFligh == null) {
approachingFligh = flight;
flight.status = FlightStatus.IN_APPROACH;
System.out.printf(" Landing Permission Granted to %s , START APPROACHING .... ", flight.name);

} else {
if (!approachingFligh.name.equalsIgnoreCase(flight.name)) {
System.out.printf(" Landing Permission Not Granted to %s as other filght (%s) has blocked the run way", flight.name, approachingFligh.name);
} else {
System.out.printf(" %S you already have the permission - ", flight.name);
}
}
}

void landFlight(Flight flight) {
if (flight.status.equals(FlightStatus.IN_APPROACH)) {
System.out.printf("Congratulation for safe Landing %s . . . ", flight.name);
flight.status = FlightStatus.LANDED;
approachingFligh = null;
} else if (flight.status.equals(FlightStatus.IN_Q)) {
System.out.printf(" Grand permission before landing - WARNING to %s ", flight.name);
} else {
System.out.printf("Flight %s is already grounded ", flight.name);
}

}

}
113 changes: 113 additions & 0 deletions pattern/src/com/premaseem/atc/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/2014/10/04/mediator-design-pattern/
*/

package com.premaseem.atc;

import java.util.Scanner;

public class Client {

public static void main(String[] args) {

System.out.println("Welcome to Air Traffic Control program which uses Mediator design based architecture");
Scanner scan = new Scanner(System.in);

// Object initialization block
AtcMediator atcMediator = new AtcMediator();
Flight inOperationFlight;
Flight flight1 = new Flight(atcMediator, "Air India ");
Flight flight2 = new Flight(atcMediator, "American Air Lines ");
Flight flight3 = new Flight(atcMediator, "British Air ways ");

// User input block
String repeatRunFlag = "yes";
while (!repeatRunFlag.equalsIgnoreCase("no")) {
System.out.println("Which flight do you want to operate ? ");
System.out.println("press 1 for Air India");
System.out.println("press 2 for American Air Lines ");
System.out.println("press 3 for British Air ways ");

int choice = scan.nextInt();
if (choice == 1) {
inOperationFlight = flight1;
} else if (choice == 2) {
inOperationFlight = flight2;
} else {
inOperationFlight = flight3;
}

// Flight should not land directly before taking grant from ATC
// Decision making logic / algorithm resides in Mediator class
System.out.println("What do you want to do with the flight " + inOperationFlight.name);
System.out.println(" Press 1 to Grant landing permission ");
System.out.println(" Press 2 to Land the Flight ");

int type = scan.nextInt();
try {
switch (type) {
case 1:
inOperationFlight.grantLandingPermission();
break;
case 2:
inOperationFlight.landFlight();
break;

}
} catch (Exception e1) {
System.out.println("################ Access is not valid ###############");
}
System.out.println("\n=============================");

System.out.println("Press No to Exit and any other character to Continue flight operations .... ");
try {
repeatRunFlag = scan.next();
} catch (Exception e) {
repeatRunFlag = "no";
}

}

// Lessons Learnt
// Classes are loosely coupled with Client
// Core decision making logic is centralized in Mediator class
// Decision making process is fast and effective

/*** BEFORE CODE ***
System.out.println("Welcome to Air Traffic Control program ");

// Create flight objects
Flight_ flight1 = new Flight_();
Flight_ flight2 = new Flight_();
Flight_ flight3 = new Flight_();

// Set objects status and communicate to other classes
flight1.setFlightStatus(Flight_.FlightStatus.IN_APPROACH);
flight1.coordianteWithOtherFlight(flight2);
flight1.coordianteWithOtherFlight(flight3);

flight2.setFlightStatus(Flight_.FlightStatus.IN_Q);
flight2.coordianteWithOtherFlight(flight1);
flight2.coordianteWithOtherFlight(flight3);

flight3.setFlightStatus(Flight_.FlightStatus.IN_APPROACH);
flight3.coordianteWithOtherFlight(flight1);
flight3.coordianteWithOtherFlight(flight2);

System.out.println("Current Fight informs other flights of its current position /status " );
System.out.println("In case of clashes, one on one communication might delay decisions and cause accidents ");

System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
System.out.println("\n $$$$$$$$$$$$$$$$$$$$$ www.premaseem.me $$$$$$$$$$$$$$$$$$$$$$ \n ");


// $$$ Lesson Learnt - BEFORE CODE $$$
// Tight coupling, Client needs to know about each flight.
// When number of objects are more, code becomes complex and confusing
// Communication between classes is not effective,
*/

}
}
30 changes: 30 additions & 0 deletions pattern/src/com/premaseem/atc/Flight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.premaseem.atc;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/

import com.premaseem.atc.AtcMediator.FlightStatus;

public class Flight {

FlightStatus status;
AtcMediator atcMediator=null;
public String name;

public Flight(AtcMediator atcMediator, String flightName) {
status = FlightStatus.IN_Q;
name = flightName;
this.atcMediator = atcMediator;
}

void grantLandingPermission(){
atcMediator.grantLandingPermission(this);
}

void landFlight(){
atcMediator.landFlight(this);
}
}
26 changes: 26 additions & 0 deletions pattern/src/com/premaseem/atc/Flight_.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.premaseem.atc;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/


public class Flight_ {



enum FlightStatus {
IN_Q, IN_APPROACH, LANDED
}
FlightStatus flightStatus;

public void setFlightStatus(FlightStatus fs){
flightStatus = fs;
}

public void coordianteWithOtherFlight(Flight_ flight_){
}

}
13 changes: 0 additions & 13 deletions patternBonus/src/com/premaseem/Client.java

This file was deleted.

53 changes: 53 additions & 0 deletions patternBonus/src/com/premaseem/lightSignal/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.premaseem.lightSignal;

import java.util.Scanner;

public class Client {

public static void main(String[] args) {

System.out.println("Welcome to Traffic light signal program which uses Mediator ");
System.out.println(" Out of 3 button only 1 could be ON and others has to in OFF state ");
Scanner scan = new Scanner(System.in);

// Object initialization block
LightMediator LightMediator = new LightMediator();
Light red = new RedLight(LightMediator);
Light yellow = new YellowLight(LightMediator);
Light green = new GreenLight(LightMediator);


// User input block
String repeatRunFlag = "yes";
while (!repeatRunFlag.equalsIgnoreCase("no")) {
System.out.println("Which light you want to turn on ? ");
System.out.println("press 1 for RED");
System.out.println("press 2 for YELLOW ");
System.out.println("press 2 for GREEN ");

int choice = scan.nextInt();
if (choice == 1) {
red.turnON();
} else if(choice == 2) {
yellow.turnON();
} else{
green.turnON();
}

System.out.println("\n=============================");

System.out.println("Press No to Exit and any other character to repeat .... ");
try {
repeatRunFlag = scan.next();
} catch (Exception e) {
repeatRunFlag = "no";
}

}

System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
System.out.println("\n $$$$$$$$$$$$$$$$$$$$$ www.premaseem.me $$$$$$$$$$$$$$$$$$$$$$ \n ");

}
}

54 changes: 54 additions & 0 deletions patternBonus/src/com/premaseem/lightSignal/Light.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.premaseem.lightSignal;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/

public abstract class Light {
Light(LightMediator LightMediator){
this.LightMediator = LightMediator;
LightMediator.registerLight(this);
}
enum state {
ON, OFF
}

state currentState;
LightMediator LightMediator;
int id;

void turnON() {
currentState = state.ON;
System.out.printf("%s is turned %s \n", this.getClass().getSimpleName(), currentState.ON);
LightMediator.turnON(this);
}

void turnOFF() {
currentState = state.OFF;
System.out.printf("%s is turned %s \n", this.getClass().getSimpleName(), currentState.OFF);
}

public void addId(int index) {
id = index;
}
}

class RedLight extends Light {
public RedLight(LightMediator LightMediator) {
super(LightMediator);
}
}

class YellowLight extends Light {
public YellowLight(LightMediator LightMediator) {
super(LightMediator);
}
}

class GreenLight extends Light {
public GreenLight(LightMediator LightMediator) {
super(LightMediator);
}
}
Loading