forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
57 changed files
with
790 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
interface AdvancedMediaPlayer { | ||
void playVlc(String fileName); | ||
void playMp4(String fileName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
interface MediaPlayer { | ||
void play(String audioType, String fileName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
interface PizzaBuilder { | ||
void buildDough(); | ||
void buildSauce(); | ||
void buildTopping(); | ||
Pizza getPizza(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
interface Command { | ||
void execute(); | ||
void undo(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
interface Employee { | ||
void showDetails(); | ||
double getSalary(); | ||
} |
Oops, something went wrong.