├── src ├── chapter28 │ └── note.txt ├── chapter27 │ ├── jdpemvc.jar │ └── Client.java ├── chapter02 │ ├── Body.java │ ├── Chassis.java │ ├── Windows.java │ ├── CarBody.java │ ├── VanBody.java │ ├── CarChassis.java │ ├── CarWindows.java │ ├── VanChassis.java │ ├── VanWindows.java │ ├── AbstractVehicleFactory.java │ ├── CarFactory.java │ ├── VanFactory.java │ └── Client.java ├── chapter15 │ ├── Command.java │ ├── VolumeUpCommand.java │ ├── VolumeDownCommand.java │ ├── WindowUpCommand.java │ ├── WindowDownCommand.java │ ├── SpeechRecogniser.java │ └── ElectricWindow.java ├── chapter26 │ ├── Gearbox.java │ ├── ManualGearbox.java │ ├── AutomaticGearbox.java │ ├── Client.java │ └── GearboxFactory.java ├── chapter16 │ ├── Expression.java │ ├── CityExpression.java │ ├── Client.java │ ├── MostEasterlyExpression.java │ ├── MostNortherlyExpression.java │ ├── MostSoutherlyExpression.java │ ├── MostWesterlyExpression.java │ └── City.java ├── chapter01 │ ├── Engine.java │ ├── TurboEngine.java │ ├── StandardEngine.java │ ├── Coupe.java │ ├── Sport.java │ ├── BoxVan.java │ ├── Pickup.java │ ├── Saloon.java │ ├── Vehicle.java │ ├── AbstractVan.java │ ├── AbstractCar.java │ ├── AbstractEngine.java │ ├── Client.java │ └── AbstractVehicle.java ├── chapter05 │ ├── Engine.java │ ├── TurboEngine.java │ ├── StandardEngine.java │ ├── Coupe.java │ ├── Sport.java │ ├── BoxVan.java │ ├── Pickup.java │ ├── Saloon.java │ ├── AbstractCar.java │ ├── AbstractVan.java │ ├── Vehicle.java │ ├── Client.java │ ├── AbstractEngine.java │ ├── VehicleManager.java │ └── AbstractVehicle.java ├── chapter10 │ ├── Engine.java │ ├── TurboEngine.java │ ├── StandardEngine.java │ ├── AlloyWheeledVehicle.java │ ├── LeatherSeatedVehicle.java │ ├── MetallicPaintedVehicle.java │ ├── AbstractCar.java │ ├── AbstractVan.java │ ├── AbstractVehicleOption.java │ ├── Vehicle.java │ ├── Coupe.java │ ├── Sport.java │ ├── BoxVan.java │ ├── Saloon.java │ ├── Pickup.java │ ├── SatNavVehicle.java │ ├── AirConditionedVehicle.java │ ├── AbstractEngine.java │ ├── AbstractVehicle.java │ └── Client.java ├── chapter11 │ ├── Engine.java │ ├── TurboEngine.java │ ├── StandardEngine.java │ ├── Documentation.java │ ├── Client.java │ ├── Coupe.java │ ├── Sport.java │ ├── BoxVan.java │ ├── Pickup.java │ ├── Saloon.java │ ├── AbstractCar.java │ ├── AbstractVan.java │ ├── Vehicle.java │ ├── VehicleFacade.java │ ├── Registration.java │ └── AbstractEngine.java ├── chapter12 │ ├── DiagnosticTool.java │ ├── TurboEngine.java │ ├── StandardEngine.java │ ├── Engine.java │ ├── EngineDiagnosticTool.java │ ├── AbstractEngine.java │ └── EngineFlyweightFactory.java ├── chapter24 │ ├── Visitable.java │ ├── Camshaft.java │ ├── Piston.java │ ├── SparkPlug.java │ ├── TurboEngine.java │ ├── Engine.java │ ├── StandardEngine.java │ ├── EngineVisitor.java │ ├── Client.java │ ├── EngineDiagnostics.java │ └── EngineInventory.java ├── chapter22 │ ├── Engine.java │ ├── GearboxStrategy.java │ ├── TurboEngine.java │ ├── StandardEngine.java │ ├── Coupe.java │ ├── Sport.java │ ├── BoxVan.java │ ├── Pickup.java │ ├── Saloon.java │ ├── Vehicle.java │ ├── AbstractVan.java │ ├── Client.java │ ├── SportGearboxStrategy.java │ ├── StandardGearboxStrategy.java │ ├── AbstractEngine.java │ ├── AbstractVehicle.java │ └── AbstractCar.java ├── chapter25 │ ├── WarningLight.java │ ├── NullObjectLight.java │ ├── OilLevelLight.java │ ├── BrakeFluidLight.java │ └── Client.java ├── chapter03 │ ├── VehicleDirector.java │ ├── VanDirector.java │ ├── CarDirector.java │ ├── VehicleBuilder.java │ ├── Client.java │ ├── VanBuilder.java │ └── CarBuilder.java ├── chapter14 │ ├── EmailHandler.java │ ├── Client.java │ ├── ManagerEmailHandler.java │ ├── SalesEmailHandler.java │ ├── SpamEmailHandler.java │ ├── ServiceEmailHandler.java │ └── GeneralEnquiriesEmailHandler.java ├── chapter08 │ ├── TurboEngine.java │ ├── StandardEngine.java │ ├── StandardControls.java │ ├── Engine.java │ ├── SportControls.java │ ├── AbstractDriverControls.java │ └── Client.java ├── chapter29 │ ├── business │ │ ├── TurboEngine.java │ │ ├── Engine.java │ │ ├── StandardEngine.java │ │ ├── AbstractEngine.java │ │ └── EngineFactory.java │ ├── db │ │ ├── EntityKeyGenerator.java │ │ ├── EntityListener.java │ │ ├── AbstractEntityPersistenceStrategy.java │ │ ├── EntityCSVStrategy.java │ │ ├── EntityEvent.java │ │ └── EntitySerializationStrategy.java │ ├── main │ │ └── Main.java │ └── ui │ │ └── EnginesFrame.java ├── chapter20 │ ├── SpeedometerListener.java │ ├── SpeedometerEvent.java │ ├── SpeedMonitor2.java │ ├── Speedometer1.java │ ├── SpeedMonitor1.java │ ├── AutomaticGearbox.java │ └── Speedometer2.java ├── chapter06 │ ├── SerialNumberGenerator.java │ ├── SerialNumberGeneratorTraditional.java │ └── Client.java ├── chapter13 │ ├── Client.java │ └── EngineProxy.java ├── chapter21 │ ├── ClockSetupState.java │ ├── YearSetupState.java │ ├── FinishedSetupState.java │ ├── HourSetupState.java │ ├── MonthSetupState.java │ ├── MinuteSetupState.java │ └── DaySetupState.java ├── chapter07 │ ├── SuperGreenEngineAdapter.java │ ├── SuperGreenEngine.java │ └── Client.java ├── chapter09 │ ├── Part.java │ ├── Item.java │ ├── Client.java │ └── Assembly.java ├── chapter04 │ ├── VanFactory.java │ ├── CarFactory.java │ ├── VehicleFactory.java │ └── Client.java ├── chapter23 │ ├── Client.java │ ├── AbstractBookletPrinter.java │ ├── SaloonBooklet.java │ └── ServiceHistoryBooklet.java ├── chapter18 │ ├── Client.java │ ├── Ignition.java │ ├── Accelerator.java │ └── Brake.java ├── chapter19 │ ├── Speedometer1.java │ ├── SpeedometerMemento1.java │ ├── Speedometer2.java │ └── SpeedometerMemento2.java └── chapter17 │ ├── VanRange.java │ ├── CarRange.java │ └── Client.java ├── manifest.mf ├── nbproject ├── project.xml └── genfiles.properties └── README.md /src/chapter28/note.txt: -------------------------------------------------------------------------------- 1 | There are no Java source files associated with this chapter. 2 | -------------------------------------------------------------------------------- /src/chapter27/jdpemvc.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abraxabooks/jdpe2/HEAD/src/chapter27/jdpemvc.jar -------------------------------------------------------------------------------- /manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.java.j2seproject 4 | 5 | 6 | jdpe2 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | nbbuild.xml.data.CRC32=df602cfb 2 | nbbuild.xml.script.CRC32=3ecac5bb 3 | nbbuild.xml.stylesheet.CRC32=8064a381@1.75.2.48 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=df602cfb 7 | nbproject/build-impl.xml.script.CRC32=4020d2d6 8 | nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 9 | -------------------------------------------------------------------------------- /src/chapter02/Body.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public interface Body { 12 | 13 | public String getBodyParts(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/chapter15/Command.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter15; 10 | 11 | public interface Command { 12 | 13 | public void execute(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/chapter26/Gearbox.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter26; 10 | 11 | public abstract class Gearbox { 12 | 13 | public Gearbox() {} 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/chapter16/Expression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter16; 10 | 11 | public interface Expression { 12 | 13 | public City interpret(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/chapter02/Chassis.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public interface Chassis { 12 | 13 | public String getChassisParts(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/chapter02/Windows.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public interface Windows { 12 | 13 | public String getWindowParts(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/chapter01/Engine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public interface Engine { 12 | 13 | public int getSize(); 14 | public boolean isTurbo(); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/chapter05/Engine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public interface Engine { 12 | 13 | public int getSize(); 14 | public boolean isTurbo(); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/chapter10/Engine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public interface Engine { 12 | 13 | public int getSize(); 14 | public boolean isTurbo(); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/chapter11/Engine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public interface Engine { 12 | 13 | public int getSize(); 14 | public boolean isTurbo(); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/chapter12/DiagnosticTool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter12; 10 | 11 | public interface DiagnosticTool { 12 | 13 | public void runDiagnosis(Object obj); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/chapter24/Visitable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter24; 10 | 11 | public interface Visitable { 12 | 13 | public void acceptEngineVisitor(EngineVisitor visitor); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/chapter22/Engine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public interface Engine { 12 | 13 | // 14 | public int getSize(); 15 | public boolean isTurbo(); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter22/GearboxStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public interface GearboxStrategy { 12 | 13 | public void ensureCorrectGear(Engine engine, int speed); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/chapter25/WarningLight.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter25; 10 | 11 | public interface WarningLight { 12 | 13 | public void turnOn(); 14 | public void turnOff(); 15 | public boolean isOn(); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter02/CarBody.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public class CarBody implements Body { 12 | 13 | public String getBodyParts() { 14 | return "Body shell parts for a car"; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter02/VanBody.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public class VanBody implements Body { 12 | 13 | public String getBodyParts() { 14 | return "Body shell parts for a van"; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter03/VehicleDirector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter03; 10 | 11 | import chapter01.*; 12 | 13 | public abstract class VehicleDirector { 14 | 15 | public abstract Vehicle build(VehicleBuilder builder); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter14/EmailHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter14; 10 | 11 | public interface EmailHandler { 12 | 13 | public void setNextHandler(EmailHandler handler); 14 | public void processHandler(String email); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/chapter01/TurboEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public class TurboEngine extends AbstractEngine { 12 | 13 | public TurboEngine(int size) { 14 | super(size, true); // turbocharges 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter02/CarChassis.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public class CarChassis implements Chassis { 12 | 13 | public String getChassisParts() { 14 | return "Chassis parts for a car"; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter02/CarWindows.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public class CarWindows implements Windows { 12 | 13 | public String getWindowParts() { 14 | return "Window glassware for a car"; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter02/VanChassis.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public class VanChassis implements Chassis { 12 | 13 | public String getChassisParts() { 14 | return "Chassis parts for a van"; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter02/VanWindows.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public class VanWindows implements Windows { 12 | 13 | public String getWindowParts() { 14 | return "Window glassware for a van"; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter05/TurboEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public class TurboEngine extends AbstractEngine { 12 | 13 | public TurboEngine(int size) { 14 | super(size, true); // turbocharges 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter08/TurboEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter08; 10 | 11 | public class TurboEngine extends AbstractEngine { 12 | 13 | public TurboEngine(int size) { 14 | super(size, true); // turbocharges 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter10/TurboEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class TurboEngine extends AbstractEngine { 12 | 13 | public TurboEngine(int size) { 14 | super(size, true); // turbocharges 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter11/TurboEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public class TurboEngine extends AbstractEngine { 12 | 13 | public TurboEngine(int size) { 14 | super(size, true); // turbocharges 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter12/TurboEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter12; 10 | 11 | public class TurboEngine extends AbstractEngine { 12 | 13 | public TurboEngine(int size) { 14 | super(size, true); // turbocharges 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter24/Camshaft.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter24; 10 | 11 | public class Camshaft implements Visitable { 12 | 13 | public void acceptEngineVisitor(EngineVisitor visitor) { 14 | visitor.visit(this); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter24/Piston.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter24; 10 | 11 | public class Piston implements Visitable { 12 | 13 | public void acceptEngineVisitor(EngineVisitor visitor) { 14 | visitor.visit(this); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter29/business/TurboEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.business; 10 | 11 | public class TurboEngine extends AbstractEngine { 12 | 13 | public TurboEngine(int size) { 14 | super(size, true); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter22/TurboEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public class TurboEngine extends AbstractEngine { 12 | 13 | // 14 | public TurboEngine(int size) { 15 | super(size, true); // turbocharges 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/chapter24/SparkPlug.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter24; 10 | 11 | public class SparkPlug implements Visitable { 12 | 13 | public void acceptEngineVisitor(EngineVisitor visitor) { 14 | visitor.visit(this); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter24/TurboEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter24; 10 | 11 | public class TurboEngine extends AbstractEngine { 12 | 13 | // 14 | public TurboEngine(int size) { 15 | super(size, true); // turbocharges 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/chapter26/ManualGearbox.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter26; 10 | 11 | public class ManualGearbox extends Gearbox { 12 | 13 | public ManualGearbox() { 14 | System.out.println("New manual gearbox created"); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter29/business/Engine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.business; 10 | 11 | import java.io.*; 12 | 13 | public interface Engine extends Serializable { 14 | 15 | public int getSize(); 16 | public boolean isTurbo(); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/chapter01/StandardEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public class StandardEngine extends AbstractEngine { 12 | 13 | public StandardEngine(int size) { 14 | super(size, false); // not turbocharges 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter05/StandardEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public class StandardEngine extends AbstractEngine { 12 | 13 | public StandardEngine(int size) { 14 | super(size, false); // not turbocharges 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter08/StandardEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter08; 10 | 11 | public class StandardEngine extends AbstractEngine { 12 | 13 | public StandardEngine(int size) { 14 | super(size, false); // not turbocharges 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter10/StandardEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class StandardEngine extends AbstractEngine { 12 | 13 | public StandardEngine(int size) { 14 | super(size, false); // not turbocharges 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter11/StandardEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public class StandardEngine extends AbstractEngine { 12 | 13 | public StandardEngine(int size) { 14 | super(size, false); // not turbocharges 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter12/StandardEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter12; 10 | 11 | public class StandardEngine extends AbstractEngine { 12 | 13 | public StandardEngine(int size) { 14 | super(size, false); // not turbocharges 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter20/SpeedometerListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter20; 10 | 11 | import java.util.*; 12 | 13 | public interface SpeedometerListener extends EventListener { 14 | 15 | public void speedChanged(SpeedometerEvent event); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter29/business/StandardEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.business; 10 | 11 | public class StandardEngine extends AbstractEngine { 12 | 13 | public StandardEngine(int size) { 14 | super(size, false); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter26/AutomaticGearbox.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter26; 10 | 11 | public class AutomaticGearbox extends Gearbox { 12 | 13 | public AutomaticGearbox() { 14 | System.out.println("New automatic gearbox created"); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter22/StandardEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public class StandardEngine extends AbstractEngine { 12 | 13 | // 14 | public StandardEngine(int size) { 15 | super(size, false); // not turbocharges 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/chapter24/Engine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter24; 10 | 11 | public interface Engine extends Visitable { 12 | 13 | public int getSize(); 14 | public boolean isTurbo(); 15 | 16 | public void acceptEngineVisitor(EngineVisitor visitor); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/chapter24/StandardEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter24; 10 | 11 | public class StandardEngine extends AbstractEngine { 12 | 13 | // 14 | public StandardEngine(int size) { 15 | super(size, false); // not turbocharges 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/chapter14/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter14; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | String email = "I need my car repaired"; 15 | AbstractEmailHandler.handle(email); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | jdpe2 2 | ===== 3 | 4 | Java Design Pattern Essentials (Second Edition) - book source code 5 | 6 | You will find included in each chapter subfolder the source code from the book. 7 | You will need to compile the sources if you want them to run. 8 | 9 | All source code is provided AS-IS without any warranty of any kind. 10 | You may not re-publish the source by any means, including posting on the Internet. 11 | 12 | This relates to the book: Java Design Pattern Essentials - Second Edition, by Tony Bevis. Published by Ability First Limited. Available from http://abraxabooks.co.uk in the UK and from Amazon worldwide. 13 | 14 | Copyright 2012, Ability First Limited. 15 | -------------------------------------------------------------------------------- /src/chapter11/Documentation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public class Documentation { 12 | 13 | public static void printBrochure(Vehicle vehicle) { 14 | // code omitted... 15 | System.out.println("Brochure printed"); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/chapter08/StandardControls.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter08; 10 | 11 | public class StandardControls extends AbstractDriverControls { 12 | 13 | public StandardControls(Engine engine) { 14 | super(engine); 15 | } 16 | 17 | // No extra features 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/chapter29/db/EntityKeyGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.db; 10 | 11 | public enum EntityKeyGenerator { 12 | 13 | ENGINE; 14 | 15 | private int nextKey; 16 | 17 | synchronized int getNextKey() { 18 | return ++nextKey; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter02/AbstractVehicleFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public abstract class AbstractVehicleFactory { 12 | 13 | public abstract Body createBody(); 14 | public abstract Chassis createChassis(); 15 | public abstract Windows createWindows(); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter29/db/EntityListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.db; 10 | 11 | import java.util.*; 12 | 13 | public interface EntityListener extends EventListener { 14 | 15 | public void entityAdded(EntityEvent event); 16 | public void entityRestored(EntityEvent event); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/chapter06/SerialNumberGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter06; 10 | 11 | public enum SerialNumberGenerator { 12 | 13 | VEHICLE, ENGINE; 14 | 15 | private int count; 16 | 17 | public synchronized int getNextSerial() { 18 | return ++count; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter24/EngineVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter24; 10 | 11 | public interface EngineVisitor { 12 | 13 | public void visit(Camshaft camshaft); 14 | public void visit(Engine engine); 15 | public void visit(Piston piston); 16 | public void visit(SparkPlug sparkPlug); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/chapter13/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter13; 10 | 11 | import chapter12.*; 12 | 13 | public class Client { 14 | 15 | public static void main(String[] args) { 16 | Engine engine = new EngineProxy(1300, false); 17 | engine.diagnose(new EngineDiagnosticTool()); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/chapter08/Engine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter08; 10 | 11 | public interface Engine { 12 | 13 | public int getSize(); 14 | public boolean isTurbo(); 15 | 16 | public void start(); 17 | public void stop(); 18 | public void increasePower(); 19 | public void decreasePower(); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter11/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | Vehicle car = new Saloon(new StandardEngine(1300)); 15 | VehicleFacade facade= new VehicleFacade(); 16 | facade.prepareForSale(car); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/chapter21/ClockSetupState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter21; 10 | 11 | public interface ClockSetupState { 12 | 13 | public void previousValue(); 14 | public void nextValue(); 15 | public void selectValue(); 16 | 17 | public String getInstructions(); 18 | public int getSelectedValue(); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/chapter07/SuperGreenEngineAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter07; 10 | 11 | import chapter01.*; 12 | 13 | public class SuperGreenEngineAdapter extends AbstractEngine { 14 | 15 | public SuperGreenEngineAdapter(SuperGreenEngine greenEngine) { 16 | super(greenEngine.getEngineSize(), false); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/chapter01/Coupe.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public class Coupe extends AbstractCar { 12 | 13 | public Coupe(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Coupe(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter01/Sport.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public class Sport extends AbstractCar { 12 | 13 | public Sport(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Sport(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter01/BoxVan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public class BoxVan extends AbstractVan { 12 | 13 | public BoxVan(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public BoxVan(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter01/Pickup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public class Pickup extends AbstractVan { 12 | 13 | public Pickup(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Pickup(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter05/Coupe.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public class Coupe extends AbstractCar { 12 | 13 | public Coupe(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Coupe(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter05/Sport.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public class Sport extends AbstractCar { 12 | 13 | public Sport(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Sport(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter11/Coupe.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public class Coupe extends AbstractCar { 12 | 13 | public Coupe(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Coupe(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter11/Sport.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public class Sport extends AbstractCar { 12 | 13 | public Sport(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Sport(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter16/CityExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter16; 10 | 11 | public class CityExpression implements Expression { 12 | 13 | private City city; 14 | 15 | public CityExpression(City city) { 16 | this.city = city; 17 | } 18 | 19 | public City interpret() { 20 | return city; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/chapter01/Saloon.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public class Saloon extends AbstractCar { 12 | 13 | public Saloon(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Saloon(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter05/BoxVan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public class BoxVan extends AbstractVan { 12 | 13 | public BoxVan(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public BoxVan(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter05/Pickup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public class Pickup extends AbstractVan { 12 | 13 | public Pickup(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Pickup(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter05/Saloon.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public class Saloon extends AbstractCar { 12 | 13 | public Saloon(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Saloon(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter08/SportControls.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter08; 10 | 11 | public class SportControls extends AbstractDriverControls { 12 | 13 | public SportControls(Engine engine) { 14 | super(engine); 15 | } 16 | 17 | public void accelerateHard() { 18 | accelerate(); 19 | accelerate(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/chapter11/BoxVan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public class BoxVan extends AbstractVan { 12 | 13 | public BoxVan(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public BoxVan(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter11/Pickup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public class Pickup extends AbstractVan { 12 | 13 | public Pickup(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Pickup(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter11/Saloon.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public class Saloon extends AbstractCar { 12 | 13 | public Saloon(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Saloon(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter12/Engine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter12; 10 | 11 | public interface Engine { 12 | 13 | // Methods having intrinsic (i.e. shared) state 14 | public int getSize(); 15 | public boolean isTurbo(); 16 | 17 | // Methods having extrinsic (i.e. unshared) state 18 | public void diagnose(DiagnosticTool tool); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/chapter22/Coupe.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public class Coupe extends AbstractCar { 12 | 13 | // 14 | public Coupe(Engine engine) { 15 | this(engine, Vehicle.Colour.UNPAINTED); 16 | } 17 | 18 | public Coupe(Engine engine, Vehicle.Colour colour) { 19 | super(engine, colour); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/chapter22/Sport.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public class Sport extends AbstractCar { 12 | 13 | // 14 | public Sport(Engine engine) { 15 | this(engine, Vehicle.Colour.UNPAINTED); 16 | } 17 | 18 | public Sport(Engine engine, Vehicle.Colour colour) { 19 | super(engine, colour); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/chapter22/BoxVan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public class BoxVan extends AbstractVan { 12 | 13 | // 14 | public BoxVan(Engine engine) { 15 | this(engine, Vehicle.Colour.UNPAINTED); 16 | } 17 | 18 | public BoxVan(Engine engine, Vehicle.Colour colour) { 19 | super(engine, colour); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/chapter22/Pickup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public class Pickup extends AbstractVan { 12 | 13 | // 14 | public Pickup(Engine engine) { 15 | this(engine, Vehicle.Colour.UNPAINTED); 16 | } 17 | 18 | public Pickup(Engine engine, Vehicle.Colour colour) { 19 | super(engine, colour); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/chapter22/Saloon.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public class Saloon extends AbstractCar { 12 | 13 | // 14 | public Saloon(Engine engine) { 15 | this(engine, Vehicle.Colour.UNPAINTED); 16 | } 17 | 18 | public Saloon(Engine engine, Vehicle.Colour colour) { 19 | super(engine, colour); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/chapter01/Vehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public interface Vehicle { 12 | 13 | public enum Colour {UNPAINTED, BLUE, BLACK, GREEN, 14 | RED, SILVER, WHITE, YELLOW}; 15 | 16 | public Engine getEngine(); 17 | public Vehicle.Colour getColour(); 18 | public void paint(Vehicle.Colour colour); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/chapter10/AlloyWheeledVehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class AlloyWheeledVehicle extends AbstractVehicleOption { 12 | 13 | public AlloyWheeledVehicle(Vehicle vehicle) { 14 | super(vehicle); 15 | } 16 | 17 | public int getPrice() { 18 | return decoratedVehicle.getPrice() + 250; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter10/LeatherSeatedVehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class LeatherSeatedVehicle extends AbstractVehicleOption { 12 | 13 | public LeatherSeatedVehicle(Vehicle vehicle) { 14 | super(vehicle); 15 | } 16 | 17 | public int getPrice() { 18 | return decoratedVehicle.getPrice() + 1200; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter10/MetallicPaintedVehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class MetallicPaintedVehicle extends AbstractVehicleOption { 12 | 13 | public MetallicPaintedVehicle(Vehicle vehicle) { 14 | super(vehicle); 15 | } 16 | 17 | public int getPrice() { 18 | return decoratedVehicle.getPrice() + 750; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter15/VolumeUpCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter15; 10 | 11 | public class VolumeUpCommand implements Command { 12 | 13 | private Radio radio; 14 | 15 | public VolumeUpCommand(Radio radio) { 16 | this.radio = radio; 17 | } 18 | 19 | @Override 20 | public void execute() { 21 | radio.volumeUp(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter01/AbstractVan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public abstract class AbstractVan extends AbstractVehicle { 12 | 13 | public AbstractVan(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public AbstractVan(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter15/VolumeDownCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter15; 10 | 11 | public class VolumeDownCommand implements Command { 12 | 13 | private Radio radio; 14 | 15 | public VolumeDownCommand(Radio radio) { 16 | this.radio = radio; 17 | } 18 | 19 | @Override 20 | public void execute() { 21 | radio.volumeDown(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter22/Vehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public interface Vehicle { 12 | 13 | // 14 | public enum Colour {UNPAINTED, BLUE, BLACK, GREEN, 15 | RED, SILVER, WHITE, YELLOW}; 16 | 17 | public Engine getEngine(); 18 | public Vehicle.Colour getColour(); 19 | public void paint(Vehicle.Colour colour); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter25/NullObjectLight.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter25; 10 | 11 | public class NullObjectLight implements WarningLight { 12 | 13 | public void turnOn() { 14 | // Do nothing... 15 | } 16 | 17 | public void turnOff() { 18 | // Do nothing... 19 | } 20 | 21 | public boolean isOn() { 22 | return false; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter01/AbstractCar.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public abstract class AbstractCar extends AbstractVehicle { 12 | 13 | public AbstractCar(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public AbstractCar(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter05/AbstractCar.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public abstract class AbstractCar extends AbstractVehicle { 12 | 13 | public AbstractCar(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public AbstractCar(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter05/AbstractVan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public abstract class AbstractVan extends AbstractVehicle { 12 | 13 | public AbstractVan(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public AbstractVan(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter10/AbstractCar.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public abstract class AbstractCar extends AbstractVehicle { 12 | 13 | public AbstractCar(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public AbstractCar(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter10/AbstractVan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public abstract class AbstractVan extends AbstractVehicle { 12 | 13 | public AbstractVan(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public AbstractVan(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter10/AbstractVehicleOption.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public abstract class AbstractVehicleOption extends AbstractVehicle { 12 | 13 | protected Vehicle decoratedVehicle; 14 | 15 | public AbstractVehicleOption(Vehicle vehicle) { 16 | super(vehicle.getEngine(), vehicle.getColour()); 17 | decoratedVehicle = vehicle; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/chapter11/AbstractCar.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public abstract class AbstractCar extends AbstractVehicle { 12 | 13 | public AbstractCar(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public AbstractCar(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter11/AbstractVan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public abstract class AbstractVan extends AbstractVehicle { 12 | 13 | public AbstractVan(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public AbstractVan(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter22/AbstractVan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public abstract class AbstractVan extends AbstractVehicle { 12 | 13 | // 14 | public AbstractVan(Engine engine) { 15 | this(engine, Vehicle.Colour.UNPAINTED); 16 | } 17 | 18 | public AbstractVan(Engine engine, Vehicle.Colour colour) { 19 | super(engine, colour); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/chapter14/ManagerEmailHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter14; 10 | 11 | public class ManagerEmailHandler extends AbstractEmailHandler { 12 | 13 | protected String[] matchingWords() { 14 | return new String[]{"complain", "bad"}; 15 | } 16 | 17 | protected void handleHere(String email) { 18 | System.out.println("Email handled by manager."); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter14/SalesEmailHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter14; 10 | 11 | public class SalesEmailHandler extends AbstractEmailHandler { 12 | 13 | protected String[] matchingWords() { 14 | return new String[]{"buy", "purchase"}; 15 | } 16 | 17 | protected void handleHere(String email) { 18 | System.out.println("Email handled by sales department."); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter14/SpamEmailHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter14; 10 | 11 | public class SpamEmailHandler extends AbstractEmailHandler { 12 | 13 | protected String[] matchingWords() { 14 | return new String[]{"viagra", "pills", "medicines"}; 15 | } 16 | 17 | protected void handleHere(String email) { 18 | System.out.println("This is a spam email."); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter15/WindowUpCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter15; 10 | 11 | public class WindowUpCommand implements Command { 12 | 13 | private ElectricWindow window; 14 | 15 | public WindowUpCommand(ElectricWindow window) { 16 | this.window = window; 17 | } 18 | 19 | @Override 20 | public void execute() { 21 | window.closeWindow(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter15/WindowDownCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter15; 10 | 11 | public class WindowDownCommand implements Command { 12 | 13 | private ElectricWindow window; 14 | 15 | public WindowDownCommand(ElectricWindow window) { 16 | this.window = window; 17 | } 18 | 19 | @Override 20 | public void execute() { 21 | window.openWindow(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter14/ServiceEmailHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter14; 10 | 11 | public class ServiceEmailHandler extends AbstractEmailHandler { 12 | 13 | protected String[] matchingWords() { 14 | return new String[]{"service", "repair"}; 15 | } 16 | 17 | protected void handleHere(String email) { 18 | System.out.println("Email handled by service department."); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter02/CarFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public class CarFactory extends AbstractVehicleFactory { 12 | 13 | public Body createBody() { 14 | return new CarBody(); 15 | } 16 | 17 | public Chassis createChassis() { 18 | return new CarChassis(); 19 | } 20 | 21 | public Windows createWindows() { 22 | return new CarWindows(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter02/VanFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public class VanFactory extends AbstractVehicleFactory { 12 | 13 | public Body createBody() { 14 | return new VanBody(); 15 | } 16 | 17 | public Chassis createChassis() { 18 | return new VanChassis(); 19 | } 20 | 21 | public Windows createWindows() { 22 | return new VanWindows(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter10/Vehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public interface Vehicle { 12 | 13 | public enum Colour {UNPAINTED, BLUE, BLACK, GREEN, 14 | RED, SILVER, WHITE, YELLOW}; 15 | 16 | public Engine getEngine(); 17 | public Vehicle.Colour getColour(); 18 | public void paint(Vehicle.Colour colour); 19 | 20 | public int getPrice(); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/chapter09/Part.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter09; 10 | 11 | public class Part extends Item { 12 | 13 | public Part(String description, int cost) { 14 | super(description, cost); 15 | } 16 | 17 | // Empty implementation for unit parts... 18 | public void addItem(Item item) {} 19 | public void removeItem(Item item) {} 20 | public Item[] getItems() {return new Item[0];} 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/chapter10/Coupe.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class Coupe extends AbstractCar { 12 | 13 | public Coupe(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Coupe(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | public int getPrice() { 22 | return 7000; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter10/Sport.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class Sport extends AbstractCar { 12 | 13 | public Sport(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Sport(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | public int getPrice() { 22 | return 8000; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter26/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter26; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | // Create an automatic gearbox 15 | Gearbox auto = GearboxFactory.create(GearboxFactory.Type.AUTOMATIC); 16 | 17 | // Create a manual gearbox 18 | Gearbox manual = GearboxFactory.create(GearboxFactory.Type.MANUAL); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter10/BoxVan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class BoxVan extends AbstractVan { 12 | 13 | public BoxVan(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public BoxVan(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | public int getPrice() { 22 | return 10000; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter10/Saloon.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class Saloon extends AbstractCar { 12 | 13 | public Saloon(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Saloon(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | public int getPrice() { 22 | return 6000; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter14/GeneralEnquiriesEmailHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter14; 10 | 11 | public class GeneralEnquiriesEmailHandler extends AbstractEmailHandler { 12 | 13 | protected String[] matchingWords() { 14 | return new String[0]; // match anything 15 | } 16 | 17 | protected void handleHere(String email) { 18 | System.out.println("Email handled by general enquires."); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter05/Vehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public interface Vehicle extends Cloneable { 12 | 13 | public enum Colour {UNPAINTED, BLUE, BLACK, GREEN, 14 | RED, SILVER, WHITE, YELLOW}; 15 | 16 | public Engine getEngine(); 17 | public Vehicle.Colour getColour(); 18 | public void paint(Vehicle.Colour colour); 19 | 20 | public Object clone(); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/chapter10/Pickup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class Pickup extends AbstractVan { 12 | 13 | public Pickup(Engine engine) { 14 | this(engine, Vehicle.Colour.UNPAINTED); 15 | } 16 | 17 | public Pickup(Engine engine, Vehicle.Colour colour) { 18 | super(engine, colour); 19 | } 20 | 21 | 22 | public int getPrice() { 23 | return 9000; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/chapter20/SpeedometerEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter20; 10 | 11 | import java.util.*; 12 | 13 | public class SpeedometerEvent extends EventObject { 14 | 15 | private int speed; 16 | 17 | public SpeedometerEvent(Object source, int speed) { 18 | super(source); 19 | this.speed = speed; 20 | } 21 | 22 | public int getSpeed() { 23 | return speed; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/chapter26/GearboxFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter26; 10 | 11 | public class GearboxFactory { 12 | 13 | public enum Type {AUTOMATIC, MANUAL}; 14 | 15 | public static Gearbox create(Type type) { 16 | if (type == Type.AUTOMATIC) { 17 | return new AutomaticGearbox(); 18 | 19 | } else { 20 | return new ManualGearbox(); 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter03/VanDirector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter03; 10 | 11 | import chapter01.*; 12 | 13 | public class VanDirector extends VehicleDirector { 14 | 15 | public Vehicle build(VehicleBuilder builder) { 16 | builder.buildChassis(); 17 | builder.buildBody(); 18 | builder.buildReinforcedStorageArea(); 19 | builder.buildWindows(); 20 | return builder.getVehicle(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/chapter03/CarDirector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter03; 10 | 11 | import chapter01.*; 12 | 13 | public class CarDirector extends VehicleDirector { 14 | 15 | public Vehicle build(VehicleBuilder builder) { 16 | builder.buildChassis(); 17 | builder.buildBody(); 18 | builder.buildPassengerArea(); 19 | builder.buildBoot(); 20 | builder.buildWindows(); 21 | return builder.getVehicle(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter27/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter27; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | // Create the MVC classes 15 | SatNavModel model = new SatNavModel(); 16 | SatNavView view = new SatNavView(model); 17 | SatNavController controller = new SatNavController(model, view); 18 | 19 | // Show the view 20 | view.show(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/chapter03/VehicleBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter03; 10 | 11 | import chapter01.*; 12 | 13 | public abstract class VehicleBuilder { 14 | 15 | public void buildBody() {} 16 | public void buildBoot() {} 17 | public void buildChassis() {} 18 | public void buildPassengerArea() {} 19 | public void buildReinforcedStorageArea() {} 20 | public void buildWindows() {} 21 | 22 | public abstract Vehicle getVehicle(); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter03/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter03; 10 | 11 | import chapter01.*; 12 | 13 | public class Client { 14 | 15 | public static void main(String[] args) { 16 | AbstractCar car = new Saloon(new StandardEngine(1300)); 17 | VehicleBuilder builder = new CarBuilder(car); 18 | VehicleDirector director = new CarDirector(); 19 | Vehicle v = director.build(builder); 20 | System.out.println(v); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/chapter07/SuperGreenEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter07; 10 | 11 | public class SuperGreenEngine { 12 | 13 | private int engineSize; 14 | 15 | public SuperGreenEngine(int engineSize) { 16 | this.engineSize = engineSize; 17 | } 18 | 19 | public int getEngineSize() { 20 | return engineSize; 21 | } 22 | 23 | public String toString() { 24 | return "SUPER ENGINE " + engineSize; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/chapter10/SatNavVehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class SatNavVehicle extends AbstractVehicleOption { 12 | 13 | public SatNavVehicle(Vehicle vehicle) { 14 | super(vehicle); 15 | } 16 | 17 | public int getPrice() { 18 | return decoratedVehicle.getPrice() + 1500; 19 | } 20 | 21 | public void setDestination(String destination) { 22 | // code to set the destination... 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter10/AirConditionedVehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class AirConditionedVehicle extends AbstractVehicleOption { 12 | 13 | public AirConditionedVehicle(Vehicle vehicle) { 14 | super(vehicle); 15 | } 16 | 17 | public int getPrice() { 18 | return decoratedVehicle.getPrice() + 600; 19 | } 20 | 21 | public void setTemperature() { 22 | // code to set the temperature... 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter24/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter24; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | // Create an engine... 15 | Engine engine = new StandardEngine(1300); 16 | 17 | // Run diagnostics on the engine... 18 | engine.acceptEngineVisitor(new EngineDiagnostics()); 19 | 20 | // Run inventory on the engine... 21 | engine.acceptEngineVisitor(new EngineInventory()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter25/OilLevelLight.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter25; 10 | 11 | public class OilLevelLight implements WarningLight { 12 | 13 | private boolean on; 14 | 15 | public void turnOn() { 16 | on = true; 17 | System.out.println("Oil level light ON"); 18 | } 19 | 20 | public void turnOff() { 21 | on = false; 22 | System.out.println("Oil level light OFF"); 23 | } 24 | 25 | public boolean isOn() { 26 | return on; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/chapter29/db/AbstractEntityPersistenceStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.db; 10 | 11 | import java.io.*; 12 | 13 | public abstract class AbstractEntityPersistenceStrategy { 14 | 15 | String getFileName(EntityTable table) { 16 | return table.getClass().getSimpleName(); 17 | } 18 | 19 | abstract String getFileSuffix(); 20 | abstract void save(EntityTable table) throws IOException; 21 | abstract EntityTable restore(EntityTable table) throws IOException; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/chapter04/VanFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter04; 10 | 11 | import chapter01.*; 12 | 13 | public class VanFactory extends VehicleFactory { 14 | 15 | protected Vehicle selectVehicle(DrivingStyle style) { 16 | if ((style == DrivingStyle.ECONOMICAL) || 17 | (style == DrivingStyle.MIDRANGE)) { 18 | return new Pickup(new StandardEngine(2200)); 19 | 20 | } else { 21 | return new BoxVan(new TurboEngine(2500)); 22 | } 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter22/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | AbstractCar myCar = new Sport(new StandardEngine(2000)); 15 | myCar.setSpeed(20); 16 | myCar.setSpeed(40); 17 | 18 | System.out.println("Switching on sports mode gearbox..."); 19 | myCar.setGearboxStrategy(new SportGearboxStrategy()); 20 | myCar.setSpeed(20); 21 | myCar.setSpeed(40); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter25/BrakeFluidLight.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter25; 10 | 11 | public class BrakeFluidLight implements WarningLight { 12 | 13 | private boolean on; 14 | 15 | public void turnOn() { 16 | on = true; 17 | System.out.println("Brake fluid light ON"); 18 | } 19 | 20 | public void turnOff() { 21 | on = false; 22 | System.out.println("Brake fluid light OFF"); 23 | } 24 | 25 | public boolean isOn() { 26 | return on; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/chapter11/Vehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public interface Vehicle { 12 | 13 | public enum Colour {UNPAINTED, BLUE, BLACK, GREEN, 14 | RED, SILVER, WHITE, YELLOW}; 15 | 16 | public Engine getEngine(); 17 | public Vehicle.Colour getColour(); 18 | public void paint(Vehicle.Colour colour); 19 | 20 | public void cleanInterior(); 21 | public void cleanExteriorBody(); 22 | public void polishWindows(); 23 | public void takeForTestDrive(); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter15/SpeechRecogniser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter15; 10 | 11 | public class SpeechRecogniser { 12 | 13 | private Command upCommand, downCommand; 14 | 15 | public void setCommands(Command upCommand, Command downCommand) { 16 | this.upCommand = upCommand; 17 | this.downCommand = downCommand; 18 | } 19 | 20 | public void hearUpSpoken() { 21 | upCommand.execute(); 22 | } 23 | 24 | public void hearDownSpoken() { 25 | downCommand.execute(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/chapter12/EngineDiagnosticTool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter12; 10 | 11 | public class EngineDiagnosticTool implements DiagnosticTool { 12 | 13 | public void runDiagnosis(Object obj) { 14 | System.out.println("Starting engine diagnostic tool for " + obj); 15 | try { 16 | Thread.sleep(5000); 17 | System.out.println("Engine diagnosis complete"); 18 | } catch (InterruptedException ex) { 19 | System.out.println("Engine diagnosis interrupted"); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/chapter23/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter23; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | System.out.println("About to print a booklet for saloon cars"); 15 | AbstractBookletPrinter saloonBooklet = new SaloonBooklet(); 16 | saloonBooklet.print(); 17 | 18 | System.out.println("About to print a service history booklet"); 19 | AbstractBookletPrinter serviceBooklet = new ServiceHistoryBooklet(); 20 | serviceBooklet.print(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/chapter11/VehicleFacade.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public class VehicleFacade { 12 | 13 | public void prepareForSale(Vehicle vehicle) { 14 | Registration reg = new Registration(vehicle); 15 | reg.allocateVehicleNumber(); 16 | reg.allocateLicensePlate(); 17 | 18 | Documentation.printBrochure(vehicle); 19 | 20 | vehicle.cleanInterior(); 21 | vehicle.cleanExteriorBody(); 22 | vehicle.polishWindows(); 23 | vehicle.takeForTestDrive(); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/chapter11/Registration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | public class Registration { 12 | 13 | private Vehicle vehicle; 14 | 15 | public Registration(Vehicle vehicle) { 16 | this.vehicle = vehicle; 17 | } 18 | 19 | public void allocateLicensePlate() { 20 | // code omitted... 21 | System.out.println("License plate allocated"); 22 | } 23 | 24 | public void allocateVehicleNumber() { 25 | // code omitted... 26 | System.out.println("Vehicle number allocated"); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/chapter04/CarFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter04; 10 | 11 | import chapter01.*; 12 | 13 | public class CarFactory extends VehicleFactory { 14 | 15 | protected Vehicle selectVehicle(DrivingStyle style) { 16 | if (style == DrivingStyle.ECONOMICAL) { 17 | return new Saloon(new StandardEngine(1300)); 18 | 19 | } else if (style == DrivingStyle.MIDRANGE) { 20 | return new Coupe(new StandardEngine(1600)); 21 | 22 | } else { 23 | return new Sport(new TurboEngine(2000)); 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/chapter29/db/EntityCSVStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.db; 10 | 11 | import java.io.*; 12 | 13 | public class EntityCSVStrategy extends AbstractEntityPersistenceStrategy { 14 | 15 | String getFileSuffix() { 16 | return ".csv"; 17 | } 18 | 19 | void save(EntityTable table) throws IOException { 20 | // code to save table data in CSV format (omitted) 21 | } 22 | 23 | EntityTable restore(EntityTable table) throws IOException { 24 | // code to restore table data from CSV format (omitted) 25 | return table; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/chapter05/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | //VehicleManager manager = new VehicleManager(); 15 | VehicleManagerLazy manager = new VehicleManagerLazy(); 16 | 17 | Vehicle saloon1 = manager.createSaloon(); 18 | Vehicle saloon2 = manager.createSaloon(); 19 | Vehicle pickup1 = manager.createPickup(); 20 | 21 | System.out.println(saloon1); 22 | System.out.println(saloon2); 23 | System.out.println(pickup1); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/chapter29/db/EntityEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.db; 10 | 11 | import java.util.*; 12 | 13 | public class EntityEvent extends EventObject { 14 | 15 | private Object value; 16 | 17 | EntityEvent() { 18 | this(0, null); // used when restoring data 19 | } 20 | 21 | EntityEvent(Integer key, Object value) { 22 | super(key); 23 | this.value = value; 24 | } 25 | 26 | public Integer getKey() { 27 | return (Integer) getSource(); 28 | } 29 | 30 | public Object getValue() { 31 | return value; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/chapter18/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter18; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | EngineManagementSystem ems = new EngineManagementSystem(); 15 | 16 | Ignition ignition = new Ignition(ems); 17 | Gearbox gearbox = new Gearbox(ems); 18 | Accelerator accelerator = new Accelerator(ems); 19 | Brake brake = new Brake(ems); 20 | 21 | ignition.start(); 22 | accelerator.accelerateToSpeed(30); 23 | gearbox.setGear(Gearbox.Gear.FOURTH); 24 | brake.apply(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/chapter20/SpeedMonitor2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter20; 10 | 11 | public class SpeedMonitor2 implements SpeedometerListener { 12 | 13 | public static final int SPEED_TO_ALERT = 70; 14 | 15 | public void speedChanged(SpeedometerEvent event) { 16 | if (event.getSpeed() > SPEED_TO_ALERT) { 17 | System.out.println("** ALERT ** Driving too fast! (" + 18 | event.getSpeed() + ")"); 19 | 20 | } else { 21 | System.out.println("... nice and steady ... (" + 22 | event.getSpeed() + ")"); 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/chapter20/Speedometer1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter20; 10 | 11 | import java.util.*; 12 | 13 | public class Speedometer1 extends Observable { 14 | 15 | private int currentSpeed; 16 | 17 | public Speedometer1() { 18 | currentSpeed = 0; 19 | } 20 | 21 | public void setCurrentSpeed(int speed) { 22 | currentSpeed = speed; 23 | 24 | // Tell all observers so they know speed has changed... 25 | setChanged(); 26 | notifyObservers(); 27 | } 28 | 29 | public int getCurrentSpeed() { 30 | return currentSpeed; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/chapter22/SportGearboxStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public class SportGearboxStrategy implements GearboxStrategy { 12 | 13 | public void ensureCorrectGear(Engine engine, int speed) { 14 | int engineSize = engine.getSize(); 15 | boolean turbo = engine.isTurbo(); 16 | 17 | // Some complicated code to determine correct gear 18 | // setting based on engineSize, turbo & speed, etc. 19 | // ... omitted ... 20 | 21 | System.out.println("Working out correct gear at " + 22 | speed + "mph for a SPORT gearbox"); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter24/EngineDiagnostics.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter24; 10 | 11 | public class EngineDiagnostics implements EngineVisitor { 12 | 13 | public void visit(Camshaft camshaft) { 14 | System.out.println("Diagnosing the camshaft"); 15 | } 16 | 17 | public void visit(Engine engine) { 18 | System.out.println("Diagnosing the engine"); 19 | } 20 | 21 | public void visit(Piston piston) { 22 | System.out.println("Diagnosing the piston"); 23 | } 24 | 25 | public void visit(SparkPlug sparkPlug) { 26 | System.out.println("Diagnosing a single spark plug"); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/chapter16/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter16; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | // Create the evaluator 15 | DirectionalEvaluator evaluator = new DirectionalEvaluator(); 16 | 17 | // This should output "London"... 18 | System.out.println(evaluator.evaluate 19 | ("london edinburgh manchester southerly")); 20 | 21 | // This should output "Aberdeen"... 22 | System.out.println(evaluator.evaluate 23 | ("london edinburgh manchester southerly aberdeen westerly")); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/chapter22/StandardGearboxStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public class StandardGearboxStrategy implements GearboxStrategy { 12 | 13 | public void ensureCorrectGear(Engine engine, int speed) { 14 | int engineSize = engine.getSize(); 15 | boolean turbo = engine.isTurbo(); 16 | 17 | // Some complicated code to determine correct gear 18 | // setting based on engineSize, turbo & speed, etc. 19 | // ... omitted ... 20 | 21 | System.out.println("Working out correct gear at " + 22 | speed + "mph for a STANDARD gearbox"); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter25/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter25; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | WarningLight[] lights = new WarningLight[3]; 15 | lights[0] = new OilLevelLight(); 16 | lights[1] = new BrakeFluidLight(); 17 | lights[2] = new NullObjectLight(); // empty slot 18 | 19 | // No need to test for null... 20 | for (WarningLight currentLight : lights) { 21 | currentLight.turnOn(); 22 | currentLight.turnOff(); 23 | System.out.println(currentLight.isOn()); 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/chapter01/AbstractEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public abstract class AbstractEngine implements Engine { 12 | 13 | private int size; 14 | private boolean turbo; 15 | 16 | public AbstractEngine(int size, boolean turbo) { 17 | this.size = size; 18 | this.turbo = turbo; 19 | } 20 | 21 | public int getSize() { 22 | return size; 23 | } 24 | 25 | public boolean isTurbo() { 26 | return turbo; 27 | } 28 | 29 | public String toString() { 30 | return getClass().getSimpleName() + 31 | " (" + size + ")"; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/chapter22/AbstractEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public abstract class AbstractEngine implements Engine { 12 | 13 | // 14 | private int size; 15 | private boolean turbo; 16 | 17 | public AbstractEngine(int size, boolean turbo) { 18 | this.size = size; 19 | this.turbo = turbo; 20 | } 21 | 22 | public int getSize() { 23 | return size; 24 | } 25 | 26 | public boolean isTurbo() { 27 | return turbo; 28 | } 29 | 30 | public String toString() { 31 | return getClass().getSimpleName() + 32 | " (" + size + ")"; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/chapter08/AbstractDriverControls.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter08; 10 | 11 | import chapter01.*; 12 | 13 | public abstract class AbstractDriverControls { 14 | 15 | private Engine engine; 16 | 17 | public AbstractDriverControls(Engine engine) { 18 | this.engine = engine; 19 | } 20 | 21 | public void ignitionOn() { 22 | engine.start(); 23 | } 24 | 25 | public void ignitionOff() { 26 | engine.stop(); 27 | } 28 | 29 | public void accelerate() { 30 | engine.increasePower(); 31 | } 32 | 33 | public void brake() { 34 | engine.decreasePower(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter01/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | // Sample engines... 15 | Engine engine1 = new StandardEngine(1300); 16 | Engine engine2 = new TurboEngine(2000); 17 | 18 | System.out.println(engine1); 19 | System.out.println(engine2); 20 | 21 | // Sample vehicles... 22 | Vehicle car = new Saloon(engine1, Vehicle.Colour.RED); 23 | Vehicle van = new BoxVan(engine2, Vehicle.Colour.WHITE); 24 | 25 | System.out.println(car); 26 | System.out.println(van); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/chapter19/Speedometer1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter19; 10 | 11 | public class Speedometer1 { 12 | 13 | // Normal private visibility but has accessor method... 14 | private int currentSpeed; 15 | 16 | // package-private visibility and no accessor method... 17 | int previousSpeed; 18 | 19 | public Speedometer1() { 20 | currentSpeed = 0; 21 | previousSpeed = 0; 22 | } 23 | 24 | public void setCurrentSpeed(int speed) { 25 | previousSpeed = currentSpeed; 26 | currentSpeed = speed; 27 | } 28 | 29 | public int getCurrentSpeed() { 30 | return currentSpeed; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/chapter29/business/AbstractEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.business; 10 | 11 | public abstract class AbstractEngine implements Engine { 12 | 13 | private int size; 14 | private boolean turbo; 15 | 16 | public AbstractEngine(int size, boolean turbo) { 17 | this.size = size; 18 | this.turbo = turbo; 19 | } 20 | 21 | public int getSize() { 22 | return size; 23 | } 24 | 25 | public boolean isTurbo() { 26 | return turbo; 27 | } 28 | 29 | public String toString() { 30 | return getClass().getSimpleName() + 31 | " (" + size + ")"; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/chapter29/business/EngineFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.business; 10 | 11 | public class EngineFactory { 12 | 13 | public enum Type {STANDARD, TURBO}; 14 | 15 | static Engine create(EngineFactory.Type type, int size) { 16 | if (type == Type.STANDARD) { 17 | return new StandardEngine(size); 18 | } else { 19 | return new TurboEngine(size); 20 | } 21 | } 22 | 23 | static Engine create(int size, boolean turbo) { 24 | return EngineFactory.create(turbo ? 25 | Type.TURBO : Type.STANDARD, size); 26 | } 27 | 28 | private EngineFactory() {} 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/chapter05/AbstractEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | /** 12 | * 13 | * @author Tony 14 | */ 15 | public abstract class AbstractEngine implements Engine { 16 | 17 | private int size; 18 | private boolean turbo; 19 | 20 | public AbstractEngine(int size, boolean turbo) { 21 | this.size = size; 22 | this.turbo = turbo; 23 | } 24 | 25 | public int getSize() { 26 | return size; 27 | } 28 | 29 | public boolean isTurbo() { 30 | return turbo; 31 | } 32 | 33 | public String toString() { 34 | return getClass().getSimpleName() + 35 | " (" + size + ")"; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/chapter10/AbstractEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | /** 12 | * 13 | * @author Tony 14 | */ 15 | public abstract class AbstractEngine implements Engine { 16 | 17 | private int size; 18 | private boolean turbo; 19 | 20 | public AbstractEngine(int size, boolean turbo) { 21 | this.size = size; 22 | this.turbo = turbo; 23 | } 24 | 25 | public int getSize() { 26 | return size; 27 | } 28 | 29 | public boolean isTurbo() { 30 | return turbo; 31 | } 32 | 33 | public String toString() { 34 | return getClass().getSimpleName() + 35 | " (" + size + ")"; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/chapter11/AbstractEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter11; 10 | 11 | /** 12 | * 13 | * @author Tony 14 | */ 15 | public abstract class AbstractEngine implements Engine { 16 | 17 | private int size; 18 | private boolean turbo; 19 | 20 | public AbstractEngine(int size, boolean turbo) { 21 | this.size = size; 22 | this.turbo = turbo; 23 | } 24 | 25 | public int getSize() { 26 | return size; 27 | } 28 | 29 | public boolean isTurbo() { 30 | return turbo; 31 | } 32 | 33 | public String toString() { 34 | return getClass().getSimpleName() + 35 | " (" + size + ")"; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/chapter19/SpeedometerMemento1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter19; 10 | 11 | public class SpeedometerMemento1 { 12 | 13 | private Speedometer1 speedometer; 14 | 15 | private int copyOfCurrentSpeed; 16 | private int copyOfPreviousSpeed; 17 | 18 | public SpeedometerMemento1(Speedometer1 speedometer) { 19 | this.speedometer = speedometer; 20 | copyOfCurrentSpeed = speedometer.getCurrentSpeed(); 21 | copyOfPreviousSpeed = speedometer.previousSpeed; 22 | } 23 | 24 | public void restoreState() { 25 | speedometer.setCurrentSpeed(copyOfCurrentSpeed); 26 | speedometer.previousSpeed = copyOfPreviousSpeed; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/chapter20/SpeedMonitor1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter20; 10 | 11 | import java.util.*; 12 | 13 | public class SpeedMonitor1 implements Observer { 14 | 15 | public static final int SPEED_TO_ALERT = 70; 16 | 17 | public void update(Observable obs, Object obj) { 18 | Speedometer1 speedo = (Speedometer1) obs; 19 | if (speedo.getCurrentSpeed() > SPEED_TO_ALERT) { 20 | System.out.println("** ALERT ** Driving too fast! (" + 21 | speedo.getCurrentSpeed() + ")"); 22 | 23 | } else { 24 | System.out.println("... nice and steady ... (" + 25 | speedo.getCurrentSpeed() + ")"); 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/chapter08/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter08; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | Engine engine = new StandardEngine(1300); 15 | StandardControls controls1 = new StandardControls(engine); 16 | controls1.ignitionOn(); 17 | controls1.accelerate(); 18 | controls1.brake(); 19 | controls1.ignitionOff(); 20 | 21 | // Now use sport controls 22 | SportControls controls2 = new SportControls(engine); 23 | controls2.ignitionOn(); 24 | controls2.accelerate(); 25 | controls2.accelerateHard(); 26 | controls2.brake(); 27 | controls2.ignitionOff(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/chapter19/Speedometer2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter19; 10 | 11 | import java.io.*; 12 | 13 | public class Speedometer2 implements Serializable { 14 | 15 | private int currentSpeed; 16 | private int previousSpeed; 17 | 18 | public Speedometer2() { 19 | currentSpeed = 0; 20 | previousSpeed = 0; 21 | } 22 | 23 | public void setCurrentSpeed(int speed) { 24 | previousSpeed = this.currentSpeed; 25 | this.currentSpeed = speed; 26 | } 27 | 28 | public int getCurrentSpeed() { 29 | return currentSpeed; 30 | } 31 | 32 | // Only defined to help testing... 33 | public int getPreviousSpeed() { 34 | return previousSpeed; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter07/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter07; 10 | 11 | import chapter01.*; 12 | import java.util.*; 13 | 14 | public class Client { 15 | 16 | public static void main(String[] args) { 17 | List engines = new ArrayList(); 18 | 19 | engines.add(new StandardEngine(1300)); 20 | engines.add(new StandardEngine(1600)); 21 | engines.add(new TurboEngine(2000)); 22 | 23 | // "Adapt" the new engine type... 24 | SuperGreenEngine greenEngine = new SuperGreenEngine(1200); 25 | engines.add(new SuperGreenEngineAdapter(greenEngine)); 26 | 27 | for (Engine engine : engines) { 28 | System.out.println(engine); 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/chapter09/Item.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter09; 10 | 11 | public abstract class Item { 12 | 13 | private String description; 14 | private int cost; 15 | 16 | public Item(String description, int cost) { 17 | this.description = description; 18 | this.cost = cost; 19 | } 20 | 21 | public String getDescription() { 22 | return description; 23 | } 24 | 25 | public int getCost() { 26 | return cost; 27 | } 28 | 29 | public abstract void addItem(Item item); 30 | public abstract void removeItem(Item item); 31 | public abstract Item[] getItems(); 32 | 33 | public String toString() { 34 | return description + " (cost " + getCost() + ")"; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter20/AutomaticGearbox.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter20; 10 | 11 | import java.util.*; 12 | 13 | public class AutomaticGearbox implements Observer { 14 | 15 | public void update(Observable obs, Object obj) { 16 | Speedometer1 speedo = (Speedometer1) obs; 17 | 18 | if (speedo.getCurrentSpeed() <= 10) { 19 | System.out.println("Now in first gear"); 20 | 21 | } else if (speedo.getCurrentSpeed() <= 20) { 22 | System.out.println("Now in second gear"); 23 | 24 | } else if (speedo.getCurrentSpeed() <= 30) { 25 | System.out.println("Now in third gear"); 26 | 27 | } else { 28 | System.out.println("Now in fourth gear"); 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/chapter23/AbstractBookletPrinter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter23; 10 | 11 | public abstract class AbstractBookletPrinter { 12 | 13 | protected abstract int getPageCount(); 14 | protected abstract void printFrontCover(); 15 | protected abstract void printTableOfContents(); 16 | protected abstract void printPage(int pageNumber); 17 | protected abstract void printIndex(); 18 | protected abstract void printBackCover(); 19 | 20 | // This is the 'template method' 21 | public final void print() { 22 | printFrontCover(); 23 | printTableOfContents(); 24 | for (int i = 1; i <= getPageCount(); i++) { 25 | printPage(i); 26 | } 27 | printIndex(); 28 | printBackCover(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/chapter06/SerialNumberGeneratorTraditional.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter06; 10 | 11 | public class SerialNumberGeneratorTraditional { 12 | 13 | // static members 14 | private static SerialNumberGeneratorTraditional instance; 15 | 16 | public synchronized static 17 | SerialNumberGeneratorTraditional getInstance() { 18 | if (instance == null) { 19 | instance = new SerialNumberGeneratorTraditional(); 20 | } 21 | return instance; 22 | } 23 | 24 | // instance variables 25 | private int count; 26 | 27 | // private constructor 28 | private SerialNumberGeneratorTraditional() {} 29 | 30 | // instance methods 31 | public synchronized int getNextSerial() { 32 | return ++count; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/chapter09/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter09; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | Item nut = new Part("Nut", 5); 15 | Item bolt = new Part("Bolt", 9); 16 | Item panel = new Part("Panel", 35); 17 | 18 | Item gizmo = new Assembly("Gizmo"); 19 | gizmo.addItem(panel); 20 | gizmo.addItem(nut); 21 | gizmo.addItem(bolt); 22 | 23 | Item widget = new Assembly("Widget"); 24 | widget.addItem(gizmo); 25 | widget.addItem(nut); 26 | 27 | System.out.println(nut); 28 | System.out.println(bolt); 29 | System.out.println(panel); 30 | System.out.println(gizmo); 31 | System.out.println(widget); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/chapter12/AbstractEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter12; 10 | 11 | /** 12 | * 13 | * @author Tony 14 | */ 15 | public abstract class AbstractEngine implements Engine { 16 | 17 | private int size; 18 | private boolean turbo; 19 | 20 | public AbstractEngine(int size, boolean turbo) { 21 | this.size = size; 22 | this.turbo = turbo; 23 | } 24 | 25 | public int getSize() { 26 | return size; 27 | } 28 | 29 | public boolean isTurbo() { 30 | return turbo; 31 | } 32 | 33 | public void diagnose(DiagnosticTool tool) { 34 | tool.runDiagnosis(this); 35 | } 36 | 37 | public String toString() { 38 | return getClass().getSimpleName() + 39 | " (" + size + ")"; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/chapter17/VanRange.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter17; 10 | 11 | import chapter01.*; 12 | import java.util.Arrays; 13 | import java.util.Iterator; 14 | 15 | public class VanRange implements Iterable { 16 | 17 | // 18 | private Vehicle[] vans; 19 | 20 | public VanRange() { 21 | vans = new Vehicle[3]; 22 | 23 | // Define the range of van models available 24 | vans[0] = new BoxVan(new StandardEngine(1600)); 25 | vans[1] = new BoxVan(new StandardEngine(2000)); 26 | vans[2] = new Pickup(new TurboEngine(2200)); 27 | } 28 | 29 | public Vehicle[] getRange() { 30 | return vans; 31 | } 32 | 33 | public Iterator iterator() { 34 | return Arrays.asList(vans).listIterator(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter15/ElectricWindow.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter15; 10 | 11 | public class ElectricWindow { 12 | 13 | private boolean open; 14 | 15 | public ElectricWindow() { 16 | open = false; 17 | System.out.println("Window is closed"); 18 | } 19 | 20 | public boolean isOpen() { 21 | return open; 22 | } 23 | 24 | public boolean isClosed() { 25 | return (! open); 26 | } 27 | 28 | public void openWindow() { 29 | if (isClosed()) { 30 | open = true; 31 | System.out.println("Window is now open"); 32 | } 33 | } 34 | 35 | public void closeWindow() { 36 | if (isOpen()) { 37 | open = false; 38 | System.out.println("Window is now closed"); 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/chapter17/CarRange.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter17; 10 | 11 | import chapter01.*; 12 | import java.util.*; 13 | 14 | public class CarRange implements Iterable { 15 | 16 | // 17 | private List cars; 18 | 19 | public CarRange() { 20 | cars = new ArrayList(); 21 | 22 | // Define the range of car models available 23 | cars.add(new Saloon(new StandardEngine(1300))); 24 | cars.add(new Saloon(new StandardEngine(1600))); 25 | cars.add(new Coupe(new StandardEngine(2000))); 26 | cars.add(new Sport(new TurboEngine(2500))); 27 | } 28 | 29 | public List getRange() { 30 | return cars; 31 | } 32 | 33 | public Iterator iterator() { 34 | return cars.listIterator(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter16/MostEasterlyExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter16; 10 | 11 | import java.util.*; 12 | 13 | public class MostEasterlyExpression implements Expression { 14 | 15 | private List expressions; 16 | 17 | public MostEasterlyExpression(List expressions) { 18 | this.expressions = expressions; 19 | } 20 | 21 | public City interpret() { 22 | City resultingCity = new City("Nowhere", -999.9, -999.9); 23 | for (Expression currentExpression : expressions) { 24 | City currentCity = currentExpression.interpret(); 25 | if (currentCity.getLongitude() > 26 | resultingCity.getLongitude()) { 27 | resultingCity = currentCity; 28 | } 29 | } 30 | return resultingCity; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/chapter16/MostNortherlyExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter16; 10 | 11 | import java.util.*; 12 | 13 | public class MostNortherlyExpression implements Expression { 14 | 15 | private List expressions; 16 | 17 | public MostNortherlyExpression(List expressions) { 18 | this.expressions = expressions; 19 | } 20 | 21 | public City interpret() { 22 | City resultingCity = new City("Nowhere", -999.9, -999.9); 23 | for (Expression currentExpression : expressions) { 24 | City currentCity = currentExpression.interpret(); 25 | if (currentCity.getLatitude() > 26 | resultingCity.getLatitude()) { 27 | resultingCity = currentCity; 28 | } 29 | } 30 | return resultingCity; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/chapter16/MostSoutherlyExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter16; 10 | 11 | import java.util.*; 12 | 13 | public class MostSoutherlyExpression implements Expression { 14 | 15 | private List expressions; 16 | 17 | public MostSoutherlyExpression(List expressions) { 18 | this.expressions = expressions; 19 | } 20 | 21 | public City interpret() { 22 | City resultingCity = new City("Nowhere", 999.9, 999.9); 23 | for (Expression currentExpression : expressions) { 24 | City currentCity = currentExpression.interpret(); 25 | if (currentCity.getLatitude() < 26 | resultingCity.getLatitude()) { 27 | resultingCity = currentCity; 28 | } 29 | } 30 | return resultingCity; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/chapter16/MostWesterlyExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter16; 10 | 11 | import java.util.*; 12 | 13 | public class MostWesterlyExpression implements Expression { 14 | 15 | private List expressions; 16 | 17 | public MostWesterlyExpression(List expressions) { 18 | this.expressions = expressions; 19 | } 20 | 21 | public City interpret() { 22 | City resultingCity = new City("Nowhere", 999.9, 999.9); 23 | for (Expression currentExpression : expressions) { 24 | City currentCity = currentExpression.interpret(); 25 | if (currentCity.getLongitude() < 26 | resultingCity.getLongitude()) { 27 | resultingCity = currentCity; 28 | } 29 | } 30 | return resultingCity; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/chapter29/main/Main.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.main; 10 | 11 | import chapter29.ui.*; 12 | import chapter29.business.*; 13 | import javax.swing.*; 14 | 15 | public class Main { 16 | 17 | public static void main(String[] args) { 18 | // create some sample data 19 | BusinessFacade.INSTANCE.addEngine(1300, EngineFactory.Type.STANDARD); 20 | BusinessFacade.INSTANCE.addEngine(1600, EngineFactory.Type.STANDARD); 21 | BusinessFacade.INSTANCE.addEngine(2000, EngineFactory.Type.STANDARD); 22 | BusinessFacade.INSTANCE.addEngine(2500, EngineFactory.Type.TURBO); 23 | 24 | // start the ui 25 | SwingUtilities.invokeLater(new Runnable() { 26 | public void run() { 27 | new EnginesFrame().setVisible(true); 28 | } 29 | }); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/chapter29/ui/EnginesFrame.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.ui; 10 | 11 | import chapter29.business.*; 12 | import java.awt.*; 13 | import javax.swing.*; 14 | 15 | public class EnginesFrame extends JFrame { 16 | 17 | private ManageEnginesPanel enginesPanel; 18 | 19 | public EnginesFrame() { 20 | super("Manage Engines"); 21 | setDefaultCloseOperation(DISPOSE_ON_CLOSE); 22 | add(buildUI(), BorderLayout.CENTER); 23 | pack(); 24 | setLocationRelativeTo(null); 25 | } 26 | 27 | private Component buildUI() { 28 | JPanel uiPanel = new JPanel(new BorderLayout()); 29 | 30 | enginesPanel = new ManageEnginesPanel(this); 31 | BusinessFacade.INSTANCE.addEngineListener(enginesPanel); 32 | uiPanel.add(enginesPanel, BorderLayout.CENTER); 33 | return uiPanel; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/chapter09/Assembly.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter09; 10 | 11 | import java.util.*; 12 | 13 | public class Assembly extends Item { 14 | 15 | private List items; 16 | 17 | public Assembly(String description) { 18 | super(description, 0); 19 | items = new ArrayList(); 20 | } 21 | 22 | public void addItem(Item item) { 23 | items.add(item); 24 | } 25 | 26 | public void removeItem(Item item) { 27 | items.remove(item); 28 | } 29 | 30 | public Item[] getItems() { 31 | return items.toArray(new Item[items.size()]); 32 | } 33 | 34 | // Also have to override getCost() to add cost of items in list 35 | public int getCost() { 36 | int total = 0; 37 | for (Item item : items) { 38 | total += item.getCost(); 39 | } 40 | return total; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/chapter18/Ignition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter18; 10 | 11 | public class Ignition { 12 | 13 | private EngineManagementSystem mediator; 14 | private boolean on; 15 | 16 | // Constructor accepts mediator as an argument 17 | public Ignition(EngineManagementSystem mediator) { 18 | this.mediator = mediator; 19 | on = false; 20 | 21 | // Register back with the mediator; 22 | mediator.registerIgnition(this); 23 | } 24 | 25 | public void start() { 26 | on = true; 27 | mediator.ignitionTurnedOn(); 28 | System.out.println("Ignition turned on"); 29 | } 30 | 31 | public void stop() { 32 | on = false; 33 | mediator.ignitionTurnedOff(); 34 | System.out.println("Ignition turned off"); 35 | } 36 | 37 | public boolean isOn() { 38 | return on; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/chapter21/YearSetupState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter21; 10 | 11 | import java.util.*; 12 | 13 | public class YearSetupState implements ClockSetupState { 14 | 15 | private ClockSetup clockSetup; 16 | private int year; 17 | 18 | public YearSetupState(ClockSetup clockSetup) { 19 | this.clockSetup = clockSetup; 20 | year = Calendar.getInstance().get(Calendar.YEAR); 21 | } 22 | 23 | public void previousValue() { 24 | year--; 25 | } 26 | 27 | public void nextValue() { 28 | year++; 29 | } 30 | 31 | public void selectValue() { 32 | System.out.println("Year set to " + year); 33 | clockSetup.setState(clockSetup.getMonthSetupState()); 34 | } 35 | 36 | public String getInstructions() { 37 | return "Please set the year..."; 38 | } 39 | 40 | public int getSelectedValue() { 41 | return year; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/chapter23/SaloonBooklet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter23; 10 | 11 | public class SaloonBooklet extends AbstractBookletPrinter { 12 | 13 | protected int getPageCount() { 14 | return 100; 15 | } 16 | 17 | protected void printFrontCover() { 18 | System.out.println("Printing front cover for Saloon car booklet"); 19 | } 20 | 21 | protected void printTableOfContents() { 22 | System.out.println("Printing table of contents for Saloon car booklet"); 23 | } 24 | 25 | protected void printPage(int pageNumber) { 26 | System.out.println("Printing page " + pageNumber + " for Saloon car booklet"); 27 | } 28 | 29 | protected void printIndex() { 30 | System.out.println("Printing index for Saloon car booklet"); 31 | } 32 | 33 | protected void printBackCover() { 34 | System.out.println("Printing back cover for Saloon car booklet"); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter01/AbstractVehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter01; 10 | 11 | public abstract class AbstractVehicle implements Vehicle { 12 | 13 | private Engine engine; 14 | private Vehicle.Colour colour; 15 | 16 | public AbstractVehicle(Engine engine) { 17 | this(engine, Vehicle.Colour.UNPAINTED); 18 | } 19 | 20 | public AbstractVehicle(Engine engine, Vehicle.Colour colour) { 21 | this.engine = engine; 22 | this.colour = colour; 23 | } 24 | 25 | public Engine getEngine() { 26 | return engine; 27 | } 28 | 29 | public Vehicle.Colour getColour() { 30 | return colour; 31 | } 32 | 33 | public void paint(Vehicle.Colour colour) { 34 | this.colour = colour; 35 | } 36 | 37 | public String toString() { 38 | return getClass().getSimpleName() + 39 | " (" + engine + ", " + colour + ")"; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/chapter22/AbstractVehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public abstract class AbstractVehicle implements Vehicle { 12 | 13 | // 14 | private Engine engine; 15 | private Vehicle.Colour colour; 16 | 17 | public AbstractVehicle(Engine engine) { 18 | this(engine, Vehicle.Colour.UNPAINTED); 19 | } 20 | 21 | public AbstractVehicle(Engine engine, Vehicle.Colour colour) { 22 | this.engine = engine; 23 | this.colour = colour; 24 | } 25 | 26 | public Engine getEngine() { 27 | return engine; 28 | } 29 | 30 | public Vehicle.Colour getColour() { 31 | return colour; 32 | } 33 | 34 | public void paint(Vehicle.Colour colour) { 35 | this.colour = colour; 36 | } 37 | 38 | public String toString() { 39 | return getClass().getSimpleName() + 40 | " (" + engine + ", " + colour + ")"; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/chapter23/ServiceHistoryBooklet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter23; 10 | 11 | public class ServiceHistoryBooklet extends AbstractBookletPrinter { 12 | 13 | protected int getPageCount() { 14 | return 12; 15 | } 16 | 17 | protected void printFrontCover() { 18 | System.out.println("Printing front cover for service history booklet"); 19 | } 20 | 21 | protected void printTableOfContents() { 22 | System.out.println("Printing table of contents for service history booklet"); 23 | } 24 | 25 | protected void printPage(int pageNumber) { 26 | System.out.println("Printing page " + pageNumber + " for service history booklet"); 27 | } 28 | 29 | protected void printIndex() { 30 | System.out.println("Printing index for service history booklet"); 31 | } 32 | 33 | protected void printBackCover() { 34 | System.out.println("Printing back cover for service history booklet"); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter21/FinishedSetupState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter21; 10 | 11 | import java.util.*; 12 | 13 | public class FinishedSetupState implements ClockSetupState { 14 | 15 | private ClockSetup clockSetup; 16 | 17 | public FinishedSetupState(ClockSetup clockSetup) { 18 | this.clockSetup = clockSetup; 19 | } 20 | 21 | public void previousValue() { 22 | System.out.println("Ignored..."); 23 | } 24 | 25 | public void nextValue() { 26 | System.out.println("Ignored..."); 27 | } 28 | 29 | public void selectValue() { 30 | Calendar selectedDate = clockSetup.getSelectedDate(); 31 | System.out.println("Date set to: " + selectedDate.getTime()); 32 | } 33 | 34 | public String getInstructions() { 35 | return "Press knob to view selected date..."; 36 | } 37 | 38 | public int getSelectedValue() { 39 | throw new IllegalStateException("Clock setup finished"); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/chapter10/AbstractVehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public abstract class AbstractVehicle implements Vehicle { 12 | 13 | private Engine engine; 14 | private Vehicle.Colour colour; 15 | 16 | public AbstractVehicle(Engine engine) { 17 | this(engine, Vehicle.Colour.UNPAINTED); 18 | } 19 | 20 | public AbstractVehicle(Engine engine, Vehicle.Colour colour) { 21 | this.engine = engine; 22 | this.colour = colour; 23 | } 24 | 25 | public Engine getEngine() { 26 | return engine; 27 | } 28 | 29 | public Vehicle.Colour getColour() { 30 | return colour; 31 | } 32 | 33 | public void paint(Vehicle.Colour colour) { 34 | this.colour = colour; 35 | } 36 | 37 | public String toString() { 38 | return getClass().getSimpleName() + 39 | " (" + engine + ", " + colour + 40 | ", price " + getPrice() + ")"; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/chapter24/EngineInventory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter24; 10 | 11 | public class EngineInventory implements EngineVisitor { 12 | 13 | private int camshaftCount; 14 | private int pistonCount; 15 | private int sparkPlugCount; 16 | 17 | public EngineInventory() { 18 | camshaftCount = 0; 19 | pistonCount = 0; 20 | sparkPlugCount = 0; 21 | } 22 | 23 | public void visit(Camshaft camshaft) { 24 | camshaftCount++; 25 | } 26 | 27 | public void visit(Engine engine) { 28 | System.out.println("The engine has: " + 29 | camshaftCount + " camshaft(s), " + 30 | pistonCount + " piston(s), and " + 31 | sparkPlugCount + " spark plug(s)"); 32 | } 33 | 34 | public void visit(Piston piston) { 35 | pistonCount++; 36 | } 37 | 38 | public void visit(SparkPlug sparkPlug) { 39 | sparkPlugCount++; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/chapter12/EngineFlyweightFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter12; 10 | 11 | import java.util.*; 12 | 13 | public class EngineFlyweightFactory { 14 | 15 | private Map standardEnginePool; 16 | private Map turboEnginePool; 17 | 18 | public EngineFlyweightFactory() { 19 | standardEnginePool = new HashMap(); 20 | turboEnginePool = new HashMap(); 21 | } 22 | 23 | public Engine getStandardEngine(int size) { 24 | Engine e = standardEnginePool.get(size); 25 | if (e == null) { 26 | e = new StandardEngine(size); 27 | standardEnginePool.put(size, e); 28 | } 29 | return e; 30 | } 31 | 32 | public Engine getTurboEngine(int size) { 33 | Engine e = turboEnginePool.get(size); 34 | if (e == null) { 35 | e = new TurboEngine(size); 36 | turboEnginePool.put(size, e); 37 | } 38 | return e; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/chapter21/HourSetupState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter21; 10 | 11 | import java.util.*; 12 | 13 | public class HourSetupState implements ClockSetupState { 14 | 15 | private ClockSetup clockSetup; 16 | private int hour; 17 | 18 | public HourSetupState(ClockSetup clockSetup) { 19 | this.clockSetup = clockSetup; 20 | hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); 21 | } 22 | 23 | public void previousValue() { 24 | if (hour > 0) { 25 | hour--; 26 | } 27 | } 28 | 29 | public void nextValue() { 30 | if (hour < 23) { 31 | hour++; 32 | } 33 | } 34 | 35 | public void selectValue() { 36 | System.out.println("Hour set to " + hour); 37 | clockSetup.setState(clockSetup.getMinuteSetupState()); 38 | } 39 | 40 | public String getInstructions() { 41 | return "Please set the hour..."; 42 | } 43 | 44 | public int getSelectedValue() { 45 | return hour; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/chapter21/MonthSetupState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter21; 10 | 11 | import java.util.*; 12 | 13 | public class MonthSetupState implements ClockSetupState { 14 | 15 | private ClockSetup clockSetup; 16 | private int month; 17 | 18 | public MonthSetupState(ClockSetup clockSetup) { 19 | this.clockSetup = clockSetup; 20 | month = Calendar.getInstance().get(Calendar.MONTH); 21 | } 22 | 23 | public void previousValue() { 24 | if (month > 0) { 25 | month--; 26 | } 27 | } 28 | 29 | public void nextValue() { 30 | if (month < 11) { 31 | month++; 32 | } 33 | } 34 | 35 | public void selectValue() { 36 | System.out.println("Month set to " + (month + 1)); 37 | clockSetup.setState(clockSetup.getDaySetupState()); 38 | } 39 | 40 | public String getInstructions() { 41 | return "Please set the month..."; 42 | } 43 | 44 | public int getSelectedValue() { 45 | return month; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/chapter03/VanBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter03; 10 | 11 | import chapter01.*; 12 | 13 | public class VanBuilder extends VehicleBuilder { 14 | 15 | private AbstractVan vanInProgress; 16 | 17 | public VanBuilder(AbstractVan van) { 18 | vanInProgress = van; 19 | } 20 | 21 | public void buildBody() { 22 | // Add body to vanInProgress 23 | System.out.println("Building van body"); 24 | } 25 | 26 | public void buildChassis() { 27 | // Add chassis to vanInProgress 28 | System.out.println("Building van chassis"); 29 | } 30 | 31 | public void buildReinforcedStorageArea() { 32 | // Add storage area to vanInProgress 33 | System.out.println("Building van passenger area"); 34 | } 35 | 36 | public void buildWindows() { 37 | // Add windows to vanInProgress 38 | System.out.println("Building van windows"); 39 | } 40 | 41 | public Vehicle getVehicle() { 42 | return vanInProgress; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/chapter21/MinuteSetupState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter21; 10 | 11 | import java.util.*; 12 | 13 | public class MinuteSetupState implements ClockSetupState { 14 | 15 | private ClockSetup clockSetup; 16 | private int minute; 17 | 18 | public MinuteSetupState(ClockSetup clockSetup) { 19 | this.clockSetup = clockSetup; 20 | minute = Calendar.getInstance().get(Calendar.MINUTE); 21 | } 22 | 23 | public void previousValue() { 24 | if (minute > 0) { 25 | minute--; 26 | } 27 | } 28 | 29 | public void nextValue() { 30 | if (minute < 59) { 31 | minute++; 32 | } 33 | } 34 | 35 | public void selectValue() { 36 | System.out.println("Minute set to " + minute); 37 | clockSetup.setState(clockSetup.getFinishedSetupState()); 38 | } 39 | 40 | public String getInstructions() { 41 | return "Please set the minute..."; 42 | } 43 | 44 | public int getSelectedValue() { 45 | return minute; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/chapter04/VehicleFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter04; 10 | 11 | import chapter01.*; 12 | 13 | public abstract class VehicleFactory { 14 | 15 | public enum Category {CAR, VAN} 16 | public enum DrivingStyle {ECONOMICAL, MIDRANGE, POWERFUL}; 17 | 18 | public static Vehicle make(Category category, 19 | DrivingStyle style, 20 | Vehicle.Colour colour) { 21 | VehicleFactory factory = null; 22 | 23 | if (category == Category.CAR) { 24 | factory = new CarFactory(); 25 | } else { 26 | factory = new VanFactory(); 27 | } 28 | 29 | return factory.build(style, colour); 30 | } 31 | 32 | public Vehicle build(DrivingStyle style, Vehicle.Colour colour) { 33 | Vehicle v = selectVehicle(style); 34 | v.paint(colour); 35 | return v; 36 | } 37 | 38 | // This is the "factory method" 39 | protected abstract Vehicle selectVehicle(DrivingStyle style); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/chapter22/AbstractCar.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter22; 10 | 11 | public abstract class AbstractCar extends AbstractVehicle { 12 | 13 | private GearboxStrategy gearboxStrategy; 14 | 15 | public AbstractCar(Engine engine) { 16 | this(engine, Vehicle.Colour.UNPAINTED); 17 | } 18 | 19 | public AbstractCar(Engine engine, Vehicle.Colour colour) { 20 | super(engine, colour); 21 | 22 | // Starts in standard gearbox mode (more economical) 23 | gearboxStrategy = new StandardGearboxStrategy(); 24 | } 25 | 26 | // Allow the gearbox strategy to be changed... 27 | public void setGearboxStrategy(GearboxStrategy gearboxStrategy) { 28 | this.gearboxStrategy = gearboxStrategy; 29 | } 30 | 31 | public GearboxStrategy getGearboxStrategy() { 32 | return gearboxStrategy; 33 | } 34 | 35 | public void setSpeed(int speed) { 36 | // Delegate to strategy in effect... 37 | gearboxStrategy.ensureCorrectGear(getEngine(), speed); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/chapter02/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter02; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | String whatToMake = "car"; // or "van" 15 | 16 | AbstractVehicleFactory factory = null; 17 | 18 | // Create the correct 'factory'... 19 | if (whatToMake.equals("car")) { 20 | factory = new CarFactory(); 21 | } else { 22 | factory = new VanFactory(); 23 | } 24 | 25 | // Create the vehicle's component parts... 26 | // These will either be all car parts or all van parts. 27 | Body vehicleBody = factory.createBody(); 28 | Chassis vehicleChassis = factory.createChassis(); 29 | Windows vehicleWindows = factory.createWindows(); 30 | 31 | // Show what we've created... 32 | System.out.println(vehicleBody.getBodyParts()); 33 | System.out.println(vehicleChassis.getChassisParts()); 34 | System.out.println(vehicleWindows.getWindowParts()); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter21/DaySetupState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter21; 10 | 11 | import java.util.*; 12 | 13 | public class DaySetupState implements ClockSetupState { 14 | 15 | private ClockSetup clockSetup; 16 | private int day; 17 | 18 | public DaySetupState(ClockSetup clockSetup) { 19 | this.clockSetup = clockSetup; 20 | day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); 21 | } 22 | 23 | public void previousValue() { 24 | if (day > 1) { 25 | day--; 26 | } 27 | } 28 | 29 | public void nextValue() { 30 | if (day < Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH)) { 31 | day++; 32 | } 33 | } 34 | 35 | public void selectValue() { 36 | System.out.println("Day set to " + day); 37 | clockSetup.setState(clockSetup.getHourSetupState()); 38 | } 39 | 40 | public String getInstructions() { 41 | return "Please set the day..."; 42 | } 43 | 44 | public int getSelectedValue() { 45 | return day; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/chapter10/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter10; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | // Create a blue saloon car... 15 | Vehicle myCar = new Saloon(new StandardEngine(1300)); 16 | myCar.paint(Vehicle.Colour.BLUE); 17 | System.out.println(myCar); 18 | 19 | // Add air-conditioning to the car... 20 | myCar = new AirConditionedVehicle(myCar); 21 | System.out.println(myCar); 22 | 23 | // Now add alloy wheels... 24 | myCar = new AlloyWheeledVehicle(myCar); 25 | System.out.println(myCar); 26 | 27 | // Now add leather seats... 28 | myCar = new LeatherSeatedVehicle(myCar); 29 | System.out.println(myCar); 30 | 31 | // Now add metallic paint... 32 | myCar = new MetallicPaintedVehicle(myCar); 33 | System.out.println(myCar); 34 | 35 | // Now add satellite-navigation... 36 | myCar = new SatNavVehicle(myCar); 37 | System.out.println(myCar); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/chapter16/City.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter16; 10 | 11 | public class City { 12 | 13 | private String name; 14 | private double latitude, longitude; 15 | 16 | public City(String name, double latitude, double longitude) { 17 | this.name = name; 18 | this.latitude = latitude; 19 | this.longitude = longitude; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public double getLatitude() { 27 | return latitude; 28 | } 29 | 30 | public double getLongitude() { 31 | return longitude; 32 | } 33 | 34 | public boolean equals(Object otherObject) { 35 | if (this == otherObject) return true; 36 | if (! (otherObject instanceof City)) return false; 37 | City otherCity = (City) otherObject; 38 | return getName().equals(otherCity.getName()); 39 | } 40 | 41 | public int hashCode() { 42 | return getName().hashCode(); 43 | } 44 | 45 | public String toString() { 46 | return getName(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/chapter13/EngineProxy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter13; 10 | 11 | import chapter12.*; 12 | 13 | public class EngineProxy implements Engine { 14 | 15 | private Engine engine; 16 | 17 | public EngineProxy(int size, boolean turbo) { 18 | if (turbo) { 19 | engine = new TurboEngine(size); 20 | } else { 21 | engine = new StandardEngine(size); 22 | } 23 | } 24 | 25 | public int getSize() { 26 | return engine.getSize(); 27 | } 28 | 29 | public boolean isTurbo() { 30 | return engine.isTurbo(); 31 | } 32 | 33 | // This method is time-consuming... 34 | public void diagnose(final DiagnosticTool tool) { 35 | // Run the method as a separate thread 36 | Thread t = new Thread(new Runnable() { 37 | public void run() { 38 | System.out.println("(Running tool as thread)"); 39 | engine.diagnose(tool); 40 | } 41 | }); 42 | t.start(); 43 | System.out.println("EngineProxy diagnose() method finished"); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/chapter05/VehicleManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | public class VehicleManager { 12 | 13 | private Vehicle saloon, coupe, sport, boxVan, pickup; 14 | 15 | public VehicleManager() { 16 | // For simplicity all vehicles use same engine type... 17 | saloon = new Saloon(new StandardEngine(1300)); 18 | coupe = new Coupe(new StandardEngine(1300)); 19 | sport = new Sport(new StandardEngine(1300)); 20 | boxVan = new BoxVan(new StandardEngine(1300)); 21 | pickup = new Pickup(new StandardEngine(1300)); 22 | } 23 | 24 | public Vehicle createSaloon() { 25 | return (Vehicle) saloon.clone(); 26 | } 27 | 28 | public Vehicle createCoupe() { 29 | return (Vehicle) coupe.clone(); 30 | } 31 | 32 | public Vehicle createSport() { 33 | return (Vehicle) sport.clone(); 34 | } 35 | 36 | public Vehicle createBoxVan() { 37 | return (Vehicle) boxVan.clone(); 38 | } 39 | 40 | public Vehicle createPickup() { 41 | return (Vehicle) pickup.clone(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/chapter04/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter04; 10 | 11 | import chapter01.*; 12 | 13 | public class Client { 14 | 15 | public static void main(String[] args) { 16 | // I want an economical car, coloured blue... 17 | VehicleFactory carFactory = new CarFactory(); 18 | Vehicle car = carFactory.build 19 | (VehicleFactory.DrivingStyle.ECONOMICAL, 20 | Vehicle.Colour.BLUE); 21 | System.out.println(car); 22 | 23 | // I am a "white van man"... 24 | VehicleFactory vanFactory = new VanFactory(); 25 | Vehicle van = vanFactory.build 26 | (VehicleFactory.DrivingStyle.POWERFUL, 27 | Vehicle.Colour.WHITE); 28 | System.out.println(van); 29 | 30 | // Create a red sports car... 31 | Vehicle sporty = VehicleFactory.make 32 | (VehicleFactory.Category.CAR, 33 | VehicleFactory.DrivingStyle.POWERFUL, 34 | Vehicle.Colour.BLUE); 35 | System.out.println(sporty); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/chapter03/CarBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter03; 10 | 11 | import chapter01.*; 12 | 13 | public class CarBuilder extends VehicleBuilder { 14 | 15 | private AbstractCar carInProgress; 16 | 17 | public CarBuilder(AbstractCar car) { 18 | carInProgress = car; 19 | } 20 | 21 | public void buildBody() { 22 | // Add body to carInProgress 23 | System.out.println("Building car body"); 24 | } 25 | 26 | public void buildBoot() { 27 | // Add boot to carInProgress 28 | System.out.println("Building car boot"); 29 | } 30 | 31 | public void buildChassis() { 32 | // Add chassis to carInProgress 33 | System.out.println("Building car chassis"); 34 | } 35 | 36 | public void buildPassengerArea() { 37 | // Add passenger area to carInProgress 38 | System.out.println("Building car passenger area"); 39 | } 40 | 41 | public void buildWindows() { 42 | // Add windows to carInProgress 43 | System.out.println("Building car windows"); 44 | } 45 | 46 | public Vehicle getVehicle() { 47 | return carInProgress; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/chapter19/SpeedometerMemento2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter19; 10 | 11 | import java.io.*; 12 | 13 | public class SpeedometerMemento2 { 14 | 15 | public SpeedometerMemento2(Speedometer2 speedometer) 16 | throws IOException { 17 | // Serialize... 18 | File speedometerFile = new File("speedometer.ser"); 19 | ObjectOutputStream oos = 20 | new ObjectOutputStream( 21 | new BufferedOutputStream( 22 | new FileOutputStream(speedometerFile))); 23 | oos.writeObject(speedometer); 24 | oos.close(); 25 | } 26 | 27 | public Speedometer2 restoreState() throws IOException, ClassNotFoundException { 28 | // Deserialize... 29 | File speedometerFile = new File("speedometer.ser"); 30 | ObjectInputStream ois = 31 | new ObjectInputStream( 32 | new BufferedInputStream( 33 | new FileInputStream(speedometerFile))); 34 | Speedometer2 speedo = (Speedometer2) ois.readObject(); 35 | ois.close(); 36 | return speedo; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/chapter06/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter06; 10 | 11 | public class Client { 12 | 13 | public static void main(String[] args) { 14 | System.out.println("Using traditional singleton"); 15 | SerialNumberGeneratorTraditional generator = 16 | SerialNumberGeneratorTraditional.getInstance(); 17 | System.out.println("next serial: " + generator.getNextSerial()); 18 | System.out.println("next serial: " + generator.getNextSerial()); 19 | System.out.println("next serial: " + generator.getNextSerial()); 20 | 21 | System.out.println("Using enum singleton"); 22 | System.out.println("next vehicle: " + 23 | SerialNumberGenerator.VEHICLE.getNextSerial()); 24 | System.out.println("next vehicle: " + 25 | SerialNumberGenerator.VEHICLE.getNextSerial()); 26 | System.out.println("next engine: " + 27 | SerialNumberGenerator.ENGINE.getNextSerial()); 28 | System.out.println("next vehicle: " + 29 | SerialNumberGenerator.VEHICLE.getNextSerial()); 30 | System.out.println("next engine: " + 31 | SerialNumberGenerator.ENGINE.getNextSerial()); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/chapter17/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter17; 10 | 11 | import chapter01.*; 12 | import java.util.*; 13 | 14 | public class Client { 15 | 16 | // 17 | public static void main(String[] args) { 18 | System.out.println("=== Our Cars ==="); 19 | CarRange carRange2 = new CarRange(); 20 | printIterator(carRange2.iterator()); 21 | 22 | System.out.println("=== Our Vans ==="); 23 | VanRange vanRange2 = new VanRange(); 24 | printIterator(vanRange2.iterator()); 25 | 26 | 27 | // USING FOR-EACH 28 | System.out.println("=== Our Cars ==="); 29 | CarRange carRange3 = new CarRange(); 30 | for (Vehicle currentVehicle : carRange3.getRange()) { 31 | System.out.println(currentVehicle); 32 | } 33 | 34 | System.out.println("=== Our Vans ==="); 35 | VanRange vanRange3 = new VanRange(); 36 | for (Vehicle currentVehicle : vanRange3.getRange()) { 37 | System.out.println(currentVehicle); 38 | } 39 | } 40 | 41 | private static void printIterator(Iterator iter) { 42 | while (iter.hasNext()) { 43 | System.out.println(iter.next()); 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/chapter18/Accelerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter18; 10 | 11 | public class Accelerator { 12 | 13 | private EngineManagementSystem mediator; 14 | private boolean enabled; 15 | private int speed; 16 | 17 | // Constructor accepts mediator as an argument 18 | public Accelerator(EngineManagementSystem mediator) { 19 | this.mediator = mediator; 20 | enabled = false; 21 | speed = 0; 22 | mediator.registerAccelerator(this); 23 | } 24 | 25 | public void enable() { 26 | enabled = true; 27 | mediator.acceleratorEnabled(); 28 | System.out.println("Accelerator enabled"); 29 | } 30 | 31 | public void disable() { 32 | enabled = false; 33 | mediator.acceleratorDisabled(); 34 | System.out.println("Accelerator disabled"); 35 | } 36 | 37 | public boolean isEnabled() { 38 | return enabled; 39 | } 40 | 41 | public void accelerateToSpeed(int speed) { 42 | if (isEnabled()) { 43 | this.speed = speed; 44 | System.out.println("Speed now " + getSpeed()); 45 | } 46 | } 47 | 48 | public int getSpeed() { 49 | return speed; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/chapter20/Speedometer2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter20; 10 | 11 | import java.util.*; 12 | 13 | public class Speedometer2 { 14 | 15 | private int currentSpeed; 16 | private List listeners; 17 | 18 | public Speedometer2() { 19 | currentSpeed = 0; 20 | listeners = new ArrayList(); 21 | } 22 | 23 | public void setCurrentSpeed(int speed) { 24 | currentSpeed = speed; 25 | 26 | // Tell all observers so they know speed has changed... 27 | fireSpeedChanged(); 28 | } 29 | 30 | public int getCurrentSpeed() { 31 | return currentSpeed; 32 | } 33 | 34 | public void addSpeedometerListener(SpeedometerListener obj) { 35 | listeners.add(obj); 36 | } 37 | 38 | public void removeSpeedometerListener(SpeedometerListener obj) { 39 | listeners.remove(obj); 40 | } 41 | 42 | protected void fireSpeedChanged() { 43 | SpeedometerEvent speedEvent = 44 | new SpeedometerEvent(this, getCurrentSpeed()); 45 | for (SpeedometerListener eachListener : listeners) { 46 | eachListener.speedChanged(speedEvent); 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/chapter05/AbstractVehicle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter05; 10 | 11 | /** 12 | * 13 | * @author Tony 14 | */ 15 | public abstract class AbstractVehicle implements Vehicle { 16 | 17 | private Engine engine; 18 | private Vehicle.Colour colour; 19 | 20 | public AbstractVehicle(Engine engine) { 21 | this(engine, Vehicle.Colour.UNPAINTED); 22 | } 23 | 24 | public AbstractVehicle(Engine engine, Vehicle.Colour colour) { 25 | this.engine = engine; 26 | this.colour = colour; 27 | } 28 | 29 | public Engine getEngine() { 30 | return engine; 31 | } 32 | 33 | public Vehicle.Colour getColour() { 34 | return colour; 35 | } 36 | 37 | public void paint(Vehicle.Colour colour) { 38 | this.colour = colour; 39 | } 40 | 41 | public Object clone() { 42 | Object obj = null; 43 | try { 44 | obj = super.clone(); 45 | } catch (CloneNotSupportedException x) { 46 | // Should not happen... 47 | } 48 | return obj; 49 | } 50 | 51 | public String toString() { 52 | return getClass().getSimpleName() + 53 | " (" + engine + ", " + colour + ")"; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/chapter18/Brake.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter18; 10 | 11 | public class Brake { 12 | 13 | private EngineManagementSystem mediator; 14 | private boolean enabled; 15 | private boolean applied; 16 | 17 | // Constructor accepts mediator as an argument 18 | public Brake(EngineManagementSystem mediator) { 19 | this.mediator = mediator; 20 | enabled = false; 21 | applied = false; 22 | mediator.registerBrake(this); 23 | } 24 | 25 | public void enable() { 26 | enabled = true; 27 | mediator.brakeEnabled(); 28 | System.out.println("Brakes enabled"); 29 | } 30 | 31 | public void disable() { 32 | enabled = false; 33 | mediator.brakeDisabled(); 34 | System.out.println("Brakes disabled"); 35 | } 36 | 37 | public boolean isEnabled() { 38 | return enabled; 39 | } 40 | 41 | public void apply() { 42 | if (isEnabled()) { 43 | applied = true; 44 | mediator.brakePressed(); 45 | System.out.println("Now braking"); 46 | } 47 | } 48 | 49 | private void release() { 50 | if (isEnabled()) { 51 | applied = false; 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/chapter29/db/EntitySerializationStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Java Design Pattern Essentials - Second Edition, by Tony Bevis 3 | * Copyright 2012, Ability First Limited 4 | * 5 | * This source code is provided to accompany the book and is provided AS-IS without warranty of any kind. 6 | * It is intended for educational and illustrative purposes only, and may not be re-published 7 | * without the express written permission of the publisher. 8 | */ 9 | package chapter29.db; 10 | 11 | import java.io.*; 12 | 13 | public class EntitySerializationStrategy extends AbstractEntityPersistenceStrategy { 14 | 15 | String getFileSuffix() { 16 | return ".ser"; 17 | } 18 | 19 | void save(EntityTable table) throws IOException { 20 | File file = new File(getFileName(table) + getFileSuffix()); 21 | FileOutputStream fos = new FileOutputStream(file); 22 | BufferedOutputStream bos = new BufferedOutputStream(fos); 23 | ObjectOutputStream oos = new ObjectOutputStream(bos); 24 | oos.writeObject(table); 25 | oos.close(); 26 | } 27 | 28 | EntityTable restore(EntityTable table) throws IOException { 29 | File file = new File(getFileName(table) + getFileSuffix()); 30 | FileInputStream fis = new FileInputStream(file); 31 | BufferedInputStream bis = new BufferedInputStream(fis); 32 | ObjectInputStream ois = new ObjectInputStream(bis); 33 | try { 34 | table = (EntityTable) ois.readObject(); 35 | } catch (ClassNotFoundException ex) { 36 | throw new IOException(ex); 37 | } 38 | ois.close(); 39 | return table; 40 | } 41 | 42 | } 43 | --------------------------------------------------------------------------------