Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package behaviouralPatterns;
//Scenario : Complex Implementation details and objects are visible
//Solution : encapsulate a complex implementations as a request in an object, allow saving the requests in a queue for quick access

abstract class Command{
public abstract void execute();
}

class VRGameCommand extends Command{
SetupBox box; TV tv; Remote remote; VRGame vrgame;

VRGameCommand(SetupBox box, TV tv, Remote remote, VRGame vrgame){
this.box = box;
this.tv = tv;
this.remote = remote;
this.vrgame = vrgame;
}
@Override
public void execute() {
tv.turnOn();
box.avMode2();
remote.increaseVolume();
vrgame.ttGame();
}

}

class NewsCommand extends Command{
SetupBox box; TV tv; Remote remote; Channel channel;

NewsCommand(SetupBox box, TV tv, Remote remote, Channel channel){
this.box = box;
this.tv = tv;
this.remote = remote;
this.channel = channel;
}
@Override
public void execute() {
tv.turnOn();
box.avMode1();
remote.decreaseVolume();
channel.newsChannel();
}

}

class SetupBox{
public void avMode1() {
System.out.println("Switching to AV mode 1");
}
public void avMode2() {
System.out.println("Switching to AV mode 2");
}
}
class TV{
public void turnOn() {
System.out.println("Turning tv on..");
}
public void turnOff() {
System.out.println("Turning tv off..");
}
}
class Remote{
public void increaseVolume() {
System.out.println("Increasing tv volume");
}
public void decreaseVolume() {
System.out.println("Decreasing tv volume");
}
}
class Channel{
public void newsChannel(){
System.out.println("Switched to news channel..");
}
public void serialChannel(){
System.out.println("Switched to serial channel..");
}
}
class VRGame{
public void archeryGame() {
System.out.println("Archery game is selected..");
}
public void ttGame() {
System.out.println("Table tennis game is selected..");
}
}

class GenieRemote{

Command genie[] = new Command[5];

public GenieRemote() {
for(int i=0;i<genie.length;i++) {
genie[i] = new Command() {
@Override
public void execute() { System.out.println("To be set slot.. ");}
};
}
}

public void setRemote(int slot, Command command) {
genie[slot] = command;
}
public void remoteFunction(int slot) {
genie[slot].execute();
}
}

public class Behavioural__CommandPattern {
public static void main(String[] args) {

GenieRemote maid = new GenieRemote();
maid.setRemote(1,new VRGameCommand(new SetupBox(), new TV(), new Remote(), new VRGame()));
maid.setRemote(2,new NewsCommand(new SetupBox(), new TV(), new Remote(), new Channel()));

maid.remoteFunction(1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package behaviouralPatterns;
//Problem : classes differ only in their behavior
//Solution : Define a family of algorithms, encapsulate each one, and make them interchangeable

class Fresher{
private Developer role;

public void setFresherRole(Developer role) {
this.role = role;
}
public Developer getFresherRole() {
return role;
}
}

interface Developer{
void techStack();
}
class FrontendDev implements Developer{
@Override
public void techStack() {
System.out.println("HTML, CSS, Js, ReactJs");
}
public void getUserPerspective() {
System.out.println("Worked with UX people and researched on usability of application");
}

}
class BackendDev implements Developer{
@Override
public void techStack() {
System.out.println("Java, SpringBoot, NodeJS, SQL ...");
}
public void useDesignPatterns() {
System.out.println("Has knowledge of Creational, Behavioural, Structural Design patterns");
}
}



public class Behavioural__StrategyPattern {
public static void main(String[] args) throws Exception{
Fresher ace = new Fresher();
ace.setFresherRole(new BackendDev());

ace.getFresherRole().techStack();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package behaviouralPatterns;
//Problem : separate operational functionalities from a class
//Solution: define a new operation without changing the classes of the elements on which it operates. new visitor functions can be added in future

import java.util.Scanner;

abstract class Human{
String name;
String address;
int age;
float salary;
public Human(String name, String address, int age, float salary) {
this.name = name;
this.address = address;
this.age = age;
this.salary = salary;
}
}
class Tenant extends Human{
float rent;
public Tenant(String name, String address, int age, float salary, float rent) {
super(name,address,age,salary);
this.rent = rent;
}

public void visit(bankTransaction bt) {
bt.pay(this.rent, this);
}

}
class Parent extends Human{
float schoolFee;
public Parent(String name, String address, int age, float salary, float schoolFee) {
super(name, address, age, salary);
this.schoolFee = schoolFee;
}
public void visit(bankTransaction bt) {
bt.pay(this.schoolFee, this);
}

}
class Pensioner extends Human{
float pension;
public Pensioner(String name, String address, int age, float salary, float pension) {
super(name, address, age, salary);
this.pension = pension;
}
public void visit(bankTransaction bt) {
bt.pay(this.pension, this);
new CurrentBalance(this).showBalance();
}

}
interface bankTransaction{
public abstract void pay(float amount, Parent man);
public abstract void pay(float amount, Tenant man);
public abstract void pay(float amount, Pensioner man);

}
class Transact implements bankTransaction{
public void pay(float amount, Parent man) {
man.salary-=amount;
System.out.println("Rent payed to house owner : "+ amount);
}
public void pay(float amount, Tenant man) {
man.salary-=amount;
System.out.println("Annual school fee paid : "+ amount);
}
public void pay(float amount, Pensioner man) {
man.salary+=amount;
System.out.println("Money added to bank: "+ amount);
}
}

class CurrentBalance{
Human citizen;
public CurrentBalance(Human citizen) {
this.citizen = citizen;
}
public void showBalance() {
System.out.println("Current balance is : "+citizen.salary);
}
}
public class Behavioural__VisitorPattern {
public static void main(String[] args) throws Exception{
Scanner scanner = new Scanner(System.in);
Parent govind = new Parent("Govindarajan","Porur, Chennai", 60, 80_000, 50_000);
bankTransaction amount = new Transact();
govind.visit(amount);

scanner.close();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package creationalPatterns;
//Problem : An application might need a mechanism for building complex objects that is independent from the ones that make up the object.
//Solution : Define an instance for creating an object but letting subclasses decide which class to instantiate

class House {
BedRoom bedroom;
Kitchen kitchen;
Hall hall;

public House(BedRoom bedroom, Kitchen kitchen, Hall hall) {
this.bedroom = bedroom;
this.kitchen = kitchen;
this.hall = hall;
}

static class BuildHouse {
BedRoom bedroom;
Kitchen kitchen;
Hall hall;

public BuildHouse buildBedRoom(BedRoom bedroom) {
this.bedroom = bedroom;
return this;
}

public BuildHouse buildKitchen(Kitchen kitchen) {
this.kitchen = kitchen;
return this;
}

public BuildHouse buildHall(Hall hall) {
this.hall = hall;
return this;
}

public House buildHouse() {
return new House(bedroom, kitchen, hall);
}

}

@Override
public String toString() {
return "House [bedroom=" + bedroom + ", kitchen=" + kitchen + ", hall=" + hall + "]";
}

}

class BedRoom {

}

class Kitchen {

}

class Hall {

}

public class Creational__BuilderPattern {
public static void main(String[] args) {
House ishwaryam = new House.BuildHouse().buildBedRoom(new BedRoom()).buildHouse();

System.out.println(ishwaryam);
}
}
Loading