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

Visitor pattern #11

Open
wants to merge 7 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
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# 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 Visitor Design Pattern
Visitor design pattern uses a visitor class which changes the executing algorithm of an element class. By this way, execution algorithm of element can vary as and when visitor varies.

## Diagram
![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/visitor-pattern/diagrams/Visitor-Pattern-class-diagram%20.png "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/visitor-pattern/diagrams/visitorPattern-generic.jpeg "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/visitor-pattern/diagrams/VisitorSequenceDiagram.png "Diagram")

### When to use Visitor Design Pattern
Visitor pattern is used when we have to perform an operation on a group of similar kind of Objects. With the help of visitor pattern, we can move the operational logic from the objects(Elements) to another class (Visitor class).

### 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.
Binary file added diagrams/Visitor-Pattern-class-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/VisitorSequenceDiagram.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/visiting faculty.JPG
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/visitorPattern-generic.jpeg
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.

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

import java.util.Scanner;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class ClientStudent {
public static void main (String[] args) {
System.out.println();
System.out.println("University Lecture application designed using Visitor design pattern \n");
System.out.println("Subject available for lectures are:-");
System.out.println("Maths");
System.out.println("Computers");
System.out.println("Aviation");
System.out.println();
System.out.println("Please pick subject ...");

Scanner scanner = new Scanner(System.in);
String subjectOption = scanner.next();
if (subjectOption.equalsIgnoreCase("Maths")){
System.out.println("Math can be taught by in house Faculty");
Subject subject = new Maths();
subject.teach();
}

if (subjectOption.equalsIgnoreCase("Computers")){
System.out.println("Computers needs visiting Faculty, lets call Prof. Prem Aseem Jain :-)");
Subject subject = new Computer();
VisitingFaculty profAseem = new ProfAseem();
subject.acceptVisitingFacultytoTeach(profAseem);
}

if (subjectOption.equalsIgnoreCase("Aviation")){
System.out.println("Aviation needs visiting Faculty, lets call Prof. Prem Aseem Jain :-)");
Subject subject = new Aviation();
VisitingFaculty profAseem = new ProfAseem();
subject.acceptVisitingFacultytoTeach(profAseem);
}

System.out.println("Class over - Go Home and have fun :-) ");

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

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

abstract void acceptVisitingFacultytoTeach (VisitingFaculty visitingFaculty);

void teach(){
System.out.println("IN-HOUSE Faculty teaching "+ this.getClass().getSimpleName()+" subject");
}
}

class Aviation extends Subject{

@Override
void acceptVisitingFacultytoTeach (VisitingFaculty visitingFaculty) {
visitingFaculty.teach(this);
}
}

class Computer extends Subject{

@Override
void acceptVisitingFacultytoTeach (VisitingFaculty visitingFaculty) {
visitingFaculty.teach(this);
}
}

class Maths extends Subject{

@Override
void acceptVisitingFacultytoTeach (VisitingFaculty visitingFaculty) {
visitingFaculty.teach(this);
}
}
49 changes: 49 additions & 0 deletions pattern/src/com/premaseem/VisitingFaculty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.premaseem;

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

public interface VisitingFaculty {
void teach (Aviation aviation);
void teach (Computer computer);
void teach (Maths maths);
}

class ProfAseem implements VisitingFaculty{

@Override
public void teach (Aviation aviation) {
System.out.println("Visiting Faculty teaching Aviation subject");
}

@Override
public void teach (Computer computer) {
System.out.println("Visiting Faculty teaching Computer subject");
}

@Override
public void teach (Maths maths) {
System.out.println("Visiting Faculty teaching Maths subject");
}
}

class AnyOtherProfessor implements VisitingFaculty{

@Override
public void teach (Aviation aviation) {
System.out.println("Visiting Faculty teaching Aviation subject");
}

@Override
public void teach (Computer computer) {
System.out.println("Visiting Faculty teaching Computer subject");
}

@Override
public void teach (Maths maths) {
System.out.println("Visiting Faculty teaching Maths subject");
}
}
13 changes: 0 additions & 13 deletions patternBonus/src/com/premaseem/Client.java

This file was deleted.

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

import java.util.Scanner;

public class ClientFile {

public static void main(String[] args) {

System.out.println("Welcome to Party host example which uses visitor pattern ");
Scanner scan = new Scanner(System.in);
Party party;
CookVisitorI visitorCook;
int repeatRunFlag = 1;
while (repeatRunFlag == 1) {
System.out.println("Which party do you want to host ");
System.out.println("press 1 for Loud party ");
System.out.println("press 2 for Silent party ");
int tvType = scan.nextInt();
if (tvType == 1) {
party = new LoudParty();
} else {
party = new CalmParty();
}

System.out.println("How would you want to manage cooking of food ");
System.out.println(" Press 1 for a visitor Veg Cook ");
System.out.println(" Press 2 for a visitor Non- Veg Cook ");
System.out.println(" Press 3 for in house cooking (no visitor) ");

int type = scan.nextInt();
try {
switch (type) {
case 1:
visitorCook = new VegCookVisitor();
party.accept(visitorCook);
break;
case 2:
visitorCook = new NonVegCookVisitor();
party.accept(visitorCook);
break;
case 3:
party.cookInHouse();
break;
}


} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("=============================");
System.out.println("Press 1 to Repeat .... ");
try {
repeatRunFlag = scan.nextInt();
} catch (Exception e) {
repeatRunFlag = 0;
}

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

public interface CookVisitorI {
void cook (LoudParty loudParty);
void cook (CalmParty calmParty);
}

class VegCookVisitor implements CookVisitorI{

@Override
public void cook(LoudParty loudParty) {
loudParty.meal = "Spicy Vegetables";
loudParty.drink = "Fruit Beer";
loudParty.music = "Loud music";
loudParty.getPartyDetail();
}

@Override
public void cook(CalmParty calmParty) {
calmParty.meal = "Boiled Vegetables";
calmParty.drink = "Fruit Juice";
calmParty.music = "Meditation Music";
calmParty.getPartyDetail();
}
}

class NonVegCookVisitor implements CookVisitorI{

@Override
public void cook(LoudParty loudParty) {
loudParty.meal = "Spicy chicken";
loudParty.drink = "Beer";
loudParty.music = "High Beat music";
loudParty.getPartyDetail();
}

@Override
public void cook(CalmParty calmParty) {
calmParty.meal = "Non spicy chicken";
calmParty.drink = "Fruit Beer";
calmParty.music = "Meditation music";
calmParty.getPartyDetail();
}
}
46 changes: 46 additions & 0 deletions patternBonus/src/com/premaseem/Party.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.premaseem;

public abstract class Party {

String music = " No Music";
String meal = " empty";
String drink = "plane water ";
CookVisitorI visitor = null;

abstract void accept(CookVisitorI visitor);


String getPartyDetail() {
String detials = visitor!= null ?visitor.getClass().getSimpleName() : "in house cooking" + " organized " + this.getClass().getSimpleName() + " has Music : " + music + " with Drink :"
+ drink + " & Meal : " + meal;
System.out.println(detials);
return detials;
}

public void cookInHouse() {
meal = "Spicy Vegetables";
drink = "in house drink";
music = "DJ music";
getPartyDetail();
}

}

class LoudParty extends Party {

@Override
void accept(CookVisitorI visitor) {
this.visitor = visitor;
visitor.cook(this);
}
}

class CalmParty extends Party {

@Override
void accept(CookVisitorI visitor) {
this.visitor = visitor;
visitor.cook(this);
}
}