Merge pull request #64 from KunwarVikas/stackandqueue
Composite Design Patterns
This commit is contained in:
32
com/javadevjournal/design/behavioral/cor/ATMDispenser.java
Normal file
32
com/javadevjournal/design/behavioral/cor/ATMDispenser.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package main.com.kunwar.designpatterns.behavioral.cor;
|
||||
import java.util.Scanner;
|
||||
/**
|
||||
* @author Kunwar Vikas
|
||||
*/
|
||||
public class ATMDispenser {
|
||||
private DispenseChain dispenseChain1;
|
||||
public ATMDispenser() {
|
||||
// initialize the chain
|
||||
this.dispenseChain1 = new Dollar50Dispenser();
|
||||
DispenseChain dispenseChain2 = new Dollar20Dispenser();
|
||||
DispenseChain dispenseChain3 = new Dollar10Dispenser();
|
||||
|
||||
// set the chain of responsibility
|
||||
dispenseChain1.setNextChain(dispenseChain2);
|
||||
dispenseChain2.setNextChain(dispenseChain3);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ATMDispenser atmDispenser = new ATMDispenser();
|
||||
int amount = 0;
|
||||
System.out.print("Enter amount to dispense: ");
|
||||
Scanner input = new Scanner(System.in);
|
||||
amount = input.nextInt();
|
||||
if (amount % 10 != 0) {
|
||||
System.out.println("Amount should be in multiple of 10s.");
|
||||
return;
|
||||
}
|
||||
// process the request
|
||||
atmDispenser.dispenseChain1.dispense(new Currency(amount));
|
||||
}
|
||||
}
|
||||
15
com/javadevjournal/design/behavioral/cor/Currency.java
Normal file
15
com/javadevjournal/design/behavioral/cor/Currency.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package main.com.kunwar.designpatterns.behavioral.cor;
|
||||
|
||||
/**
|
||||
* @author Kunwar Vikas
|
||||
*/
|
||||
public class Currency {
|
||||
private int amount;
|
||||
|
||||
public Currency(int amount){
|
||||
this.amount=amount;
|
||||
}
|
||||
public int getAmount(){
|
||||
return this.amount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package main.com.kunwar.designpatterns.behavioral.cor;
|
||||
|
||||
/**
|
||||
* @author Kunwar Vikas
|
||||
*/
|
||||
public interface DispenseChain {
|
||||
void setNextChain(DispenseChain nextChain);
|
||||
void dispense(Currency currency);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package main.com.kunwar.designpatterns.behavioral.cor;
|
||||
|
||||
/**
|
||||
* @author Kunwar Vikas
|
||||
*/
|
||||
public class Dollar10Dispenser implements DispenseChain {
|
||||
private DispenseChain dispenseChain;
|
||||
|
||||
@Override
|
||||
public void setNextChain(DispenseChain nextChain) {
|
||||
this.dispenseChain =nextChain;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispense(Currency currency) {
|
||||
if(currency.getAmount() >= 10){
|
||||
int num = currency.getAmount()/10;
|
||||
int remainder = currency.getAmount() % 10;
|
||||
System.out.println("Dispensing "+num+" 10$ note");
|
||||
if(remainder !=0) this.dispenseChain.dispense(new Currency(remainder));
|
||||
}else{
|
||||
this.dispenseChain.dispense(currency);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package main.com.kunwar.designpatterns.behavioral.cor;
|
||||
|
||||
/**
|
||||
* @author Kunwar Vikas
|
||||
*/
|
||||
public class Dollar20Dispenser implements DispenseChain {
|
||||
private DispenseChain dispenseChain;
|
||||
|
||||
@Override
|
||||
public void setNextChain(DispenseChain nextChain) {
|
||||
this.dispenseChain = nextChain;
|
||||
}
|
||||
@Override
|
||||
public void dispense(Currency currency) {
|
||||
if (currency.getAmount() >= 20) {
|
||||
int num = currency.getAmount() / 20;
|
||||
int remainder = currency.getAmount() % 20;
|
||||
System.out.println("Dispensing " + num + " 20$ note");
|
||||
if (remainder != 0) this.dispenseChain.dispense(new Currency(remainder));
|
||||
} else {
|
||||
this.dispenseChain.dispense(currency);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package main.com.kunwar.designpatterns.behavioral.cor;
|
||||
|
||||
/**
|
||||
* @author Kunwar Vikas
|
||||
*/
|
||||
public class Dollar50Dispenser implements DispenseChain {
|
||||
private DispenseChain dispenseChain;
|
||||
|
||||
@Override
|
||||
public void setNextChain(DispenseChain nextChain) {
|
||||
this.dispenseChain =nextChain;
|
||||
}
|
||||
@Override
|
||||
public void dispense(Currency currency) {
|
||||
if(currency.getAmount() >= 50){
|
||||
int num = currency.getAmount()/50;
|
||||
int remainder = currency.getAmount() % 50;
|
||||
System.out.println("Dispensing "+num+" 50$ note");
|
||||
if(remainder !=0) this.dispenseChain.dispense(new Currency(remainder));
|
||||
}else{
|
||||
this.dispenseChain.dispense(currency);
|
||||
}
|
||||
}
|
||||
}
|
||||
21
com/javadevjournal/design/behavioral/template/Cricket.java
Normal file
21
com/javadevjournal/design/behavioral/template/Cricket.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package main.com.kunwar.designpatterns.behavioral.template;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class Cricket extends Game {
|
||||
|
||||
@Override
|
||||
void endPlay() {System.out.println("Cricket Game Finished!");
|
||||
}
|
||||
|
||||
@Override
|
||||
void initialize() {
|
||||
System.out.println("Cricket Game Initialized! Start playing.");
|
||||
}
|
||||
|
||||
@Override
|
||||
void startPlay() {
|
||||
System.out.println("Cricket Game Started. Enjoy the game!");
|
||||
}
|
||||
}
|
||||
22
com/javadevjournal/design/behavioral/template/Football.java
Normal file
22
com/javadevjournal/design/behavioral/template/Football.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package main.com.kunwar.designpatterns.behavioral.template;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class Football extends Game {
|
||||
|
||||
@Override
|
||||
void endPlay() {
|
||||
System.out.println("Football Game Finished!");
|
||||
}
|
||||
|
||||
@Override
|
||||
void initialize() {
|
||||
System.out.println("Football Game Initialized! Start playing.");
|
||||
}
|
||||
|
||||
@Override
|
||||
void startPlay() {
|
||||
System.out.println("Football Game Started. Enjoy the game!");
|
||||
}
|
||||
}
|
||||
20
com/javadevjournal/design/behavioral/template/Game.java
Normal file
20
com/javadevjournal/design/behavioral/template/Game.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package main.com.kunwar.designpatterns.behavioral.template;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public abstract class Game {
|
||||
abstract void initialize();
|
||||
abstract void startPlay();
|
||||
abstract void endPlay();
|
||||
|
||||
//template method
|
||||
public final void play(){
|
||||
//initialize the game
|
||||
initialize();
|
||||
//start game
|
||||
startPlay();
|
||||
//end game
|
||||
endPlay();
|
||||
}
|
||||
}
|
||||
22
com/javadevjournal/design/behavioral/template/Hockey.java
Normal file
22
com/javadevjournal/design/behavioral/template/Hockey.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package main.com.kunwar.designpatterns.behavioral.template;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class Hockey extends Game {
|
||||
|
||||
@Override
|
||||
void endPlay() {
|
||||
System.out.println("Hockey Game Finished!");
|
||||
}
|
||||
|
||||
@Override
|
||||
void initialize() {
|
||||
System.out.println("Hockey Game Initialized! Start playing.");
|
||||
}
|
||||
|
||||
@Override
|
||||
void startPlay() {
|
||||
System.out.println("Hockey Game Started. Enjoy the game!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package main.com.kunwar.designpatterns.behavioral.template;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class TemplatePatternDemo {
|
||||
public static void main(String[] args) {
|
||||
Game game = new Cricket();
|
||||
game.play();
|
||||
System.out.println();
|
||||
game = new Football();
|
||||
game.play();
|
||||
System.out.println();
|
||||
game = new Hockey();
|
||||
game.play();
|
||||
System.out.println();
|
||||
game = new Volleyball();
|
||||
game.play();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package main.com.kunwar.designpatterns.behavioral.template;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class Volleyball extends Game {
|
||||
|
||||
@Override
|
||||
void endPlay() {
|
||||
System.out.println("Volleyball Game Finished!");
|
||||
}
|
||||
|
||||
@Override
|
||||
void initialize() {
|
||||
System.out.println("Volleyball Game Initialized! Start playing.");
|
||||
}
|
||||
|
||||
@Override
|
||||
void startPlay() {
|
||||
System.out.println("Volleyball Game Started. Enjoy the game!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package javadevjournal.design.structural.Bridge;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class BridgePatternClient {
|
||||
public static void main(String[] args) {
|
||||
QuestionFormat questions = new QuestionFormat("Java Programming Language");
|
||||
questions.question = new JavaQuestions();
|
||||
questions.display();
|
||||
questions.previous();
|
||||
|
||||
questions.next();
|
||||
questions.display();
|
||||
|
||||
questions.next();
|
||||
questions.display();
|
||||
|
||||
questions.previous();
|
||||
questions.display();
|
||||
|
||||
questions.newOne("What is inheritance? ");
|
||||
questions.newOne("How many types of inheritance are there in java?");
|
||||
|
||||
questions.displayAll();
|
||||
}
|
||||
}
|
||||
18
com/javadevjournal/design/structural/Bridge/IQuestion.java
Normal file
18
com/javadevjournal/design/structural/Bridge/IQuestion.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package javadevjournal.design.structural.Bridge;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public interface IQuestion {
|
||||
public void nextQuestion();
|
||||
|
||||
public void previousQuestion();
|
||||
|
||||
public void newQuestion(String q);
|
||||
|
||||
public void deleteQuestion(String q);
|
||||
|
||||
public void displayQuestion();
|
||||
|
||||
public void displayAllQuestions();
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package javadevjournal.design.structural.Bridge;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class JavaQuestions implements IQuestion {
|
||||
|
||||
private List<String> questionsList = new ArrayList<String>();
|
||||
private int currQuesCounter = 0;
|
||||
|
||||
public JavaQuestions() {
|
||||
questionsList.add("What is class? ");
|
||||
questionsList.add("What is interface? ");
|
||||
questionsList.add("What is abstraction? ");
|
||||
questionsList.add("How multiple polymorphism is achieved in java? ");
|
||||
questionsList.add("How many types of exception handling are there in java? ");
|
||||
questionsList.add("Define the keyword final for variable, method, and class in java? ");
|
||||
questionsList.add("What is abstract class? ");
|
||||
questionsList.add("What is multi-threading? ");
|
||||
}
|
||||
|
||||
public void nextQuestion() {
|
||||
if (currQuesCounter <= questionsList.size() - 1) {
|
||||
currQuesCounter++;
|
||||
} else {
|
||||
System.out.println("We are already at the last question");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void previousQuestion() {
|
||||
if (currQuesCounter > 0) {
|
||||
currQuesCounter--;
|
||||
} else {
|
||||
System.out.println("We are already at the first question");
|
||||
}
|
||||
}
|
||||
|
||||
public void newQuestion(String question) {
|
||||
questionsList.add(question);
|
||||
}
|
||||
|
||||
public void deleteQuestion(String question) {
|
||||
questionsList.remove(question);
|
||||
}
|
||||
|
||||
public void displayQuestion() {
|
||||
System.out.println(questionsList.get(currQuesCounter));
|
||||
}
|
||||
|
||||
public void displayAllQuestions() {
|
||||
for (String question : questionsList) {
|
||||
System.out.println(question);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package javadevjournal.design.structural.Bridge;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class QuestionFormat extends QuestionManager {
|
||||
public QuestionFormat(String catalog) {
|
||||
super(catalog);
|
||||
}
|
||||
|
||||
public void displayAll() {
|
||||
System.out.println("\n---------------------------------------------------------");
|
||||
super.displayAll();
|
||||
System.out.println("-----------------------------------------------------------");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package javadevjournal.design.structural.Bridge;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class QuestionManager {
|
||||
protected IQuestion question;
|
||||
public String catalog;
|
||||
|
||||
public QuestionManager(String catalog) {
|
||||
this.catalog = catalog;
|
||||
}
|
||||
|
||||
public void next() {
|
||||
question.nextQuestion();
|
||||
}
|
||||
|
||||
public void previous() {
|
||||
question.previousQuestion();
|
||||
}
|
||||
|
||||
public void newOne(String quest) {
|
||||
question.newQuestion(quest);
|
||||
}
|
||||
|
||||
public void delete(String quest) {
|
||||
question.deleteQuestion(quest);
|
||||
}
|
||||
|
||||
public void display() {
|
||||
question.displayQuestion();
|
||||
}
|
||||
|
||||
public void displayAll() {
|
||||
System.out.println("Question Paper: " + catalog);
|
||||
question.displayAllQuestions();
|
||||
}
|
||||
}
|
||||
12
com/javadevjournal/design/structural/Composite/Circle.java
Normal file
12
com/javadevjournal/design/structural/Composite/Circle.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package javadevjournal.design.structural.Composite;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class Circle implements Shape {
|
||||
|
||||
@Override
|
||||
public void drawShape(String color) {
|
||||
System.out.println("Drawing Circle with color " + color);
|
||||
}
|
||||
}
|
||||
35
com/javadevjournal/design/structural/Composite/Drawing.java
Normal file
35
com/javadevjournal/design/structural/Composite/Drawing.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package javadevjournal.design.structural.Composite;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class Drawing implements Shape {
|
||||
|
||||
private List<Shape> shapesList = new ArrayList<Shape>();
|
||||
|
||||
@Override
|
||||
public void drawShape(String fillColor) {
|
||||
for (Shape shape : shapesList) {
|
||||
shape.drawShape(fillColor);
|
||||
}
|
||||
}
|
||||
|
||||
//adding shape to drawing
|
||||
public void add(Shape s) {
|
||||
this.shapesList.add(s);
|
||||
}
|
||||
|
||||
//removing shape from drawing
|
||||
public void remove(Shape s) {
|
||||
shapesList.remove(s);
|
||||
}
|
||||
|
||||
//removing all the shapes
|
||||
public void clear() {
|
||||
System.out.println("Clearing all the shapes from drawing");
|
||||
this.shapesList.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package javadevjournal.design.structural.Composite;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class Rectangle implements Shape {
|
||||
@Override
|
||||
public void drawShape(String color) {
|
||||
System.out.println("Drawing Rectangle with color " + color);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package javadevjournal.design.structural.Composite;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public interface Shape {
|
||||
public void drawShape(String fillColor);
|
||||
}
|
||||
11
com/javadevjournal/design/structural/Composite/Square.java
Normal file
11
com/javadevjournal/design/structural/Composite/Square.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package javadevjournal.design.structural.Composite;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class Square implements Shape {
|
||||
@Override
|
||||
public void drawShape(String color) {
|
||||
System.out.println("Drawing Square with color " + color);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package javadevjournal.design.structural.Composite;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class TestCompositePattern {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Shape triangle = new Triangle();
|
||||
Shape triangle1 = new Triangle();
|
||||
Shape circle = new Circle();
|
||||
Shape square = new Square();
|
||||
Shape rectangle = new Rectangle();
|
||||
|
||||
Drawing drawing = new Drawing();
|
||||
drawing.add(triangle1);
|
||||
drawing.add(triangle1);
|
||||
drawing.add(circle);
|
||||
drawing.add(square);
|
||||
drawing.add(rectangle);
|
||||
drawing.drawShape("Red");
|
||||
|
||||
drawing.clear();
|
||||
|
||||
drawing.add(triangle);
|
||||
drawing.add(circle);
|
||||
drawing.add(square);
|
||||
drawing.add(rectangle);
|
||||
drawing.drawShape("Blue");
|
||||
}
|
||||
}
|
||||
12
com/javadevjournal/design/structural/Composite/Triangle.java
Normal file
12
com/javadevjournal/design/structural/Composite/Triangle.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package javadevjournal.design.structural.Composite;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class Triangle implements Shape {
|
||||
|
||||
@Override
|
||||
public void drawShape(String color) {
|
||||
System.out.println("Drawing Triangle with color " + color);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user