Skip to content

Commit

Permalink
Add java design pattern code
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishps1 committed Jun 24, 2024
1 parent 1094665 commit 812a1a3
Show file tree
Hide file tree
Showing 57 changed files with 790 additions and 0 deletions.
10 changes: 10 additions & 0 deletions design-patterns/java/adapter/AdapterDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class AdapterDemo {
public static void main(String[] args) {
AudioPlayer audioPlayer = new AudioPlayer();

audioPlayer.play("mp3", "beyond_the_horizon.mp3");
audioPlayer.play("mp4", "alone.mp4");
audioPlayer.play("vlc", "far_far_away.vlc");
audioPlayer.play("avi", "mind_me.avi");
}
}
4 changes: 4 additions & 0 deletions design-patterns/java/adapter/AdvancedMediaPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface AdvancedMediaPlayer {
void playVlc(String fileName);
void playMp4(String fileName);
}
18 changes: 18 additions & 0 deletions design-patterns/java/adapter/AudioPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class AudioPlayer implements MediaPlayer {
MediaAdapter mediaAdapter;

@Override
public void play(String audioType, String fileName) {
// inbuilt support to play mp3 music files
if (audioType.equalsIgnoreCase("mp3")) {
System.out.println("Playing mp3 file. Name: " + fileName);
}
// mediaAdapter is providing support to play other file formats
else if (audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")) {
mediaAdapter = new MediaAdapter(audioType);
mediaAdapter.play(audioType, fileName);
} else {
System.out.println("Invalid media. " + audioType + " format not supported");
}
}
}
20 changes: 20 additions & 0 deletions design-patterns/java/adapter/MediaAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class MediaAdapter implements MediaPlayer {
AdvancedMediaPlayer advancedMusicPlayer;

public MediaAdapter(String audioType) {
if (audioType.equalsIgnoreCase("vlc")) {
advancedMusicPlayer = new VlcPlayer();
} else if (audioType.equalsIgnoreCase("mp4")) {
advancedMusicPlayer = new Mp4Player();
}
}

@Override
public void play(String audioType, String fileName) {
if (audioType.equalsIgnoreCase("vlc")) {
advancedMusicPlayer.playVlc(fileName);
} else if (audioType.equalsIgnoreCase("mp4")) {
advancedMusicPlayer.playMp4(fileName);
}
}
}
3 changes: 3 additions & 0 deletions design-patterns/java/adapter/MediaPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface MediaPlayer {
void play(String audioType, String fileName);
}
11 changes: 11 additions & 0 deletions design-patterns/java/adapter/Mp4Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Mp4Player implements AdvancedMediaPlayer {
@Override
public void playVlc(String fileName) {
// do nothing
}

@Override
public void playMp4(String fileName) {
System.out.println("Playing mp4 file. Name: " + fileName);
}
}
11 changes: 11 additions & 0 deletions design-patterns/java/adapter/VlcPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class VlcPlayer implements AdvancedMediaPlayer {
@Override
public void playVlc(String fileName) {
System.out.println("Playing vlc file. Name: " + fileName);
}

@Override
public void playMp4(String fileName) {
// do nothing
}
}
17 changes: 17 additions & 0 deletions design-patterns/java/builder/BuilderDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class BuilderDemo {
public static void main(String[] args) {
Waiter waiter = new Waiter();
PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();
PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();

waiter.setPizzaBuilder(hawaiianPizzaBuilder);
waiter.constructPizza();
Pizza hawaiianPizza = waiter.getPizza();
System.out.println(hawaiianPizza);

waiter.setPizzaBuilder(spicyPizzaBuilder);
waiter.constructPizza();
Pizza spicyPizza = waiter.getPizza();
System.out.println(spicyPizza);
}
}
10 changes: 10 additions & 0 deletions design-patterns/java/builder/HawaiianPizzaBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class HawaiianPizzaBuilder implements PizzaBuilder {
private Pizza pizza;

public HawaiianPizzaBuilder() { this.pizza = new Pizza(); }

public void buildDough() { pizza.setDough("cross"); }
public void buildSauce() { pizza.setSauce("mild"); }
public void buildTopping() { pizza.setTopping("ham and pineapple"); }
public Pizza getPizza() { return this.pizza; }
}
13 changes: 13 additions & 0 deletions design-patterns/java/builder/Pizza.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Pizza {
private String dough = "";
private String sauce = "";
private String topping = "";

public void setDough(String dough) { this.dough = dough; }
public void setSauce(String sauce) { this.sauce = sauce; }
public void setTopping(String topping) { this.topping = topping; }

public String toString() {
return "Pizza with " + dough + " dough, " + sauce + " sauce, and " + topping + " topping.";
}
}
6 changes: 6 additions & 0 deletions design-patterns/java/builder/PizzaBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface PizzaBuilder {
void buildDough();
void buildSauce();
void buildTopping();
Pizza getPizza();
}
10 changes: 10 additions & 0 deletions design-patterns/java/builder/SpicyPizzaBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class SpicyPizzaBuilder implements PizzaBuilder {
private Pizza pizza;

public SpicyPizzaBuilder() { this.pizza = new Pizza(); }

public void buildDough() { pizza.setDough("pan baked"); }
public void buildSauce() { pizza.setSauce("hot"); }
public void buildTopping() { pizza.setTopping("pepperoni and jalapeno"); }
public Pizza getPizza() { return this.pizza; }
}
13 changes: 13 additions & 0 deletions design-patterns/java/builder/Waiter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Waiter {
private PizzaBuilder pizzaBuilder;

public void setPizzaBuilder(PizzaBuilder pb) { pizzaBuilder = pb; }

public Pizza getPizza() { return pizzaBuilder.getPizza(); }

public void constructPizza() {
pizzaBuilder.buildDough();
pizzaBuilder.buildSauce();
pizzaBuilder.buildTopping();
}
}
4 changes: 4 additions & 0 deletions design-patterns/java/command/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface Command {
void execute();
void undo();
}
41 changes: 41 additions & 0 deletions design-patterns/java/command/CommandPatternDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class CommandPatternDemo {
public static void main(String[] args) {
// Set up the receiver
Light livingRoomLight = new Light("Living Room");
Light kitchenLight = new Light("Kitchen");

// Create concrete commands
Command livingRoomLightOn = new LightOnCommand(livingRoomLight);
Command livingRoomLightOff = new LightOffCommand(livingRoomLight);
Command kitchenLightOn = new LightOnCommand(kitchenLight);
Command kitchenLightOff = new LightOffCommand(kitchenLight);

// Set up the invoker
RemoteControl remote = new RemoteControl();

// Use the invoker to execute commands
remote.setCommand(livingRoomLightOn);
remote.pressButton();
remote.setCommand(kitchenLightOn);
remote.pressButton();
remote.setCommand(livingRoomLightOff);
remote.pressButton();
remote.setCommand(kitchenLightOff);
remote.pressButton();

// Demonstrate undo
System.out.println("\nDemonstrating undo:");
remote.setCommand(livingRoomLightOn);
remote.pressButton();
remote.pressUndoButton();

// Demonstrate RemoteControlWithUndo
System.out.println("\nDemonstrating RemoteControlWithUndo:");
RemoteControlWithUndo advancedRemote = new RemoteControlWithUndo();
advancedRemote.addCommand(livingRoomLightOn);
advancedRemote.addCommand(kitchenLightOn);
advancedRemote.executeCommands();
advancedRemote.undoLastCommand();
advancedRemote.undoLastCommand();
}
}
18 changes: 18 additions & 0 deletions design-patterns/java/command/Light.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Light {
private boolean isOn = false;
private String location;

public Light(String location) {
this.location = location;
}

public void turnOn() {
isOn = true;
System.out.println(location + " light is now ON");
}

public void turnOff() {
isOn = false;
System.out.println(location + " light is now OFF");
}
}
15 changes: 15 additions & 0 deletions design-patterns/java/command/LightOffCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class LightOffCommand implements Command {
private Light light;

public LightOffCommand(Light light) {
this.light = light;
}

public void execute() {
light.turnOff();
}

public void undo() {
light.turnOn();
}
}
15 changes: 15 additions & 0 deletions design-patterns/java/command/LightOnCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class LightOnCommand implements Command {
private Light light;

public LightOnCommand(Light light) {
this.light = light;
}

public void execute() {
light.turnOn();
}

public void undo() {
light.turnOff();
}
}
15 changes: 15 additions & 0 deletions design-patterns/java/command/RemoteControl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class RemoteControl {
private Command command;

public void setCommand(Command command) {
this.command = command;
}

public void pressButton() {
command.execute();
}

public void pressUndoButton() {
command.undo();
}
}
23 changes: 23 additions & 0 deletions design-patterns/java/command/RemoteControlWithUndo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class RemoteControlWithUndo {
private List<Command> commands = new ArrayList<>();
private List<Command> undoCommands = new ArrayList<>();

public void addCommand(Command command) {
commands.add(command);
}

public void executeCommands() {
for (Command command : commands) {
command.execute();
undoCommands.add(command);
}
commands.clear();
}

public void undoLastCommand() {
if (!undoCommands.isEmpty()) {
Command lastCommand = undoCommands.remove(undoCommands.size() - 1);
lastCommand.undo();
}
}
}
20 changes: 20 additions & 0 deletions design-patterns/java/composite/CompositeDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class CompositeDemo {
public static void main(String[] args) {
Developer dev1 = new Developer("John Doe", 100000);
Developer dev2 = new Developer("Jane Smith", 120000);
Designer designer = new Designer("Mike Johnson", 90000);

Manager engManager = new Manager("Emily Brown", 200000);
engManager.addEmployee(dev1);
engManager.addEmployee(dev2);
engManager.addEmployee(designer);

Manager generalManager = new Manager("David Wilson", 300000);
generalManager.addEmployee(engManager);

System.out.println("Company Structure:");
generalManager.showDetails();

System.out.println("\nTotal Salary Budget: $" + generalManager.getSalary());
}
}
19 changes: 19 additions & 0 deletions design-patterns/java/composite/Designer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Designer implements Employee {
private String name;
private double salary;

public Designer(String name, double salary) {
this.name = name;
this.salary = salary;
}

@Override
public void showDetails() {
System.out.println("Designer: " + name + ", Salary: $" + salary);
}

@Override
public double getSalary() {
return salary;
}
}
19 changes: 19 additions & 0 deletions design-patterns/java/composite/Developer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Developer implements Employee {
private String name;
private double salary;

public Developer(String name, double salary) {
this.name = name;
this.salary = salary;
}

@Override
public void showDetails() {
System.out.println("Developer: " + name + ", Salary: $" + salary);
}

@Override
public double getSalary() {
return salary;
}
}
4 changes: 4 additions & 0 deletions design-patterns/java/composite/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface Employee {
void showDetails();
double getSalary();
}
Loading

0 comments on commit 812a1a3

Please sign in to comment.