├── .gitignore ├── Strategy Pattern ├── Chipset │ ├── Chipset.java │ ├── IntelChipset.java │ └── AppleM1Chipset.java ├── GraphicsCard │ ├── GraphicsCard.java │ ├── AmdRadeon.java │ └── NvidiaGeForce.java ├── ChromeBook.java ├── HpPavillion.java ├── AppleMacbook.java ├── Runner.java └── Computer.java ├── Command Pattern ├── Newsletter Example │ ├── Command │ │ ├── Command.java │ │ ├── MacroCommand.java │ │ ├── SendWelcomeMail.java │ │ ├── SendAcknowledgementMessage.java │ │ └── CollectSubscriberEmails.java │ ├── Service │ │ ├── MailService.java │ │ ├── MessageService.java │ │ └── UserService.java │ ├── Scheduler.java │ └── SchedulerTest.java └── Headfirst Design Book Example │ ├── Command │ ├── Command.java │ ├── NoCommand.java │ ├── LightOnCommand.java │ ├── LightOffCommand.java │ ├── GarageDoorUpCommand.java │ ├── GarageDoorDownCommand.java │ ├── MacroCommand.java │ └── CeilingFanHighCommand.java │ ├── Device │ ├── GarageDoor.java │ ├── Light.java │ └── CeilingFan.java │ ├── SimpleRemoteControl.java │ ├── RemoteControl.java │ └── RemoteControlTest.java ├── Observer Pattern ├── Newsletter Example │ ├── Observer │ │ ├── Observer.java │ │ ├── IndividualSubscriber.java │ │ └── CommunitySubscriber.java │ ├── Observable │ │ ├── Subject.java │ │ └── SystemsThatScale.java │ └── NewsletterRunner.java └── Head First Example │ ├── Actual Implementation │ ├── Observer │ │ ├── DisplayElement.java │ │ ├── Observer.java │ │ ├── ForecastDisplay.java │ │ ├── StatisticsDisplay.java │ │ └── CurrentConditionsDisplay.java │ ├── Observable │ │ ├── Observable.java │ │ └── WeatherData.java │ └── WeatherDataRunner.java │ ├── Inefficient Implementation │ ├── WeatherDataRunner.java │ ├── StatisticsDisplay.java │ ├── ForecastDisplay.java │ ├── CurrentConditionsDisplay.java │ └── WeatherData.java │ └── Java In Built Implementation │ ├── WeatherMetric.java │ ├── WeatherDataRunner.java │ ├── WeatherData.java │ └── CurrentConditionsDisplay.java ├── Decorator Pattern ├── Headfirst Design Book Example │ ├── CondimentDecorator.java │ ├── Beverage.java │ ├── Decaf.java │ ├── Espresso.java │ ├── DarkRoast.java │ ├── HouseBlend.java │ ├── Soy.java │ ├── Whip.java │ ├── Mocha.java │ ├── SteamedMilk.java │ └── StarbuzzCoffee.java ├── Newsletter Example │ ├── Effective Implementation │ │ ├── AddOn.java │ │ ├── Meal.java │ │ ├── EggBurger.java │ │ ├── VegBurger.java │ │ ├── ChickenBurger.java │ │ ├── Chips.java │ │ ├── Sauce.java │ │ ├── Cheese.java │ │ ├── Veggies.java │ │ └── BurgerKing.java │ ├── Alternate Implementation │ │ ├── EggBurger.java │ │ ├── VegBurger.java │ │ ├── ChickenBurger.java │ │ ├── BurgerKing.java │ │ └── Meal.java │ └── Class Explosion Implementation │ │ ├── Meal.java │ │ ├── BurgerKing.java │ │ └── ChickenBurgerWithCheeseAndSauce.java └── Alternative Implementation │ ├── Decaf.java │ ├── Espresso.java │ ├── DarkRoast.java │ ├── HouseBlend.java │ ├── StarbuzzCoffee.java │ └── Beverage.java ├── Facade Pattern ├── Components │ ├── Xbox.java │ ├── Lights.java │ ├── Display.java │ └── AudioBox.java ├── Runner.java └── GamingStation │ └── GamingStation.java ├── Factory Pattern ├── Head First Design Patterns Example │ ├── Pizza │ │ ├── GreekPizza.java │ │ ├── CheesePizza.java │ │ ├── PepperoniPizza.java │ │ ├── NYStyleGreekPizza.java │ │ ├── NYStyleCheesePizza.java │ │ ├── ChicagoStyleGreekPizza.java │ │ ├── ChicagoStyleCheesePizza.java │ │ ├── NYStylePepperoniPizza.java │ │ ├── ChicagoStylePepperoniPizza.java │ │ └── Pizza.java │ ├── Factory Method Pattern │ │ ├── PizzaStore.java │ │ ├── NYStylePizzaStore.java │ │ ├── ChicagoStylePizzaStore.java │ │ └── Runner.java │ ├── Simple Factory Pattern │ │ ├── PizzaStore.java │ │ ├── Runner.java │ │ └── SimplePizzaFactory.java │ └── Inefficient Implementation │ │ ├── Runner.java │ │ └── PizzaStore.java └── Newsletter Example │ ├── Device │ ├── Device.java │ ├── MobileDevice.java │ ├── LaptopDevice.java │ ├── ProLaptopDevice.java │ ├── ProMobileDevice.java │ ├── StandardMobileDevice.java │ ├── WearableDevice.java │ ├── StandardLaptopDevice.java │ ├── ProWearableDevice.java │ └── StandardWearableDevice.java │ ├── Factory Method Pattern │ ├── DeviceStore.java │ ├── ProDeviceStore.java │ ├── StandardDeviceStore.java │ └── Runner.java │ ├── Simple Factory Pattern │ ├── DeviceStore.java │ ├── Runner.java │ └── SimpleDeviceFactory.java │ └── Inefficient Implementation │ ├── Runner.java │ └── DeviceStore.java ├── Singleton Pattern ├── Eager Initialization │ ├── CacheService.java │ └── Runner.java ├── Lazy Initialization │ ├── CacheService.java │ └── Runner.java └── Synchronized Implementation │ ├── CacheService.java │ └── Runner.java └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Exclude all compiled .class files 2 | *.class 3 | -------------------------------------------------------------------------------- /Strategy Pattern/Chipset/Chipset.java: -------------------------------------------------------------------------------- 1 | package strategy.chipset; 2 | 3 | public interface Chipset { 4 | public void installChipset(); 5 | } -------------------------------------------------------------------------------- /Strategy Pattern/GraphicsCard/GraphicsCard.java: -------------------------------------------------------------------------------- 1 | package strategy.graphics; 2 | 3 | public interface GraphicsCard { 4 | public void installGraphicsCard(); 5 | } -------------------------------------------------------------------------------- /Command Pattern/Newsletter Example/Command/Command.java: -------------------------------------------------------------------------------- 1 | package commandpattern.newsletter.command; 2 | 3 | public interface Command { 4 | public void execute(); 5 | } -------------------------------------------------------------------------------- /Observer Pattern/Newsletter Example/Observer/Observer.java: -------------------------------------------------------------------------------- 1 | package observer.newsletter.observer; 2 | 3 | public interface Observer { 4 | public void update(String articleContent); 5 | } 6 | -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/Command/Command.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst.command; 2 | 3 | public interface Command { 4 | public void execute(); 5 | 6 | public void undo(); 7 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Actual Implementation/Observer/DisplayElement.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.actual.observer; 2 | 3 | public interface DisplayElement { 4 | public void display(); 5 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Actual Implementation/Observer/Observer.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.actual.observer; 2 | 3 | public interface Observer { 4 | public void update(double temp, double humidity, double pressure); 5 | } -------------------------------------------------------------------------------- /Strategy Pattern/Chipset/IntelChipset.java: -------------------------------------------------------------------------------- 1 | package strategy.chipset; 2 | 3 | public class IntelChipset implements Chipset { 4 | @Override 5 | public void installChipset() { 6 | System.out.println("Installing Intel Chipset"); 7 | } 8 | } -------------------------------------------------------------------------------- /Strategy Pattern/Chipset/AppleM1Chipset.java: -------------------------------------------------------------------------------- 1 | package strategy.chipset; 2 | 3 | public class AppleM1Chipset implements Chipset { 4 | @Override 5 | public void installChipset() { 6 | System.out.println("Installing Apple M1 Chipset"); 7 | } 8 | } -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/Command/NoCommand.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst.command; 2 | 3 | public class NoCommand implements Command { 4 | @Override 5 | public void execute() {} 6 | 7 | @Override 8 | public void undo() {} 9 | } -------------------------------------------------------------------------------- /Decorator Pattern/Headfirst Design Book Example/CondimentDecorator.java: -------------------------------------------------------------------------------- 1 | package decorator.headfirst; 2 | 3 | import decorator.headfirst.Beverage; 4 | 5 | public abstract class CondimentDecorator extends Beverage { 6 | public abstract String getDescription(); 7 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Effective Implementation/AddOn.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.effective; 2 | 3 | import decorator.newsletter.effective.Meal; 4 | 5 | public abstract class AddOn extends Meal { 6 | public abstract String getDescription(); 7 | } -------------------------------------------------------------------------------- /Strategy Pattern/GraphicsCard/AmdRadeon.java: -------------------------------------------------------------------------------- 1 | package strategy.graphics; 2 | 3 | public class AmdRadeon implements GraphicsCard { 4 | @Override 5 | public void installGraphicsCard() { 6 | System.out.println("Installing AMD Radeon graphics card"); 7 | } 8 | } -------------------------------------------------------------------------------- /Decorator Pattern/Alternative Implementation/Decaf.java: -------------------------------------------------------------------------------- 1 | package decorator.alternate; 2 | 3 | import decorator.alternate.Beverage; 4 | 5 | public class Decaf extends Beverage { 6 | Decaf() { 7 | description = "Decaf"; 8 | cost = 1.05; 9 | } 10 | } -------------------------------------------------------------------------------- /Decorator Pattern/Alternative Implementation/Espresso.java: -------------------------------------------------------------------------------- 1 | package decorator.alternate; 2 | 3 | import decorator.alternate.Beverage; 4 | 5 | public class Espresso extends Beverage { 6 | Espresso() { 7 | description = "Espresso"; 8 | cost = 1.99; 9 | } 10 | } -------------------------------------------------------------------------------- /Strategy Pattern/GraphicsCard/NvidiaGeForce.java: -------------------------------------------------------------------------------- 1 | package strategy.graphics; 2 | 3 | public class NvidiaGeForce implements GraphicsCard { 4 | @Override 5 | public void installGraphicsCard() { 6 | System.out.println("Installing Nvidia GeForce graphics card"); 7 | } 8 | } -------------------------------------------------------------------------------- /Decorator Pattern/Alternative Implementation/DarkRoast.java: -------------------------------------------------------------------------------- 1 | package decorator.alternate; 2 | 3 | import decorator.alternate.Beverage; 4 | 5 | public class DarkRoast extends Beverage { 6 | DarkRoast() { 7 | description = "Dark Roast"; 8 | cost = 0.99; 9 | } 10 | } -------------------------------------------------------------------------------- /Decorator Pattern/Alternative Implementation/HouseBlend.java: -------------------------------------------------------------------------------- 1 | package decorator.alternate; 2 | 3 | import decorator.alternate.Beverage; 4 | 5 | public class HouseBlend extends Beverage { 6 | HouseBlend() { 7 | description = "House Blend"; 8 | cost = 0.89; 9 | } 10 | } -------------------------------------------------------------------------------- /Facade Pattern/Components/Xbox.java: -------------------------------------------------------------------------------- 1 | package facade.components; 2 | 3 | public class Xbox { 4 | public void switchOn() { 5 | System.out.println("Xbox is powered On"); 6 | } 7 | 8 | public void switchOff() { 9 | System.out.println("Xbox is powered Off"); 10 | } 11 | } -------------------------------------------------------------------------------- /Facade Pattern/Components/Lights.java: -------------------------------------------------------------------------------- 1 | package facade.components; 2 | 3 | public class Lights { 4 | public void switchOn() { 5 | System.out.println("Switching On the Lights"); 6 | } 7 | 8 | public void switchOff() { 9 | System.out.println("Switching Off the Lights"); 10 | } 11 | } -------------------------------------------------------------------------------- /Facade Pattern/Components/Display.java: -------------------------------------------------------------------------------- 1 | package facade.components; 2 | 3 | public class Display { 4 | public void powerOn() { 5 | System.out.println("Power On the Display unit"); 6 | } 7 | 8 | public void powerOff() { 9 | System.out.println("Power Off the Display unit"); 10 | } 11 | } -------------------------------------------------------------------------------- /Decorator Pattern/Headfirst Design Book Example/Beverage.java: -------------------------------------------------------------------------------- 1 | package decorator.headfirst; 2 | 3 | public abstract class Beverage { 4 | String description = "Unknown Beverage"; 5 | 6 | public String getDescription() { 7 | return description; 8 | } 9 | 10 | public abstract double cost(); 11 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Alternate Implementation/EggBurger.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.alternateimpl; 2 | 3 | import decorator.newsletter.alternateimpl.Meal; 4 | 5 | public class EggBurger extends Meal { 6 | EggBurger() { 7 | description = "Egg Burger"; 8 | price = 65.0; 9 | } 10 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Alternate Implementation/VegBurger.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.alternateimpl; 2 | 3 | import decorator.newsletter.alternateimpl.Meal; 4 | 5 | public class VegBurger extends Meal { 6 | VegBurger() { 7 | description = "Veg Burger"; 8 | price = 60.0; 9 | } 10 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Effective Implementation/Meal.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.effective; 2 | 3 | public abstract class Meal { 4 | String description; 5 | 6 | public String getDescription() { 7 | return description; 8 | } 9 | 10 | public abstract double getPrice(); 11 | } 12 | -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Class Explosion Implementation/Meal.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.classexplosion; 2 | 3 | public abstract class Meal { 4 | String description; 5 | 6 | public String getDescription() { 7 | return description; 8 | } 9 | 10 | public abstract double getPrice(); 11 | } -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/Device/GarageDoor.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst.device; 2 | 3 | public class GarageDoor { 4 | public void up() { 5 | System.out.println("Garage Door is Up!"); 6 | } 7 | 8 | public void down() { 9 | System.out.println("Garage Door is Down!"); 10 | } 11 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Alternate Implementation/ChickenBurger.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.alternateimpl; 2 | 3 | import decorator.newsletter.alternateimpl.Meal; 4 | 5 | public class ChickenBurger extends Meal { 6 | ChickenBurger() { 7 | description = "Chicken Burger"; 8 | price = 70.0; 9 | } 10 | } -------------------------------------------------------------------------------- /Observer Pattern/Newsletter Example/Observable/Subject.java: -------------------------------------------------------------------------------- 1 | package observer.newsletter.observable; 2 | 3 | import observer.newsletter.observer.Observer; 4 | 5 | public interface Subject { 6 | public void registerObserver(Observer o); 7 | 8 | public void removeObserver(Observer o); 9 | 10 | public void notifyObservers(); 11 | } 12 | -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Pizza/GreekPizza.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.pizza; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | 5 | public class GreekPizza extends Pizza { 6 | public GreekPizza() { 7 | setName(); 8 | } 9 | 10 | public void setName() { 11 | this.name = "Greek Pizza"; 12 | } 13 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Pizza/CheesePizza.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.pizza; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | 5 | public class CheesePizza extends Pizza { 6 | public CheesePizza() { 7 | setName(); 8 | } 9 | 10 | public void setName() { 11 | this.name = "Cheese Pizza"; 12 | } 13 | } -------------------------------------------------------------------------------- /Decorator Pattern/Headfirst Design Book Example/Decaf.java: -------------------------------------------------------------------------------- 1 | package decorator.headfirst; 2 | 3 | import decorator.headfirst.Beverage; 4 | 5 | public class Decaf extends Beverage { 6 | private double COST = 1.05; 7 | 8 | public Decaf() { 9 | description = "Decaf"; 10 | } 11 | 12 | public double cost() { 13 | return COST; 14 | } 15 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Pizza/PepperoniPizza.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.pizza; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | 5 | public class PepperoniPizza extends Pizza { 6 | public PepperoniPizza() { 7 | setName(); 8 | } 9 | 10 | public void setName() { 11 | this.name = "Pepperoni Pizza"; 12 | } 13 | } -------------------------------------------------------------------------------- /Decorator Pattern/Headfirst Design Book Example/Espresso.java: -------------------------------------------------------------------------------- 1 | package decorator.headfirst; 2 | 3 | import decorator.headfirst.Beverage; 4 | 5 | public class Espresso extends Beverage { 6 | private double COST = 1.99; 7 | 8 | public Espresso() { 9 | description = "Espresso"; 10 | } 11 | 12 | public double cost() { 13 | return COST; 14 | } 15 | } -------------------------------------------------------------------------------- /Decorator Pattern/Headfirst Design Book Example/DarkRoast.java: -------------------------------------------------------------------------------- 1 | package decorator.headfirst; 2 | 3 | import decorator.headfirst.Beverage; 4 | 5 | public class DarkRoast extends Beverage { 6 | private double COST = 0.99; 7 | 8 | public DarkRoast() { 9 | description = "Dark Roast"; 10 | } 11 | 12 | public double cost() { 13 | return COST; 14 | } 15 | } -------------------------------------------------------------------------------- /Decorator Pattern/Headfirst Design Book Example/HouseBlend.java: -------------------------------------------------------------------------------- 1 | package decorator.headfirst; 2 | 3 | import decorator.headfirst.Beverage; 4 | 5 | public class HouseBlend extends Beverage { 6 | private double COST = 0.89; 7 | 8 | public HouseBlend() { 9 | description = "House Blend"; 10 | } 11 | 12 | public double cost() { 13 | return COST; 14 | } 15 | } -------------------------------------------------------------------------------- /Command Pattern/Newsletter Example/Service/MailService.java: -------------------------------------------------------------------------------- 1 | package commandpattern.newsletter.service; 2 | 3 | public class MailService { 4 | public void prepareWelcomeMailTemplate() { 5 | System.out.println("Prepare welcome mail template"); 6 | } 7 | 8 | public void sendWelcomeMail() { 9 | System.out.println("Send welcome mail to the subscribers"); 10 | } 11 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Pizza/NYStyleGreekPizza.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.pizza; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | 5 | public class NYStyleGreekPizza extends Pizza { 6 | public NYStyleGreekPizza() { 7 | setName(); 8 | } 9 | 10 | public void setName() { 11 | this.name = "New York Style Greek Pizza"; 12 | } 13 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Actual Implementation/Observable/Observable.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.actual.observable; 2 | 3 | import observer.headfirst.actual.observer.Observer; 4 | 5 | public interface Observable { 6 | public void registerObserver(Observer observer); 7 | 8 | public void removeObserver(Observer observer); 9 | 10 | public void notifyObserver(); 11 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Pizza/NYStyleCheesePizza.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.pizza; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | 5 | public class NYStyleCheesePizza extends Pizza { 6 | public NYStyleCheesePizza() { 7 | setName(); 8 | } 9 | 10 | public void setName() { 11 | this.name = "New York Style Cheese Pizza"; 12 | } 13 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Pizza/ChicagoStyleGreekPizza.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.pizza; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | 5 | public class ChicagoStyleGreekPizza extends Pizza { 6 | public ChicagoStyleGreekPizza() { 7 | setName(); 8 | } 9 | 10 | public void setName() { 11 | this.name = "Chicago Style Greek Pizza"; 12 | } 13 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Pizza/ChicagoStyleCheesePizza.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.pizza; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | 5 | public class ChicagoStyleCheesePizza extends Pizza { 6 | public ChicagoStyleCheesePizza() { 7 | setName(); 8 | } 9 | 10 | public void setName() { 11 | this.name = "Chicago Style Cheese Pizza"; 12 | } 13 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Pizza/NYStylePepperoniPizza.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.pizza; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | 5 | public class NYStylePepperoniPizza extends Pizza { 6 | public NYStylePepperoniPizza() { 7 | setName(); 8 | } 9 | 10 | public void setName() { 11 | this.name = "New York Style Pepperoni Pizza"; 12 | } 13 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Effective Implementation/EggBurger.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.effective; 2 | 3 | import decorator.newsletter.effective.Meal; 4 | 5 | public class EggBurger extends Meal { 6 | private double PRICE = 65; 7 | 8 | public EggBurger() { 9 | description = "Egg Burger"; 10 | } 11 | 12 | public double getPrice() { 13 | return PRICE; 14 | } 15 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Effective Implementation/VegBurger.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.effective; 2 | 3 | import decorator.newsletter.effective.Meal; 4 | 5 | public class VegBurger extends Meal { 6 | private double PRICE = 60; 7 | 8 | public VegBurger() { 9 | description = "Veg Burger"; 10 | } 11 | 12 | public double getPrice() { 13 | return PRICE; 14 | } 15 | } -------------------------------------------------------------------------------- /Facade Pattern/Components/AudioBox.java: -------------------------------------------------------------------------------- 1 | package facade.components; 2 | 3 | public class AudioBox { 4 | public void turnOn() { 5 | System.out.println("Turn On Audio Box"); 6 | } 7 | 8 | public void turnOff() { 9 | System.out.println("Turn Off Audio Box"); 10 | } 11 | 12 | public void adjustVolume() { 13 | System.out.println("Adjusted the Audio Box's volume"); 14 | } 15 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Pizza/ChicagoStylePepperoniPizza.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.pizza; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | 5 | public class ChicagoStylePepperoniPizza extends Pizza { 6 | public ChicagoStylePepperoniPizza() { 7 | setName(); 8 | } 9 | 10 | public void setName() { 11 | this.name = "Chicago Style Pepperoni Pizza"; 12 | } 13 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Effective Implementation/ChickenBurger.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.effective; 2 | 3 | import decorator.newsletter.effective.Meal; 4 | 5 | public class ChickenBurger extends Meal { 6 | private double PRICE = 70; 7 | 8 | public ChickenBurger() { 9 | description = "Chicken Burger"; 10 | } 11 | 12 | public double getPrice() { 13 | return PRICE; 14 | } 15 | } -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/SimpleRemoteControl.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst; 2 | 3 | import commandpattern.headfirst.command.Command; 4 | 5 | public class SimpleRemoteControl { 6 | Command slot; 7 | 8 | public SimpleRemoteControl() {} 9 | 10 | public void setCommand(Command command) { 11 | slot = command; 12 | } 13 | 14 | public void buttonWasPressed() { 15 | slot.execute(); 16 | } 17 | } -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/Device/Light.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst.device; 2 | 3 | public class Light { 4 | String area; 5 | 6 | public Light(String area) { 7 | this.area = area; 8 | } 9 | 10 | public void on() { 11 | System.out.println(area + " Light is switched On!"); 12 | } 13 | 14 | public void off() { 15 | System.out.println(area + " Light is switched Off!"); 16 | } 17 | } -------------------------------------------------------------------------------- /Command Pattern/Newsletter Example/Command/MacroCommand.java: -------------------------------------------------------------------------------- 1 | package commandpattern.newsletter.command; 2 | 3 | public class MacroCommand implements Command { 4 | Command[] commands; 5 | 6 | public MacroCommand(Command[] commands) { 7 | this.commands = commands; 8 | } 9 | 10 | @Override 11 | public void execute() { 12 | for(Command command : this.commands) { 13 | command.execute(); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Command Pattern/Newsletter Example/Service/MessageService.java: -------------------------------------------------------------------------------- 1 | package commandpattern.newsletter.service; 2 | 3 | public class MessageService { 4 | public void prepareAcknowledgementMessage() { 5 | System.out.println("Prepare acknowledgement message " 6 | + "mentioning the welcome mails are sent to the " 7 | + "subscribers"); 8 | } 9 | 10 | public void sendAcknowledgement() { 11 | System.out.println("Send acknowledgement to the Author"); 12 | } 13 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Class Explosion Implementation/BurgerKing.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.classexplosion; 2 | 3 | import decorator.newsletter.classexplosion.ChickenBurgerWithCheeseAndSauce; 4 | 5 | public class BurgerKing { 6 | public static void main(String[] args) { 7 | Meal meal = new ChickenBurgerWithCheeseAndSauce(); 8 | System.out.println("Order placed: " + meal.getDescription()); 9 | System.out.println("Cost: Rs. " + meal.getPrice()); 10 | } 11 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Device/Device.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.device; 2 | 3 | public abstract class Device { 4 | protected String name; 5 | 6 | public String getName() { 7 | return name; 8 | } 9 | 10 | public abstract void viewConfiguration(); 11 | 12 | public void box() { 13 | System.out.println("Boxing the " + this.name + "..."); 14 | } 15 | 16 | public void ship() { 17 | System.out.println("Shipping the " + this.name + "..."); 18 | } 19 | } -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/Command/LightOnCommand.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst.command; 2 | 3 | import commandpattern.headfirst.device.Light; 4 | 5 | public class LightOnCommand implements Command { 6 | Light light; 7 | 8 | public LightOnCommand(Light light) { 9 | this.light = light; 10 | } 11 | 12 | @Override 13 | public void execute() { 14 | light.on(); 15 | } 16 | 17 | @Override 18 | public void undo() { 19 | light.off(); 20 | } 21 | } -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/Command/LightOffCommand.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst.command; 2 | 3 | import commandpattern.headfirst.device.Light; 4 | 5 | public class LightOffCommand implements Command { 6 | Light light; 7 | 8 | public LightOffCommand(Light light) { 9 | this.light = light; 10 | } 11 | 12 | @Override 13 | public void execute() { 14 | light.off(); 15 | } 16 | 17 | @Override 18 | public void undo() { 19 | light.on(); 20 | } 21 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Inefficient Implementation/WeatherDataRunner.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.inefficient; 2 | 3 | import observer.headfirst.inefficient.CurrentConditionsDisplay; 4 | import observer.headfirst.inefficient.StatisticsDisplay; 5 | import observer.headfirst.inefficient.ForecastDisplay; 6 | 7 | public class WeatherDataRunner { 8 | public static void main(String[] args) { 9 | WeatherData weatherData = new WeatherData(); 10 | weatherData.measurementChanged(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/Command/GarageDoorUpCommand.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst.command; 2 | 3 | import commandpattern.headfirst.device.GarageDoor; 4 | 5 | public class GarageDoorUpCommand implements Command { 6 | GarageDoor garageDoor; 7 | 8 | public GarageDoorUpCommand(GarageDoor garageDoor) { 9 | this.garageDoor = garageDoor; 10 | } 11 | 12 | @Override 13 | public void execute() { 14 | garageDoor.up(); 15 | } 16 | 17 | @Override 18 | public void undo() { 19 | garageDoor.down(); 20 | } 21 | } -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/Command/GarageDoorDownCommand.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst.command; 2 | 3 | import commandpattern.headfirst.device.GarageDoor; 4 | 5 | public class GarageDoorDownCommand implements Command { 6 | GarageDoor garageDoor; 7 | 8 | public GarageDoorDownCommand(GarageDoor garageDoor) { 9 | this.garageDoor = garageDoor; 10 | } 11 | 12 | @Override 13 | public void execute() { 14 | garageDoor.down(); 15 | } 16 | 17 | @Override 18 | public void undo() { 19 | garageDoor.up(); 20 | } 21 | } -------------------------------------------------------------------------------- /Command Pattern/Newsletter Example/Command/SendWelcomeMail.java: -------------------------------------------------------------------------------- 1 | package commandpattern.newsletter.command; 2 | 3 | import commandpattern.newsletter.command.Command; 4 | import commandpattern.newsletter.service.MailService; 5 | 6 | public class SendWelcomeMail implements Command { 7 | private MailService mailService; 8 | 9 | public SendWelcomeMail(MailService mailService) { 10 | this.mailService = mailService; 11 | } 12 | 13 | @Override 14 | public void execute() { 15 | mailService.prepareWelcomeMailTemplate(); 16 | mailService.sendWelcomeMail(); 17 | } 18 | } -------------------------------------------------------------------------------- /Decorator Pattern/Headfirst Design Book Example/Soy.java: -------------------------------------------------------------------------------- 1 | package decorator.headfirst; 2 | 3 | import decorator.headfirst.CondimentDecorator; 4 | import decorator.headfirst.Beverage; 5 | 6 | public class Soy extends CondimentDecorator { 7 | private Beverage beverage; 8 | private double COST = 0.15; 9 | 10 | public Soy(Beverage beverage) { 11 | this.beverage = beverage; 12 | } 13 | 14 | public String getDescription() { 15 | return beverage.getDescription() + ", Soy"; 16 | } 17 | 18 | public double cost() { 19 | return beverage.cost() + COST; 20 | } 21 | } -------------------------------------------------------------------------------- /Decorator Pattern/Headfirst Design Book Example/Whip.java: -------------------------------------------------------------------------------- 1 | package decorator.headfirst; 2 | 3 | import decorator.headfirst.CondimentDecorator; 4 | import decorator.headfirst.Beverage; 5 | 6 | public class Whip extends CondimentDecorator { 7 | private Beverage beverage; 8 | private double COST = 0.10; 9 | 10 | public Whip(Beverage beverage) { 11 | this.beverage = beverage; 12 | } 13 | 14 | public String getDescription() { 15 | return beverage.getDescription() + ", Whip"; 16 | } 17 | 18 | public double cost() { 19 | return beverage.cost() + COST; 20 | } 21 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Effective Implementation/Chips.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.effective; 2 | 3 | import decorator.newsletter.effective.AddOn; 4 | import decorator.newsletter.effective.Meal; 5 | 6 | public class Chips extends AddOn { 7 | private double PRICE = 10.0; 8 | private Meal meal; 9 | 10 | public Chips(Meal meal) { 11 | this.meal = meal; 12 | } 13 | 14 | public String getDescription() { 15 | return meal.getDescription() + ", Chips"; 16 | } 17 | 18 | public double getPrice() { 19 | return PRICE + meal.getPrice(); 20 | } 21 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Effective Implementation/Sauce.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.effective; 2 | 3 | import decorator.newsletter.effective.AddOn; 4 | import decorator.newsletter.effective.Meal; 5 | 6 | public class Sauce extends AddOn { 7 | private double PRICE = 8.0; 8 | private Meal meal; 9 | 10 | public Sauce(Meal meal) { 11 | this.meal = meal; 12 | } 13 | 14 | public String getDescription() { 15 | return meal.getDescription() + ", Sauce"; 16 | } 17 | 18 | public double getPrice() { 19 | return PRICE + meal.getPrice(); 20 | } 21 | } -------------------------------------------------------------------------------- /Decorator Pattern/Headfirst Design Book Example/Mocha.java: -------------------------------------------------------------------------------- 1 | package decorator.headfirst; 2 | 3 | import decorator.headfirst.CondimentDecorator; 4 | import decorator.headfirst.Beverage; 5 | 6 | public class Mocha extends CondimentDecorator { 7 | private Beverage beverage; 8 | private double COST = 0.20; 9 | 10 | public Mocha(Beverage beverage) { 11 | this.beverage = beverage; 12 | } 13 | 14 | public String getDescription() { 15 | return beverage.getDescription() + ", Mocha"; 16 | } 17 | 18 | public double cost() { 19 | return beverage.cost() + COST; 20 | } 21 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Effective Implementation/Cheese.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.effective; 2 | 3 | import decorator.newsletter.effective.AddOn; 4 | import decorator.newsletter.effective.Meal; 5 | 6 | public class Cheese extends AddOn { 7 | private double PRICE = 20.0; 8 | private Meal meal; 9 | 10 | public Cheese(Meal meal) { 11 | this.meal = meal; 12 | } 13 | 14 | public String getDescription() { 15 | return meal.getDescription() + ", Cheese"; 16 | } 17 | 18 | public double getPrice() { 19 | return PRICE + meal.getPrice(); 20 | } 21 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Effective Implementation/Veggies.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.effective; 2 | 3 | import decorator.newsletter.effective.AddOn; 4 | import decorator.newsletter.effective.Meal; 5 | 6 | public class Veggies extends AddOn { 7 | private double PRICE = 15.0; 8 | private Meal meal; 9 | 10 | public Veggies(Meal meal) { 11 | this.meal = meal; 12 | } 13 | 14 | public String getDescription() { 15 | return meal.getDescription() + ", Veggies"; 16 | } 17 | 18 | public double getPrice() { 19 | return PRICE + meal.getPrice(); 20 | } 21 | } -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/Command/MacroCommand.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst.command; 2 | 3 | public class MacroCommand implements Command { 4 | Command[] commands; 5 | 6 | public MacroCommand(Command[] commands) { 7 | this.commands = commands; 8 | } 9 | 10 | @Override 11 | public void execute() { 12 | for(Command command : this.commands) { 13 | command.execute(); 14 | } 15 | } 16 | 17 | @Override 18 | public void undo() { 19 | for(Command command : this.commands) { 20 | command.undo(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Class Explosion Implementation/ChickenBurgerWithCheeseAndSauce.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.classexplosion; 2 | 3 | import decorator.newsletter.classexplosion.Meal; 4 | 5 | public class ChickenBurgerWithCheeseAndSauce extends Meal { 6 | public ChickenBurgerWithCheeseAndSauce() { 7 | description = "Chicken Burger with Cheese and Sauce"; 8 | } 9 | 10 | public double getPrice() { 11 | // Price of: 12 | // 1. Chicken Burger: Rs. 70 13 | // 2. Ceese: Rs. 20 14 | // 3. Sauce: Rs. 10 15 | return (70.0 + 20.0 + 10.0); 16 | } 17 | } -------------------------------------------------------------------------------- /Decorator Pattern/Headfirst Design Book Example/SteamedMilk.java: -------------------------------------------------------------------------------- 1 | package decorator.headfirst; 2 | 3 | import decorator.headfirst.CondimentDecorator; 4 | import decorator.headfirst.Beverage; 5 | 6 | public class SteamedMilk extends CondimentDecorator { 7 | private Beverage beverage; 8 | private double COST = 0.10; 9 | 10 | public SteamedMilk(Beverage beverage) { 11 | this.beverage = beverage; 12 | } 13 | 14 | public String getDescription() { 15 | return beverage.getDescription() + ", Steamed Milk"; 16 | } 17 | 18 | public double cost() { 19 | return beverage.cost() + COST; 20 | } 21 | } -------------------------------------------------------------------------------- /Strategy Pattern/ChromeBook.java: -------------------------------------------------------------------------------- 1 | package strategy; 2 | 3 | import strategy.Computer; 4 | import strategy.chipset.IntelChipset; 5 | import strategy.graphics.AmdRadeon; 6 | 7 | public class ChromeBook extends Computer { 8 | public ChromeBook() { 9 | super(new IntelChipset(), new AmdRadeon()); 10 | } 11 | 12 | public void display() { 13 | System.out.println("Starting the display"); 14 | } 15 | 16 | public void powerOn() { 17 | System.out.println("Chromebook Powering On!"); 18 | } 19 | 20 | public void powerOff() { 21 | System.out.println("Chromebook Powering Off!"); 22 | } 23 | } -------------------------------------------------------------------------------- /Command Pattern/Newsletter Example/Service/UserService.java: -------------------------------------------------------------------------------- 1 | package commandpattern.newsletter.service; 2 | 3 | public class UserService { 4 | public void readUsersFromDb() { 5 | System.out.println("Collect user rows from " 6 | + "the User table in database"); 7 | } 8 | 9 | public void filterSubscribers() { 10 | System.out.println("Filter the unsubscribed " 11 | + "users from the collected user rows"); 12 | } 13 | 14 | public void snedSubscribersDataToMailService() { 15 | System.out.println("Send filtered subscriber details " 16 | + "to the mail service"); 17 | } 18 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Factory Method Pattern/DeviceStore.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.factorymethod; 2 | 3 | import factory.newsletter.device.Device; 4 | 5 | public abstract class DeviceStore { 6 | enum DeviceType { 7 | MOBILE, 8 | WEARABLE, 9 | LAPTOP 10 | } 11 | 12 | public Device orderDevice(DeviceType deviceType) { 13 | Device device = createDevice(deviceType); 14 | 15 | device.viewConfiguration(); 16 | device.box(); 17 | device.ship(); 18 | 19 | return device; 20 | } 21 | 22 | protected abstract Device createDevice(DeviceType deviceType); 23 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Java In Built Implementation/WeatherMetric.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.inbuilt; 2 | 3 | public class WeatherMetric { 4 | public double temp; 5 | public double pressure; 6 | public double humidity; 7 | 8 | public WeatherMetric(double temp, double humidity, double pressure) { 9 | this.temp = temp; 10 | this.pressure = pressure; 11 | this.humidity = humidity; 12 | } 13 | 14 | public void setMetric(double temp, double humidity, double pressure) { 15 | this.temp = temp; 16 | this.pressure = pressure; 17 | this.humidity = humidity; 18 | } 19 | } -------------------------------------------------------------------------------- /Strategy Pattern/HpPavillion.java: -------------------------------------------------------------------------------- 1 | package strategy; 2 | 3 | import strategy.Computer; 4 | import strategy.chipset.IntelChipset; 5 | import strategy.graphics.NvidiaGeForce; 6 | 7 | public class HpPavillion extends Computer { 8 | public HpPavillion() { 9 | super(new IntelChipset(), new NvidiaGeForce()); 10 | } 11 | 12 | public void display() { 13 | System.out.println("Starting the display"); 14 | } 15 | 16 | public void powerOn() { 17 | System.out.println("HP Pavillion Powering On!"); 18 | } 19 | 20 | public void powerOff() { 21 | System.out.println("HP Pavillion Powering Off!"); 22 | } 23 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Factory Method Pattern/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.factorymethod; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | 5 | public abstract class PizzaStore { 6 | enum PizzaType { 7 | CHEESE, 8 | GREEK, 9 | PEPPERONI 10 | } 11 | 12 | public Pizza orderPizza(PizzaType pizzaType) { 13 | Pizza pizza = createPizza(pizzaType); 14 | 15 | pizza.prepare(); 16 | pizza.bake(); 17 | pizza.cut(); 18 | pizza.box(); 19 | 20 | return pizza; 21 | } 22 | 23 | protected abstract Pizza createPizza(PizzaType pizzaType); 24 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Alternate Implementation/BurgerKing.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.alternateimpl; 2 | 3 | import decorator.newsletter.alternateimpl.Meal; 4 | import decorator.newsletter.alternateimpl.ChickenBurger; 5 | 6 | public class BurgerKing { 7 | public static void main(String[] args) { 8 | // Order a Chicken Burger with Cheese and Sauce 9 | Meal burgerMeal = new ChickenBurger(); 10 | burgerMeal.putCheese(); 11 | burgerMeal.putSauce(); 12 | 13 | System.out.println("Order placed: " + burgerMeal.getDescription()); 14 | System.out.println("Cost: " + burgerMeal.getPrice()); 15 | } 16 | } -------------------------------------------------------------------------------- /Strategy Pattern/AppleMacbook.java: -------------------------------------------------------------------------------- 1 | package strategy; 2 | 3 | import strategy.Computer; 4 | import strategy.chipset.AppleM1Chipset; 5 | import strategy.graphics.NvidiaGeForce; 6 | 7 | public class AppleMacbook extends Computer { 8 | public AppleMacbook() { 9 | super(new AppleM1Chipset(), new NvidiaGeForce()); 10 | } 11 | 12 | public void display() { 13 | System.out.println("Starting the display"); 14 | } 15 | 16 | public void powerOn() { 17 | System.out.println("Apple Macbook Powering On!"); 18 | } 19 | 20 | public void powerOff() { 21 | System.out.println("Apple Macbook Powering Off!"); 22 | } 23 | } -------------------------------------------------------------------------------- /Command Pattern/Newsletter Example/Command/SendAcknowledgementMessage.java: -------------------------------------------------------------------------------- 1 | package commandpattern.newsletter.command; 2 | 3 | import commandpattern.newsletter.command.Command; 4 | import commandpattern.newsletter.service.MessageService; 5 | 6 | public class SendAcknowledgementMessage implements Command { 7 | private MessageService messageService; 8 | 9 | public SendAcknowledgementMessage(MessageService messageService) { 10 | this.messageService = messageService; 11 | } 12 | 13 | @Override 14 | public void execute() { 15 | messageService.prepareAcknowledgementMessage(); 16 | messageService.sendAcknowledgement(); 17 | } 18 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Device/MobileDevice.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.device; 2 | 3 | import factory.newsletter.device.Device; 4 | 5 | public class MobileDevice extends Device { 6 | public MobileDevice() { 7 | this.name = "Mobile Device"; 8 | } 9 | 10 | public void viewConfiguration() { 11 | System.out.println("=====================\n"); 12 | System.out.println("Configuration for Mobile Device: "); 13 | System.out.println("5 inch Standard Display"); 14 | System.out.println("8 GB RAM"); 15 | System.out.println("64 GB Storage"); 16 | System.out.println("=====================\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Java In Built Implementation/WeatherDataRunner.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.inbuilt; 2 | 3 | import observer.headfirst.inbuilt.WeatherData; 4 | import observer.headfirst.inbuilt.CurrentConditionsDisplay; 5 | 6 | public class WeatherDataRunner { 7 | public static void main(String[] args) { 8 | WeatherData weatherData = new WeatherData(); 9 | CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData); 10 | weatherData.setMeasurements(27.6, 10.0, 8.9); 11 | 12 | // Measurements changed 13 | weatherData.setMeasurements(31.2, 9.5, 11.2); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Command Pattern/Newsletter Example/Command/CollectSubscriberEmails.java: -------------------------------------------------------------------------------- 1 | package commandpattern.newsletter.command; 2 | 3 | import commandpattern.newsletter.command.Command; 4 | import commandpattern.newsletter.service.UserService; 5 | 6 | public class CollectSubscriberEmails implements Command { 7 | private UserService userService; 8 | 9 | public CollectSubscriberEmails(UserService userService) { 10 | this.userService = userService; 11 | } 12 | 13 | @Override 14 | public void execute() { 15 | userService.readUsersFromDb(); 16 | userService.filterSubscribers(); 17 | userService.snedSubscribersDataToMailService(); 18 | } 19 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Device/LaptopDevice.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.device; 2 | 3 | import factory.newsletter.device.Device; 4 | 5 | public class LaptopDevice extends Device { 6 | public LaptopDevice() { 7 | this.name = "Laptop Device"; 8 | } 9 | 10 | public void viewConfiguration() { 11 | System.out.println("=====================\n"); 12 | System.out.println("Configuration for Laptop Device: "); 13 | System.out.println("13 inch Standard Display"); 14 | System.out.println("8 GB RAM"); 15 | System.out.println("Standard Intel chipset"); 16 | System.out.println("=====================\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Device/ProLaptopDevice.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.device; 2 | 3 | import factory.newsletter.device.Device; 4 | 5 | public class ProLaptopDevice extends Device { 6 | public ProLaptopDevice() { 7 | this.name = "Pro Laptop Device"; 8 | } 9 | 10 | public void viewConfiguration() { 11 | System.out.println("=====================\n"); 12 | System.out.println("Configuration for Pro Laptop Device: "); 13 | System.out.println("16 inch Full HDR Display"); 14 | System.out.println("16 GB RAM"); 15 | System.out.println("M1 chipset"); 16 | System.out.println("=====================\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Device/ProMobileDevice.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.device; 2 | 3 | import factory.newsletter.device.Device; 4 | 5 | public class ProMobileDevice extends Device { 6 | public ProMobileDevice() { 7 | this.name = "Pro Mobile device"; 8 | } 9 | 10 | public void viewConfiguration() { 11 | System.out.println("=====================\n"); 12 | System.out.println("Configuration for Pro Mobile Device: "); 13 | System.out.println("5.5 inch HD XDR Display"); 14 | System.out.println("16 GB RAM"); 15 | System.out.println("128 GB Storage"); 16 | System.out.println("=====================\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Device/StandardMobileDevice.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.device; 2 | 3 | import factory.newsletter.device.Device; 4 | 5 | public class StandardMobileDevice extends Device { 6 | public StandardMobileDevice() { 7 | this.name = "Standard Mobile device"; 8 | } 9 | 10 | public void viewConfiguration() { 11 | System.out.println("=====================\n"); 12 | System.out.println("Configuration for Standard Mobile Device: "); 13 | System.out.println("5 inch Standard Display"); 14 | System.out.println("8 GB RAM"); 15 | System.out.println("64 GB Storage"); 16 | System.out.println("=====================\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Device/WearableDevice.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.device; 2 | 3 | import factory.newsletter.device.Device; 4 | 5 | public class WearableDevice extends Device { 6 | public WearableDevice() { 7 | this.name = "Wearable Device"; 8 | } 9 | 10 | public void viewConfiguration() { 11 | System.out.println("=====================\n"); 12 | System.out.println("Configuration for Wearable Device: "); 13 | System.out.println("2 inch Standard Display"); 14 | System.out.println("Heart Beat measurement support"); 15 | System.out.println("10+ Wrokout Activity tracking"); 16 | System.out.println("=====================\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Device/StandardLaptopDevice.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.device; 2 | 3 | import factory.newsletter.device.Device; 4 | 5 | public class StandardLaptopDevice extends Device { 6 | public StandardLaptopDevice() { 7 | this.name = "Standard Laptop Device"; 8 | } 9 | 10 | public void viewConfiguration() { 11 | System.out.println("=====================\n"); 12 | System.out.println("Configuration for Standard Laptop Device: "); 13 | System.out.println("13 inch Standard Display"); 14 | System.out.println("8 GB RAM"); 15 | System.out.println("Standard Intel chipset"); 16 | System.out.println("=====================\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Java In Built Implementation/WeatherData.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.inbuilt; 2 | 3 | import java.util.Observable; 4 | import observer.headfirst.inbuilt.WeatherMetric; 5 | 6 | public class WeatherData extends Observable { 7 | private WeatherMetric weatherMetric; 8 | 9 | public WeatherData() { 10 | weatherMetric = new WeatherMetric(0.0, 0.0, 0.0); 11 | } 12 | 13 | private void measurementsChanged() { 14 | setChanged(); 15 | notifyObservers(weatherMetric); 16 | } 17 | 18 | public void setMeasurements(double temp, double humidity, double pressure) { 19 | weatherMetric.setMetric(temp, humidity, pressure); 20 | measurementsChanged(); 21 | } 22 | } -------------------------------------------------------------------------------- /Singleton Pattern/Eager Initialization/CacheService.java: -------------------------------------------------------------------------------- 1 | package singleton.eager; 2 | 3 | import java.util.HashMap; 4 | 5 | // Singleton Class for Cache Service - Eager Implementation. 6 | public class CacheService { 7 | private static CacheService service = new CacheService(); 8 | private HashMap cacheMap; 9 | 10 | private CacheService() { 11 | cacheMap = new HashMap(); 12 | } 13 | 14 | public static CacheService getCache() { 15 | return service; 16 | } 17 | 18 | public String getValue(int key) { 19 | return cacheMap.getOrDefault(key, ""); 20 | } 21 | 22 | public void store(int key, String value) { 23 | cacheMap.put(key, value); 24 | } 25 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Pizza/Pizza.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.pizza; 2 | 3 | public abstract class Pizza { 4 | protected String name; 5 | 6 | public void prepare() { 7 | System.out.println("Preparing " + this.name + "..."); 8 | } 9 | 10 | public void bake() { 11 | System.out.println("Baking " + this.name + "..."); 12 | } 13 | 14 | public void cut() { 15 | System.out.println("Cutting " + this.name + " into slices..."); 16 | } 17 | 18 | public void box() { 19 | System.out.println("Boxing your " + this.name + "..."); 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public abstract void setName(); 27 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Device/ProWearableDevice.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.device; 2 | 3 | import factory.newsletter.device.Device; 4 | 5 | public class ProWearableDevice extends Device { 6 | public ProWearableDevice() { 7 | this.name = "Pro Wearable device"; 8 | } 9 | 10 | public void viewConfiguration() { 11 | System.out.println("=====================\n"); 12 | System.out.println("Configuration for Pro Wearable Device: "); 13 | System.out.println("2.5 inch Fulld HD Display"); 14 | System.out.println("Heart Beat measurement and Blood Oxygen support"); 15 | System.out.println("50+ Wrokout Activity tracking"); 16 | System.out.println("=====================\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Device/StandardWearableDevice.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.device; 2 | 3 | import factory.newsletter.device.Device; 4 | 5 | public class StandardWearableDevice extends Device { 6 | public StandardWearableDevice() { 7 | this.name = "Standard Wearable device"; 8 | } 9 | 10 | public void viewConfiguration() { 11 | System.out.println("=====================\n"); 12 | System.out.println("Configuration for Standard Wearable Device: "); 13 | System.out.println("2 inch Standard Display"); 14 | System.out.println("Heart Beat measurement support"); 15 | System.out.println("10+ Wrokout Activity tracking"); 16 | System.out.println("=====================\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Simple Factory Pattern/DeviceStore.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.simplefactory; 2 | 3 | import factory.newsletter.device.Device; 4 | import factory.newsletter.simplefactory.SimpleDeviceFactory.DeviceType; 5 | import factory.newsletter.simplefactory.SimpleDeviceFactory; 6 | 7 | public class DeviceStore { 8 | SimpleDeviceFactory factory; 9 | 10 | public DeviceStore(SimpleDeviceFactory factory) { 11 | this.factory = factory; 12 | } 13 | 14 | Device orderDevice(DeviceType deviceType) { 15 | Device device = this.factory.createDevice(deviceType); 16 | 17 | device.viewConfiguration(); 18 | device.box(); 19 | device.ship(); 20 | 21 | return device; 22 | } 23 | } -------------------------------------------------------------------------------- /Strategy Pattern/Runner.java: -------------------------------------------------------------------------------- 1 | package strategy; 2 | 3 | import strategy.HpPavillion; 4 | import strategy.AppleMacbook; 5 | import strategy.ChromeBook; 6 | 7 | public class Runner { 8 | public static void main(String[] args) { 9 | HpPavillion hpPavillion = new HpPavillion(); 10 | hpPavillion.powerOn(); 11 | hpPavillion.display(); 12 | hpPavillion.powerOff(); 13 | 14 | AppleMacbook macBook = new AppleMacbook(); 15 | macBook.powerOn(); 16 | macBook.display(); 17 | macBook.powerOff(); 18 | 19 | ChromeBook chromeBook = new ChromeBook(); 20 | chromeBook.powerOn(); 21 | chromeBook.display(); 22 | chromeBook.powerOff(); 23 | 24 | System.out.println(); 25 | } 26 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Simple Factory Pattern/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.simplefactory; 2 | 3 | import factory.headfirst.simplefactory.SimplePizzaFactory; 4 | import factory.headfirst.simplefactory.SimplePizzaFactory.PizzaType; 5 | import factory.headfirst.pizza.Pizza; 6 | 7 | public class PizzaStore { 8 | SimplePizzaFactory factory; 9 | 10 | public PizzaStore(SimplePizzaFactory factory) { 11 | this.factory = factory; 12 | } 13 | 14 | Pizza orderPizza(PizzaType pizzaType) { 15 | Pizza pizza = factory.createPizza(pizzaType); 16 | 17 | pizza.prepare(); 18 | pizza.bake(); 19 | pizza.cut(); 20 | pizza.box(); 21 | 22 | return pizza; 23 | } 24 | } -------------------------------------------------------------------------------- /Singleton Pattern/Lazy Initialization/CacheService.java: -------------------------------------------------------------------------------- 1 | package singleton.lazy; 2 | 3 | import java.util.HashMap; 4 | 5 | // Singleton Class for Cache Service - Lazy Implementation. 6 | public class CacheService { 7 | private static CacheService service = null; 8 | private HashMap cacheMap; 9 | 10 | private CacheService() { 11 | cacheMap = new HashMap(); 12 | } 13 | 14 | public static CacheService getCache() { 15 | if (service == null) { 16 | service = new CacheService(); 17 | } 18 | return service; 19 | } 20 | 21 | public String getValue(int key) { 22 | return cacheMap.getOrDefault(key, ""); 23 | } 24 | 25 | public void store(int key, String value) { 26 | cacheMap.put(key, value); 27 | } 28 | } -------------------------------------------------------------------------------- /Singleton Pattern/Synchronized Implementation/CacheService.java: -------------------------------------------------------------------------------- 1 | package singleton.sync; 2 | 3 | import java.util.HashMap; 4 | 5 | // Singleton Class for Cache Service - Synchronized Implementation. 6 | public class CacheService { 7 | private static CacheService service = null; 8 | private HashMap cacheMap; 9 | 10 | private CacheService() { 11 | cacheMap = new HashMap(); 12 | } 13 | 14 | public static synchronized CacheService getCache() { 15 | if (service == null) { 16 | service = new CacheService(); 17 | } 18 | return service; 19 | } 20 | 21 | public String getValue(int key) { 22 | return cacheMap.getOrDefault(key, ""); 23 | } 24 | 25 | public void store(int key, String value) { 26 | cacheMap.put(key, value); 27 | } 28 | } -------------------------------------------------------------------------------- /Strategy Pattern/Computer.java: -------------------------------------------------------------------------------- 1 | package strategy; 2 | 3 | import strategy.chipset.Chipset; 4 | import strategy.graphics.GraphicsCard; 5 | 6 | public abstract class Computer { 7 | private Chipset chipset; 8 | private GraphicsCard graphicsCard; 9 | 10 | public Computer(Chipset chipset, GraphicsCard graphicsCard) { 11 | this.chipset = chipset; 12 | this.graphicsCard = graphicsCard; 13 | configure(); 14 | } 15 | 16 | private void configure() { 17 | System.out.println(); 18 | System.out.println("Configuring the computer..."); 19 | this.chipset.installChipset(); 20 | this.graphicsCard.installGraphicsCard(); 21 | System.out.println("Success!"); 22 | System.out.println(); 23 | } 24 | 25 | public abstract void display(); 26 | 27 | public abstract void powerOn(); 28 | 29 | public abstract void powerOff(); 30 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Effective Implementation/BurgerKing.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.effective; 2 | 3 | import decorator.newsletter.effective.Meal; 4 | import decorator.newsletter.effective.ChickenBurger; 5 | import decorator.newsletter.effective.Cheese; 6 | import decorator.newsletter.effective.Veggies; 7 | import decorator.newsletter.effective.Chips; 8 | 9 | public class BurgerKing { 10 | public static void main(String[] args) { 11 | // Order: Chicken Burger with Cheese, Veggies and Chips 12 | Meal burgerMeal = new ChickenBurger(); 13 | burgerMeal = new Cheese(burgerMeal); 14 | burgerMeal = new Veggies(burgerMeal); 15 | burgerMeal = new Chips(burgerMeal); 16 | 17 | System.out.println("Order placed: " + burgerMeal.getDescription()); 18 | System.out.println("Price: " + burgerMeal.getPrice()); 19 | } 20 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Inefficient Implementation/StatisticsDisplay.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.inefficient; 2 | 3 | public class StatisticsDisplay { 4 | private double temp; 5 | private double pressure; 6 | private double humidity; 7 | 8 | public void update(double temp, double humidity, double pressure) { 9 | this.temp = temp; 10 | this.humidity = humidity; 11 | this.pressure = pressure; 12 | display(); 13 | } 14 | 15 | public void display() { 16 | // Calculate Staistics 17 | System.out.println("================================="); 18 | System.out.println("Current Stats: "); 19 | System.out.println("Temperature - " + temp); 20 | System.out.println("Humidity - " + humidity); 21 | System.out.println("Pressure - " + pressure); 22 | System.out.println(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Inefficient Implementation/ForecastDisplay.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.inefficient; 2 | 3 | public class ForecastDisplay { 4 | private double temp; 5 | private double pressure; 6 | private double humidity; 7 | 8 | public void update(double temp, double humidity, double pressure) { 9 | this.temp = temp; 10 | this.humidity = humidity; 11 | this.pressure = pressure; 12 | display(); 13 | } 14 | 15 | public void display() { 16 | // Calculate Forecast 17 | System.out.println("================================="); 18 | System.out.println("ForecastDisplay Stats: "); 19 | System.out.println("Temperature - " + temp); 20 | System.out.println("Humidity - " + humidity); 21 | System.out.println("Pressure - " + pressure); 22 | System.out.println(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Facade Pattern/Runner.java: -------------------------------------------------------------------------------- 1 | package facade; 2 | 3 | import facade.components.AudioBox; 4 | import facade.components.Display; 5 | import facade.components.Xbox; 6 | import facade.components.Lights; 7 | import facade.gamingstation.GamingStation; 8 | 9 | public class Runner { 10 | public static void main(String[] args) { 11 | // Initialize all the sub components; 12 | AudioBox audioBox = new AudioBox(); 13 | Display display = new Display(); 14 | Xbox xbox = new Xbox(); 15 | Lights lights = new Lights(); 16 | 17 | // Start the Gaming Station. 18 | GamingStation gamingstation = 19 | new GamingStation(audioBox, display, xbox, lights); 20 | 21 | // Power On the Gaming Station. 22 | gamingstation.powerOnGamingStation(); 23 | 24 | // Power Off the Gaming Station. 25 | gamingstation.powerOffGamingStation(); 26 | } 27 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Inefficient Implementation/CurrentConditionsDisplay.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.inefficient; 2 | 3 | public class CurrentConditionsDisplay { 4 | private double temp; 5 | private double pressure; 6 | private double humidity; 7 | 8 | public void update(double temp, double humidity, double pressure) { 9 | this.temp = temp; 10 | this.humidity = humidity; 11 | this.pressure = pressure; 12 | display(); 13 | } 14 | 15 | public void display() { 16 | // Display current conditions. 17 | System.out.println("================================="); 18 | System.out.println("Current Conditions: "); 19 | System.out.println("Temperature - " + temp); 20 | System.out.println("Humidity - " + humidity); 21 | System.out.println("Pressure - " + pressure); 22 | System.out.println(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Inefficient Implementation/Runner.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.inefficient; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | import factory.headfirst.inefficient.PizzaStore; 5 | import factory.headfirst.inefficient.PizzaStore.PizzaType; 6 | 7 | public class Runner { 8 | public static void main(String[] args) { 9 | // Initializing a Pizza Store 10 | PizzaStore pizzaStore = new PizzaStore(); 11 | 12 | // Ordering a Cheese Pizze 13 | Pizza cheesePizza = pizzaStore.orderPizza(PizzaType.CHEESE); 14 | System.out.println("Pizza Ordered: " + cheesePizza.getName()); 15 | 16 | System.out.println("\n ====================== \n"); 17 | 18 | // Ordering a Pepperoni Pizze 19 | Pizza pepperoniPizza = pizzaStore.orderPizza(PizzaType.PEPPERONI); 20 | System.out.println("Pizza Ordered: " + pepperoniPizza.getName()); 21 | } 22 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Inefficient Implementation/Runner.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.inefficient; 2 | 3 | import factory.newsletter.device.Device; 4 | import factory.newsletter.inefficient.DeviceStore; 5 | import factory.newsletter.inefficient.DeviceStore.DeviceType; 6 | 7 | public class Runner { 8 | public static void main(String[] args) { 9 | // Initializing a Device Store 10 | DeviceStore deviceStore = new DeviceStore(); 11 | 12 | // Ordering a Mobile Device 13 | Device mobileDevice = deviceStore.orderDevice(DeviceType.MOBILE); 14 | System.out.println("Device Ordered: " + mobileDevice.getName()); 15 | 16 | System.out.println("\n ====================== \n"); 17 | 18 | // Ordering a Wearable Device 19 | Device wearableDevice = deviceStore.orderDevice(DeviceType.WEARABLE); 20 | System.out.println("Device Ordered: " + wearableDevice.getName()); 21 | } 22 | } -------------------------------------------------------------------------------- /Singleton Pattern/Eager Initialization/Runner.java: -------------------------------------------------------------------------------- 1 | package singleton.eager; 2 | 3 | import singleton.eager.CacheService; 4 | 5 | public class Runner { 6 | public static void main(String[] args) { 7 | System.out.println("============== Eager Implementation ============== \n"); 8 | CacheService service = CacheService.getCache(); 9 | System.out.println("Got the instance for the first time \n"); 10 | 11 | service.store(1, "Saurav"); 12 | System.out.println("Added a key value pair: [1, Saurav] \n"); 13 | 14 | CacheService secondService = CacheService.getCache(); 15 | System.out.println("Got the instance for the second time \n"); 16 | 17 | String value = secondService.getValue(1); 18 | System.out.println("Value returned from the second Cache service: " + value); 19 | System.out.println("This proves that a single instance will be returned everytime. \n"); 20 | } 21 | } -------------------------------------------------------------------------------- /Singleton Pattern/Lazy Initialization/Runner.java: -------------------------------------------------------------------------------- 1 | package singleton.lazy; 2 | 3 | import singleton.lazy.CacheService; 4 | 5 | public class Runner { 6 | public static void main(String[] args) { 7 | System.out.println("============== Lazy Implementation ============== \n"); 8 | CacheService service = CacheService.getCache(); 9 | System.out.println("Got the instance for the first time \n"); 10 | 11 | service.store(1, "Saurav"); 12 | System.out.println("Added a key value pair: [1, Saurav] \n"); 13 | 14 | CacheService secondService = CacheService.getCache(); 15 | System.out.println("Got the instance for the second time \n"); 16 | 17 | String value = secondService.getValue(1); 18 | System.out.println("Value returned from the second Cache service: " + value); 19 | System.out.println("This proves that a single instance will be returned everytime. \n"); 20 | } 21 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Factory Method Pattern/ProDeviceStore.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.factorymethod; 2 | 3 | import factory.newsletter.factorymethod.DeviceStore; 4 | import factory.newsletter.factorymethod.DeviceStore.DeviceType; 5 | import factory.newsletter.device.Device; 6 | import factory.newsletter.device.ProMobileDevice; 7 | import factory.newsletter.device.ProWearableDevice; 8 | import factory.newsletter.device.ProLaptopDevice; 9 | 10 | public class ProDeviceStore extends DeviceStore { 11 | 12 | public Device createDevice(DeviceType deviceType) { 13 | switch (deviceType) { 14 | case MOBILE: 15 | return new ProMobileDevice(); 16 | case WEARABLE: 17 | return new ProWearableDevice(); 18 | case LAPTOP: 19 | return new ProLaptopDevice(); 20 | } 21 | 22 | throw new AssertionError("Invalid or Unknown Device type"); 23 | } 24 | } -------------------------------------------------------------------------------- /Command Pattern/Newsletter Example/Scheduler.java: -------------------------------------------------------------------------------- 1 | package commandpattern.newsletter; 2 | 3 | import commandpattern.newsletter.command.Command; 4 | import java.util.Queue; 5 | import java.util.LinkedList; 6 | 7 | public class Scheduler { 8 | private Queue scheduler; 9 | 10 | public Scheduler() { 11 | scheduler = new LinkedList(); 12 | } 13 | 14 | public void scheduleRequest(Command command) { 15 | scheduler.add(command); 16 | } 17 | 18 | public void executeScheduledRequest() { 19 | if (scheduler.isEmpty()) { 20 | System.out.println("No requests to execute!"); 21 | return; 22 | } 23 | 24 | Command command = scheduler.remove(); 25 | System.out.println(); 26 | System.out.println("=========================="); 27 | command.execute(); 28 | System.out.println("=========================="); 29 | System.out.println(); 30 | } 31 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Simple Factory Pattern/Runner.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.simplefactory; 2 | 3 | import factory.newsletter.device.Device; 4 | import factory.newsletter.simplefactory.SimpleDeviceFactory.DeviceType; 5 | 6 | public class Runner { 7 | public static void main(String[] args) { 8 | // Initializing a Device Store 9 | SimpleDeviceFactory factory = new SimpleDeviceFactory(); 10 | DeviceStore deviceStore = new DeviceStore(factory); 11 | 12 | // Ordering a Mobile Device 13 | Device mobileDevice = deviceStore.orderDevice(DeviceType.MOBILE); 14 | System.out.println("Device Ordered: " + mobileDevice.getName()); 15 | 16 | System.out.println("\n====================== \n"); 17 | 18 | // Ordering a Laptop Device 19 | Device laptopDevice = deviceStore.orderDevice(DeviceType.LAPTOP); 20 | System.out.println("Device Ordered: " + laptopDevice.getName()); 21 | } 22 | } -------------------------------------------------------------------------------- /Singleton Pattern/Synchronized Implementation/Runner.java: -------------------------------------------------------------------------------- 1 | package singleton.sync; 2 | 3 | import singleton.sync.CacheService; 4 | 5 | public class Runner { 6 | public static void main(String[] args) { 7 | System.out.println("============== Synchronized Implementation ============== \n"); 8 | CacheService service = CacheService.getCache(); 9 | System.out.println("Got the instance for the first time \n"); 10 | 11 | service.store(1, "Saurav"); 12 | System.out.println("Added a key value pair: [1, Saurav] \n"); 13 | 14 | CacheService secondService = CacheService.getCache(); 15 | System.out.println("Got the instance for the second time \n"); 16 | 17 | String value = secondService.getValue(1); 18 | System.out.println("Value returned from the second Cache service: " + value); 19 | System.out.println("This proves that a single instance will be returned everytime. \n"); 20 | } 21 | } -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/Command/CeilingFanHighCommand.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst.command; 2 | 3 | import commandpattern.headfirst.device.CeilingFan; 4 | 5 | public class CeilingFanHighCommand implements Command { 6 | CeilingFan ceilingFan; 7 | CeilingFan.SPEED previousSpeed; 8 | 9 | public CeilingFanHighCommand(CeilingFan ceilingFan) { 10 | this.ceilingFan = ceilingFan; 11 | } 12 | 13 | @Override 14 | public void execute() { 15 | previousSpeed = ceilingFan.getSpeed(); 16 | ceilingFan.high(); 17 | } 18 | 19 | @Override 20 | public void undo() { 21 | switch (previousSpeed) { 22 | case HIGH: 23 | ceilingFan.high(); 24 | case MEDIUM: 25 | ceilingFan.medium(); 26 | case LOW: 27 | ceilingFan.low(); 28 | case OFF: 29 | ceilingFan.off(); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Factory Method Pattern/NYStylePizzaStore.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.factorymethod; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | import factory.headfirst.pizza.NYStyleCheesePizza; 5 | import factory.headfirst.pizza.NYStyleGreekPizza; 6 | import factory.headfirst.pizza.NYStylePepperoniPizza; 7 | import factory.headfirst.factorymethod.PizzaStore; 8 | import factory.headfirst.factorymethod.PizzaStore.PizzaType; 9 | 10 | public class NYStylePizzaStore extends PizzaStore { 11 | 12 | protected Pizza createPizza(PizzaType pizzaType) { 13 | switch (pizzaType) { 14 | case CHEESE: 15 | return new NYStyleCheesePizza(); 16 | case GREEK: 17 | return new NYStyleGreekPizza(); 18 | case PEPPERONI: 19 | return new NYStylePepperoniPizza(); 20 | } 21 | throw new AssertionError("Invalid or Unknown Pizza type"); 22 | } 23 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Factory Method Pattern/StandardDeviceStore.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.factorymethod; 2 | 3 | import factory.newsletter.factorymethod.DeviceStore; 4 | import factory.newsletter.factorymethod.DeviceStore.DeviceType; 5 | import factory.newsletter.device.Device; 6 | import factory.newsletter.device.StandardMobileDevice; 7 | import factory.newsletter.device.StandardWearableDevice; 8 | import factory.newsletter.device.StandardLaptopDevice; 9 | 10 | public class StandardDeviceStore extends DeviceStore { 11 | 12 | public Device createDevice(DeviceType deviceType) { 13 | switch (deviceType) { 14 | case MOBILE: 15 | return new StandardMobileDevice(); 16 | case WEARABLE: 17 | return new StandardWearableDevice(); 18 | case LAPTOP: 19 | return new StandardLaptopDevice(); 20 | } 21 | 22 | throw new AssertionError("Invalid or Unknown Device type"); 23 | } 24 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Factory Method Pattern/ChicagoStylePizzaStore.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.factorymethod; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | import factory.headfirst.pizza.ChicagoStyleCheesePizza; 5 | import factory.headfirst.pizza.ChicagoStyleGreekPizza; 6 | import factory.headfirst.pizza.ChicagoStylePepperoniPizza; 7 | import factory.headfirst.factorymethod.PizzaStore; 8 | import factory.headfirst.factorymethod.PizzaStore.PizzaType; 9 | 10 | public class ChicagoStylePizzaStore extends PizzaStore { 11 | 12 | protected Pizza createPizza(PizzaType pizzaType) { 13 | switch (pizzaType) { 14 | case CHEESE: 15 | return new ChicagoStyleCheesePizza(); 16 | case GREEK: 17 | return new ChicagoStyleGreekPizza(); 18 | case PEPPERONI: 19 | return new ChicagoStylePepperoniPizza(); 20 | } 21 | throw new AssertionError("Invalid or Unknown Pizza type"); 22 | } 23 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Simple Factory Pattern/Runner.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.simplefactory; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | import factory.headfirst.simplefactory.PizzaStore; 5 | import factory.headfirst.simplefactory.SimplePizzaFactory.PizzaType; 6 | 7 | public class Runner { 8 | public static void main(String[] args) { 9 | // Initializing a Pizza Store 10 | SimplePizzaFactory factory = new SimplePizzaFactory(); 11 | PizzaStore pizzaStore = new PizzaStore(factory); 12 | 13 | // Ordering a Cheese Pizze 14 | Pizza cheesePizza = pizzaStore.orderPizza(PizzaType.CHEESE); 15 | System.out.println("Pizza Ordered: " + cheesePizza.getName()); 16 | 17 | System.out.println("\n ====================== \n"); 18 | 19 | // Ordering a Pepperoni Pizze 20 | Pizza pepperoniPizza = pizzaStore.orderPizza(PizzaType.PEPPERONI); 21 | System.out.println("Pizza Ordered: " + pepperoniPizza.getName()); 22 | } 23 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Simple Factory Pattern/SimplePizzaFactory.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.simplefactory; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | import factory.headfirst.pizza.CheesePizza; 5 | import factory.headfirst.pizza.GreekPizza; 6 | import factory.headfirst.pizza.PepperoniPizza; 7 | 8 | public class SimplePizzaFactory { 9 | enum PizzaType { 10 | CHEESE, 11 | GREEK, 12 | PEPPERONI 13 | } 14 | 15 | public Pizza createPizza(PizzaType pizzaType) { 16 | Pizza pizza = null; 17 | 18 | switch (pizzaType) { 19 | case CHEESE: 20 | pizza = new CheesePizza(); 21 | break; 22 | case PEPPERONI: 23 | pizza = new PepperoniPizza(); 24 | break; 25 | case GREEK: 26 | pizza = new GreekPizza(); 27 | break; 28 | default: 29 | break; 30 | } 31 | 32 | return pizza; 33 | } 34 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Simple Factory Pattern/SimpleDeviceFactory.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.simplefactory; 2 | 3 | import factory.newsletter.device.Device; 4 | import factory.newsletter.device.MobileDevice; 5 | import factory.newsletter.device.WearableDevice; 6 | import factory.newsletter.device.LaptopDevice; 7 | 8 | public class SimpleDeviceFactory { 9 | enum DeviceType { 10 | MOBILE, 11 | WEARABLE, 12 | LAPTOP 13 | } 14 | 15 | public Device createDevice(DeviceType deviceType) { 16 | Device device = null; 17 | 18 | switch (deviceType) { 19 | case MOBILE: 20 | device = new MobileDevice(); 21 | break; 22 | case WEARABLE: 23 | device = new WearableDevice(); 24 | break; 25 | case LAPTOP: 26 | device = new LaptopDevice(); 27 | break; 28 | default: 29 | break; 30 | } 31 | 32 | return device; 33 | } 34 | } -------------------------------------------------------------------------------- /Observer Pattern/Newsletter Example/Observer/IndividualSubscriber.java: -------------------------------------------------------------------------------- 1 | package observer.newsletter.observer; 2 | 3 | import observer.newsletter.observable.Subject; 4 | 5 | public class IndividualSubscriber implements Observer { 6 | private String articleContent; 7 | private Subject newsletter; 8 | private String name; 9 | 10 | public IndividualSubscriber(String name, Subject newsletter) { 11 | this.newsletter = newsletter; 12 | this.name = name; 13 | this.newsletter.registerObserver(this); 14 | } 15 | 16 | public void update(String articleContent) { 17 | this.articleContent = articleContent; 18 | articleContentReceived(); 19 | } 20 | 21 | public void articleContentReceived() { 22 | System.out.println("======================="); 23 | System.out.println("New Article Content received by " + this.name); 24 | System.out.println("Article name: " + this.articleContent); 25 | System.out.println("======================="); 26 | System.out.println(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Observer Pattern/Newsletter Example/Observable/SystemsThatScale.java: -------------------------------------------------------------------------------- 1 | package observer.newsletter.observable; 2 | 3 | import observer.newsletter.observable.Subject; 4 | import observer.newsletter.observer.Observer; 5 | import java.util.ArrayList; 6 | 7 | public class SystemsThatScale implements Subject { 8 | private ArrayList observers; 9 | private String articleContent; 10 | 11 | public SystemsThatScale() { 12 | observers = new ArrayList<>(); 13 | } 14 | 15 | public void registerObserver(Observer o) { 16 | observers.add(o); 17 | } 18 | 19 | public void removeObserver(Observer o) { 20 | int index = observers.indexOf(o); 21 | if (index >= 0) { 22 | observers.remove(index); 23 | } 24 | } 25 | 26 | public void notifyObservers() { 27 | for (Observer o : observers) { 28 | o.update(articleContent); 29 | } 30 | } 31 | 32 | public void newArticlePublished(String articleContent) { 33 | this.articleContent = articleContent; 34 | notifyObservers(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Actual Implementation/WeatherDataRunner.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.actual; 2 | 3 | import observer.headfirst.actual.observable.WeatherData; 4 | import observer.headfirst.actual.observer.CurrentConditionsDisplay; 5 | import observer.headfirst.actual.observer.ForecastDisplay; 6 | import observer.headfirst.actual.observer.StatisticsDisplay; 7 | 8 | public class WeatherDataRunner { 9 | public static void main(String[] args) { 10 | WeatherData weatherData = new WeatherData(); 11 | CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData); 12 | ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); 13 | weatherData.setMeasurements(27.6, 10.0, 8.9); 14 | 15 | // Registering Statistics Display 16 | StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); 17 | // Remove Forecast Display 18 | weatherData.removeObserver(forecastDisplay); 19 | 20 | // Measurements changed 21 | weatherData.setMeasurements(31.2, 9.5, 11.2); 22 | } 23 | } -------------------------------------------------------------------------------- /Observer Pattern/Newsletter Example/Observer/CommunitySubscriber.java: -------------------------------------------------------------------------------- 1 | package observer.newsletter.observer; 2 | 3 | import observer.newsletter.observable.Subject; 4 | 5 | public class CommunitySubscriber implements Observer { 6 | private String articleContent; 7 | private Subject newsletter; 8 | private String name; 9 | 10 | public CommunitySubscriber(String name, Subject newsletter) { 11 | this.newsletter = newsletter; 12 | this.name = name; 13 | this.newsletter.registerObserver(this); 14 | } 15 | 16 | public void update(String articleContent) { 17 | this.articleContent = articleContent; 18 | sendToCommunityMembers(); 19 | } 20 | 21 | public void sendToCommunityMembers() { 22 | System.out.println("======================="); 23 | System.out.println( 24 | this.name + 25 | " is sending the article content to all the community members"); 26 | System.out.println("Article name: " + this.articleContent); 27 | System.out.println("======================="); 28 | System.out.println(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/Device/CeilingFan.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst.device; 2 | 3 | public class CeilingFan { 4 | public static enum SPEED { 5 | OFF, 6 | LOW, 7 | MEDIUM, 8 | HIGH, 9 | } 10 | 11 | private String location; 12 | private SPEED currentSpeed; 13 | 14 | public CeilingFan(String location) { 15 | this.location = location; 16 | currentSpeed = SPEED.OFF; 17 | } 18 | 19 | public void high() { 20 | currentSpeed = SPEED.HIGH; 21 | System.out.println(location + " Fan's speed set to High"); 22 | } 23 | 24 | public void medium() { 25 | currentSpeed = SPEED.MEDIUM; 26 | System.out.println(location + " Fan's speed set to Medium"); 27 | } 28 | 29 | public void low() { 30 | currentSpeed = SPEED.LOW; 31 | System.out.println(location + " Fan's speed set to Low"); 32 | } 33 | 34 | public void off() { 35 | currentSpeed = SPEED.OFF; 36 | System.out.println(location + " Fan is switched Off!"); 37 | } 38 | 39 | public SPEED getSpeed() { 40 | return currentSpeed; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Actual Implementation/Observer/ForecastDisplay.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.actual.observer; 2 | 3 | import observer.headfirst.actual.observable.Observable; 4 | 5 | public class ForecastDisplay implements Observer, DisplayElement { 6 | private Observable observable; 7 | private double temp; 8 | private double pressure; 9 | private double humidity; 10 | 11 | public ForecastDisplay(Observable observable) { 12 | this.observable = observable; 13 | observable.registerObserver(this); 14 | } 15 | 16 | public void update(double temp, double humidity, double pressure) { 17 | this.temp = temp; 18 | this.pressure = pressure; 19 | this.humidity = humidity; 20 | display(); 21 | } 22 | 23 | public void display() { 24 | // Calculate Forecast 25 | System.out.println("================================="); 26 | System.out.println("ForecastDisplay Stats -----------"); 27 | System.out.println("Temperature : " + temp); 28 | System.out.println("Humidity : " + humidity); 29 | System.out.println("Pressure : " + pressure); 30 | System.out.println(); 31 | } 32 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Actual Implementation/Observer/StatisticsDisplay.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.actual.observer; 2 | 3 | import observer.headfirst.actual.observable.Observable; 4 | 5 | public class StatisticsDisplay implements Observer, DisplayElement { 6 | private Observable observable; 7 | private double temp; 8 | private double pressure; 9 | private double humidity; 10 | 11 | public StatisticsDisplay(Observable observable) { 12 | this.observable = observable; 13 | observable.registerObserver(this); 14 | } 15 | 16 | public void update(double temp, double humidity, double pressure) { 17 | this.temp = temp; 18 | this.pressure = pressure; 19 | this.humidity = humidity; 20 | display(); 21 | } 22 | 23 | public void display() { 24 | // Calculate Forecast 25 | System.out.println("================================="); 26 | System.out.println("StatisticsDisplay Stats -----------"); 27 | System.out.println("Temperature : " + temp); 28 | System.out.println("Humidity : " + humidity); 29 | System.out.println("Pressure : " + pressure); 30 | System.out.println(); 31 | } 32 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Actual Implementation/Observer/CurrentConditionsDisplay.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.actual.observer; 2 | 3 | import observer.headfirst.actual.observable.Observable; 4 | 5 | public class CurrentConditionsDisplay implements Observer, DisplayElement { 6 | private Observable observable; 7 | private double temp; 8 | private double pressure; 9 | private double humidity; 10 | 11 | public CurrentConditionsDisplay(Observable observable) { 12 | this.observable = observable; 13 | observable.registerObserver(this); 14 | } 15 | 16 | public void update(double temp, double humidity, double pressure) { 17 | this.temp = temp; 18 | this.pressure = pressure; 19 | this.humidity = humidity; 20 | display(); 21 | } 22 | 23 | public void display() { 24 | // Display current conditions. 25 | System.out.println("================================="); 26 | System.out.println("Current Conditions --------------"); 27 | System.out.println("Temperature : " + temp); 28 | System.out.println("Humidity : " + humidity); 29 | System.out.println("Pressure : " + pressure); 30 | System.out.println(); 31 | } 32 | } -------------------------------------------------------------------------------- /Facade Pattern/GamingStation/GamingStation.java: -------------------------------------------------------------------------------- 1 | package facade.gamingstation; 2 | 3 | import facade.components.AudioBox; 4 | import facade.components.Display; 5 | import facade.components.Xbox; 6 | import facade.components.Lights; 7 | 8 | public class GamingStation { 9 | private AudioBox audioBox; 10 | private Display display; 11 | private Xbox xbox; 12 | private Lights lights; 13 | 14 | public GamingStation( 15 | AudioBox audioBox, Display display, Xbox xbox, Lights lights) { 16 | this.audioBox = audioBox; 17 | this.display = display; 18 | this.xbox = xbox; 19 | this.lights = lights; 20 | } 21 | 22 | public void powerOnGamingStation() { 23 | this.xbox.switchOn(); 24 | this.display.powerOn(); 25 | this.lights.switchOn(); 26 | this.audioBox.turnOn(); 27 | System.out.println("Gaming Station powered On!"); 28 | System.out.println(); 29 | } 30 | 31 | public void powerOffGamingStation() { 32 | this.xbox.switchOff(); 33 | this.display.powerOff(); 34 | this.lights.switchOff(); 35 | this.audioBox.turnOff(); 36 | System.out.println("Gaming Station powered Off!"); 37 | System.out.println(); 38 | } 39 | } -------------------------------------------------------------------------------- /Decorator Pattern/Alternative Implementation/StarbuzzCoffee.java: -------------------------------------------------------------------------------- 1 | package decorator.alternate; 2 | 3 | import decorator.alternate.Beverage; 4 | import decorator.alternate.HouseBlend; 5 | import decorator.alternate.DarkRoast; 6 | import decorator.alternate.Espresso; 7 | 8 | public class StarbuzzCoffee { 9 | public static void main(String[] args) { 10 | // Order: House Blend with Soy, Steamed Milk and a Whip 11 | Beverage beverage1 = new HouseBlend(); 12 | beverage1.setSoy(); 13 | beverage1.setSteamedMilk(); 14 | beverage1.setWhip(); 15 | System.out.println("Coffee ordered: " + beverage1.getDescription()); 16 | System.out.println("Cost: " + beverage1.cost()); 17 | 18 | // Order: Dark Roast with Mocha and a Whip 19 | Beverage beverage2 = new DarkRoast(); 20 | beverage2.setMocha(); 21 | beverage2.setWhip(); 22 | System.out.println("Coffee ordered: " + beverage2.getDescription()); 23 | System.out.println("Cost: " + beverage2.cost()); 24 | 25 | // Order: Espresso 26 | Beverage beverage3 = new Espresso(); 27 | System.out.println("Coffee ordered: " + beverage3.getDescription()); 28 | System.out.println("Cost: " + beverage3.cost()); 29 | } 30 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Inefficient Implementation/DeviceStore.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.inefficient; 2 | 3 | import factory.newsletter.device.Device; 4 | import factory.newsletter.device.MobileDevice; 5 | import factory.newsletter.device.WearableDevice; 6 | import factory.newsletter.device.LaptopDevice; 7 | 8 | public class DeviceStore { 9 | enum DeviceType { 10 | MOBILE, 11 | WEARABLE, 12 | LAPTOP 13 | } 14 | 15 | Device orderDevice(DeviceType deviceType) { 16 | Device device; 17 | 18 | switch (deviceType) { 19 | case MOBILE: 20 | device = new MobileDevice(); 21 | prepareDevice(device); 22 | return device; 23 | case WEARABLE: 24 | device = new WearableDevice(); 25 | prepareDevice(device); 26 | return device; 27 | case LAPTOP: 28 | device = new LaptopDevice(); 29 | prepareDevice(device); 30 | return device; 31 | } 32 | 33 | throw new AssertionError("Invalid or Unknown Device type"); 34 | } 35 | 36 | private void prepareDevice(Device device) { 37 | device.viewConfiguration(); 38 | device.box(); 39 | device.ship(); 40 | } 41 | } -------------------------------------------------------------------------------- /Observer Pattern/Newsletter Example/NewsletterRunner.java: -------------------------------------------------------------------------------- 1 | package observer.newsletter; 2 | 3 | import observer.newsletter.observer.Observer; 4 | import observer.newsletter.observer.IndividualSubscriber; 5 | import observer.newsletter.observer.CommunitySubscriber; 6 | import observer.newsletter.observable.Subject; 7 | import observer.newsletter.observable.SystemsThatScale; 8 | 9 | public class NewsletterRunner { 10 | public static void main(String[] args) { 11 | SystemsThatScale newsletter = new SystemsThatScale(); 12 | 13 | // Alex and Max are subscribed to the newsletter. 14 | Observer alex = new IndividualSubscriber("Alex", newsletter); 15 | Observer max = new IndividualSubscriber("Max", newsletter); 16 | 17 | // GeekForces organisation is subscribed to the newsletter. 18 | Observer geeksForces = new CommunitySubscriber("Geeks Force", newsletter); 19 | 20 | // New Article is published and all the subscribers are notified. 21 | newsletter.newArticlePublished("Observer Pattern explained"); 22 | 23 | // Max unsubscribes from the newsletter 24 | newsletter.removeObserver(max); 25 | 26 | // New Article is published and all the subscribers are notified. 27 | newsletter.newArticlePublished("Factory Pattern explained"); 28 | } 29 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Inefficient Implementation/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.inefficient; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | import factory.headfirst.pizza.CheesePizza; 5 | import factory.headfirst.pizza.GreekPizza; 6 | import factory.headfirst.pizza.PepperoniPizza; 7 | 8 | public class PizzaStore { 9 | enum PizzaType { 10 | CHEESE, 11 | GREEK, 12 | PEPPERONI 13 | } 14 | 15 | Pizza orderPizza(PizzaType pizzaType) { 16 | Pizza pizza; 17 | 18 | switch (pizzaType) { 19 | case CHEESE: 20 | pizza = new CheesePizza(); 21 | preparePizza(pizza); 22 | return pizza; 23 | case GREEK: 24 | pizza = new GreekPizza(); 25 | preparePizza(pizza); 26 | return pizza; 27 | case PEPPERONI: 28 | pizza = new PepperoniPizza(); 29 | preparePizza(pizza); 30 | return pizza; 31 | } 32 | 33 | throw new AssertionError("Invalid or Unknown Pizza type"); 34 | } 35 | 36 | private Pizza preparePizza(Pizza pizza) { 37 | pizza.prepare(); 38 | pizza.bake(); 39 | pizza.cut(); 40 | pizza.box(); 41 | return pizza; 42 | } 43 | } -------------------------------------------------------------------------------- /Factory Pattern/Head First Design Patterns Example/Factory Method Pattern/Runner.java: -------------------------------------------------------------------------------- 1 | package factory.headfirst.factorymethod; 2 | 3 | import factory.headfirst.pizza.Pizza; 4 | import factory.headfirst.factorymethod.PizzaStore.PizzaType; 5 | 6 | public class Runner { 7 | public static void main(String[] args) { 8 | System.out.println("\n ====================== \n"); 9 | 10 | // Create a New York Style Greek Pizza 11 | PizzaStore nyPizzaStore = new NYStylePizzaStore(); 12 | Pizza nyGreekPizza = nyPizzaStore.orderPizza(PizzaType.GREEK); 13 | System.out.println("Orederd Pizza: " + nyGreekPizza.getName()); 14 | 15 | System.out.println("\n ====================== \n"); 16 | 17 | // Create a Chicago Style Cheese Pizza 18 | PizzaStore chicagoPizzaStore = new ChicagoStylePizzaStore(); 19 | Pizza chicagoCheesePizza = chicagoPizzaStore.orderPizza(PizzaType.CHEESE); 20 | System.out.println("Orederd Pizza: " + chicagoCheesePizza.getName()); 21 | 22 | System.out.println("\n ====================== \n"); 23 | 24 | // Create New York Style Pepperoni Pizza 25 | Pizza nyPepperoniPizza = nyPizzaStore.orderPizza(PizzaType.PEPPERONI); 26 | System.out.println("Orederd Pizza: " + nyPepperoniPizza.getName()); 27 | 28 | System.out.println("\n ====================== \n"); 29 | } 30 | } -------------------------------------------------------------------------------- /Factory Pattern/Newsletter Example/Factory Method Pattern/Runner.java: -------------------------------------------------------------------------------- 1 | package factory.newsletter.factorymethod; 2 | 3 | import factory.newsletter.device.Device; 4 | import factory.newsletter.factorymethod.DeviceStore.DeviceType; 5 | 6 | public class Runner { 7 | public static void main(String[] args) { 8 | System.out.println("\n ====================== \n"); 9 | 10 | // Ordering a Pro Mobile Device 11 | ProDeviceStore proDeviceStore = new ProDeviceStore(); 12 | Device proMobileDevice = proDeviceStore.orderDevice(DeviceType.MOBILE); 13 | System.out.println("Oredered Device: " + proMobileDevice.getName()); 14 | 15 | System.out.println("\n ====================== \n"); 16 | 17 | // Ordering a Standard Wearable Device 18 | StandardDeviceStore standardDeviceStore = new StandardDeviceStore(); 19 | Device standardWearableDevice = standardDeviceStore.orderDevice(DeviceType.WEARABLE); 20 | System.out.println("Oredered Device: " + standardWearableDevice.getName()); 21 | 22 | System.out.println("\n ====================== \n"); 23 | 24 | // Ordering a Pro Laptop Device 25 | Device proLaptopDevice = proDeviceStore.orderDevice(DeviceType.LAPTOP); 26 | System.out.println("Oredered Device: " + proLaptopDevice.getName()); 27 | 28 | System.out.println("\n ====================== \n"); 29 | } 30 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Actual Implementation/Observable/WeatherData.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.actual.observable; 2 | 3 | import observer.headfirst.actual.observable.Observable; 4 | import observer.headfirst.actual.observer.Observer; 5 | import java.util.ArrayList; 6 | 7 | public class WeatherData implements Observable { 8 | private ArrayList observers; 9 | private double temp; 10 | private double pressure; 11 | private double humidity; 12 | 13 | public WeatherData() { 14 | observers = new ArrayList<>(); 15 | } 16 | 17 | public void registerObserver(Observer observer) { 18 | observers.add(observer); 19 | } 20 | 21 | public void removeObserver(Observer observer) { 22 | int index = observers.indexOf(observer); 23 | if (index >= 0) { 24 | observers.remove(index); 25 | } 26 | } 27 | 28 | public void notifyObserver() { 29 | for (Observer observer : observers) { 30 | observer.update(temp, pressure, humidity); 31 | } 32 | } 33 | 34 | private void measurementsChanged() { 35 | notifyObserver(); 36 | } 37 | 38 | public void setMeasurements(double temp, double humidity, double pressure) { 39 | this.temp = temp; 40 | this.humidity = humidity; 41 | this.pressure = pressure; 42 | measurementsChanged(); 43 | } 44 | } -------------------------------------------------------------------------------- /Decorator Pattern/Newsletter Example/Alternate Implementation/Meal.java: -------------------------------------------------------------------------------- 1 | package decorator.newsletter.alternateimpl; 2 | 3 | public class Meal { 4 | String description; 5 | double price; 6 | 7 | private boolean cheese; 8 | private boolean veggies; 9 | private boolean chips; 10 | private boolean sauce; 11 | 12 | public String getDescription() { 13 | if (cheese) { 14 | description += ", Cheese"; 15 | } 16 | if (veggies) { 17 | description += ", Veggies"; 18 | } 19 | if (chips) { 20 | description += ", Chips"; 21 | } 22 | if (sauce) { 23 | description += ", Sauce"; 24 | } 25 | return description; 26 | } 27 | 28 | public double getPrice() { 29 | if (cheese) { 30 | price += 20.0; 31 | } 32 | if (veggies) { 33 | price += 15.0; 34 | } 35 | if (chips) { 36 | price += 10.0; 37 | } 38 | if (sauce) { 39 | price += 8.0; 40 | } 41 | return price; 42 | } 43 | 44 | public void putCheese() { 45 | cheese = true; 46 | } 47 | 48 | public void putVeggies() { 49 | veggies = true; 50 | } 51 | 52 | public void putChips() { 53 | chips = true; 54 | } 55 | 56 | public void putSauce() { 57 | sauce = true; 58 | } 59 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Java In Built Implementation/CurrentConditionsDisplay.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.inbuilt; 2 | 3 | import java.util.Observer; 4 | import java.util.Observable; 5 | import observer.headfirst.inbuilt.WeatherData; 6 | import observer.headfirst.inbuilt.WeatherMetric; 7 | 8 | public class CurrentConditionsDisplay implements Observer { 9 | private Observable observable; 10 | private double temp; 11 | private double pressure; 12 | private double humidity; 13 | 14 | public CurrentConditionsDisplay(Observable observable) { 15 | this.observable = observable; 16 | this.observable.addObserver(this); 17 | } 18 | 19 | public void update(Observable obs, Object arg) { 20 | if (obs instanceof WeatherData) { 21 | WeatherData weatherData = (WeatherData)obs; 22 | WeatherMetric weatherMetric = (WeatherMetric)arg; 23 | this.temp = weatherMetric.temp; 24 | this.pressure = weatherMetric.pressure; 25 | this.humidity = weatherMetric.humidity; 26 | display(); 27 | } 28 | } 29 | 30 | public void display() { 31 | // Display current conditions. 32 | System.out.println("================================="); 33 | System.out.println("Current Conditions --------------"); 34 | System.out.println("Temperature : " + temp); 35 | System.out.println("Humidity : " + humidity); 36 | System.out.println("Pressure : " + pressure); 37 | System.out.println(); 38 | } 39 | } -------------------------------------------------------------------------------- /Decorator Pattern/Headfirst Design Book Example/StarbuzzCoffee.java: -------------------------------------------------------------------------------- 1 | package decorator.headfirst; 2 | 3 | import decorator.headfirst.Beverage; 4 | import decorator.headfirst.Espresso; 5 | import decorator.headfirst.DarkRoast; 6 | import decorator.headfirst.Mocha; 7 | import decorator.headfirst.Whip; 8 | import decorator.headfirst.SteamedMilk; 9 | import decorator.headfirst.Soy; 10 | import decorator.headfirst.HouseBlend; 11 | 12 | public class StarbuzzCoffee { 13 | public static void main(String[] args) { 14 | // Order: Espresso 15 | Beverage beverage1 = new Espresso(); 16 | System.out.println("Coffee ordered: " + beverage1.getDescription()); 17 | System.out.println("Total cost: " + beverage1.cost()); 18 | 19 | // Order: Dark Roast with double Mocha and a Whip 20 | Beverage beverage2 = new DarkRoast(); 21 | beverage2 = new Mocha(beverage2); 22 | beverage2 = new Mocha(beverage2); 23 | beverage2 = new Whip(beverage2); 24 | System.out.println("Coffee ordered: " + beverage2.getDescription()); 25 | System.out.println("Total cost: " + beverage2.cost()); 26 | 27 | // Order: House Blend with Soy, Steamed Milk and a Whip 28 | Beverage beverage3 = new HouseBlend(); 29 | beverage3 = new Soy(beverage3); 30 | beverage3 = new SteamedMilk(beverage3); 31 | beverage3 = new Whip(beverage3); 32 | System.out.println("Coffee ordered: " + beverage3.getDescription()); 33 | System.out.println("Total cost: " + beverage3.cost()); 34 | } 35 | } -------------------------------------------------------------------------------- /Observer Pattern/Head First Example/Inefficient Implementation/WeatherData.java: -------------------------------------------------------------------------------- 1 | package observer.headfirst.inefficient; 2 | 3 | import observer.headfirst.inefficient.CurrentConditionsDisplay; 4 | import observer.headfirst.inefficient.StatisticsDisplay; 5 | import observer.headfirst.inefficient.ForecastDisplay; 6 | 7 | public class WeatherData { 8 | private final CurrentConditionsDisplay currentConditionsDisplay; 9 | private final StatisticsDisplay statisticsDisplay; 10 | private final ForecastDisplay forecastDisplay; 11 | 12 | public WeatherData() { 13 | this.currentConditionsDisplay = new CurrentConditionsDisplay(); 14 | this.statisticsDisplay = new StatisticsDisplay(); 15 | this.forecastDisplay = new ForecastDisplay(); 16 | } 17 | 18 | public void measurementChanged() { 19 | double temp = getTemperature(); 20 | double humidity = getHumidity(); 21 | double pressure = getPressure(); 22 | 23 | currentConditionsDisplay.update(temp, humidity, pressure); 24 | statisticsDisplay.update(temp, humidity, pressure); 25 | forecastDisplay.update(temp, humidity, pressure); 26 | } 27 | 28 | public double getTemperature() { 29 | // Logic to get temperature from the sensors. 30 | return 32.2; 31 | } 32 | 33 | public double getHumidity() { 34 | // Logic to get humidiy from the sensors. 35 | return 10.1; 36 | } 37 | 38 | public double getPressure() { 39 | // Logic to get pressure from the sensors. 40 | return 22.2; 41 | } 42 | } -------------------------------------------------------------------------------- /Decorator Pattern/Alternative Implementation/Beverage.java: -------------------------------------------------------------------------------- 1 | package decorator.alternate; 2 | 3 | public class Beverage { 4 | private boolean mocha; 5 | private boolean soy; 6 | private boolean steamedMilk; 7 | private boolean whip; 8 | String description; 9 | double cost; 10 | 11 | public String getDescription() { 12 | if (mocha) { 13 | description += ", Mocha"; 14 | } 15 | if (soy) { 16 | description += ", Soy";; 17 | } 18 | if (whip) { 19 | description += ", Whip";; 20 | } 21 | if (steamedMilk) { 22 | description += ", Steamed Milk";; 23 | } 24 | return description; 25 | } 26 | 27 | public double cost() { 28 | if (mocha) { 29 | cost += 0.20; 30 | } 31 | if (soy) { 32 | cost += 0.15; 33 | } 34 | if (whip) { 35 | cost += 0.10; 36 | } 37 | if (steamedMilk) { 38 | cost += 0.10; 39 | } 40 | return cost; 41 | } 42 | 43 | public void setMocha() { 44 | mocha = true; 45 | } 46 | 47 | public void setSoy() { 48 | soy = true; 49 | } 50 | 51 | public void setWhip() { 52 | whip = true; 53 | } 54 | 55 | public void setSteamedMilk() { 56 | steamedMilk = true; 57 | } 58 | 59 | public boolean hasMocha() { 60 | return mocha; 61 | } 62 | 63 | public boolean hasSoy() { 64 | return soy; 65 | } 66 | 67 | public boolean hasWhip() { 68 | return whip; 69 | } 70 | 71 | public boolean hasSteamedMilk() { 72 | return steamedMilk; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Command Pattern/Headfirst Design Book Example/RemoteControl.java: -------------------------------------------------------------------------------- 1 | package commandpattern.headfirst; 2 | 3 | import commandpattern.headfirst.command.Command; 4 | import commandpattern.headfirst.command.NoCommand; 5 | 6 | public class RemoteControl { 7 | private static final int SLOT_SIZE = 7; 8 | 9 | Command[] offCommands; 10 | Command[] onCommands; 11 | Command undoCommand; 12 | 13 | public RemoteControl() { 14 | onCommands = new Command[SLOT_SIZE]; 15 | offCommands = new Command[SLOT_SIZE]; 16 | 17 | NoCommand noCommand = new NoCommand(); 18 | 19 | for (int i=0; i