├── .gitignore ├── Adapter and Facade ├── Adapter and Facade.xcodeproj │ └── project.pbxproj └── Adapter and Facade │ └── main.cpp ├── Command Pattern ├── Command Pattern.xcodeproj │ └── project.pbxproj └── Command Pattern │ ├── CeilingFan.cpp │ ├── CeilingFan.hpp │ ├── CeilingFanCommand.cpp │ ├── CeilingFanCommand.hpp │ ├── Command.hpp │ ├── GarageDoor.cpp │ ├── GarageDoor.hpp │ ├── GarageDoorCloseCommand.cpp │ ├── GarageDoorCloseCommand.hpp │ ├── GarageDoorOpenCommand.cpp │ ├── GarageDoorOpenCommand.hpp │ ├── HotTub.cpp │ ├── HotTub.hpp │ ├── HotTubOffCommand.hpp │ ├── HotTubOnCommand.hpp │ ├── Light.cpp │ ├── Light.hpp │ ├── LightDimCommand.cpp │ ├── LightDimCommand.hpp │ ├── LightOffCommand.hpp │ ├── LightOnCommand.hpp │ ├── MacroCommand.cpp │ ├── MacroCommand.hpp │ ├── NoCommand.hpp │ ├── OnOffCommand.hpp │ ├── RemoteControlWithUndo.cpp │ ├── RemoteControlWithUndo.hpp │ ├── Stereo.cpp │ ├── Stereo.hpp │ ├── StereoOffCommand.hpp │ ├── StereoOnCommand.hpp │ ├── StereoOnWithCDCommand.cpp │ ├── StereoOnWithCDCommand.hpp │ ├── TV.cpp │ ├── TV.hpp │ ├── TVOffCommand.hpp │ ├── TVOnCommand.hpp │ └── main.cpp ├── Decorator Pattern ├── Decorator Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Decorator Pattern │ ├── Beverage.hpp │ ├── CondimentDecorator.hpp │ ├── DarkRoast.cpp │ ├── DarkRoast.hpp │ ├── Decaf.cpp │ ├── Decaf.hpp │ ├── Espresso.cpp │ ├── Espresso.hpp │ ├── HouseBlend.cpp │ ├── HouseBlend.hpp │ ├── Milk.cpp │ ├── Milk.hpp │ ├── Mocha.cpp │ ├── Mocha.hpp │ ├── Soy.cpp │ ├── Soy.hpp │ ├── Whip.cpp │ ├── Whip.hpp │ └── main.cpp ├── Factory Pattern ├── AbstractFactoryShenanigans │ ├── Pizza.cpp │ ├── Pizza.hpp │ ├── PizzaStore.cpp │ └── PizzaStore.hpp ├── Factory Pattern.xcodeproj │ └── project.pbxproj ├── MultiplePizzaTypes │ ├── MultiplePizzaTypes.hpp │ └── main.cpp ├── OnePizzaType │ ├── OnePizzaType.hpp │ └── main.cpp ├── PizzaStoreFranchise │ ├── ChicagoPizzaStore.cpp │ ├── ChicagoPizzaStore.hpp │ ├── ChicagoStyleCheesePizza.cpp │ ├── ChicagoStyleCheesePizza.hpp │ ├── ChicagoStylePepperoniPizza.cpp │ ├── ChicagoStylePepperoniPizza.hpp │ ├── ChicagoStyleVeggiePizza.cpp │ ├── ChicagoStyleVeggiePizza.hpp │ ├── NYPizzaStore.cpp │ ├── NYPizzaStore.hpp │ ├── NYStyleCheesePizza.cpp │ ├── NYStyleCheesePizza.hpp │ ├── NYStylePepperoniPizza.cpp │ ├── NYStylePepperoniPizza.hpp │ ├── Pizza.cpp │ ├── Pizza.hpp │ ├── PizzaStore.cpp │ ├── PizzaStore.hpp │ └── main.cpp ├── Simple Factory Pattern │ ├── CheesePizza.cpp │ ├── CheesePizza.hpp │ ├── ClamPizza.cpp │ ├── ClamPizza.hpp │ ├── PepperoniPizza.cpp │ ├── PepperoniPizza.hpp │ ├── Pizza.cpp │ ├── Pizza.hpp │ ├── PizzaStore.cpp │ ├── PizzaStore.hpp │ ├── SimplePizzaFactory.cpp │ ├── SimplePizzaFactory.hpp │ ├── VeggiePizza.cpp │ ├── VeggiePizza.hpp │ └── main.cpp └── main.cpp ├── Head First Design Patterns C++.xcworkspace └── contents.xcworkspacedata ├── LICENSE ├── Observer Pattern ├── Observer Pattern.xcodeproj │ └── project.pbxproj └── Observer Pattern │ ├── Weather │ ├── CurrentConditionsDisplay.cpp │ ├── CurrentConditionsDisplay.hpp │ ├── DisplayElement.hpp │ ├── ForecastDisplay.cpp │ ├── ForecastDisplay.hpp │ ├── HeatIndexDisplay.cpp │ ├── HeatIndexDisplay.hpp │ ├── Observer.hpp │ ├── StatisticsDisplay.cpp │ ├── StatisticsDisplay.hpp │ ├── Subject.hpp │ ├── WeatherData.cpp │ ├── WeatherData.hpp │ ├── WeatherStation.cpp │ └── WeatherStation.hpp │ ├── Weather_Lambdas │ ├── CurrentConditionsDisplay.cpp │ ├── CurrentConditionsDisplay.hpp │ ├── DisplayElement.hpp │ ├── ForecastDisplay.cpp │ ├── ForecastDisplay.hpp │ ├── HeatIndexDisplay.cpp │ ├── HeatIndexDisplay.hpp │ ├── StatisticsDisplay.cpp │ ├── StatisticsDisplay.hpp │ ├── Subject.hpp │ ├── WeatherData.cpp │ ├── WeatherData.hpp │ ├── WeatherStation.cpp │ └── WeatherStation.hpp │ └── main.cpp ├── README.md ├── Singleton Pattern ├── Singleton Pattern.xcodeproj │ └── project.pbxproj └── Singleton Pattern │ ├── ClassicSingleton.cpp │ ├── ClassicSingleton.hpp │ ├── MyersSingleton.cpp │ ├── MyersSingleton.hpp │ ├── ThreadSafeClassicSingleton.cpp │ ├── ThreadSafeClassicSingleton.hpp │ └── main.cpp └── Strategy Pattern ├── Strategy Pattern.xcodeproj └── project.pbxproj └── Strategy Pattern ├── DecoyDuck.cpp ├── DecoyDuck.hpp ├── Duck.cpp ├── Duck.hpp ├── FlyBehavior.cpp ├── FlyBehavior.hpp ├── FlyNoWay.cpp ├── FlyNoWay.hpp ├── FlyRocketPowered.cpp ├── FlyRocketPowered.hpp ├── FlyWithWings.cpp ├── FlyWithWings.hpp ├── MallardDuck.cpp ├── MallardDuck.hpp ├── ModelDuck.cpp ├── ModelDuck.hpp ├── MuteQuack.cpp ├── MuteQuack.hpp ├── Quack.cpp ├── Quack.hpp ├── QuackBehavior.cpp ├── QuackBehavior.hpp ├── RedHeadDuck.cpp ├── RedHeadDuck.hpp ├── RubberDuck.cpp ├── RubberDuck.hpp ├── Squeak.cpp ├── Squeak.hpp └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | xcuserdata 3 | -------------------------------------------------------------------------------- /Adapter and Facade/Adapter and Facade.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B694142F20273FC3008A03E0 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B694142E20273FC3008A03E0 /* main.cpp */; }; 11 | /* End PBXBuildFile section */ 12 | 13 | /* Begin PBXCopyFilesBuildPhase section */ 14 | B694142920273FC3008A03E0 /* CopyFiles */ = { 15 | isa = PBXCopyFilesBuildPhase; 16 | buildActionMask = 2147483647; 17 | dstPath = /usr/share/man/man1/; 18 | dstSubfolderSpec = 0; 19 | files = ( 20 | ); 21 | runOnlyForDeploymentPostprocessing = 1; 22 | }; 23 | /* End PBXCopyFilesBuildPhase section */ 24 | 25 | /* Begin PBXFileReference section */ 26 | B694142B20273FC3008A03E0 /* Adapter and Facade */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Adapter and Facade"; sourceTree = BUILT_PRODUCTS_DIR; }; 27 | B694142E20273FC3008A03E0 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; 28 | /* End PBXFileReference section */ 29 | 30 | /* Begin PBXFrameworksBuildPhase section */ 31 | B694142820273FC3008A03E0 /* Frameworks */ = { 32 | isa = PBXFrameworksBuildPhase; 33 | buildActionMask = 2147483647; 34 | files = ( 35 | ); 36 | runOnlyForDeploymentPostprocessing = 0; 37 | }; 38 | /* End PBXFrameworksBuildPhase section */ 39 | 40 | /* Begin PBXGroup section */ 41 | B694142220273FC3008A03E0 = { 42 | isa = PBXGroup; 43 | children = ( 44 | B694142D20273FC3008A03E0 /* Adapter and Facade */, 45 | B694142C20273FC3008A03E0 /* Products */, 46 | ); 47 | sourceTree = ""; 48 | }; 49 | B694142C20273FC3008A03E0 /* Products */ = { 50 | isa = PBXGroup; 51 | children = ( 52 | B694142B20273FC3008A03E0 /* Adapter and Facade */, 53 | ); 54 | name = Products; 55 | sourceTree = ""; 56 | }; 57 | B694142D20273FC3008A03E0 /* Adapter and Facade */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | B694142E20273FC3008A03E0 /* main.cpp */, 61 | ); 62 | path = "Adapter and Facade"; 63 | sourceTree = ""; 64 | }; 65 | /* End PBXGroup section */ 66 | 67 | /* Begin PBXNativeTarget section */ 68 | B694142A20273FC3008A03E0 /* Adapter and Facade */ = { 69 | isa = PBXNativeTarget; 70 | buildConfigurationList = B694143220273FC3008A03E0 /* Build configuration list for PBXNativeTarget "Adapter and Facade" */; 71 | buildPhases = ( 72 | B694142720273FC3008A03E0 /* Sources */, 73 | B694142820273FC3008A03E0 /* Frameworks */, 74 | B694142920273FC3008A03E0 /* CopyFiles */, 75 | ); 76 | buildRules = ( 77 | ); 78 | dependencies = ( 79 | ); 80 | name = "Adapter and Facade"; 81 | productName = "Adapter and Facade"; 82 | productReference = B694142B20273FC3008A03E0 /* Adapter and Facade */; 83 | productType = "com.apple.product-type.tool"; 84 | }; 85 | /* End PBXNativeTarget section */ 86 | 87 | /* Begin PBXProject section */ 88 | B694142320273FC3008A03E0 /* Project object */ = { 89 | isa = PBXProject; 90 | attributes = { 91 | LastUpgradeCheck = 0930; 92 | ORGANIZATIONNAME = "Brian Arnold"; 93 | TargetAttributes = { 94 | B694142A20273FC3008A03E0 = { 95 | CreatedOnToolsVersion = 9.3; 96 | ProvisioningStyle = Automatic; 97 | }; 98 | }; 99 | }; 100 | buildConfigurationList = B694142620273FC3008A03E0 /* Build configuration list for PBXProject "Adapter and Facade" */; 101 | compatibilityVersion = "Xcode 8.0"; 102 | developmentRegion = en; 103 | hasScannedForEncodings = 0; 104 | knownRegions = ( 105 | en, 106 | ); 107 | mainGroup = B694142220273FC3008A03E0; 108 | productRefGroup = B694142C20273FC3008A03E0 /* Products */; 109 | projectDirPath = ""; 110 | projectRoot = ""; 111 | targets = ( 112 | B694142A20273FC3008A03E0 /* Adapter and Facade */, 113 | ); 114 | }; 115 | /* End PBXProject section */ 116 | 117 | /* Begin PBXSourcesBuildPhase section */ 118 | B694142720273FC3008A03E0 /* Sources */ = { 119 | isa = PBXSourcesBuildPhase; 120 | buildActionMask = 2147483647; 121 | files = ( 122 | B694142F20273FC3008A03E0 /* main.cpp in Sources */, 123 | ); 124 | runOnlyForDeploymentPostprocessing = 0; 125 | }; 126 | /* End PBXSourcesBuildPhase section */ 127 | 128 | /* Begin XCBuildConfiguration section */ 129 | B694143020273FC3008A03E0 /* Debug */ = { 130 | isa = XCBuildConfiguration; 131 | buildSettings = { 132 | ALWAYS_SEARCH_USER_PATHS = NO; 133 | CLANG_ANALYZER_NONNULL = YES; 134 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 135 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 136 | CLANG_CXX_LIBRARY = "libc++"; 137 | CLANG_ENABLE_MODULES = YES; 138 | CLANG_ENABLE_OBJC_ARC = YES; 139 | CLANG_ENABLE_OBJC_WEAK = YES; 140 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 141 | CLANG_WARN_BOOL_CONVERSION = YES; 142 | CLANG_WARN_COMMA = YES; 143 | CLANG_WARN_CONSTANT_CONVERSION = YES; 144 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 145 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 146 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 147 | CLANG_WARN_EMPTY_BODY = YES; 148 | CLANG_WARN_ENUM_CONVERSION = YES; 149 | CLANG_WARN_INFINITE_RECURSION = YES; 150 | CLANG_WARN_INT_CONVERSION = YES; 151 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 152 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 153 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 154 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 155 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 156 | CLANG_WARN_STRICT_PROTOTYPES = YES; 157 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 158 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 159 | CLANG_WARN_UNREACHABLE_CODE = YES; 160 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 161 | CODE_SIGN_IDENTITY = "-"; 162 | COPY_PHASE_STRIP = NO; 163 | DEBUG_INFORMATION_FORMAT = dwarf; 164 | ENABLE_STRICT_OBJC_MSGSEND = YES; 165 | ENABLE_TESTABILITY = YES; 166 | GCC_C_LANGUAGE_STANDARD = gnu11; 167 | GCC_DYNAMIC_NO_PIC = NO; 168 | GCC_NO_COMMON_BLOCKS = YES; 169 | GCC_OPTIMIZATION_LEVEL = 0; 170 | GCC_PREPROCESSOR_DEFINITIONS = ( 171 | "DEBUG=1", 172 | "$(inherited)", 173 | ); 174 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 175 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 176 | GCC_WARN_UNDECLARED_SELECTOR = YES; 177 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 178 | GCC_WARN_UNUSED_FUNCTION = YES; 179 | GCC_WARN_UNUSED_VARIABLE = YES; 180 | MACOSX_DEPLOYMENT_TARGET = 10.13; 181 | MTL_ENABLE_DEBUG_INFO = YES; 182 | ONLY_ACTIVE_ARCH = YES; 183 | SDKROOT = macosx; 184 | }; 185 | name = Debug; 186 | }; 187 | B694143120273FC3008A03E0 /* Release */ = { 188 | isa = XCBuildConfiguration; 189 | buildSettings = { 190 | ALWAYS_SEARCH_USER_PATHS = NO; 191 | CLANG_ANALYZER_NONNULL = YES; 192 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 193 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 194 | CLANG_CXX_LIBRARY = "libc++"; 195 | CLANG_ENABLE_MODULES = YES; 196 | CLANG_ENABLE_OBJC_ARC = YES; 197 | CLANG_ENABLE_OBJC_WEAK = YES; 198 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 199 | CLANG_WARN_BOOL_CONVERSION = YES; 200 | CLANG_WARN_COMMA = YES; 201 | CLANG_WARN_CONSTANT_CONVERSION = YES; 202 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 203 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 204 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 205 | CLANG_WARN_EMPTY_BODY = YES; 206 | CLANG_WARN_ENUM_CONVERSION = YES; 207 | CLANG_WARN_INFINITE_RECURSION = YES; 208 | CLANG_WARN_INT_CONVERSION = YES; 209 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 210 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 211 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 212 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 213 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 214 | CLANG_WARN_STRICT_PROTOTYPES = YES; 215 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 216 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 217 | CLANG_WARN_UNREACHABLE_CODE = YES; 218 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 219 | CODE_SIGN_IDENTITY = "-"; 220 | COPY_PHASE_STRIP = NO; 221 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 222 | ENABLE_NS_ASSERTIONS = NO; 223 | ENABLE_STRICT_OBJC_MSGSEND = YES; 224 | GCC_C_LANGUAGE_STANDARD = gnu11; 225 | GCC_NO_COMMON_BLOCKS = YES; 226 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 227 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 228 | GCC_WARN_UNDECLARED_SELECTOR = YES; 229 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 230 | GCC_WARN_UNUSED_FUNCTION = YES; 231 | GCC_WARN_UNUSED_VARIABLE = YES; 232 | MACOSX_DEPLOYMENT_TARGET = 10.13; 233 | MTL_ENABLE_DEBUG_INFO = NO; 234 | SDKROOT = macosx; 235 | }; 236 | name = Release; 237 | }; 238 | B694143320273FC3008A03E0 /* Debug */ = { 239 | isa = XCBuildConfiguration; 240 | buildSettings = { 241 | CODE_SIGN_STYLE = Automatic; 242 | PRODUCT_NAME = "$(TARGET_NAME)"; 243 | }; 244 | name = Debug; 245 | }; 246 | B694143420273FC3008A03E0 /* Release */ = { 247 | isa = XCBuildConfiguration; 248 | buildSettings = { 249 | CODE_SIGN_STYLE = Automatic; 250 | PRODUCT_NAME = "$(TARGET_NAME)"; 251 | }; 252 | name = Release; 253 | }; 254 | /* End XCBuildConfiguration section */ 255 | 256 | /* Begin XCConfigurationList section */ 257 | B694142620273FC3008A03E0 /* Build configuration list for PBXProject "Adapter and Facade" */ = { 258 | isa = XCConfigurationList; 259 | buildConfigurations = ( 260 | B694143020273FC3008A03E0 /* Debug */, 261 | B694143120273FC3008A03E0 /* Release */, 262 | ); 263 | defaultConfigurationIsVisible = 0; 264 | defaultConfigurationName = Release; 265 | }; 266 | B694143220273FC3008A03E0 /* Build configuration list for PBXNativeTarget "Adapter and Facade" */ = { 267 | isa = XCConfigurationList; 268 | buildConfigurations = ( 269 | B694143320273FC3008A03E0 /* Debug */, 270 | B694143420273FC3008A03E0 /* Release */, 271 | ); 272 | defaultConfigurationIsVisible = 0; 273 | defaultConfigurationName = Release; 274 | }; 275 | /* End XCConfigurationList section */ 276 | }; 277 | rootObject = B694142320273FC3008A03E0 /* Project object */; 278 | } 279 | -------------------------------------------------------------------------------- /Adapter and Facade/Adapter and Facade/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Adapter and Facade 4 | // 5 | // Created by Brian Arnold on 2/4/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | int main(int argc, const char * argv[]) { 12 | // insert code here... 13 | std::cout << "Hello, World!\n"; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/CeilingFan.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CeilingFan.cpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "CeilingFan.hpp" 12 | 13 | #include 14 | 15 | CeilingFan::CeilingFan(const std::string& location) : 16 | location(location) { 17 | 18 | } 19 | 20 | CeilingFan::Speed CeilingFan::getSpeed() const { 21 | return speed; 22 | } 23 | 24 | void CeilingFan::setSpeed(Speed speed) { 25 | this->speed = speed; 26 | std::cout << getDescription() << std::endl; 27 | } 28 | 29 | std::string CeilingFan::getDescription() const { 30 | std::string state; 31 | 32 | switch (speed) { 33 | case Speed::off: state = "off"; break; 34 | case Speed::low: state = "on low"; break; 35 | case Speed::medium: state = "on medium"; break; 36 | case Speed::high: state = "on high"; break; 37 | case Speed::invalid: state = "INVALID"; break; 38 | } 39 | 40 | return location + " Ceiling Fan is " + state; 41 | } 42 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/CeilingFan.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // CeilingFan.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef CeilingFan_hpp 12 | #define CeilingFan_hpp 13 | 14 | #include 15 | 16 | class CeilingFan { 17 | public: 18 | CeilingFan(const std::string& location); 19 | 20 | // IN C++: Use an enum class rather than individual int constants. 21 | // IN C++: Java sample code Command classes have undefined state for previousSpeed 22 | // between the time the constructor is called and the time execute() is called. 23 | // We would use std::optional when it becomes available in C++17. 24 | // In the meantime, specify an "invalid" value for the enum. 25 | enum class Speed { off, low, medium, high, invalid }; 26 | 27 | // Notice that the CielingFan class holds local state representing the speed of the ceiling fan. 28 | Speed getSpeed() const; 29 | 30 | void setSpeed(Speed speed); 31 | 32 | std::string getDescription() const; 33 | 34 | private: 35 | std::string location; 36 | Speed speed; 37 | }; 38 | 39 | 40 | #endif /* CeilingFan_hpp */ 41 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/CeilingFanCommand.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CeilingFanCommand.cpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "CeilingFanCommand.hpp" 12 | 13 | CeilingFanCommand::CeilingFanCommand(const std::shared_ptr& ceilingFan, CeilingFan::Speed speed) : 14 | ceilingFan(ceilingFan), 15 | newSpeed(speed), 16 | previousSpeed(CeilingFan::Speed::invalid) { 17 | } 18 | 19 | void CeilingFanCommand::execute() { 20 | // In execute, before we change the speed of the fan, we need to first record its previous state, just in case we need to undo our actions. 21 | previousSpeed = ceilingFan->getSpeed(); 22 | ceilingFan->setSpeed(newSpeed); 23 | } 24 | 25 | void CeilingFanCommand::undo() { 26 | // To undo, we set the speed of the fan back to its previous speed. 27 | // IN C++: Check for invalid speed (if undo() is called before execute() is called) 28 | if (previousSpeed == CeilingFan::Speed::invalid) { return; } 29 | ceilingFan->setSpeed(previousSpeed); 30 | } 31 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/CeilingFanCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // CeilingFanCommand.hpp 3 | // Command Pattern 4 | // 5 | // Created by Brian Arnold on 3/15/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef CeilingFanCommand_hpp 10 | #define CeilingFanCommand_hpp 11 | 12 | #include "Command.hpp" 13 | 14 | #include "CeilingFan.hpp" 15 | 16 | // IN C++: we create a base class to store the new speed. 17 | // IN C++: we won't create individual command classes for setting the speed or turning the fan off. This has a consequence of exposing CeilingFan.Speed to clients, but we might make this tradeoff to reduce the overall number of command classes. 18 | class CeilingFanCommand: public Command { 19 | public: 20 | CeilingFanCommand(const std::shared_ptr& ceilingFan, CeilingFan::Speed speed); 21 | 22 | void execute() override; 23 | 24 | void undo() override; 25 | 26 | private: 27 | std::shared_ptr ceilingFan; 28 | CeilingFan::Speed newSpeed; 29 | 30 | // We've added local state to keep track of the previous speed of the fan. 31 | // IN C++: Use "std::optional" (C++17) because it's not initialized until execute() is called. 32 | CeilingFan::Speed previousSpeed; 33 | }; 34 | 35 | 36 | #endif /* CeilingFanCommand_hpp */ 37 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/Command.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Command.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef Command_hpp 12 | #define Command_hpp 13 | 14 | class Command { 15 | public: 16 | virtual void execute() = 0; 17 | 18 | virtual void undo() = 0; 19 | 20 | virtual ~Command() = default; 21 | }; 22 | 23 | #endif /* Command_hpp */ 24 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/GarageDoor.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // GarageDoor.cpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "GarageDoor.hpp" 12 | 13 | #include 14 | 15 | GarageDoor::GarageDoor(const std::string& location) : 16 | location(location) { 17 | } 18 | 19 | void GarageDoor::up() { 20 | std::cout << location << " Garage Door is opened." << std::endl; 21 | } 22 | 23 | void GarageDoor::down() { 24 | std::cout << location << " Garage Door is closed." << std::endl; 25 | } 26 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/GarageDoor.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // GarageDoor.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef GarageDoor_hpp 12 | #define GarageDoor_hpp 13 | 14 | #include 15 | 16 | class GarageDoor { 17 | public: 18 | GarageDoor(const std::string& location); 19 | 20 | void up(); 21 | 22 | void down(); 23 | 24 | private: 25 | std::string location; 26 | }; 27 | 28 | #endif /* GarageDoor_hpp */ 29 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/GarageDoorCloseCommand.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // GarageDoorCloseCommand.cpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "GarageDoorCloseCommand.hpp" 12 | 13 | GarageDoorCloseCommand::GarageDoorCloseCommand(const std::shared_ptr& garageDoor) : 14 | garageDoor(garageDoor) { 15 | } 16 | 17 | void GarageDoorCloseCommand::execute() { 18 | garageDoor->down(); 19 | } 20 | 21 | void GarageDoorCloseCommand::undo() { 22 | garageDoor->up(); 23 | } 24 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/GarageDoorCloseCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // GarageDoorCloseCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef GarageDoorCloseCommand_hpp 12 | #define GarageDoorCloseCommand_hpp 13 | 14 | #include "Command.hpp" 15 | 16 | #include "GarageDoor.hpp" 17 | 18 | class GarageDoorCloseCommand: public Command { 19 | public: 20 | GarageDoorCloseCommand(const std::shared_ptr& garageDoor); 21 | 22 | void execute() override; 23 | 24 | void undo() override; 25 | 26 | private: 27 | std::shared_ptr garageDoor; 28 | }; 29 | 30 | 31 | #endif /* GarageDoorCloseCommand_hpp */ 32 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/GarageDoorOpenCommand.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // GarageDoorOpenCommand.cpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "GarageDoorOpenCommand.hpp" 12 | 13 | GarageDoorOpenCommand::GarageDoorOpenCommand(const std::shared_ptr& garageDoor) : 14 | garageDoor(garageDoor) { 15 | } 16 | 17 | void GarageDoorOpenCommand::execute() { 18 | garageDoor->down(); 19 | } 20 | 21 | void GarageDoorOpenCommand::undo() { 22 | garageDoor->up(); 23 | } 24 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/GarageDoorOpenCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // GarageDoorOpenCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef GarageDoorOpenCommand_hpp 12 | #define GarageDoorOpenCommand_hpp 13 | 14 | #include "Command.hpp" 15 | 16 | #include "GarageDoor.hpp" 17 | 18 | class GarageDoorOpenCommand: public Command { 19 | public: 20 | GarageDoorOpenCommand(const std::shared_ptr& garageDoor); 21 | 22 | void execute() override; 23 | 24 | void undo() override; 25 | 26 | private: 27 | std::shared_ptr garageDoor; 28 | }; 29 | 30 | #endif /* GarageDoorOpenCommand_hpp */ 31 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/HotTub.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // HotTub.cpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "HotTub.hpp" 12 | 13 | #include 14 | 15 | HotTub::HotTub(const std::string& location) : 16 | location(location) { 17 | } 18 | 19 | void HotTub::on() { 20 | std::cout << location << " Hot Tub is on." << std::endl; 21 | } 22 | 23 | void HotTub::off() { 24 | std::cout << location << " Hot Tub is off." << std::endl; 25 | } 26 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/HotTub.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // HotTub.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef HotTub_hpp 12 | #define HotTub_hpp 13 | 14 | #include 15 | 16 | class HotTub { 17 | public: 18 | HotTub(const std::string& location); 19 | 20 | void on(); 21 | 22 | void off(); 23 | 24 | private: 25 | std::string location; 26 | }; 27 | 28 | #endif /* HotTub_hpp */ 29 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/HotTubOffCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // HotTubOffCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef HotTubOffCommand_hpp 12 | #define HotTubOffCommand_hpp 13 | 14 | #include "OnOffCommand.hpp" 15 | 16 | #include "HotTub.hpp" 17 | 18 | using HotTubOffCommand = OnOffCommand; 19 | 20 | #endif /* HotTubOffCommand_hpp */ 21 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/HotTubOnCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // HotTubOnCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef HotTubOnCommand_hpp 12 | #define HotTubOnCommand_hpp 13 | 14 | #include "OnOffCommand.hpp" 15 | 16 | #include "HotTub.hpp" 17 | 18 | using HotTubOnCommand = OnOffCommand; 19 | 20 | #endif /* HotTubOnCommand_hpp */ 21 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/Light.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Light.cpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "Light.hpp" 12 | 13 | #include 14 | 15 | Light::Light(const std::string& location) : 16 | location(location), 17 | level(0) { 18 | } 19 | 20 | void Light::on() { 21 | dimmed(100); 22 | } 23 | 24 | void Light::off() { 25 | dimmed(0); 26 | } 27 | 28 | const int Light::invalidDimmedLevel = -1; 29 | 30 | void Light::dimmed(int level) { 31 | // IN C++: check for valid range. For example, invalidDimmedLevel (-1) is not in the valid range. 32 | if (level < 0 || level > 100) { return; } 33 | 34 | this->level = level; 35 | 36 | switch (level) { 37 | case 0: 38 | std::cout << location << " Light is off" << std::endl; 39 | break; 40 | case 100: 41 | std::cout << location << " Light is on" << std::endl; 42 | break; 43 | default: 44 | std::cout << location << " Light is dimmed to " << level << "%" << std::endl; 45 | } 46 | } 47 | 48 | int Light::getLevel() const { 49 | return level; 50 | } 51 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/Light.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Light.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef Light_hpp 12 | #define Light_hpp 13 | 14 | #include 15 | 16 | class Light { 17 | public: 18 | Light(const std::string& location); 19 | 20 | void on(); 21 | 22 | void off(); 23 | 24 | // IN C++: Java sample code Command classes have undefined state for previousLevel 25 | // between the time the constructor is called and the time execute() is called. 26 | // We would use std::optional when it becomes available in C++17. 27 | // In the meantime, specify an "invalid" (out of range 0...100) value for the int. 28 | static const int invalidDimmedLevel; 29 | 30 | void dimmed(int level); 31 | 32 | int getLevel() const; 33 | 34 | private: 35 | std::string location; 36 | int level; 37 | }; 38 | 39 | #endif /* Light_hpp */ 40 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/LightDimCommand.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // LightDimCommand.cpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "LightDimCommand.hpp" 12 | 13 | LightDimCommand::LightDimCommand(const std::shared_ptr& light, int level) : 14 | light(light), 15 | level(level), 16 | previousLevel(Light::invalidDimmedLevel) { 17 | } 18 | 19 | void LightDimCommand::execute() { 20 | previousLevel = light->getLevel(); 21 | light->dimmed(level); 22 | } 23 | 24 | void LightDimCommand::undo() { 25 | // IN C++: check for invalid dimmed level and exit early (undo() called before execute() called). 26 | if (previousLevel == Light::invalidDimmedLevel) { return; } 27 | light->dimmed(previousLevel); 28 | } 29 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/LightDimCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // LightDimCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef LightDimCommand_hpp 12 | #define LightDimCommand_hpp 13 | 14 | #include "Command.hpp" 15 | 16 | #include "Light.hpp" 17 | 18 | #include 19 | 20 | class LightDimCommand: public Command { 21 | public: 22 | LightDimCommand(const std::shared_ptr& light, int level); 23 | 24 | void execute() override; 25 | 26 | void undo() override; 27 | 28 | private: 29 | std::shared_ptr light; 30 | int level; 31 | int previousLevel; 32 | }; 33 | 34 | 35 | #endif /* LightDimCommand_hpp */ 36 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/LightOffCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // LightOffCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef LightOffCommand_hpp 12 | #define LightOffCommand_hpp 13 | 14 | #include "LightDimCommand.hpp" 15 | 16 | class LightOffCommand: public LightDimCommand { 17 | public: 18 | LightOffCommand(const std::shared_ptr& light) : 19 | LightDimCommand(light, 0) { 20 | } 21 | }; 22 | 23 | #endif /* LightOffCommand_hpp */ 24 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/LightOnCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // LightOnCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/15/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef LightOnCommand_hpp 12 | #define LightOnCommand_hpp 13 | 14 | #include "LightDimCommand.hpp" 15 | 16 | // IN C++: Because this implementation is trivial, inline it. 17 | class LightOnCommand: public LightDimCommand { 18 | public: 19 | LightOnCommand(const std::shared_ptr& light) : 20 | LightDimCommand(light, 100) { 21 | } 22 | }; 23 | 24 | #endif /* LightOnCommand_hpp */ 25 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/MacroCommand.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // MacroCommand.cpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/16/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "MacroCommand.hpp" 12 | 13 | MacroCommand::MacroCommand(const std::vector>& commands) : 14 | commands(commands) { 15 | // Take an array of Commands and store them in the Macro command 16 | } 17 | 18 | void MacroCommand::execute() { 19 | for (auto& command : commands) { 20 | // When the macro gets executed by the remote, execute those commands one at a time. 21 | command->execute(); 22 | } 23 | } 24 | 25 | void MacroCommand::undo() { 26 | // Make sure to "undo" the commands in reverse order. 27 | std::for_each(commands.rbegin(), commands.rend(), [](auto& command) { 28 | command->undo(); 29 | }); 30 | } 31 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/MacroCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // MacroCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/16/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef MacroCommand_hpp 12 | #define MacroCommand_hpp 13 | 14 | #include "Command.hpp" 15 | 16 | #include 17 | 18 | class MacroCommand: public Command { 19 | public: 20 | MacroCommand(const std::vector>& commands); 21 | 22 | void execute() override; 23 | 24 | void undo() override; 25 | 26 | private: 27 | std::vector> commands; 28 | }; 29 | 30 | #endif /* MacroCommand_hpp */ 31 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/NoCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // NoCommand.hpp 3 | // Command Pattern 4 | // 5 | // Created by Brian Arnold on 3/16/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef NoCommand_hpp 10 | #define NoCommand_hpp 11 | 12 | #include "Command.hpp" 13 | 14 | class NoCommand: public Command { 15 | 16 | void execute() override { } 17 | 18 | void undo() override { } 19 | 20 | }; 21 | 22 | #endif /* NoCommand_hpp */ 23 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/OnOffCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // OnOffCommand.hpp 3 | // Command Pattern 4 | // 5 | // Created by Brian Arnold on 4/8/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef OnOffCommand_hpp 10 | #define OnOffCommand_hpp 11 | 12 | #include "Command.hpp" 13 | 14 | // IN C++: Use a template to reduce the amount of boilerplate in "on()"/"off()" commands. 15 | template 16 | class OnOffCommand : public Command 17 | { 18 | public: 19 | OnOffCommand(const std::shared_ptr& _entity) : 20 | entity(_entity) { } 21 | 22 | void execute() override { isOn ? entity->on() : entity->off(); } 23 | 24 | void undo() override { isOn ? entity->off() : entity->on(); } 25 | 26 | private: 27 | std::shared_ptr entity; 28 | }; 29 | 30 | #endif /* OnOffCommand_hpp */ 31 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/RemoteControlWithUndo.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // RemoteControlWithUndo.cpp 3 | // Command Pattern 4 | // 5 | // Created by Brian Arnold on 3/16/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "RemoteControlWithUndo.hpp" 10 | 11 | #include "NoCommand.hpp" 12 | 13 | #include // std::string 14 | #include // std::cout 15 | #include // std::stringstream 16 | 17 | RemoteControlWithUndo::RemoteControlWithUndo() { 18 | auto noCommand = std::make_shared(); 19 | onCommands = std::vector>(7); 20 | offCommands = std::vector>(7); 21 | undoCommand = nullptr; 22 | } 23 | 24 | void RemoteControlWithUndo::setCommand(std::size_t slot, std::shared_ptr onCommand, std::shared_ptr offCommand) { 25 | onCommands[slot] = onCommand; 26 | offCommands[slot] = offCommand; 27 | } 28 | 29 | void RemoteControlWithUndo::onButtonWasPushed(std::size_t slot) { 30 | onCommands[slot]->execute(); 31 | undoCommand = onCommands[slot]; 32 | } 33 | 34 | void RemoteControlWithUndo::offButtonWasPushed(std::size_t slot) { 35 | offCommands[slot]->execute(); 36 | undoCommand = offCommands[slot]; 37 | } 38 | 39 | void RemoteControlWithUndo::undoButtonWasPushed() { 40 | undoCommand->undo(); 41 | } 42 | 43 | std::ostream& operator<<(std::ostream& os, const RemoteControlWithUndo& remote) 44 | { 45 | //std::stringstream desc; 46 | 47 | os << "\n----------- Remote Control -----------\n"; 48 | for (int index = 0; index < remote.onCommands.size(); ++index) { 49 | auto onCommand = remote.onCommands[index]; 50 | auto offCommand = remote.offCommands[index]; 51 | os << "[slot " << index << "] " << onCommand << " " << offCommand << std::endl; 52 | } 53 | 54 | os << "[undo] " << remote.undoCommand << std::endl; 55 | 56 | return os; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/RemoteControlWithUndo.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // RemoteControlWithUndo.hpp 3 | // Command Pattern 4 | // 5 | // Created by Brian Arnold on 3/16/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef RemoteControlWithUndo_hpp 10 | #define RemoteControlWithUndo_hpp 11 | 12 | #include "Command.hpp" 13 | 14 | #include 15 | #include 16 | 17 | class RemoteControlWithUndo { 18 | public: 19 | RemoteControlWithUndo(); 20 | 21 | void setCommand(std::size_t slot, std::shared_ptr onCommand, std::shared_ptr offCommand); 22 | 23 | void onButtonWasPushed(std::size_t slot); 24 | 25 | void offButtonWasPushed(std::size_t slot); 26 | 27 | void undoButtonWasPushed(); 28 | 29 | std::string toString() const; 30 | 31 | private: 32 | std::vector> onCommands; 33 | std::vector> offCommands; 34 | std::shared_ptr undoCommand; 35 | 36 | friend std::ostream& operator<<(std::ostream& os, const RemoteControlWithUndo& remote); 37 | }; 38 | 39 | // IN C++: use ostream instead of toString 40 | std::ostream& operator<<(std::ostream& os, const RemoteControlWithUndo& remote); 41 | 42 | #endif /* RemoteControlWithUndo_hpp */ 43 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/Stereo.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stereo.cpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/16/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "Stereo.hpp" 12 | 13 | #include 14 | 15 | Stereo::Stereo(const std::string& location) : 16 | location(location) { 17 | } 18 | 19 | void Stereo::on() { 20 | std::cout << location << " Stereo is on" << std::endl; 21 | } 22 | 23 | void Stereo::off() { 24 | std::cout << location << " Stereo is off" << std::endl; 25 | } 26 | 27 | void Stereo::setCD() { 28 | std::cout << location << " Stereo is set to CD" << std::endl; 29 | } 30 | 31 | void Stereo::setDVD() { 32 | std::cout << location << " Stereo is set to DVD" << std::endl; 33 | 34 | } 35 | 36 | void Stereo::setRadio() { 37 | std::cout << location << " Stereo is set to Radio" << std::endl; 38 | 39 | } 40 | 41 | void Stereo::setVolume(int volume) { 42 | // code to set the volume 43 | // valid range: 1-11 (after all 11 is better than 10, right?) 44 | std::cout << location << " Stereo volume set to " << volume << std::endl; 45 | } 46 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/Stereo.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stereo.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/16/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef Stereo_hpp 12 | #define Stereo_hpp 13 | 14 | #include 15 | 16 | class Stereo { 17 | public: 18 | Stereo(const std::string& location); 19 | 20 | void on(); 21 | 22 | void off(); 23 | 24 | void setCD(); 25 | 26 | void setDVD(); 27 | 28 | void setRadio(); 29 | 30 | void setVolume(int volume); 31 | 32 | private: 33 | std::string location; 34 | 35 | }; 36 | 37 | #endif /* Stereo_hpp */ 38 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/StereoOffCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // StereoOffCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/16/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef StereoOffCommand_hpp 12 | #define StereoOffCommand_hpp 13 | 14 | #include "OnOffCommand.hpp" 15 | 16 | #include "Stereo.hpp" 17 | 18 | using StereoOffCommand = OnOffCommand; 19 | 20 | #endif /* StereoOffCommand_hpp */ 21 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/StereoOnCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // StereoOnCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/16/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef StereoOnCommand_hpp 12 | #define StereoOnCommand_hpp 13 | 14 | #include "OnOffCommand.hpp" 15 | 16 | #include "Stereo.hpp" 17 | 18 | using StereoOnCommand = OnOffCommand; 19 | 20 | #endif /* StereoOnCommand_hpp */ 21 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/StereoOnWithCDCommand.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // StereoOnWithCDCommand.cpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/16/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "StereoOnWithCDCommand.hpp" 12 | 13 | StereoOnWithCDCommand::StereoOnWithCDCommand(const std::shared_ptr& stereo) : 14 | stereo(stereo) { 15 | } 16 | 17 | void StereoOnWithCDCommand::execute() { 18 | stereo->on(); 19 | stereo->setCD(); 20 | stereo->setVolume(11); 21 | } 22 | 23 | void StereoOnWithCDCommand::undo() { 24 | stereo->off(); 25 | } 26 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/StereoOnWithCDCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // StereoOnWithCDCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/16/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef StereoOnWithCDCommand_hpp 12 | #define StereoOnWithCDCommand_hpp 13 | 14 | #include "Command.hpp" 15 | 16 | #include "Stereo.hpp" 17 | 18 | class StereoOnWithCDCommand: public Command { 19 | public: 20 | StereoOnWithCDCommand(const std::shared_ptr& stereo); 21 | 22 | void execute() override; 23 | 24 | void undo() override; 25 | 26 | private: 27 | std::shared_ptr stereo; 28 | }; 29 | 30 | #endif /* StereoOnWithCDCommand_hpp */ 31 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/TV.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // TV.cpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/16/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "TV.hpp" 12 | 13 | 14 | #include 15 | 16 | TV::TV(const std::string& location) : 17 | location(location) { 18 | } 19 | 20 | void TV::on() { 21 | std::cout << location << " TV is on." << std::endl; 22 | } 23 | 24 | void TV::off() { 25 | std::cout << location << " TV is off." << std::endl; 26 | } 27 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/TV.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // TV.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/16/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef TV_hpp 12 | #define TV_hpp 13 | 14 | #include 15 | 16 | class TV { 17 | public: 18 | TV(const std::string& location); 19 | 20 | void on(); 21 | 22 | void off(); 23 | 24 | private: 25 | std::string location; 26 | }; 27 | 28 | #endif /* TV_hpp */ 29 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/TVOffCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // TVOffCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/16/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef TVOffCommand_hpp 12 | #define TVOffCommand_hpp 13 | 14 | #include "OnOffCommand.hpp" 15 | 16 | #include "TV.hpp" 17 | 18 | using TVOffCommand = OnOffCommand; 19 | 20 | #endif /* TVOffCommand_hpp */ 21 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/TVOnCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // TVOnCommand.hpp 3 | // Command Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 3/16/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef TVOnCommand_hpp 12 | #define TVOnCommand_hpp 13 | 14 | #include "OnOffCommand.hpp" 15 | 16 | #include "TV.hpp" 17 | 18 | using TVOnCommand = OnOffCommand; 19 | 20 | #endif /* TVOnCommand_hpp */ 21 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Command Pattern 4 | // 5 | // Created by Brian Arnold on 2/4/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | 12 | #include "RemoteControlWithUndo.hpp" 13 | #include "Light.hpp" 14 | #include "LightOnCommand.hpp" 15 | #include "LightOffCommand.hpp" 16 | #include "CeilingFan.hpp" 17 | #include "CeilingFanCommand.hpp" 18 | #include "TV.hpp" 19 | #include "TVOnCommand.hpp" 20 | #include "TVOffCommand.hpp" 21 | #include "Stereo.hpp" 22 | #include "StereoOnWithCDCommand.hpp" 23 | #include "StereoOffCommand.hpp" 24 | #include "HotTub.hpp" 25 | #include "HotTubOnCommand.hpp" 26 | #include "HotTubOffCommand.hpp" 27 | #include "MacroCommand.hpp" 28 | 29 | int main(int argc, const char * argv[]) { 30 | RemoteControlWithUndo remoteControl; 31 | auto livingRoomLight = std::make_shared("Living Room"); 32 | auto livingRoomLightOn = std::make_shared(livingRoomLight); 33 | auto livingRoomLightOff = std::make_shared(livingRoomLight); 34 | 35 | remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); 36 | 37 | remoteControl.onButtonWasPushed(0); 38 | remoteControl.offButtonWasPushed(0); 39 | std::cout << remoteControl; 40 | remoteControl.undoButtonWasPushed(); 41 | remoteControl.offButtonWasPushed(0); 42 | remoteControl.onButtonWasPushed(0); 43 | std::cout << remoteControl; 44 | remoteControl.undoButtonWasPushed(); 45 | 46 | auto ceilingFan = std::make_shared("Living Room"); 47 | 48 | auto ceilingFanMedium = std::make_shared(ceilingFan, CeilingFan::Speed::medium); 49 | auto ceilingFanHigh = std::make_shared(ceilingFan, CeilingFan::Speed::high); 50 | auto ceilingFanOff = std::make_shared(ceilingFan, CeilingFan::Speed::off); 51 | 52 | remoteControl.setCommand(0, ceilingFanMedium, ceilingFanOff); 53 | remoteControl.setCommand(1, ceilingFanHigh, ceilingFanOff); 54 | 55 | remoteControl.onButtonWasPushed(0); 56 | remoteControl.offButtonWasPushed(0); 57 | std::cout << remoteControl; 58 | remoteControl.undoButtonWasPushed(); 59 | 60 | remoteControl.onButtonWasPushed(1); 61 | std::cout << remoteControl; 62 | remoteControl.undoButtonWasPushed(); 63 | 64 | //: The following demonstrates the use of the macro command with undo. 65 | 66 | // Create all the devices: a light, tv, stereo and hot tub. 67 | auto light = std::make_shared("Living Room"); 68 | auto tv = std::make_shared("Living Room"); 69 | auto stereo = std::make_shared("Living Room"); 70 | auto hotTub = std::make_shared("Back Yard"); 71 | 72 | // Now create all the On and Off commands to control them. 73 | auto lightOn = std::make_shared(light); 74 | auto lightOff = std::make_shared(light); 75 | auto tvOn = std::make_shared(tv); 76 | auto tvOff = std::make_shared(tv); 77 | auto stereoOn = std::make_shared(stereo); 78 | auto stereoOff = std::make_shared(stereo); 79 | auto hotTubOn = std::make_shared(hotTub); 80 | auto hotTubOff = std::make_shared(hotTub); 81 | 82 | // Create an array for On and an array for Off commands. 83 | std::vector> partyOn = { lightOn, stereoOn, tvOn, hotTubOn }; 84 | std::vector> partyOff = { lightOff, stereoOff, tvOff, hotTubOff }; 85 | 86 | // ... and create two corresponding macros to hold them. 87 | auto partyOnMacro = std::make_shared(partyOn); 88 | auto partyOffMacro = std::make_shared(partyOff); 89 | 90 | // Assign the macro to a button as we would any command. 91 | remoteControl.setCommand(2, partyOnMacro, partyOffMacro); 92 | 93 | //: Finally, we just need ot push some buttons and see if this works. 94 | 95 | std::cout << std::endl << remoteControl << std::endl; 96 | std::cout << "--------- Pushing Macro On ---------" << std::endl; 97 | remoteControl.onButtonWasPushed(2); 98 | std::cout << "--------- Pushing Macro Off --------" << std::endl; 99 | remoteControl.offButtonWasPushed(2); 100 | 101 | return 0; 102 | } 103 | 104 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 3D2DFFAD203DD2C40050E7C3 /* Mocha.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D733C0120336CEB006C8694 /* Mocha.cpp */; }; 11 | 3D733BEE20336ACF006C8694 /* HouseBlend.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D733BEC20336ACF006C8694 /* HouseBlend.cpp */; }; 12 | 3D733BF120336AE2006C8694 /* DarkRoast.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D733BEF20336AE2006C8694 /* DarkRoast.cpp */; }; 13 | 3D733BF420336AED006C8694 /* Decaf.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D733BF220336AED006C8694 /* Decaf.cpp */; }; 14 | 3D8F2FFC203CE6A300CEF165 /* Whip.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D733C0720336D05006C8694 /* Whip.cpp */; }; 15 | 3D8F2FFD203CE78800CEF165 /* Soy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D733C0420336CFB006C8694 /* Soy.cpp */; }; 16 | 3DE5F6FD20324CC50033BA18 /* Espresso.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DE5F6FB20324CC50033BA18 /* Espresso.cpp */; }; 17 | B66DFE0F20273C7400C180B4 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B66DFE0E20273C7400C180B4 /* main.cpp */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXCopyFilesBuildPhase section */ 21 | B66DFE0920273C7400C180B4 /* CopyFiles */ = { 22 | isa = PBXCopyFilesBuildPhase; 23 | buildActionMask = 2147483647; 24 | dstPath = /usr/share/man/man1/; 25 | dstSubfolderSpec = 0; 26 | files = ( 27 | ); 28 | runOnlyForDeploymentPostprocessing = 1; 29 | }; 30 | /* End PBXCopyFilesBuildPhase section */ 31 | 32 | /* Begin PBXFileReference section */ 33 | 3D733BEC20336ACF006C8694 /* HouseBlend.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = HouseBlend.cpp; sourceTree = ""; }; 34 | 3D733BED20336ACF006C8694 /* HouseBlend.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = HouseBlend.hpp; sourceTree = ""; }; 35 | 3D733BEF20336AE2006C8694 /* DarkRoast.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = DarkRoast.cpp; sourceTree = ""; }; 36 | 3D733BF020336AE2006C8694 /* DarkRoast.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = DarkRoast.hpp; sourceTree = ""; }; 37 | 3D733BF220336AED006C8694 /* Decaf.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Decaf.cpp; sourceTree = ""; }; 38 | 3D733BF320336AED006C8694 /* Decaf.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Decaf.hpp; sourceTree = ""; }; 39 | 3D733BFB20336C49006C8694 /* Milk.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Milk.hpp; sourceTree = ""; }; 40 | 3D733BFC20336C49006C8694 /* CondimentDecorator.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = CondimentDecorator.hpp; sourceTree = ""; }; 41 | 3D733BFE20336C49006C8694 /* Milk.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Milk.cpp; sourceTree = ""; }; 42 | 3D733C0120336CEB006C8694 /* Mocha.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Mocha.cpp; sourceTree = ""; }; 43 | 3D733C0220336CEB006C8694 /* Mocha.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Mocha.hpp; sourceTree = ""; }; 44 | 3D733C0420336CFB006C8694 /* Soy.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Soy.cpp; sourceTree = ""; }; 45 | 3D733C0520336CFB006C8694 /* Soy.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Soy.hpp; sourceTree = ""; }; 46 | 3D733C0720336D05006C8694 /* Whip.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Whip.cpp; sourceTree = ""; }; 47 | 3D733C0820336D05006C8694 /* Whip.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Whip.hpp; sourceTree = ""; }; 48 | 3DE5F6F6203248FE0033BA18 /* Beverage.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Beverage.hpp; sourceTree = ""; }; 49 | 3DE5F6FB20324CC50033BA18 /* Espresso.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Espresso.cpp; sourceTree = ""; }; 50 | 3DE5F6FC20324CC50033BA18 /* Espresso.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Espresso.hpp; sourceTree = ""; }; 51 | B66DFE0B20273C7400C180B4 /* Decorator Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Decorator Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 52 | B66DFE0E20273C7400C180B4 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; 53 | /* End PBXFileReference section */ 54 | 55 | /* Begin PBXFrameworksBuildPhase section */ 56 | B66DFE0820273C7400C180B4 /* Frameworks */ = { 57 | isa = PBXFrameworksBuildPhase; 58 | buildActionMask = 2147483647; 59 | files = ( 60 | ); 61 | runOnlyForDeploymentPostprocessing = 0; 62 | }; 63 | /* End PBXFrameworksBuildPhase section */ 64 | 65 | /* Begin PBXGroup section */ 66 | 3D733BF520336BAF006C8694 /* Beverages */ = { 67 | isa = PBXGroup; 68 | children = ( 69 | 3DE5F6F6203248FE0033BA18 /* Beverage.hpp */, 70 | 3D733BEF20336AE2006C8694 /* DarkRoast.cpp */, 71 | 3D733BF020336AE2006C8694 /* DarkRoast.hpp */, 72 | 3D733BF220336AED006C8694 /* Decaf.cpp */, 73 | 3D733BF320336AED006C8694 /* Decaf.hpp */, 74 | 3DE5F6FB20324CC50033BA18 /* Espresso.cpp */, 75 | 3DE5F6FC20324CC50033BA18 /* Espresso.hpp */, 76 | 3D733BEC20336ACF006C8694 /* HouseBlend.cpp */, 77 | 3D733BED20336ACF006C8694 /* HouseBlend.hpp */, 78 | ); 79 | name = Beverages; 80 | sourceTree = ""; 81 | }; 82 | 3D733BFA20336C23006C8694 /* Condiments */ = { 83 | isa = PBXGroup; 84 | children = ( 85 | 3D733BFC20336C49006C8694 /* CondimentDecorator.hpp */, 86 | 3D733BFE20336C49006C8694 /* Milk.cpp */, 87 | 3D733BFB20336C49006C8694 /* Milk.hpp */, 88 | 3D733C0120336CEB006C8694 /* Mocha.cpp */, 89 | 3D733C0220336CEB006C8694 /* Mocha.hpp */, 90 | 3D733C0420336CFB006C8694 /* Soy.cpp */, 91 | 3D733C0520336CFB006C8694 /* Soy.hpp */, 92 | 3D733C0720336D05006C8694 /* Whip.cpp */, 93 | 3D733C0820336D05006C8694 /* Whip.hpp */, 94 | ); 95 | name = Condiments; 96 | sourceTree = ""; 97 | }; 98 | B66DFE0220273C7400C180B4 = { 99 | isa = PBXGroup; 100 | children = ( 101 | B66DFE0D20273C7400C180B4 /* Decorator Pattern */, 102 | B66DFE0C20273C7400C180B4 /* Products */, 103 | ); 104 | sourceTree = ""; 105 | }; 106 | B66DFE0C20273C7400C180B4 /* Products */ = { 107 | isa = PBXGroup; 108 | children = ( 109 | B66DFE0B20273C7400C180B4 /* Decorator Pattern */, 110 | ); 111 | name = Products; 112 | sourceTree = ""; 113 | }; 114 | B66DFE0D20273C7400C180B4 /* Decorator Pattern */ = { 115 | isa = PBXGroup; 116 | children = ( 117 | 3D733BF520336BAF006C8694 /* Beverages */, 118 | 3D733BFA20336C23006C8694 /* Condiments */, 119 | B66DFE0E20273C7400C180B4 /* main.cpp */, 120 | ); 121 | path = "Decorator Pattern"; 122 | sourceTree = ""; 123 | }; 124 | /* End PBXGroup section */ 125 | 126 | /* Begin PBXNativeTarget section */ 127 | B66DFE0A20273C7400C180B4 /* Decorator Pattern */ = { 128 | isa = PBXNativeTarget; 129 | buildConfigurationList = B66DFE1220273C7400C180B4 /* Build configuration list for PBXNativeTarget "Decorator Pattern" */; 130 | buildPhases = ( 131 | B66DFE0720273C7400C180B4 /* Sources */, 132 | B66DFE0820273C7400C180B4 /* Frameworks */, 133 | B66DFE0920273C7400C180B4 /* CopyFiles */, 134 | ); 135 | buildRules = ( 136 | ); 137 | dependencies = ( 138 | ); 139 | name = "Decorator Pattern"; 140 | productName = "Decorator Pattern"; 141 | productReference = B66DFE0B20273C7400C180B4 /* Decorator Pattern */; 142 | productType = "com.apple.product-type.tool"; 143 | }; 144 | /* End PBXNativeTarget section */ 145 | 146 | /* Begin PBXProject section */ 147 | B66DFE0320273C7400C180B4 /* Project object */ = { 148 | isa = PBXProject; 149 | attributes = { 150 | LastUpgradeCheck = 0930; 151 | ORGANIZATIONNAME = "Brian Arnold"; 152 | TargetAttributes = { 153 | B66DFE0A20273C7400C180B4 = { 154 | CreatedOnToolsVersion = 9.3; 155 | ProvisioningStyle = Automatic; 156 | }; 157 | }; 158 | }; 159 | buildConfigurationList = B66DFE0620273C7400C180B4 /* Build configuration list for PBXProject "Decorator Pattern" */; 160 | compatibilityVersion = "Xcode 8.0"; 161 | developmentRegion = en; 162 | hasScannedForEncodings = 0; 163 | knownRegions = ( 164 | en, 165 | ); 166 | mainGroup = B66DFE0220273C7400C180B4; 167 | productRefGroup = B66DFE0C20273C7400C180B4 /* Products */; 168 | projectDirPath = ""; 169 | projectRoot = ""; 170 | targets = ( 171 | B66DFE0A20273C7400C180B4 /* Decorator Pattern */, 172 | ); 173 | }; 174 | /* End PBXProject section */ 175 | 176 | /* Begin PBXSourcesBuildPhase section */ 177 | B66DFE0720273C7400C180B4 /* Sources */ = { 178 | isa = PBXSourcesBuildPhase; 179 | buildActionMask = 2147483647; 180 | files = ( 181 | 3D733BF120336AE2006C8694 /* DarkRoast.cpp in Sources */, 182 | 3D733BEE20336ACF006C8694 /* HouseBlend.cpp in Sources */, 183 | 3D2DFFAD203DD2C40050E7C3 /* Mocha.cpp in Sources */, 184 | 3D8F2FFD203CE78800CEF165 /* Soy.cpp in Sources */, 185 | 3DE5F6FD20324CC50033BA18 /* Espresso.cpp in Sources */, 186 | 3D8F2FFC203CE6A300CEF165 /* Whip.cpp in Sources */, 187 | B66DFE0F20273C7400C180B4 /* main.cpp in Sources */, 188 | 3D733BF420336AED006C8694 /* Decaf.cpp in Sources */, 189 | ); 190 | runOnlyForDeploymentPostprocessing = 0; 191 | }; 192 | /* End PBXSourcesBuildPhase section */ 193 | 194 | /* Begin XCBuildConfiguration section */ 195 | B66DFE1020273C7400C180B4 /* Debug */ = { 196 | isa = XCBuildConfiguration; 197 | buildSettings = { 198 | ALWAYS_SEARCH_USER_PATHS = NO; 199 | CLANG_ANALYZER_NONNULL = YES; 200 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 201 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 202 | CLANG_CXX_LIBRARY = "libc++"; 203 | CLANG_ENABLE_MODULES = YES; 204 | CLANG_ENABLE_OBJC_ARC = YES; 205 | CLANG_ENABLE_OBJC_WEAK = YES; 206 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 207 | CLANG_WARN_BOOL_CONVERSION = YES; 208 | CLANG_WARN_COMMA = YES; 209 | CLANG_WARN_CONSTANT_CONVERSION = YES; 210 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 211 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 212 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 213 | CLANG_WARN_EMPTY_BODY = YES; 214 | CLANG_WARN_ENUM_CONVERSION = YES; 215 | CLANG_WARN_INFINITE_RECURSION = YES; 216 | CLANG_WARN_INT_CONVERSION = YES; 217 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 218 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 219 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 220 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 221 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 222 | CLANG_WARN_STRICT_PROTOTYPES = YES; 223 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 224 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 225 | CLANG_WARN_UNREACHABLE_CODE = YES; 226 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 227 | CODE_SIGN_IDENTITY = "-"; 228 | COPY_PHASE_STRIP = NO; 229 | DEBUG_INFORMATION_FORMAT = dwarf; 230 | ENABLE_STRICT_OBJC_MSGSEND = YES; 231 | ENABLE_TESTABILITY = YES; 232 | GCC_C_LANGUAGE_STANDARD = gnu11; 233 | GCC_DYNAMIC_NO_PIC = NO; 234 | GCC_NO_COMMON_BLOCKS = YES; 235 | GCC_OPTIMIZATION_LEVEL = 0; 236 | GCC_PREPROCESSOR_DEFINITIONS = ( 237 | "DEBUG=1", 238 | "$(inherited)", 239 | ); 240 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 241 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 242 | GCC_WARN_UNDECLARED_SELECTOR = YES; 243 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 244 | GCC_WARN_UNUSED_FUNCTION = YES; 245 | GCC_WARN_UNUSED_VARIABLE = YES; 246 | MACOSX_DEPLOYMENT_TARGET = 10.13; 247 | MTL_ENABLE_DEBUG_INFO = YES; 248 | ONLY_ACTIVE_ARCH = YES; 249 | SDKROOT = macosx; 250 | }; 251 | name = Debug; 252 | }; 253 | B66DFE1120273C7400C180B4 /* Release */ = { 254 | isa = XCBuildConfiguration; 255 | buildSettings = { 256 | ALWAYS_SEARCH_USER_PATHS = NO; 257 | CLANG_ANALYZER_NONNULL = YES; 258 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 259 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 260 | CLANG_CXX_LIBRARY = "libc++"; 261 | CLANG_ENABLE_MODULES = YES; 262 | CLANG_ENABLE_OBJC_ARC = YES; 263 | CLANG_ENABLE_OBJC_WEAK = YES; 264 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 265 | CLANG_WARN_BOOL_CONVERSION = YES; 266 | CLANG_WARN_COMMA = YES; 267 | CLANG_WARN_CONSTANT_CONVERSION = YES; 268 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 269 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 270 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 271 | CLANG_WARN_EMPTY_BODY = YES; 272 | CLANG_WARN_ENUM_CONVERSION = YES; 273 | CLANG_WARN_INFINITE_RECURSION = YES; 274 | CLANG_WARN_INT_CONVERSION = YES; 275 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 276 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 277 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 278 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 279 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 280 | CLANG_WARN_STRICT_PROTOTYPES = YES; 281 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 282 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 283 | CLANG_WARN_UNREACHABLE_CODE = YES; 284 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 285 | CODE_SIGN_IDENTITY = "-"; 286 | COPY_PHASE_STRIP = NO; 287 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 288 | ENABLE_NS_ASSERTIONS = NO; 289 | ENABLE_STRICT_OBJC_MSGSEND = YES; 290 | GCC_C_LANGUAGE_STANDARD = gnu11; 291 | GCC_NO_COMMON_BLOCKS = YES; 292 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 293 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 294 | GCC_WARN_UNDECLARED_SELECTOR = YES; 295 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 296 | GCC_WARN_UNUSED_FUNCTION = YES; 297 | GCC_WARN_UNUSED_VARIABLE = YES; 298 | MACOSX_DEPLOYMENT_TARGET = 10.13; 299 | MTL_ENABLE_DEBUG_INFO = NO; 300 | SDKROOT = macosx; 301 | }; 302 | name = Release; 303 | }; 304 | B66DFE1320273C7400C180B4 /* Debug */ = { 305 | isa = XCBuildConfiguration; 306 | buildSettings = { 307 | CODE_SIGN_STYLE = Automatic; 308 | MACOSX_DEPLOYMENT_TARGET = 10.12; 309 | PRODUCT_NAME = "$(TARGET_NAME)"; 310 | }; 311 | name = Debug; 312 | }; 313 | B66DFE1420273C7400C180B4 /* Release */ = { 314 | isa = XCBuildConfiguration; 315 | buildSettings = { 316 | CODE_SIGN_STYLE = Automatic; 317 | MACOSX_DEPLOYMENT_TARGET = 10.12; 318 | PRODUCT_NAME = "$(TARGET_NAME)"; 319 | }; 320 | name = Release; 321 | }; 322 | /* End XCBuildConfiguration section */ 323 | 324 | /* Begin XCConfigurationList section */ 325 | B66DFE0620273C7400C180B4 /* Build configuration list for PBXProject "Decorator Pattern" */ = { 326 | isa = XCConfigurationList; 327 | buildConfigurations = ( 328 | B66DFE1020273C7400C180B4 /* Debug */, 329 | B66DFE1120273C7400C180B4 /* Release */, 330 | ); 331 | defaultConfigurationIsVisible = 0; 332 | defaultConfigurationName = Release; 333 | }; 334 | B66DFE1220273C7400C180B4 /* Build configuration list for PBXNativeTarget "Decorator Pattern" */ = { 335 | isa = XCConfigurationList; 336 | buildConfigurations = ( 337 | B66DFE1320273C7400C180B4 /* Debug */, 338 | B66DFE1420273C7400C180B4 /* Release */, 339 | ); 340 | defaultConfigurationIsVisible = 0; 341 | defaultConfigurationName = Release; 342 | }; 343 | /* End XCConfigurationList section */ 344 | }; 345 | rootObject = B66DFE0320273C7400C180B4 /* Project object */; 346 | } 347 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Beverage.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Beverage.hpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/12/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef Beverage_hpp 10 | #define Beverage_hpp 11 | 12 | #include 13 | 14 | class Beverage { 15 | // pure abstract class! 16 | public: 17 | virtual ~Beverage() = default; 18 | virtual std::string getDescription() const = 0; 19 | virtual double cost() const = 0; 20 | }; 21 | 22 | #endif /* Beverage_hpp */ 23 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/CondimentDecorator.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // CondimentDecorator.hpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/12/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef CondimentDecorator_hpp 10 | #define CondimentDecorator_hpp 11 | 12 | #include "Beverage.hpp" 13 | 14 | class CondimentDecorator: public Beverage { 15 | protected: 16 | CondimentDecorator(std::unique_ptr beverage) : beverage(std::move(beverage)) {}; 17 | std::unique_ptr beverage; 18 | }; 19 | 20 | #endif /* CondimentDecorator_hpp */ 21 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/DarkRoast.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // DarkRoast.cpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "DarkRoast.hpp" 10 | 11 | std::string DarkRoast::getDescription() const { 12 | return "Dark Roast Coffee"; 13 | } 14 | 15 | double DarkRoast::cost() const { 16 | return 0.99; 17 | } 18 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/DarkRoast.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // DarkRoast.hpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef DarkRoast_hpp 10 | #define DarkRoast_hpp 11 | 12 | #include "Beverage.hpp" 13 | 14 | class DarkRoast: public Beverage { 15 | public: 16 | std::string getDescription() const override; 17 | double cost() const override; 18 | }; 19 | #endif /* DarkRoast_hpp */ 20 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Decaf.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Decaf.cpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "Decaf.hpp" 10 | 11 | std::string Decaf::getDescription() const { 12 | return "Decaf"; 13 | } 14 | 15 | double Decaf::cost() const { 16 | return 1.05; 17 | } 18 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Decaf.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Decaf.hpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef Decaf_hpp 10 | #define Decaf_hpp 11 | 12 | #include "Beverage.hpp" 13 | 14 | class Decaf: public Beverage { 15 | public: 16 | std::string getDescription() const override; 17 | double cost() const override; 18 | }; 19 | 20 | #endif /* Decaf_hpp */ 21 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Espresso.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Espresso.cpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/12/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "Espresso.hpp" 10 | 11 | std::string Espresso::getDescription() const { 12 | return "Espresso"; 13 | } 14 | 15 | double Espresso::cost() const { 16 | return 1.99; 17 | } 18 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Espresso.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Espresso.hpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/12/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef Espresso_hpp 10 | #define Espresso_hpp 11 | 12 | #include "Beverage.hpp" 13 | 14 | class Espresso: public Beverage { 15 | public: 16 | std::string getDescription() const override; 17 | double cost() const override; 18 | }; 19 | 20 | #endif /* Espresso_hpp */ 21 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/HouseBlend.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // HouseBlend.cpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "HouseBlend.hpp" 10 | 11 | std::string HouseBlend::getDescription() const { 12 | return "House Blend Coffee"; 13 | } 14 | 15 | double HouseBlend::cost() const { 16 | return 0.89; 17 | } 18 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/HouseBlend.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // HouseBlend.hpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef HouseBlend_hpp 10 | #define HouseBlend_hpp 11 | 12 | #include "Beverage.hpp" 13 | 14 | class HouseBlend: public Beverage { 15 | public: 16 | std::string getDescription() const override; 17 | double cost() const override; 18 | }; 19 | 20 | #endif /* HouseBlend_hpp */ 21 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Milk.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Milk.cpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "Milk.hpp" 10 | 11 | Milk::Milk(std::unique_ptr beverage) : CondimentDecorator(std::move(beverage)) { 12 | } 13 | 14 | std::string Milk::getDescription() const { 15 | return beverage->getDescription() + ", Steamed Milk"; 16 | } 17 | 18 | double Milk::cost() const { 19 | return beverage->cost() + 0.10; 20 | } 21 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Milk.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Milk.hpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef Milk_hpp 10 | #define Milk_hpp 11 | 12 | #include "CondimentDecorator.hpp" 13 | 14 | class Milk: public CondimentDecorator { 15 | public: 16 | Milk(std::unique_ptr beverage); 17 | std::string getDescription() const override; 18 | double cost() const override; 19 | }; 20 | #endif /* Milk_hpp */ 21 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Mocha.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Mocha.cpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "Mocha.hpp" 10 | 11 | Mocha::Mocha(std::unique_ptr beverage) : CondimentDecorator(std::move(beverage)) { 12 | } 13 | 14 | std::string Mocha::getDescription() const { 15 | return beverage->getDescription() + ", Mocha"; 16 | } 17 | 18 | double Mocha::cost() const { 19 | return beverage->cost() + 0.20; 20 | } 21 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Mocha.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Mocha.hpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef Mocha_hpp 10 | #define Mocha_hpp 11 | 12 | #include "CondimentDecorator.hpp" 13 | 14 | class Mocha: public CondimentDecorator { 15 | public: 16 | Mocha(std::unique_ptr beverage); 17 | std::string getDescription() const override; 18 | double cost() const override; 19 | }; 20 | 21 | #endif /* Mocha_hpp */ 22 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Soy.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Soy.cpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "Soy.hpp" 10 | 11 | Soy::Soy(std::unique_ptr beverage) : CondimentDecorator(std::move(beverage)) { 12 | } 13 | 14 | std::string Soy::getDescription() const { 15 | return beverage->getDescription() + ", Soy"; 16 | } 17 | 18 | double Soy::cost() const { 19 | return beverage->cost() + 0.15; 20 | } 21 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Soy.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Soy.hpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef Soy_hpp 10 | #define Soy_hpp 11 | 12 | #include "CondimentDecorator.hpp" 13 | 14 | class Soy: public CondimentDecorator { 15 | public: 16 | Soy(std::unique_ptr beverage); 17 | std::string getDescription() const override; 18 | double cost() const override; 19 | }; 20 | #endif /* Soy_hpp */ 21 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Whip.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Whip.cpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "Whip.hpp" 10 | 11 | Whip::Whip(std::unique_ptr beverage) : CondimentDecorator(std::move(beverage)){ 12 | } 13 | 14 | std::string Whip::getDescription() const { 15 | return beverage->getDescription() + ", Whip"; 16 | } 17 | 18 | double Whip::cost() const { 19 | return beverage->cost() + 0.10; 20 | } 21 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Whip.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Whip.hpp 3 | // Decorator Pattern 4 | // 5 | // Created by Kevin Lee on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef Whip_hpp 10 | #define Whip_hpp 11 | 12 | #include "CondimentDecorator.hpp" 13 | 14 | class Whip: public CondimentDecorator { 15 | public: 16 | Whip(std::unique_ptr beverage); 17 | std::string getDescription() const override; 18 | double cost() const override; 19 | }; 20 | #endif /* Whip_hpp */ 21 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Decorator Pattern 4 | // 5 | // Created by Brian Arnold on 2/4/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "DarkRoast.hpp" 10 | #include "Decaf.hpp" 11 | #include "Espresso.hpp" 12 | #include "HouseBlend.hpp" 13 | 14 | #include "Milk.hpp" 15 | #include "Mocha.hpp" 16 | #include "Soy.hpp" 17 | #include "Whip.hpp" 18 | 19 | #include 20 | 21 | int main(int argc, const char * argv[]) { 22 | 23 | Espresso beverage; 24 | std::cout << beverage.getDescription() << " $" << beverage.cost() << std::endl; 25 | 26 | std::unique_ptr beverage2 = std::make_unique(); 27 | beverage2 = std::make_unique(std::move(beverage2)); 28 | beverage2 = std::make_unique(std::move(beverage2)); 29 | beverage2 = std::make_unique(std::move(beverage2)); 30 | std::cout << beverage2->getDescription() << " $" << beverage2->cost() << std::endl; 31 | 32 | std::unique_ptr beverage3 = std::make_unique(); 33 | beverage3 = std::make_unique(std::move(beverage3)); 34 | beverage3 = std::make_unique(std::move(beverage3)); 35 | beverage3 = std::make_unique(std::move(beverage3)); 36 | std::cout << beverage3->getDescription() << " $" << beverage3->cost() << std::endl; 37 | } 38 | -------------------------------------------------------------------------------- /Factory Pattern/AbstractFactoryShenanigans/Pizza.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Pizza.cpp 3 | // AbstractFactoryShenanigans 4 | // 5 | // Created by Cindy Solomon on 2/21/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "Pizza.hpp" 10 | 11 | #include 12 | #include 13 | 14 | const std::string& Pizza::getName() const 15 | { 16 | return this->name; 17 | } 18 | 19 | void Pizza::bake() const 20 | { 21 | std::cout << "Baking for 25 min at 350 degrees "<< std::endl; 22 | } 23 | 24 | void Pizza::cut() const 25 | { 26 | std::cout << "Cut the pizza into diagonal slices "<< std::endl; 27 | } 28 | void Pizza::box() const 29 | { 30 | std::cout << "Boxing in official PizzaStore boxes" << std::endl; 31 | } 32 | 33 | std::ostream& operator<<(std::ostream& os, const Pizza& pizza) 34 | { 35 | std::string str; 36 | 37 | str = "\n---- " + pizza.getName() + "----\n"; 38 | 39 | if (pizza.dough != null) 40 | { 41 | str.append(pizza.dough + "\n"); 42 | } 43 | 44 | if (pizza.sauce != null) 45 | { 46 | str.append(pizza.sauce + "\n"); 47 | } 48 | 49 | for (auto& veggie : pizza.veggies) 50 | { 51 | str.append(" " + veggie + "\n"); 52 | } 53 | 54 | if (pizza.pepperoni != null) 55 | { 56 | str.append(pizza.pepperoni + "\n"); 57 | } 58 | 59 | 60 | str.append("\n"); 61 | 62 | return os << str; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /Factory Pattern/AbstractFactoryShenanigans/Pizza.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Pizza.hpp 3 | // AbstractFactoryShenanigans 4 | // 5 | // Created by Cindy Solomon on 2/21/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef Pizza_hpp 10 | #define Pizza_hpp 11 | 12 | #include 13 | 14 | class Pizza 15 | { 16 | public: 17 | std::string name; 18 | // Dough dough; 19 | // Sauce sauce; 20 | // Veggies std::vector toppings; 21 | //Cheese cheese; 22 | //Pepperoni pepperoni; 23 | 24 | 25 | const std::string& getName() const; 26 | 27 | virtual void prepare() const; 28 | virtual void bake() const; 29 | virtual void cut() const; 30 | virtual void box() const; 31 | virtual ~Pizza() = default; 32 | 33 | }; 34 | 35 | std::ostream& operator<<(std::ostream& os, const Pizza& pizza); 36 | 37 | 38 | 39 | #endif /* Pizza_hpp */ 40 | -------------------------------------------------------------------------------- /Factory Pattern/AbstractFactoryShenanigans/PizzaStore.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // PizzaStore.cpp 3 | // AbstractFactoryShenanigans 4 | // 5 | // Created by Cindy Solomon on 2/21/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "Pizza.hpp" 10 | #include "PizzaStore.hpp" 11 | 12 | std::unique_ptr PizzaStore::orderPizza(std::string type) 13 | { 14 | auto pizza = makePizza(type); 15 | 16 | if (pizza != nullptr) 17 | { 18 | std::cout << "\n--- Making a " + pizza->getName() + " ---\n \n"; 19 | pizza->prepare(); 20 | pizza->bake(); 21 | pizza->cut(); 22 | pizza->box(); 23 | } 24 | else 25 | { 26 | std::cout << "Not a valid pizza type. I don't know how to make this pizza!!" << std::endl; 27 | } 28 | return pizza; 29 | }; 30 | -------------------------------------------------------------------------------- /Factory Pattern/AbstractFactoryShenanigans/PizzaStore.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // PizzaStore.hpp 3 | // AbstractFactoryShenanigans 4 | // 5 | // Created by Cindy Solomon on 2/21/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef PizzaStore_hpp 10 | #define PizzaStore_hpp 11 | 12 | #include "Pizza.hpp" 13 | #include 14 | 15 | class PizzaStore 16 | { 17 | 18 | public: 19 | 20 | std::unique_ptr orderPizza(std::string type); 21 | virtual std::unique_ptr makePizza(std::string type) = 0; 22 | 23 | }; 24 | 25 | #endif /* PizzaStore_hpp */ 26 | -------------------------------------------------------------------------------- /Factory Pattern/MultiplePizzaTypes/MultiplePizzaTypes.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // MultiplePizzaTypes.hpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | 7 | #include 8 | #include 9 | 10 | class Pizza { 11 | 12 | public: 13 | 14 | virtual void prepare() = 0; 15 | virtual void bake() = 0; 16 | virtual void cut() = 0; 17 | virtual void box() = 0; 18 | }; 19 | 20 | class CheesePizza: public Pizza { 21 | void prepare() 22 | { 23 | std::cout << "Preparing cheese" << std::endl; 24 | }; 25 | 26 | void bake() 27 | { 28 | std::cout << "Baking cheese" << std::endl; 29 | }; 30 | 31 | void cut() 32 | { 33 | std::cout << "Cutting cheese" << std::endl; 34 | }; 35 | 36 | void box() 37 | { 38 | std::cout << "Boxing cheese" << std::endl; 39 | }; 40 | }; 41 | 42 | class GreekPizza: public Pizza { 43 | void prepare() 44 | { 45 | std::cout << "Preparing Greek" << std::endl; 46 | }; 47 | 48 | void bake() 49 | { 50 | std::cout << "Baking Greek" << std::endl; 51 | }; 52 | 53 | void cut() 54 | { 55 | std::cout << "Cutting Greek" << std::endl; 56 | }; 57 | 58 | void box() 59 | { 60 | std::cout << "Boxing Greek" << std::endl; 61 | }; 62 | }; 63 | 64 | class PepperoniPizza: public Pizza { 65 | void prepare() 66 | { 67 | std::cout << "Preparing pepperoni" << std::endl; 68 | }; 69 | 70 | void bake() 71 | { 72 | std::cout << "Baking pepperoni" << std::endl; 73 | }; 74 | 75 | void cut() 76 | { 77 | std::cout << "Cutting pepperoni" << std::endl; 78 | }; 79 | 80 | void box() 81 | { 82 | std::cout << "Boxing pepperoni" << std::endl; 83 | }; 84 | }; 85 | 86 | std::unique_ptr orderPizza(std::string type) 87 | { 88 | std::unique_ptr pizza = nullptr; 89 | 90 | if (type == "cheese") 91 | { 92 | pizza = std::make_unique(); 93 | } 94 | else if (type == "greek") 95 | { 96 | pizza = std::make_unique(); 97 | } 98 | else if (type == "pepperoni") 99 | { 100 | pizza = std::make_unique(); 101 | } 102 | 103 | // Each Pizza subtype (CheesePizza, VeggiePizza, etc.) knows how to prepare itself. 104 | pizza->prepare(); 105 | pizza->bake(); 106 | pizza->cut(); 107 | pizza->box(); 108 | 109 | return pizza; 110 | }; 111 | 112 | //const Pizza& orderPizza(std::string type) 113 | //{ 114 | // CheesePizza p; 115 | // Pizza& pizza = p; 116 | // 117 | // if (type == "cheese") 118 | // { 119 | // //CheesePizza p = CheesePizza(); 120 | // pizza = p; 121 | // } 122 | // else if (type == "greek") 123 | // { 124 | // pizza = GreekPizza(); 125 | // } 126 | // else if (type == "pepperoni") 127 | // { 128 | // pizza = PepperoniPizza(); 129 | // } 130 | // 131 | // // Each Pizza subtype (CheesePizza, VeggiePizza, etc.) knows how to prepare itself. 132 | // pizza.prepare(); 133 | // pizza.bake(); 134 | // pizza.cut(); 135 | // pizza.box(); 136 | // 137 | // return pizza; 138 | //}; 139 | 140 | -------------------------------------------------------------------------------- /Factory Pattern/MultiplePizzaTypes/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // MultiplePizzaType 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "MultiplePizzaTypes.hpp" 10 | 11 | int main(int argc, const char * argv[]) { 12 | // insert code here... 13 | 14 | orderPizza("cheese"); 15 | 16 | orderPizza("pepperoni"); 17 | 18 | return 0; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Factory Pattern/OnePizzaType/OnePizzaType.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // OnePizzaType.hpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | class Pizza { 12 | 13 | public: 14 | 15 | void prepare() 16 | { 17 | std::cout << "Preparing " << std::endl; 18 | }; 19 | 20 | void bake() 21 | { 22 | std::cout << "Baking " << std::endl; 23 | }; 24 | 25 | void cut() 26 | { 27 | std::cout << "Cutting " << std::endl; 28 | }; 29 | 30 | void box() 31 | { 32 | std::cout << "Boxing " << std::endl; 33 | }; 34 | }; 35 | 36 | Pizza orderPizza() 37 | { 38 | // For flexibility, we really want this to be an abstract class or interface, but we can't directly instantiate either of those. 39 | Pizza pizza = Pizza(); 40 | 41 | pizza.prepare(); 42 | pizza.bake(); 43 | pizza.cut(); 44 | pizza.box(); 45 | 46 | return pizza; 47 | }; 48 | -------------------------------------------------------------------------------- /Factory Pattern/OnePizzaType/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // OnePizzaType 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "OnePizzaType.hpp" 11 | 12 | int main(int argc, const char * argv[]) { 13 | // insert code here... 14 | 15 | Pizza basicPizza; 16 | 17 | basicPizza = orderPizza(); 18 | 19 | return 0; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/ChicagoPizzaStore.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ChicagoPizzaStore.cpp 3 | // PizzaStoreFranchise 4 | // 5 | // Created by Cindy Solomon on 2/20/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "ChicagoPizzaStore.hpp" 10 | #include "ChicagoStyleCheesePizza.hpp" 11 | #include "ChicagoStylePepperoniPizza.hpp" 12 | #include "ChicagoStyleVeggiePizza.hpp" 13 | 14 | std::unique_ptr ChicagoPizzaStore::makePizza(std::string type) 15 | { 16 | std::unique_ptr pizza = nullptr; 17 | 18 | if (type == "cheese") 19 | { 20 | pizza = std::make_unique(); 21 | } 22 | else if (type == "pepperoni") 23 | { 24 | pizza = std::make_unique(); 25 | } 26 | else if (type == "veggie") 27 | { 28 | pizza = std::make_unique(); 29 | } 30 | return pizza; 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/ChicagoPizzaStore.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ChicagoPizzaStore.hpp 3 | // PizzaStoreFranchise 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef ChicagoPizzaStore_hpp 10 | #define ChicagoPizzaStore_hpp 11 | 12 | #include "Pizza.hpp" 13 | #include "PizzaStore.hpp" 14 | 15 | 16 | class ChicagoPizzaStore: public PizzaStore 17 | { 18 | public: 19 | 20 | std::unique_ptr makePizza(std::string type); 21 | }; 22 | 23 | 24 | #endif /* ChicagoPizzaStore_hpp */ 25 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/ChicagoStyleCheesePizza.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ChicagoStyleCheesePizza.cpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "ChicagoStyleCheesePizza.hpp" 10 | 11 | ChicagoStyleCheesePizza::ChicagoStyleCheesePizza() 12 | { 13 | name = "Chicago Style Deep Dish Pizza"; 14 | dough = "Extra thick crust dough"; 15 | sauce = "Plum tomato sauce"; 16 | toppings.push_back("Shredded mozzarella"); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/ChicagoStyleCheesePizza.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ChicagoStyleCheesePizza.hpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef ChicagoStyleCheesePizza_hpp 10 | #define ChicagoStyleCheesePizza_hpp 11 | 12 | #include "Pizza.hpp" 13 | 14 | class ChicagoStyleCheesePizza: public Pizza 15 | { 16 | public: 17 | 18 | ChicagoStyleCheesePizza(); 19 | 20 | }; 21 | 22 | #endif /* ChicagoStyleCheesePizza_hpp */ 23 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/ChicagoStylePepperoniPizza.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Factory Pattern 3 | // 4 | // Created by Cindy Solomon on 2/13/18. 5 | // Copyright © 2018 Brian Arnold. All rights reserved. 6 | // 7 | 8 | #include "ChicagoStylePepperoniPizza.hpp" 9 | #include 10 | 11 | ChicagoStylePepperoniPizza::ChicagoStylePepperoniPizza() 12 | { 13 | name = "Chicago Style Pepperoni Pizza"; 14 | dough = "Extra thick crust dough"; 15 | sauce = "Plum tomato sauce"; 16 | toppings.push_back("Shredded mozzarella"); 17 | toppings.push_back("Sliced pepperoni"); 18 | toppings.push_back("No olives or eggplant because that should not go on a pizza"); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/ChicagoStylePepperoniPizza.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ChicagoStylePepperoniPizza.hpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef ChicagoStylePepperoniPizza_hpp 10 | #define ChicagoStylePepperoniPizza_hpp 11 | 12 | #include "Pizza.hpp" 13 | 14 | class ChicagoStylePepperoniPizza: public Pizza 15 | { 16 | public: 17 | 18 | ChicagoStylePepperoniPizza(); 19 | 20 | }; 21 | 22 | #endif /* ChicagoStylePepperoniPizza_hpp */ 23 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/ChicagoStyleVeggiePizza.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ChicagoStyleVeggiePizza.cpp 3 | // PizzaStoreFranchise 4 | // 5 | // Created by Cindy Solomon on 2/20/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "ChicagoStyleVeggiePizza.hpp" 10 | 11 | 12 | ChicagoStyleVeggiePizza::ChicagoStyleVeggiePizza() 13 | { 14 | name = "Chicago Style Veggie Pizza"; 15 | dough = "Extra thick crust dough"; 16 | sauce = "Plum tomato sauce"; 17 | toppings.push_back("Shredded mozzarella"); 18 | toppings.push_back("Spinach"); 19 | toppings.push_back("Mushrooms"); 20 | toppings.push_back("No olives or eggplant because that should not go on a pizza"); 21 | } 22 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/ChicagoStyleVeggiePizza.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ChicagoStyleVeggiePizza.hpp 3 | // PizzaStoreFranchise 4 | // 5 | // Created by Cindy Solomon on 2/20/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef ChicagoStyleVeggiePizza_hpp 10 | #define ChicagoStyleVeggiePizza_hpp 11 | 12 | #include "Pizza.hpp" 13 | 14 | class ChicagoStyleVeggiePizza: public Pizza 15 | { 16 | public: 17 | 18 | ChicagoStyleVeggiePizza(); 19 | 20 | }; 21 | 22 | #endif /* ChicagoStyleVeggiePizza_hpp */ 23 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/NYPizzaStore.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // NYPizzaStore.cpp 3 | // PizzaStoreFranchise 4 | // 5 | // Created by Cindy Solomon on 2/20/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "NYPizzaStore.hpp" 10 | #include "NYStyleCheesePizza.hpp" 11 | #include "NYStylePepperoniPizza.hpp" 12 | 13 | 14 | std::unique_ptr NYPizzaStore::makePizza(std::string type) 15 | { 16 | std::unique_ptr pizza = nullptr; 17 | 18 | if (type == "cheese") 19 | { 20 | pizza = std::make_unique(); 21 | } 22 | else if (type == "pepperoni") 23 | { 24 | pizza = std::make_unique(); 25 | } 26 | return pizza; 27 | } 28 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/NYPizzaStore.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // NYPizzaStore.hpp 3 | // PizzaStoreFranchise 4 | // 5 | // Created by Cindy Solomon on 2/20/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef NYPizzaStore_hpp 10 | #define NYPizzaStore_hpp 11 | 12 | #include "Pizza.hpp" 13 | #include "PizzaStore.hpp" 14 | 15 | 16 | class NYPizzaStore: public PizzaStore 17 | { 18 | public: 19 | 20 | std::unique_ptr makePizza(std::string type); 21 | }; 22 | 23 | #endif /* NYPizzaStore_hpp */ 24 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/NYStyleCheesePizza.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // NYStyleCheesePizza.cpp 3 | // PizzaStoreFranchise 4 | // 5 | // Created by Cindy Solomon on 2/21/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "NYStyleCheesePizza.hpp" 10 | 11 | NYStyleCheesePizza::NYStyleCheesePizza() 12 | { 13 | name = "NY Style sauce and cheese pizza"; 14 | dough = "Thin crust dough"; 15 | sauce = "Marinara sauce"; 16 | toppings.push_back("Gratted reggiano cheese"); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/NYStyleCheesePizza.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // NYStyleCheesePizza.hpp 3 | // PizzaStoreFranchise 4 | // 5 | // Created by Cindy Solomon on 2/21/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef NYStyleCheesePizza_hpp 10 | #define NYStyleCheesePizza_hpp 11 | 12 | #include "Pizza.hpp" 13 | 14 | class NYStyleCheesePizza: public Pizza 15 | { 16 | public: 17 | 18 | NYStyleCheesePizza(); 19 | 20 | }; 21 | 22 | #endif /* NYStyleCheesePizza_hpp */ 23 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/NYStylePepperoniPizza.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // NYStylePepperoniPizza.cpp 3 | // PizzaStoreFranchise 4 | // 5 | // Created by Cindy Solomon on 2/21/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "NYStylePepperoniPizza.hpp" 10 | 11 | 12 | NYStylePepperoniPizza::NYStylePepperoniPizza() 13 | { 14 | name = "NY style pepperoni pizza"; 15 | dough = "Thin crust dough"; 16 | sauce = "Marinara sauce"; 17 | toppings.push_back("Grated reggiano cheese"); 18 | toppings.push_back("Sliced pepperoni"); 19 | toppings.push_back("Garlic"); 20 | toppings.push_back("Onion"); 21 | toppings.push_back("Mushrooms"); 22 | toppings.push_back("Red pepper"); 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/NYStylePepperoniPizza.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // NYStylePepperoniPizza.hpp 3 | // PizzaStoreFranchise 4 | // 5 | // Created by Cindy Solomon on 2/21/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef NYStylePepperoniPizza_hpp 10 | #define NYStylePepperoniPizza_hpp 11 | 12 | #include "Pizza.hpp" 13 | 14 | class NYStylePepperoniPizza: public Pizza 15 | { 16 | public: 17 | 18 | NYStylePepperoniPizza(); 19 | 20 | }; 21 | 22 | #endif /* NYStylePepperoniPizza_hpp */ 23 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/Pizza.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Pizza.cpp 3 | // SimpleFactoryPattern 4 | // 5 | // Created by Cindy Solomon on 2/20/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "Pizza.hpp" 10 | #include 11 | #include 12 | 13 | const std::string& Pizza::getName() const 14 | { 15 | return this->name; 16 | } 17 | 18 | void Pizza::prepare() const 19 | { 20 | std::cout << "Preparing " + getName() << std::endl; 21 | std::cout << "Tossing dough..." << std::endl; 22 | std::cout << "Adding sauce..." << std::endl; 23 | std::cout << "Adding toppings: " << std::endl; 24 | for (auto& topping : this->toppings) 25 | { 26 | std::cout << " " + topping + "\n"; 27 | } 28 | } 29 | void Pizza::bake() const 30 | { 31 | std::cout << "Baking for 25 min at 350 degrees "<< std::endl; 32 | } 33 | 34 | void Pizza::cut() const 35 | { 36 | std::cout << "Cut the pizza into diagonal slices "<< std::endl; 37 | } 38 | void Pizza::box() const 39 | { 40 | std::cout << "Boxing in official PizzaStore boxes" << std::endl; 41 | } 42 | 43 | std::ostream& operator<<(std::ostream& os, const Pizza& pizza) 44 | { 45 | std::string str; 46 | 47 | str = "\n---- " + pizza.getName() + "----\n"; 48 | str.append(pizza.dough + "\n"); 49 | str.append(pizza.sauce + "\n"); 50 | 51 | for (auto& topping : pizza.toppings) 52 | { 53 | str.append(" " + topping + "\n"); 54 | } 55 | 56 | str.append("\n"); 57 | 58 | return os << str; 59 | } 60 | 61 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/Pizza.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Pizza.hpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | 7 | 8 | #ifndef Pizza_hpp 9 | #define Pizza_hpp 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | class Pizza 17 | { 18 | public: 19 | std::string name; 20 | std::string dough; 21 | std::string sauce; 22 | std::vector toppings; 23 | 24 | const std::string& getName() const; 25 | 26 | virtual void prepare() const; 27 | virtual void bake() const; 28 | virtual void cut() const; 29 | virtual void box() const; 30 | virtual ~Pizza() = default; 31 | 32 | //std::ostream& operator<<(std::ostream& os); 33 | }; 34 | 35 | std::ostream& operator<<(std::ostream& os, const Pizza& pizza); 36 | 37 | 38 | #endif /* Pizza_hpp */ 39 | 40 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/PizzaStore.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // PizzaStore.cpp 3 | // PizzaStoreFranchise 4 | // 5 | // Created by Cindy Solomon on 2/20/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "Pizza.hpp" 10 | #include "PizzaStore.hpp" 11 | 12 | std::unique_ptr PizzaStore::orderPizza(std::string type) 13 | { 14 | auto pizza = makePizza(type); 15 | 16 | if (pizza != nullptr) 17 | { 18 | std::cout << "\n--- Making a " + pizza->getName() + " ---\n \n"; 19 | pizza->prepare(); 20 | pizza->bake(); 21 | pizza->cut(); 22 | pizza->box(); 23 | } 24 | else 25 | { 26 | std::cout << "Not a valid pizza type. I don't know how to make this pizza!!" << std::endl; 27 | } 28 | return pizza; 29 | }; 30 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/PizzaStore.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // PizzaStore.hpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef PizzaStore_hpp 10 | #define PizzaStore_hpp 11 | 12 | #include "Pizza.hpp" 13 | #include 14 | 15 | class PizzaStore 16 | { 17 | 18 | public: 19 | 20 | std::unique_ptr orderPizza(std::string type); 21 | virtual std::unique_ptr makePizza(std::string type) = 0; 22 | 23 | }; 24 | 25 | #endif /* PizzaStore_hpp */ 26 | -------------------------------------------------------------------------------- /Factory Pattern/PizzaStoreFranchise/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Factory Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Cindy Solomon 8 | 9 | #include "NYPizzaStore.hpp" 10 | #include "ChicagoPizzaStore.hpp" 11 | #include "PizzaStore.hpp" 12 | #include "Pizza.hpp" 13 | 14 | int main(int argc, const char * argv[]) 15 | { 16 | auto nyStore = new NYPizzaStore(); 17 | auto chicagoStore = new ChicagoPizzaStore(); 18 | 19 | auto nyCheese = nyStore->orderPizza("cheese"); 20 | auto chicagoCheese = chicagoStore->orderPizza("cheese"); 21 | 22 | 23 | auto nyPepperoni = nyStore->orderPizza("pepperoni"); 24 | auto chicagoPepperoni = chicagoStore->orderPizza("pepperoni"); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/CheesePizza.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CheesePizza.cpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "CheesePizza.hpp" 10 | #include 11 | 12 | CheesePizza::CheesePizza() 13 | { 14 | name = "Cheese Pizza"; 15 | dough = "Regular crust"; 16 | sauce = "Marinara Pizza sauce"; 17 | toppings.push_back("Fresh mozzarella"); 18 | toppings.push_back("Parmesan"); 19 | } 20 | 21 | 22 | //void CheesePizza::prepare() override 23 | //{ 24 | // std::cout << "Preparing " << std::endl; 25 | //} 26 | // 27 | //void CheesePizza::bake() override 28 | //{ 29 | // std::cout << "Baking " << std::endl; 30 | //} 31 | // 32 | //void CheesePizza::cut() override 33 | //{ 34 | // std::cout << "Cutting " << std::endl; 35 | //} 36 | // 37 | //void CheesePizza::box() override 38 | //{ 39 | // std::cout << "Boxing " << std::endl; 40 | //} 41 | // 42 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/CheesePizza.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // CheesePizza.hpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef CheesePizza_hpp 10 | #define CheesePizza_hpp 11 | 12 | #include "Pizza.hpp" 13 | 14 | class CheesePizza: public Pizza 15 | { 16 | public: 17 | 18 | CheesePizza(); 19 | // void prepare() override; 20 | // void bake() override; 21 | // void cut() override; 22 | // void box() override; 23 | 24 | }; 25 | 26 | #endif /* CheesePizza_hpp */ 27 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/ClamPizza.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ClamPizza.cpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "ClamPizza.hpp" 10 | #include 11 | 12 | ClamPizza::ClamPizza() 13 | { 14 | name = "Clam Pizza"; 15 | dough = "Thin crust"; 16 | sauce = "White garlic sauce"; 17 | toppings.push_back("Clams"); 18 | toppings.push_back("Grated parmesan cheese"); 19 | } 20 | 21 | 22 | 23 | //void ClamPizza::prepare() override 24 | //{ 25 | // std::cout << "Preparing " << std::endl; 26 | //} 27 | // 28 | //void ClamPizza::bake() override 29 | //{ 30 | // std::cout << "Baking " << std::endl; 31 | //} 32 | // 33 | //void ClamPizza::cut() override 34 | //{ 35 | // std::cout << "Cutting " << std::endl; 36 | //} 37 | // 38 | //void ClamPizza::box() override 39 | //{ 40 | // std::cout << "Boxing " << std::endl; 41 | //} 42 | 43 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/ClamPizza.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ClamPizza.hpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef ClamPizza_hpp 10 | #define ClamPizza_hpp 11 | 12 | #include "Pizza.hpp" 13 | 14 | class ClamPizza: public Pizza 15 | { 16 | public: 17 | 18 | ClamPizza(); 19 | // void prepare() override; 20 | // void bake() override; 21 | // void cut() override; 22 | // void box() override; 23 | 24 | }; 25 | 26 | #endif /* ClamPizza_hpp */ 27 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/PepperoniPizza.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // PepperoniPizza.cpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "PepperoniPizza.hpp" 10 | #include 11 | 12 | PepperoniPizza::PepperoniPizza() 13 | { 14 | name = "Pepperoni Pizza"; 15 | dough = "Regular crust"; 16 | sauce = "Marinara Pizza sauce"; 17 | toppings.push_back("Sliced pepperoni"); 18 | toppings.push_back("Sliced onion"); 19 | toppings.push_back("Grated parmesan chese"); 20 | 21 | } 22 | 23 | //void PepperoniPizza::prepare() override 24 | //{ 25 | // std::cout << "Preparing " << std::endl; 26 | //} 27 | // 28 | //void PepperoniPizza::bake() override 29 | //{ 30 | // std::cout << "Baking " << std::endl; 31 | //} 32 | // 33 | //void PepperoniPizza::cut() override 34 | //{ 35 | // std::cout << "Cutting " << std::endl; 36 | //} 37 | // 38 | //void PepperoniPizza::box() override 39 | //{ 40 | // std::cout << "Boxing " << std::endl; 41 | //} 42 | 43 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/PepperoniPizza.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // PepperoniPizza.hpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef PepperoniPizza_hpp 10 | #define PepperoniPizza_hpp 11 | 12 | #include "Pizza.hpp" 13 | 14 | class PepperoniPizza: public Pizza 15 | { 16 | public: 17 | 18 | PepperoniPizza(); 19 | 20 | // void prepare() override; 21 | // void bake() override; 22 | // void cut() override; 23 | // void box() override; 24 | 25 | }; 26 | 27 | #endif /* PepperoniPizza_hpp */ 28 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/Pizza.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Pizza.cpp 3 | // SimpleFactoryPattern 4 | // 5 | // Created by Cindy Solomon on 2/20/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "Pizza.hpp" 10 | #include 11 | #include 12 | 13 | const std::string& Pizza::getName() const 14 | { 15 | return this->name; 16 | } 17 | 18 | void Pizza::prepare() const 19 | { 20 | std::cout << "Preparing " + getName() << std::endl; 21 | } 22 | void Pizza::bake() const 23 | { 24 | std::cout << "Baking " + getName() << std::endl; 25 | } 26 | 27 | void Pizza::cut() const 28 | { 29 | std::cout << "Cutting " + getName() << std::endl; 30 | } 31 | void Pizza::box() const 32 | { 33 | std::cout << "Boxing " + getName() << std::endl; 34 | } 35 | 36 | std::ostream& operator<<(std::ostream& os, const Pizza& pizza) 37 | { 38 | std::string str; 39 | 40 | str = "\n---- " + pizza.getName() + "----\n"; 41 | str.append(pizza.dough + "\n"); 42 | str.append(pizza.sauce + "\n"); 43 | 44 | for (auto& topping : pizza.toppings) 45 | { 46 | str.append(topping + "\n"); 47 | } 48 | 49 | str.append("\n"); 50 | 51 | return os << str; 52 | } 53 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/Pizza.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Pizza.hpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | 7 | 8 | #ifndef Pizza_hpp 9 | #define Pizza_hpp 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | class Pizza 17 | { 18 | public: 19 | std::string name; 20 | std::string dough; 21 | std::string sauce; 22 | std::vector toppings; 23 | 24 | const std::string& getName() const; 25 | 26 | virtual void prepare() const; 27 | virtual void bake() const; 28 | virtual void cut() const; 29 | virtual void box() const; 30 | virtual ~Pizza() = default; 31 | 32 | //std::ostream& operator<<(std::ostream& os); 33 | }; 34 | 35 | std::ostream& operator<<(std::ostream& os, const Pizza& pizza); 36 | 37 | 38 | #endif /* Pizza_hpp */ 39 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/PizzaStore.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // PizzaStore.cpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "PizzaStore.hpp" 10 | #include "SimplePizzaFactory.hpp" 11 | #include 12 | 13 | PizzaStore::PizzaStore(SimplePizzaFactory factory) 14 | { 15 | this->factory = factory; 16 | } 17 | 18 | std::unique_ptr PizzaStore::orderPizza(std::string type) 19 | { 20 | 21 | std::unique_ptr pizza = nullptr; 22 | 23 | pizza = factory.createPizza(type); 24 | 25 | if (pizza != nullptr) 26 | { 27 | pizza->prepare(); 28 | pizza->bake(); 29 | pizza->cut(); 30 | pizza->box(); 31 | } 32 | else 33 | { 34 | std::cout << "Not a valid pizza type. I don't know how to make this pizza!!" << std::endl; 35 | } 36 | return pizza; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/PizzaStore.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // PizzaStore.hpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef PizzaStore_hpp 10 | #define PizzaStore_hpp 11 | 12 | #include "SimplePizzaFactory.hpp" 13 | #include "Pizza.hpp" 14 | #include 15 | 16 | class PizzaStore 17 | { 18 | SimplePizzaFactory factory; 19 | 20 | public: 21 | 22 | PizzaStore(SimplePizzaFactory factory); 23 | std::unique_ptr orderPizza(std::string type); 24 | 25 | }; 26 | 27 | #endif /* PizzaStore_hpp */ 28 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/SimplePizzaFactory.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // SimplePizzaFactory.cpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/12/18. 6 | 7 | #include "SimplePizzaFactory.hpp" 8 | #include "Pizza.hpp" 9 | #include "CheesePizza.hpp" 10 | #include "PepperoniPizza.hpp" 11 | #include "ClamPizza.hpp" 12 | #include "VeggiePizza.hpp" 13 | 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | std::unique_ptr SimplePizzaFactory::createPizza(const std::string& type) 20 | { 21 | // If not a valid type, returns a null object. 22 | 23 | std::unique_ptr pizza = nullptr; 24 | 25 | if (type == "cheese") 26 | { 27 | pizza = std::make_unique(); 28 | } 29 | else if (type == "pepperoni") 30 | { 31 | pizza = std::make_unique(); 32 | } 33 | else if (type == "clam") 34 | { 35 | pizza = std::make_unique(); 36 | } 37 | else if (type == "veggie") 38 | { 39 | pizza = std::make_unique(); 40 | } 41 | return pizza; 42 | 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/SimplePizzaFactory.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // SimplePizzaFactory.hpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/12/18. 6 | 7 | 8 | #ifndef SimplePizzaFactory_hpp 9 | #define SimplePizzaFactory_hpp 10 | 11 | #include "Pizza.hpp" 12 | #include 13 | 14 | class SimplePizzaFactory 15 | { 16 | public: 17 | 18 | std::unique_ptr createPizza(const std::string& type); 19 | 20 | }; 21 | 22 | #endif /* SimplePizzaFactory_hpp */ 23 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/VeggiePizza.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // PepperoniPizza.cpp 3 | // Factory Pattern 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "VeggiePizza.hpp" 10 | #include 11 | 12 | VeggiePizza::VeggiePizza() 13 | { 14 | name = "Veggie Pizza"; 15 | dough = "Regular crust"; 16 | sauce = "Marinara sauce"; 17 | toppings.push_back("Shredded mozzarella"); 18 | toppings.push_back("Grated Parmesan"); 19 | toppings.push_back("Diced onion"); 20 | toppings.push_back("Sliced mushrooms"); 21 | toppings.push_back("Sliced red pepper"); 22 | toppings.push_back("No olives because they're gross"); 23 | } 24 | 25 | //void VeggiePizza::prepare() override 26 | //{ 27 | // std::cout << "Preparing " << std::endl; 28 | //} 29 | // 30 | //void VeggiePizza::bake() override 31 | //{ 32 | // std::cout << "Baking " << std::endl; 33 | //} 34 | // 35 | //void VeggiePizza::cut() override 36 | //{ 37 | // std::cout << "Cutting " << std::endl; 38 | //} 39 | // 40 | //void VeggiePizza::box() override 41 | //{ 42 | // std::cout << "Boxing " << std::endl; 43 | //} 44 | 45 | 46 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/VeggiePizza.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // VeggiePizza.hpp 3 | // SimpleFactoryPattern 4 | // 5 | // Created by Cindy Solomon on 2/20/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef VeggiePizza_hpp 10 | #define VeggiePizza_hpp 11 | 12 | #include "Pizza.hpp" 13 | 14 | class VeggiePizza: public Pizza 15 | { 16 | public: 17 | 18 | VeggiePizza(); 19 | // void prepare() override; 20 | // void bake() override; 21 | // void cut() override; 22 | // void box() override; 23 | 24 | }; 25 | 26 | #endif /* VeggiePizza_hpp */ 27 | -------------------------------------------------------------------------------- /Factory Pattern/Simple Factory Pattern/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Factory Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Cindy Solomon 8 | 9 | #include "SimplePizzaFactory.hpp" 10 | #include "PizzaStore.hpp" 11 | #include "Pizza.hpp" 12 | #include 13 | 14 | int main(int argc, const char * argv[]) 15 | { 16 | // insert code here... 17 | SimplePizzaFactory factory; 18 | 19 | PizzaStore pizzaStore(factory); 20 | 21 | auto pizza = pizzaStore.orderPizza("cheese"); 22 | std::cout << "We ordered a " + pizza->getName() << std::endl; 23 | std::cout << *pizza; 24 | 25 | auto vegPizza = pizzaStore.orderPizza("veggie"); 26 | std::cout << "We ordered a " + vegPizza->getName() << std::endl; 27 | std::cout << *vegPizza; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Factory Pattern/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // OnePizzaType 4 | // 5 | // Created by Cindy Solomon on 2/13/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "OnePizzaType.hpp" 11 | 12 | int main(int argc, const char * argv[]) { 13 | // insert code here... 14 | 15 | Pizza basicPizza; 16 | 17 | basicPizza = orderPizza(); 18 | 19 | return 0; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Head First Design Patterns C++.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 12 | 13 | 15 | 16 | 18 | 19 | 21 | 22 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Brian Arnold 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/CurrentConditionsDisplay.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CurrentConditions.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | #include 9 | #include "CurrentConditionsDisplay.hpp" 10 | namespace weather { 11 | CurrentConditionsDisplay::CurrentConditionsDisplay(Subject& inWeatherData) 12 | : temperature(0.0f) 13 | , humidity(0.0f) 14 | , weatherData(inWeatherData) 15 | { 16 | weatherData.registerObserver(this); 17 | } 18 | 19 | CurrentConditionsDisplay::~CurrentConditionsDisplay() 20 | { 21 | weatherData.removeObserver(this); 22 | } 23 | 24 | void CurrentConditionsDisplay::update(float temp, float hum, float pressure) { 25 | temperature = temp; 26 | humidity = hum; 27 | display(); 28 | } 29 | 30 | void CurrentConditionsDisplay::display() const { 31 | std::cout << "Current conditions: " << temperature 32 | << "F degrees and " << humidity << "% humidity" << std::endl; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/CurrentConditionsDisplay.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // CurrentConditions.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef CurrentConditionsDisplay_hpp 10 | #define CurrentConditionsDisplay_hpp 11 | 12 | #include "Observer.hpp" 13 | #include "DisplayElement.hpp" 14 | #include "Subject.hpp" 15 | namespace weather { 16 | class CurrentConditionsDisplay : public Observer, public DisplayElement { 17 | private: 18 | float temperature; 19 | float humidity; 20 | Subject& weatherData; 21 | public: 22 | explicit CurrentConditionsDisplay(Subject& inWeatherData); 23 | ~CurrentConditionsDisplay(); 24 | void update(float temperature, float humidity, float pressure) override; 25 | 26 | void display() const override; 27 | }; 28 | } 29 | #endif /* CurrentConditionsDisplay_hpp */ 30 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/DisplayElement.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // DisplayElement.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef DisplayElement_h 10 | #define DisplayElement_h 11 | namespace weather { 12 | class DisplayElement { 13 | public: 14 | virtual ~DisplayElement() = default; 15 | virtual void display() const = 0; 16 | }; 17 | } 18 | #endif /* DisplayElement_h */ 19 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/ForecastDisplay.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ForecastDisplay.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | #include 9 | #include "ForecastDisplay.hpp" 10 | 11 | namespace weather { 12 | ForecastDisplay::ForecastDisplay(Subject& weatherData) 13 | : currentPressure(29.92f) 14 | , lastPressure(0.0f) 15 | , weatherData(weatherData) 16 | { 17 | weatherData.registerObserver(this); 18 | } 19 | 20 | ForecastDisplay::~ForecastDisplay() 21 | { 22 | weatherData.removeObserver(this); 23 | } 24 | 25 | void ForecastDisplay::update(float temp, float humidity, float pressure) { 26 | lastPressure = currentPressure; 27 | currentPressure = pressure; 28 | display(); 29 | } 30 | 31 | void ForecastDisplay::display() const { 32 | std::cout << "Forecast: "; 33 | if (currentPressure > lastPressure) { 34 | std::cout << "Improving weather on the way!"<< std::endl; 35 | } else if (currentPressure == lastPressure) { 36 | std::cout << "More of the same"<< std::endl; 37 | } else if (currentPressure < lastPressure) { 38 | std::cout << "Watch out for cooler, rainy weather"<< std::endl; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/ForecastDisplay.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ForecastDisplay.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef ForecastDisplay_hpp 10 | #define ForecastDisplay_hpp 11 | 12 | #include "Observer.hpp" 13 | #include "DisplayElement.hpp" 14 | #include "WeatherData.hpp" 15 | namespace weather { 16 | class ForecastDisplay : public Observer, public DisplayElement { 17 | private: 18 | float currentPressure; 19 | float lastPressure; 20 | Subject& weatherData; 21 | public: 22 | explicit ForecastDisplay(Subject& inWeatherData); 23 | ~ForecastDisplay(); 24 | void update(float temperature, float humidity, float pressure) override; 25 | void display() const override; 26 | }; 27 | } 28 | #endif /* ForecastDisplay_hpp */ 29 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/HeatIndexDisplay.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // HeatIndexDisplay.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | #include 9 | #include "HeatIndexDisplay.hpp" 10 | namespace weather { 11 | HeatIndexDisplay::HeatIndexDisplay(Subject& weatherData) 12 | : heatIndex(0.0f) 13 | , weatherData(weatherData) 14 | { 15 | weatherData.registerObserver(this); 16 | } 17 | 18 | HeatIndexDisplay::~HeatIndexDisplay() 19 | { 20 | weatherData.removeObserver(this); 21 | } 22 | 23 | void HeatIndexDisplay::update(float t, float rh, float pressure) { 24 | heatIndex = computeHeatIndex(t, rh); 25 | display(); 26 | } 27 | 28 | float HeatIndexDisplay::computeHeatIndex(float t, float rh) const { 29 | float index = (float)((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh) 30 | + (0.00941695 * (t * t)) + (0.00728898 * (rh * rh)) 31 | + (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) + 32 | (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 * 33 | (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) + 34 | (0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) + 35 | 0.000000000843296 * (t * t * rh * rh * rh)) - 36 | (0.0000000000481975 * (t * t * t * rh * rh * rh))); 37 | return index; 38 | } 39 | 40 | void HeatIndexDisplay::display() const { 41 | std::cout << "Heat index is " << heatIndex << std::endl; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/HeatIndexDisplay.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // HeatIndexDisplay.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef HeatIndexDisplay_hpp 10 | #define HeatIndexDisplay_hpp 11 | 12 | #include "Observer.hpp" 13 | #include "DisplayElement.hpp" 14 | #include "WeatherData.hpp" 15 | namespace weather { 16 | class HeatIndexDisplay : public Observer, public DisplayElement { 17 | private: 18 | float heatIndex; 19 | Subject& weatherData; 20 | public: 21 | explicit HeatIndexDisplay(Subject& inWeatherData); 22 | ~HeatIndexDisplay(); 23 | void update(float temperature, float humidity, float pressure) override; 24 | void display() const override; 25 | float computeHeatIndex(float t, float rh) const; 26 | }; 27 | } 28 | #endif /* HeatIndexDisplay_hpp */ 29 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/Observer.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Observer.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef Observer_h 10 | #define Observer_h 11 | namespace weather { 12 | class Observer { 13 | public: 14 | virtual ~Observer() = default; 15 | virtual void update(float temp, float humidity, float pressure) = 0; 16 | }; 17 | } 18 | #endif /* Observer_h */ 19 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/StatisticsDisplay.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // StatisticsDisplay.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | #include 9 | #include "StatisticsDisplay.hpp" 10 | 11 | namespace weather { 12 | StatisticsDisplay::StatisticsDisplay(Subject& weatherData) 13 | : maxTemp(0.0f) 14 | , minTemp(200) 15 | , tempSum(0.0f) 16 | , numReadings(0) 17 | , weatherData(weatherData) 18 | { 19 | weatherData.registerObserver(this); 20 | } 21 | 22 | StatisticsDisplay::~StatisticsDisplay() 23 | { 24 | weatherData.removeObserver(this); 25 | } 26 | 27 | void StatisticsDisplay::update(float temp, float humidity, float pressure) { 28 | tempSum += temp; 29 | numReadings++; 30 | 31 | if (temp > maxTemp) { 32 | maxTemp = temp; 33 | } 34 | 35 | if (temp < minTemp) { 36 | minTemp = temp; 37 | } 38 | 39 | display(); 40 | } 41 | 42 | void StatisticsDisplay::display() const { 43 | std::cout << "Avg/Max/Min temperature = " << (tempSum / numReadings) 44 | << "/" << maxTemp << "/" << minTemp << std::endl; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/StatisticsDisplay.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // StatisticsDisplay.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef StatisticsDisplay_hpp 10 | #define StatisticsDisplay_hpp 11 | 12 | #include "Observer.hpp" 13 | #include "DisplayElement.hpp" 14 | #include "Subject.hpp" 15 | namespace weather { 16 | class StatisticsDisplay : public Observer, public DisplayElement { 17 | private: 18 | float maxTemp = 0.0f; 19 | float minTemp = 200; 20 | float tempSum= 0.0f; 21 | int numReadings; 22 | Subject& weatherData; 23 | public: 24 | explicit StatisticsDisplay(Subject& inWeatherData); 25 | ~StatisticsDisplay(); 26 | void update(float temperature, float humidity, float pressure) override; 27 | void display() const override; 28 | }; 29 | } 30 | #endif /* StatisticsDisplay_hpp */ 31 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/Subject.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Subject.h 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef Subject_h 10 | #define Subject_h 11 | 12 | namespace weather { 13 | class Observer; // forward declaration 14 | class Subject { 15 | public: 16 | virtual ~Subject() = default; 17 | virtual void registerObserver(Observer* o) = 0; 18 | virtual void removeObserver(Observer* o) = 0; 19 | virtual void notifyObservers() = 0; 20 | }; 21 | } 22 | 23 | #endif /* Subject_h */ 24 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/WeatherData.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // WeatherData.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "WeatherData.hpp" 10 | #include "Observer.hpp" 11 | namespace weather { 12 | void WeatherData::registerObserver(Observer* o) { 13 | observers.push_back(o); 14 | } 15 | 16 | void WeatherData::removeObserver(Observer* o) { 17 | for(auto beg = observers.begin(); beg < observers.end(); beg++) 18 | { 19 | if(*beg == o) 20 | beg = observers.erase(beg); 21 | } 22 | } 23 | 24 | void WeatherData::setMeasurements(float temp, float h, float p) { 25 | temperature = temp; 26 | humidity = h; 27 | pressure = p; 28 | measurementsChanged(); 29 | } 30 | 31 | void WeatherData::measurementsChanged() { 32 | notifyObservers(); 33 | } 34 | 35 | void WeatherData::notifyObservers() { 36 | for (auto observer : observers) { 37 | observer->update(temperature, humidity, pressure); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/WeatherData.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // WeatherData.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef WeatherData_hpp 10 | #define WeatherData_hpp 11 | #include 12 | #include "Subject.hpp" 13 | #include "Observer.hpp" 14 | namespace weather { 15 | class WeatherData : public Subject { 16 | 17 | public: 18 | WeatherData() = default; 19 | virtual ~WeatherData() = default; 20 | 21 | virtual void registerObserver(Observer* o) override; 22 | 23 | virtual void removeObserver(Observer* o) override; 24 | 25 | virtual void notifyObservers() override; 26 | 27 | void measurementsChanged(); 28 | 29 | void setMeasurements(float temperature, float humidity, float pressure); 30 | 31 | float getTemperature() const { 32 | return temperature; 33 | } 34 | 35 | float getHumidity() const { 36 | return humidity; 37 | } 38 | 39 | float getPressure() const { 40 | return pressure; 41 | } 42 | 43 | private: 44 | std::vector observers; 45 | float temperature; 46 | float humidity; 47 | float pressure; 48 | }; 49 | } 50 | #endif /* WeatherData_hpp */ 51 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/WeatherStation.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // WeatherStation.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/21/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "WeatherStation.hpp" 10 | #include "WeatherData.hpp" 11 | #include "CurrentConditionsDisplay.hpp" 12 | #include "ForecastDisplay.hpp" 13 | #include "StatisticsDisplay.hpp" 14 | #include "HeatIndexDisplay.hpp" 15 | 16 | void WeatherStation() 17 | { 18 | weather::WeatherData weatherData; 19 | weather::CurrentConditionsDisplay currentDisplay(weatherData); 20 | weather::StatisticsDisplay statisticsDisplay(weatherData); 21 | weather::ForecastDisplay forecastDisplay(weatherData); 22 | 23 | weatherData.setMeasurements(80, 65, 30.4f); 24 | weatherData.setMeasurements(82, 70, 29.2f); 25 | weatherData.setMeasurements(78, 90, 29.2f); 26 | } 27 | 28 | void WeatherStationHeatIndex() 29 | { 30 | weather::WeatherData weatherData; 31 | weather::CurrentConditionsDisplay currentDisplay(weatherData); 32 | weather::StatisticsDisplay statisticsDisplay(weatherData); 33 | weather::ForecastDisplay forecastDisplay(weatherData); 34 | weather::HeatIndexDisplay heatIndexDisplay(weatherData); 35 | 36 | weatherData.setMeasurements(80, 65, 30.4f); 37 | weatherData.setMeasurements(82, 70, 29.2f); 38 | weatherData.setMeasurements(78, 90, 29.2f); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather/WeatherStation.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // WeatherStation.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/21/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef WeatherStation_hpp 10 | #define WeatherStation_hpp 11 | 12 | void WeatherStation(); 13 | void WeatherStationHeatIndex(); 14 | 15 | #endif /* WeatherStation_hpp */ 16 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/CurrentConditionsDisplay.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CurrentConditions.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | #include 9 | #include "CurrentConditionsDisplay.hpp" 10 | namespace wf { 11 | CurrentConditionsDisplay::CurrentConditionsDisplay(const std::shared_ptr& weatherData) 12 | : temperature(0.0f) 13 | , humidity(0.0f) 14 | , weatherData(weatherData) 15 | { 16 | token = weatherData->registerObserver([this](float temp, float hum, float pressure) { 17 | update(temp, hum, pressure); 18 | }); 19 | } 20 | 21 | CurrentConditionsDisplay::~CurrentConditionsDisplay() 22 | { 23 | weatherData->removeObserver(token); 24 | } 25 | 26 | void CurrentConditionsDisplay::update(float temp, float hum, float pressure) { 27 | temperature = temp; 28 | humidity = hum; 29 | display(); 30 | } 31 | 32 | void CurrentConditionsDisplay::display() const { 33 | std::cout << "Current conditions: " << temperature 34 | << "F degrees and " << humidity << "% humidity" << std::endl; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/CurrentConditionsDisplay.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // CurrentConditions.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef CurrentConditionsDisplay_hpp 10 | #define CurrentConditionsDisplay_hpp 11 | 12 | #include "DisplayElement.hpp" 13 | #include "Subject.hpp" 14 | namespace wf { 15 | class CurrentConditionsDisplay : public DisplayElement { 16 | private: 17 | float temperature; 18 | float humidity; 19 | std::shared_ptr weatherData; 20 | ListenerToken token; 21 | public: 22 | explicit CurrentConditionsDisplay(const std::shared_ptr& inWeatherData); 23 | ~CurrentConditionsDisplay(); 24 | 25 | void update(float temperature, float humidity, float pressure); 26 | 27 | void display() const override; 28 | }; 29 | } 30 | #endif /* CurrentConditionsDisplay_hpp */ 31 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/DisplayElement.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // DisplayElement.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef DisplayElement_h 10 | #define DisplayElement_h 11 | namespace wf { 12 | class DisplayElement { 13 | public: 14 | virtual ~DisplayElement() = default; 15 | virtual void display() const = 0; 16 | }; 17 | } 18 | #endif /* DisplayElement_h */ 19 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/ForecastDisplay.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ForecastDisplay.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | #include 9 | #include "ForecastDisplay.hpp" 10 | 11 | namespace wf { 12 | ForecastDisplay::ForecastDisplay(const std::shared_ptr& weatherData) 13 | : currentPressure(29.92f) 14 | , lastPressure(0.0f) 15 | , weatherData(weatherData) 16 | { 17 | token = weatherData->registerObserver([this](float temp, float hum, float pressure) { 18 | update(temp, hum, pressure); 19 | }); 20 | } 21 | 22 | ForecastDisplay::~ForecastDisplay() 23 | { 24 | weatherData->removeObserver(token); 25 | } 26 | 27 | void ForecastDisplay::update(float temp, float humidity, float pressure) { 28 | lastPressure = currentPressure; 29 | currentPressure = pressure; 30 | display(); 31 | } 32 | 33 | void ForecastDisplay::display() const { 34 | std::cout << "Forecast: "; 35 | if (currentPressure > lastPressure) { 36 | std::cout << "Improving weather on the way!"<< std::endl; 37 | } else if (currentPressure == lastPressure) { 38 | std::cout << "More of the same"<< std::endl; 39 | } else if (currentPressure < lastPressure) { 40 | std::cout << "Watch out for cooler, rainy weather"<< std::endl; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/ForecastDisplay.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ForecastDisplay.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef ForecastDisplay_hpp 10 | #define ForecastDisplay_hpp 11 | 12 | #include "DisplayElement.hpp" 13 | #include "WeatherData.hpp" 14 | namespace wf { 15 | class ForecastDisplay : public DisplayElement { 16 | private: 17 | float currentPressure; 18 | float lastPressure; 19 | std::shared_ptr weatherData; 20 | ListenerToken token; 21 | public: 22 | explicit ForecastDisplay(const std::shared_ptr& inWeatherData); 23 | ~ForecastDisplay(); 24 | void update(float temperature, float humidity, float pressure); 25 | 26 | void display() const override; 27 | }; 28 | } 29 | #endif /* ForecastDisplay_hpp */ 30 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/HeatIndexDisplay.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // HeatIndexDisplay.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | #include 9 | #include "HeatIndexDisplay.hpp" 10 | namespace wf { 11 | HeatIndexDisplay::HeatIndexDisplay(const std::shared_ptr& weatherData) 12 | : heatIndex(0.0f) 13 | , weatherData(weatherData) 14 | { 15 | token = weatherData->registerObserver(std::bind(&HeatIndexDisplay::update, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); 16 | } 17 | 18 | HeatIndexDisplay::~HeatIndexDisplay() 19 | { 20 | weatherData->removeObserver(token); 21 | } 22 | 23 | void HeatIndexDisplay::update(float t, float rh, float pressure) { 24 | heatIndex = computeHeatIndex(t, rh); 25 | display(); 26 | } 27 | 28 | float HeatIndexDisplay::computeHeatIndex(float t, float rh) const { 29 | float index = (float)((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh) 30 | + (0.00941695 * (t * t)) + (0.00728898 * (rh * rh)) 31 | + (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) + 32 | (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 * 33 | (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) + 34 | (0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) + 35 | 0.000000000843296 * (t * t * rh * rh * rh)) - 36 | (0.0000000000481975 * (t * t * t * rh * rh * rh))); 37 | return index; 38 | } 39 | 40 | void HeatIndexDisplay::display() const { 41 | std::cout << "Heat index is " << heatIndex << std::endl; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/HeatIndexDisplay.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // HeatIndexDisplay.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef HeatIndexDisplay_hpp 10 | #define HeatIndexDisplay_hpp 11 | 12 | #include "DisplayElement.hpp" 13 | #include "WeatherData.hpp" 14 | namespace wf { 15 | class HeatIndexDisplay : public DisplayElement { 16 | private: 17 | float heatIndex; 18 | std::shared_ptr weatherData; 19 | ListenerToken token; 20 | public: 21 | explicit HeatIndexDisplay(const std::shared_ptr& inWeatherData); 22 | ~HeatIndexDisplay(); 23 | void update(float temperature, float humidity, float pressure); 24 | 25 | void display() const override; 26 | 27 | float computeHeatIndex(float t, float rh) const; 28 | }; 29 | } 30 | #endif /* HeatIndexDisplay_hpp */ 31 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/StatisticsDisplay.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // StatisticsDisplay.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | #include 9 | #include "StatisticsDisplay.hpp" 10 | 11 | namespace wf { 12 | StatisticsDisplay::StatisticsDisplay(const std::shared_ptr& weatherData) 13 | : maxTemp(0.0f) 14 | , minTemp(200) 15 | , tempSum(0.0f) 16 | , numReadings(0) 17 | , weatherData(weatherData) 18 | { 19 | token = weatherData->registerObserver(std::bind(&StatisticsDisplay::update, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); 20 | } 21 | 22 | StatisticsDisplay::~StatisticsDisplay() 23 | { 24 | weatherData->removeObserver(token); 25 | } 26 | 27 | void StatisticsDisplay::update(float temp, float humidity, float pressure) { 28 | tempSum += temp; 29 | numReadings++; 30 | 31 | if (temp > maxTemp) { 32 | maxTemp = temp; 33 | } 34 | 35 | if (temp < minTemp) { 36 | minTemp = temp; 37 | } 38 | 39 | display(); 40 | } 41 | 42 | void StatisticsDisplay::display() const { 43 | std::cout << "Avg/Max/Min temperature = " << (tempSum / numReadings) 44 | << "/" << maxTemp << "/" << minTemp << std::endl; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/StatisticsDisplay.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // StatisticsDisplay.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef StatisticsDisplay_hpp 10 | #define StatisticsDisplay_hpp 11 | 12 | #include "DisplayElement.hpp" 13 | #include "Subject.hpp" 14 | namespace wf { 15 | class StatisticsDisplay : public DisplayElement { 16 | private: 17 | float maxTemp = 0.0f; 18 | float minTemp = 200; 19 | float tempSum= 0.0f; 20 | int numReadings; 21 | std::shared_ptr weatherData; 22 | ListenerToken token; 23 | public: 24 | explicit StatisticsDisplay(const std::shared_ptr& inWeatherData); 25 | ~StatisticsDisplay(); 26 | void update(float temperature, float humidity, float pressure); 27 | 28 | void display() const override; 29 | }; 30 | } 31 | #endif /* StatisticsDisplay_hpp */ 32 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/Subject.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Subject.h 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef Subject_h 10 | #define Subject_h 11 | 12 | #include 13 | 14 | typedef size_t ListenerToken; 15 | typedef std::function ListenerCallback; 16 | 17 | namespace wf { 18 | class Subject { 19 | public: 20 | virtual ~Subject() = default; 21 | virtual ListenerToken registerObserver(ListenerCallback o) = 0; 22 | virtual void removeObserver(ListenerToken tok) = 0; 23 | virtual void notifyObservers() = 0; 24 | }; 25 | } 26 | 27 | #endif /* Subject_h */ 28 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/WeatherData.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // WeatherData.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "WeatherData.hpp" 10 | 11 | namespace wf { 12 | ListenerToken WeatherData::tokenId = 1; 13 | 14 | ListenerToken WeatherData::registerObserver(ListenerCallback func) { 15 | ListenerToken tok = ++tokenId; 16 | observerMap.insert(std::make_pair(tok, func)); 17 | return tok; 18 | } 19 | 20 | void WeatherData::removeObserver(ListenerToken token) { 21 | observerMap.erase(token); 22 | } 23 | 24 | void WeatherData::setMeasurements(float temp, float h, float p) { 25 | temperature = temp; 26 | humidity = h; 27 | pressure = p; 28 | measurementsChanged(); 29 | } 30 | 31 | void WeatherData::measurementsChanged() { 32 | notifyObservers(); 33 | } 34 | 35 | void WeatherData::notifyObservers() { 36 | for (auto& kv : observerMap) { 37 | kv.second(temperature, humidity, pressure); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/WeatherData.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // WeatherData.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/14/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef WeatherData_hpp 10 | #define WeatherData_hpp 11 | #include 12 | #include 13 | #include "Subject.hpp" 14 | namespace wf { 15 | class WeatherData : public Subject { 16 | public: 17 | WeatherData() = default; 18 | virtual ~WeatherData() = default; 19 | 20 | virtual ListenerToken registerObserver(ListenerCallback o) override; 21 | virtual void removeObserver(ListenerToken t) override; 22 | virtual void notifyObservers() override; 23 | 24 | void measurementsChanged(); 25 | void setMeasurements(float temperature, float humidity, float pressure); 26 | 27 | float getTemperature() const { 28 | return temperature; 29 | } 30 | 31 | float getHumidity() const { 32 | return humidity; 33 | } 34 | 35 | float getPressure() const { 36 | return pressure; 37 | } 38 | 39 | private: 40 | std::unordered_map observerMap; 41 | float temperature; 42 | float humidity; 43 | float pressure; 44 | static ListenerToken tokenId; 45 | }; 46 | } 47 | #endif /* WeatherData_hpp */ 48 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/WeatherStation.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // WeatherStation.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/21/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "WeatherStation.hpp" 10 | #include "WeatherData.hpp" 11 | #include "CurrentConditionsDisplay.hpp" 12 | #include "ForecastDisplay.hpp" 13 | #include "StatisticsDisplay.hpp" 14 | #include "HeatIndexDisplay.hpp" 15 | 16 | void WeatherStationWithFunctions() 17 | { 18 | auto weatherData = std::make_shared(); 19 | auto currentDisplay = std::make_shared(weatherData); 20 | auto statisticsDisplay = std::make_shared(weatherData); 21 | auto forecastDisplay = std::make_shared(weatherData); 22 | auto heatIndexDisplay = std::make_shared(weatherData); 23 | weatherData->setMeasurements(80, 65, 30.4f); 24 | weatherData->setMeasurements(82, 70, 29.2f); 25 | weatherData->setMeasurements(78, 90, 29.2f); 26 | } 27 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Weather_Lambdas/WeatherStation.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // WeatherStation.hpp 3 | // Observer Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 2/21/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef WeatherStation2_hpp 10 | #define WeatherStation2_hpp 11 | 12 | void WeatherStationWithFunctions(); 13 | 14 | #endif /* WeatherStation_hpp */ 15 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Observer Pattern 4 | // 5 | // Created by Brian Arnold on 2/4/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | #include "Weather/WeatherStation.hpp" 12 | #include "Weather_Lambdas/WeatherStation.hpp" 13 | 14 | int main(int argc, const char * argv[]) { 15 | std::cout << " ============= WeatherStation ============" << std::endl; 16 | WeatherStation(); 17 | std::cout << " ============= WeatherStationHeatIndex ============" << std::endl; 18 | WeatherStationHeatIndex(); 19 | std::cout << " ============= WeatherStationHeatIndex using std::function / lambda ============" << std::endl; 20 | WeatherStationWithFunctions(); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Head First Design Patterns in C++ 2 | This repository contains sample code in C++ based on the book, "[Head First Design Patterns](http://shop.oreilly.com/product/9780596007126.do)", by Freeman & Robson, published by O'Reilly. 3 | 4 | The original sample code in Java, which the book's examples are based on, can be found here: https://github.com/bethrobson/Head-First-Design-Patterns. 5 | 6 | This code has been developed as part of a **Book Club** I am running with members of my team. 7 | 8 | Each chapter has its own Xcode project and C++ source files that produce a command line executable. Where relevant, comments with IN C++ indicate deviations from the Java sample code, in order to conform to C++ requirements or best practices. 9 | 10 | ## Chapters 11 | * Strategy Pattern 12 | * Observer Pattern 13 | * Decorator Pattern 14 | * Factory Pattern *(under construction)* 15 | * Singleton Pattern *(under construction, see branch)* 16 | * Command Pattern 17 | * Adapter and Facade *(not started)* 18 | 19 | Note: our book club ended before we completed the code for the other chapters, but we hope this was helpful to you. 20 | -------------------------------------------------------------------------------- /Singleton Pattern/Singleton Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B694140920273E21008A03E0 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B694140820273E21008A03E0 /* main.cpp */; }; 11 | D79C9496206BCCBB009A5E4D /* ClassicSingleton.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D79C9494206BCCBB009A5E4D /* ClassicSingleton.cpp */; }; 12 | D79C9499206BDB67009A5E4D /* MyersSingleton.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D79C9497206BDB67009A5E4D /* MyersSingleton.cpp */; }; 13 | D79C949C206BDD0E009A5E4D /* ThreadSafeClassicSingleton.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D79C949A206BDD0E009A5E4D /* ThreadSafeClassicSingleton.cpp */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXCopyFilesBuildPhase section */ 17 | B694140320273E21008A03E0 /* CopyFiles */ = { 18 | isa = PBXCopyFilesBuildPhase; 19 | buildActionMask = 2147483647; 20 | dstPath = /usr/share/man/man1/; 21 | dstSubfolderSpec = 0; 22 | files = ( 23 | ); 24 | runOnlyForDeploymentPostprocessing = 1; 25 | }; 26 | /* End PBXCopyFilesBuildPhase section */ 27 | 28 | /* Begin PBXFileReference section */ 29 | B694140520273E21008A03E0 /* Singleton Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Singleton Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 30 | B694140820273E21008A03E0 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; 31 | D79C9494206BCCBB009A5E4D /* ClassicSingleton.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ClassicSingleton.cpp; sourceTree = ""; }; 32 | D79C9495206BCCBB009A5E4D /* ClassicSingleton.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ClassicSingleton.hpp; sourceTree = ""; }; 33 | D79C9497206BDB67009A5E4D /* MyersSingleton.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = MyersSingleton.cpp; sourceTree = ""; }; 34 | D79C9498206BDB67009A5E4D /* MyersSingleton.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = MyersSingleton.hpp; sourceTree = ""; }; 35 | D79C949A206BDD0E009A5E4D /* ThreadSafeClassicSingleton.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ThreadSafeClassicSingleton.cpp; sourceTree = ""; }; 36 | D79C949B206BDD0E009A5E4D /* ThreadSafeClassicSingleton.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ThreadSafeClassicSingleton.hpp; sourceTree = ""; }; 37 | /* End PBXFileReference section */ 38 | 39 | /* Begin PBXFrameworksBuildPhase section */ 40 | B694140220273E21008A03E0 /* Frameworks */ = { 41 | isa = PBXFrameworksBuildPhase; 42 | buildActionMask = 2147483647; 43 | files = ( 44 | ); 45 | runOnlyForDeploymentPostprocessing = 0; 46 | }; 47 | /* End PBXFrameworksBuildPhase section */ 48 | 49 | /* Begin PBXGroup section */ 50 | B69413FC20273E21008A03E0 = { 51 | isa = PBXGroup; 52 | children = ( 53 | B694140720273E21008A03E0 /* Singleton Pattern */, 54 | B694140620273E21008A03E0 /* Products */, 55 | ); 56 | sourceTree = ""; 57 | }; 58 | B694140620273E21008A03E0 /* Products */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | B694140520273E21008A03E0 /* Singleton Pattern */, 62 | ); 63 | name = Products; 64 | sourceTree = ""; 65 | }; 66 | B694140720273E21008A03E0 /* Singleton Pattern */ = { 67 | isa = PBXGroup; 68 | children = ( 69 | B694140820273E21008A03E0 /* main.cpp */, 70 | D79C9494206BCCBB009A5E4D /* ClassicSingleton.cpp */, 71 | D79C9495206BCCBB009A5E4D /* ClassicSingleton.hpp */, 72 | D79C9497206BDB67009A5E4D /* MyersSingleton.cpp */, 73 | D79C9498206BDB67009A5E4D /* MyersSingleton.hpp */, 74 | D79C949A206BDD0E009A5E4D /* ThreadSafeClassicSingleton.cpp */, 75 | D79C949B206BDD0E009A5E4D /* ThreadSafeClassicSingleton.hpp */, 76 | ); 77 | path = "Singleton Pattern"; 78 | sourceTree = ""; 79 | }; 80 | /* End PBXGroup section */ 81 | 82 | /* Begin PBXNativeTarget section */ 83 | B694140420273E21008A03E0 /* Singleton Pattern */ = { 84 | isa = PBXNativeTarget; 85 | buildConfigurationList = B694140C20273E21008A03E0 /* Build configuration list for PBXNativeTarget "Singleton Pattern" */; 86 | buildPhases = ( 87 | B694140120273E21008A03E0 /* Sources */, 88 | B694140220273E21008A03E0 /* Frameworks */, 89 | B694140320273E21008A03E0 /* CopyFiles */, 90 | ); 91 | buildRules = ( 92 | ); 93 | dependencies = ( 94 | ); 95 | name = "Singleton Pattern"; 96 | productName = "Singleton Pattern"; 97 | productReference = B694140520273E21008A03E0 /* Singleton Pattern */; 98 | productType = "com.apple.product-type.tool"; 99 | }; 100 | /* End PBXNativeTarget section */ 101 | 102 | /* Begin PBXProject section */ 103 | B69413FD20273E21008A03E0 /* Project object */ = { 104 | isa = PBXProject; 105 | attributes = { 106 | LastUpgradeCheck = 0930; 107 | ORGANIZATIONNAME = "Brian Arnold"; 108 | TargetAttributes = { 109 | B694140420273E21008A03E0 = { 110 | CreatedOnToolsVersion = 9.3; 111 | ProvisioningStyle = Automatic; 112 | }; 113 | }; 114 | }; 115 | buildConfigurationList = B694140020273E21008A03E0 /* Build configuration list for PBXProject "Singleton Pattern" */; 116 | compatibilityVersion = "Xcode 8.0"; 117 | developmentRegion = en; 118 | hasScannedForEncodings = 0; 119 | knownRegions = ( 120 | en, 121 | ); 122 | mainGroup = B69413FC20273E21008A03E0; 123 | productRefGroup = B694140620273E21008A03E0 /* Products */; 124 | projectDirPath = ""; 125 | projectRoot = ""; 126 | targets = ( 127 | B694140420273E21008A03E0 /* Singleton Pattern */, 128 | ); 129 | }; 130 | /* End PBXProject section */ 131 | 132 | /* Begin PBXSourcesBuildPhase section */ 133 | B694140120273E21008A03E0 /* Sources */ = { 134 | isa = PBXSourcesBuildPhase; 135 | buildActionMask = 2147483647; 136 | files = ( 137 | D79C9499206BDB67009A5E4D /* MyersSingleton.cpp in Sources */, 138 | D79C9496206BCCBB009A5E4D /* ClassicSingleton.cpp in Sources */, 139 | B694140920273E21008A03E0 /* main.cpp in Sources */, 140 | D79C949C206BDD0E009A5E4D /* ThreadSafeClassicSingleton.cpp in Sources */, 141 | ); 142 | runOnlyForDeploymentPostprocessing = 0; 143 | }; 144 | /* End PBXSourcesBuildPhase section */ 145 | 146 | /* Begin XCBuildConfiguration section */ 147 | B694140A20273E21008A03E0 /* Debug */ = { 148 | isa = XCBuildConfiguration; 149 | buildSettings = { 150 | ALWAYS_SEARCH_USER_PATHS = NO; 151 | CLANG_ANALYZER_NONNULL = YES; 152 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 153 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 154 | CLANG_CXX_LIBRARY = "libc++"; 155 | CLANG_ENABLE_MODULES = YES; 156 | CLANG_ENABLE_OBJC_ARC = YES; 157 | CLANG_ENABLE_OBJC_WEAK = YES; 158 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 159 | CLANG_WARN_BOOL_CONVERSION = YES; 160 | CLANG_WARN_COMMA = YES; 161 | CLANG_WARN_CONSTANT_CONVERSION = YES; 162 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 163 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 164 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 165 | CLANG_WARN_EMPTY_BODY = YES; 166 | CLANG_WARN_ENUM_CONVERSION = YES; 167 | CLANG_WARN_INFINITE_RECURSION = YES; 168 | CLANG_WARN_INT_CONVERSION = YES; 169 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 170 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 171 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 172 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 173 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 174 | CLANG_WARN_STRICT_PROTOTYPES = YES; 175 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 176 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 177 | CLANG_WARN_UNREACHABLE_CODE = YES; 178 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 179 | CODE_SIGN_IDENTITY = "-"; 180 | COPY_PHASE_STRIP = NO; 181 | DEBUG_INFORMATION_FORMAT = dwarf; 182 | ENABLE_STRICT_OBJC_MSGSEND = YES; 183 | ENABLE_TESTABILITY = YES; 184 | GCC_C_LANGUAGE_STANDARD = gnu11; 185 | GCC_DYNAMIC_NO_PIC = NO; 186 | GCC_NO_COMMON_BLOCKS = YES; 187 | GCC_OPTIMIZATION_LEVEL = 0; 188 | GCC_PREPROCESSOR_DEFINITIONS = ( 189 | "DEBUG=1", 190 | "$(inherited)", 191 | ); 192 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 193 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 194 | GCC_WARN_UNDECLARED_SELECTOR = YES; 195 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 196 | GCC_WARN_UNUSED_FUNCTION = YES; 197 | GCC_WARN_UNUSED_VARIABLE = YES; 198 | MACOSX_DEPLOYMENT_TARGET = 10.13; 199 | MTL_ENABLE_DEBUG_INFO = YES; 200 | ONLY_ACTIVE_ARCH = YES; 201 | SDKROOT = macosx; 202 | }; 203 | name = Debug; 204 | }; 205 | B694140B20273E21008A03E0 /* Release */ = { 206 | isa = XCBuildConfiguration; 207 | buildSettings = { 208 | ALWAYS_SEARCH_USER_PATHS = NO; 209 | CLANG_ANALYZER_NONNULL = YES; 210 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 211 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 212 | CLANG_CXX_LIBRARY = "libc++"; 213 | CLANG_ENABLE_MODULES = YES; 214 | CLANG_ENABLE_OBJC_ARC = YES; 215 | CLANG_ENABLE_OBJC_WEAK = YES; 216 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 217 | CLANG_WARN_BOOL_CONVERSION = YES; 218 | CLANG_WARN_COMMA = YES; 219 | CLANG_WARN_CONSTANT_CONVERSION = YES; 220 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 221 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 222 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 223 | CLANG_WARN_EMPTY_BODY = YES; 224 | CLANG_WARN_ENUM_CONVERSION = YES; 225 | CLANG_WARN_INFINITE_RECURSION = YES; 226 | CLANG_WARN_INT_CONVERSION = YES; 227 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 228 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 229 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 230 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 231 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 232 | CLANG_WARN_STRICT_PROTOTYPES = YES; 233 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 234 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 235 | CLANG_WARN_UNREACHABLE_CODE = YES; 236 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 237 | CODE_SIGN_IDENTITY = "-"; 238 | COPY_PHASE_STRIP = NO; 239 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 240 | ENABLE_NS_ASSERTIONS = NO; 241 | ENABLE_STRICT_OBJC_MSGSEND = YES; 242 | GCC_C_LANGUAGE_STANDARD = gnu11; 243 | GCC_NO_COMMON_BLOCKS = YES; 244 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 245 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 246 | GCC_WARN_UNDECLARED_SELECTOR = YES; 247 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 248 | GCC_WARN_UNUSED_FUNCTION = YES; 249 | GCC_WARN_UNUSED_VARIABLE = YES; 250 | MACOSX_DEPLOYMENT_TARGET = 10.13; 251 | MTL_ENABLE_DEBUG_INFO = NO; 252 | SDKROOT = macosx; 253 | }; 254 | name = Release; 255 | }; 256 | B694140D20273E21008A03E0 /* Debug */ = { 257 | isa = XCBuildConfiguration; 258 | buildSettings = { 259 | CODE_SIGN_STYLE = Automatic; 260 | MACOSX_DEPLOYMENT_TARGET = 10.12; 261 | PRODUCT_NAME = "$(TARGET_NAME)"; 262 | }; 263 | name = Debug; 264 | }; 265 | B694140E20273E21008A03E0 /* Release */ = { 266 | isa = XCBuildConfiguration; 267 | buildSettings = { 268 | CODE_SIGN_STYLE = Automatic; 269 | MACOSX_DEPLOYMENT_TARGET = 10.12; 270 | PRODUCT_NAME = "$(TARGET_NAME)"; 271 | }; 272 | name = Release; 273 | }; 274 | /* End XCBuildConfiguration section */ 275 | 276 | /* Begin XCConfigurationList section */ 277 | B694140020273E21008A03E0 /* Build configuration list for PBXProject "Singleton Pattern" */ = { 278 | isa = XCConfigurationList; 279 | buildConfigurations = ( 280 | B694140A20273E21008A03E0 /* Debug */, 281 | B694140B20273E21008A03E0 /* Release */, 282 | ); 283 | defaultConfigurationIsVisible = 0; 284 | defaultConfigurationName = Release; 285 | }; 286 | B694140C20273E21008A03E0 /* Build configuration list for PBXNativeTarget "Singleton Pattern" */ = { 287 | isa = XCConfigurationList; 288 | buildConfigurations = ( 289 | B694140D20273E21008A03E0 /* Debug */, 290 | B694140E20273E21008A03E0 /* Release */, 291 | ); 292 | defaultConfigurationIsVisible = 0; 293 | defaultConfigurationName = Release; 294 | }; 295 | /* End XCConfigurationList section */ 296 | }; 297 | rootObject = B69413FD20273E21008A03E0 /* Project object */; 298 | } 299 | -------------------------------------------------------------------------------- /Singleton Pattern/Singleton Pattern/ClassicSingleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ClassicSingleton.cpp 3 | // Singleton Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 3/28/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "ClassicSingleton.hpp" 10 | 11 | namespace classic { 12 | Singleton* Singleton::instance = nullptr; // memory leak 13 | 14 | Singleton* Singleton::GetInstance() 15 | { 16 | if(instance == nullptr) 17 | { 18 | instance = new Singleton(); 19 | } 20 | return instance; 21 | } 22 | 23 | Singleton2 Singleton2::instance; // global initialization 24 | 25 | Singleton2& Singleton2::GetInstance() 26 | { 27 | return instance; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Singleton Pattern/Singleton Pattern/ClassicSingleton.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ClassicSingleton.hpp 3 | // Singleton Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 3/28/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef ClassicSingleton_hpp 10 | #define ClassicSingleton_hpp 11 | 12 | namespace classic { 13 | class Singleton { 14 | public: 15 | static Singleton* GetInstance(); 16 | ~Singleton() = default; 17 | private: 18 | Singleton() = default; 19 | static Singleton* instance; 20 | }; 21 | 22 | class Singleton2 { 23 | public: 24 | static Singleton2& GetInstance(); 25 | ~Singleton2() = default; 26 | private: 27 | Singleton2() = default; 28 | static Singleton2 instance; 29 | }; 30 | } 31 | 32 | #endif /* ClassicSingleton_hpp */ 33 | -------------------------------------------------------------------------------- /Singleton Pattern/Singleton Pattern/MyersSingleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Cpp11Singleton.cpp 3 | // Singleton Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 3/28/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "MyersSingleton.hpp" 10 | 11 | MyersSingleton& MyersSingleton::GetInstance() 12 | { 13 | static MyersSingleton instance; 14 | return instance; 15 | } 16 | -------------------------------------------------------------------------------- /Singleton Pattern/Singleton Pattern/MyersSingleton.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Cpp11Singleton.hpp 3 | // Singleton Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 3/28/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef Cpp11Singleton_hpp 10 | #define Cpp11Singleton_hpp 11 | 12 | class MyersSingleton 13 | { 14 | public: 15 | static MyersSingleton& GetInstance(); 16 | ~MyersSingleton() = default; 17 | private: 18 | MyersSingleton() = default; 19 | }; 20 | 21 | #endif /* Cpp11Singleton_hpp */ 22 | -------------------------------------------------------------------------------- /Singleton Pattern/Singleton Pattern/ThreadSafeClassicSingleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ThreadSafeClassicSingleton.cpp 3 | // Singleton Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 3/28/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "ThreadSafeClassicSingleton.hpp" 10 | 11 | namespace threadssafe_classic { 12 | std::shared_ptr Singleton::instance; 13 | std::mutex Singleton::singletonMutex; 14 | 15 | std::shared_ptr Singleton::GetInstance() 16 | { 17 | if(instance == nullptr) 18 | { 19 | std::lock_guard lk(singletonMutex); 20 | if(instance == nullptr) 21 | { 22 | instance.reset(new Singleton()); 23 | } 24 | } 25 | return instance; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Singleton Pattern/Singleton Pattern/ThreadSafeClassicSingleton.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ThreadSafeClassicSingleton.hpp 3 | // Singleton Pattern 4 | // 5 | // Created by Abhinay Reddyreddy on 3/28/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #ifndef ThreadSafeClassicSingleton_hpp 10 | #define ThreadSafeClassicSingleton_hpp 11 | #include 12 | #include 13 | 14 | namespace threadssafe_classic { 15 | class Singleton { 16 | public: 17 | static std::shared_ptr GetInstance(); 18 | ~Singleton() = default; 19 | private: 20 | Singleton() = default; 21 | static std::shared_ptr instance; 22 | static std::mutex singletonMutex; 23 | }; 24 | } 25 | 26 | #endif /* ThreadSafeClassicSingleton_hpp */ 27 | -------------------------------------------------------------------------------- /Singleton Pattern/Singleton Pattern/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Singleton Pattern 4 | // 5 | // Created by Brian Arnold on 2/4/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "ClassicSingleton.hpp" 11 | #include "ThreadSafeClassicSingleton.hpp" 12 | #include "MyersSingleton.hpp" 13 | 14 | int main(int argc, const char * argv[]) { 15 | // insert code here... 16 | std::cout << "*************** Classic Singleton using pointer ********************" << std::endl; 17 | std::cout << "Singleton instance is " << classic::Singleton::GetInstance() << std::endl; 18 | std::cout << "Singleton instance is " << classic::Singleton::GetInstance() << std::endl; 19 | std::cout << "Singleton instance is " << classic::Singleton::GetInstance() << std::endl; 20 | std::cout << "Singleton instance is " << classic::Singleton::GetInstance() << std::endl; 21 | std::cout << "*************** Classic Singleton using reference *************" << std::endl; 22 | std::cout << "Singleton instance is " << &(classic::Singleton2::GetInstance()) << std::endl; 23 | std::cout << "Singleton instance is " << &(classic::Singleton2::GetInstance()) << std::endl; 24 | std::cout << "Singleton instance is " << &(classic::Singleton2::GetInstance()) << std::endl; 25 | std::cout << "Singleton instance is " << &(classic::Singleton2::GetInstance()) << std::endl; 26 | std::cout << "*************** Thread-safe classic Singleton using shared_ptr *************" << std::endl; 27 | std::cout << "Singleton instance is " << threadssafe_classic::Singleton::GetInstance() << std::endl; 28 | std::cout << "Singleton instance is " << threadssafe_classic::Singleton::GetInstance() << std::endl; 29 | std::cout << "Singleton instance is " << threadssafe_classic::Singleton::GetInstance() << std::endl; 30 | std::cout << "Singleton instance is " << threadssafe_classic::Singleton::GetInstance() << std::endl; 31 | std::cout << "*************** Myers Singleton using Cpp11 magic statics (thread-safe) *************" << std::endl; 32 | std::cout << "Singleton instance is " << &(MyersSingleton::GetInstance()) << std::endl; 33 | std::cout << "Singleton instance is " << &(MyersSingleton::GetInstance()) << std::endl; 34 | std::cout << "Singleton instance is " << &(MyersSingleton::GetInstance()) << std::endl; 35 | std::cout << "Singleton instance is " << &(MyersSingleton::GetInstance()) << std::endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B61A23191FFE859300FD7B18 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A23181FFE859300FD7B18 /* main.cpp */; }; 11 | B61A23271FFE885A00FD7B18 /* Duck.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A23251FFE885A00FD7B18 /* Duck.cpp */; }; 12 | B61A232A1FFE8CF800FD7B18 /* FlyNoWay.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A23281FFE8CF800FD7B18 /* FlyNoWay.cpp */; }; 13 | B61A232D1FFE8E4E00FD7B18 /* FlyRocketPowered.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A232B1FFE8E4E00FD7B18 /* FlyRocketPowered.cpp */; }; 14 | B61A23301FFE8EAA00FD7B18 /* FlyWithWings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A232E1FFE8EAA00FD7B18 /* FlyWithWings.cpp */; }; 15 | B61A23331FFE94B300FD7B18 /* MallardDuck.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A23311FFE94B300FD7B18 /* MallardDuck.cpp */; }; 16 | B61A23361FFE94BF00FD7B18 /* ModelDuck.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A23341FFE94BF00FD7B18 /* ModelDuck.cpp */; }; 17 | B61A23391FFE94CC00FD7B18 /* MuteQuack.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A23371FFE94CC00FD7B18 /* MuteQuack.cpp */; }; 18 | B61A233C1FFE94DA00FD7B18 /* RedHeadDuck.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A233A1FFE94DA00FD7B18 /* RedHeadDuck.cpp */; }; 19 | B61A233F1FFE94E300FD7B18 /* RubberDuck.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A233D1FFE94E300FD7B18 /* RubberDuck.cpp */; }; 20 | B61A23421FFE94F200FD7B18 /* Squeak.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A23401FFE94F200FD7B18 /* Squeak.cpp */; }; 21 | B61A23451FFE991800FD7B18 /* Quack.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A23431FFE991800FD7B18 /* Quack.cpp */; }; 22 | B61A23481FFEA63C00FD7B18 /* DecoyDuck.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B61A23461FFEA63C00FD7B18 /* DecoyDuck.cpp */; }; 23 | /* End PBXBuildFile section */ 24 | 25 | /* Begin PBXCopyFilesBuildPhase section */ 26 | B61A23131FFE859300FD7B18 /* CopyFiles */ = { 27 | isa = PBXCopyFilesBuildPhase; 28 | buildActionMask = 2147483647; 29 | dstPath = /usr/share/man/man1/; 30 | dstSubfolderSpec = 0; 31 | files = ( 32 | ); 33 | runOnlyForDeploymentPostprocessing = 1; 34 | }; 35 | /* End PBXCopyFilesBuildPhase section */ 36 | 37 | /* Begin PBXFileReference section */ 38 | B61A23151FFE859300FD7B18 /* Strategy Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Strategy Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 39 | B61A23181FFE859300FD7B18 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; 40 | B61A23201FFE865700FD7B18 /* FlyBehavior.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = FlyBehavior.hpp; sourceTree = ""; }; 41 | B61A23231FFE866200FD7B18 /* QuackBehavior.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = QuackBehavior.hpp; sourceTree = ""; }; 42 | B61A23251FFE885A00FD7B18 /* Duck.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Duck.cpp; sourceTree = ""; }; 43 | B61A23261FFE885A00FD7B18 /* Duck.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Duck.hpp; sourceTree = ""; }; 44 | B61A23281FFE8CF800FD7B18 /* FlyNoWay.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = FlyNoWay.cpp; sourceTree = ""; }; 45 | B61A23291FFE8CF800FD7B18 /* FlyNoWay.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = FlyNoWay.hpp; sourceTree = ""; }; 46 | B61A232B1FFE8E4E00FD7B18 /* FlyRocketPowered.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = FlyRocketPowered.cpp; sourceTree = ""; }; 47 | B61A232C1FFE8E4E00FD7B18 /* FlyRocketPowered.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = FlyRocketPowered.hpp; sourceTree = ""; }; 48 | B61A232E1FFE8EAA00FD7B18 /* FlyWithWings.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = FlyWithWings.cpp; sourceTree = ""; }; 49 | B61A232F1FFE8EAA00FD7B18 /* FlyWithWings.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = FlyWithWings.hpp; sourceTree = ""; }; 50 | B61A23311FFE94B300FD7B18 /* MallardDuck.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = MallardDuck.cpp; sourceTree = ""; }; 51 | B61A23321FFE94B300FD7B18 /* MallardDuck.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = MallardDuck.hpp; sourceTree = ""; }; 52 | B61A23341FFE94BF00FD7B18 /* ModelDuck.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ModelDuck.cpp; sourceTree = ""; }; 53 | B61A23351FFE94BF00FD7B18 /* ModelDuck.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ModelDuck.hpp; sourceTree = ""; }; 54 | B61A23371FFE94CC00FD7B18 /* MuteQuack.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = MuteQuack.cpp; sourceTree = ""; }; 55 | B61A23381FFE94CC00FD7B18 /* MuteQuack.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = MuteQuack.hpp; sourceTree = ""; }; 56 | B61A233A1FFE94DA00FD7B18 /* RedHeadDuck.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = RedHeadDuck.cpp; sourceTree = ""; }; 57 | B61A233B1FFE94DA00FD7B18 /* RedHeadDuck.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = RedHeadDuck.hpp; sourceTree = ""; }; 58 | B61A233D1FFE94E300FD7B18 /* RubberDuck.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = RubberDuck.cpp; sourceTree = ""; }; 59 | B61A233E1FFE94E300FD7B18 /* RubberDuck.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = RubberDuck.hpp; sourceTree = ""; }; 60 | B61A23401FFE94F200FD7B18 /* Squeak.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Squeak.cpp; sourceTree = ""; }; 61 | B61A23411FFE94F200FD7B18 /* Squeak.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Squeak.hpp; sourceTree = ""; }; 62 | B61A23431FFE991800FD7B18 /* Quack.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Quack.cpp; sourceTree = ""; }; 63 | B61A23441FFE991800FD7B18 /* Quack.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Quack.hpp; sourceTree = ""; }; 64 | B61A23461FFEA63C00FD7B18 /* DecoyDuck.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = DecoyDuck.cpp; sourceTree = ""; }; 65 | B61A23471FFEA63C00FD7B18 /* DecoyDuck.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = DecoyDuck.hpp; sourceTree = ""; }; 66 | /* End PBXFileReference section */ 67 | 68 | /* Begin PBXFrameworksBuildPhase section */ 69 | B61A23121FFE859300FD7B18 /* Frameworks */ = { 70 | isa = PBXFrameworksBuildPhase; 71 | buildActionMask = 2147483647; 72 | files = ( 73 | ); 74 | runOnlyForDeploymentPostprocessing = 0; 75 | }; 76 | /* End PBXFrameworksBuildPhase section */ 77 | 78 | /* Begin PBXGroup section */ 79 | B61A230C1FFE859300FD7B18 = { 80 | isa = PBXGroup; 81 | children = ( 82 | B61A23171FFE859300FD7B18 /* Strategy Pattern */, 83 | B61A23161FFE859300FD7B18 /* Products */, 84 | ); 85 | sourceTree = ""; 86 | }; 87 | B61A23161FFE859300FD7B18 /* Products */ = { 88 | isa = PBXGroup; 89 | children = ( 90 | B61A23151FFE859300FD7B18 /* Strategy Pattern */, 91 | ); 92 | name = Products; 93 | sourceTree = ""; 94 | }; 95 | B61A23171FFE859300FD7B18 /* Strategy Pattern */ = { 96 | isa = PBXGroup; 97 | children = ( 98 | B61A23181FFE859300FD7B18 /* main.cpp */, 99 | B61A23461FFEA63C00FD7B18 /* DecoyDuck.cpp */, 100 | B61A23471FFEA63C00FD7B18 /* DecoyDuck.hpp */, 101 | B61A23251FFE885A00FD7B18 /* Duck.cpp */, 102 | B61A23261FFE885A00FD7B18 /* Duck.hpp */, 103 | B61A23201FFE865700FD7B18 /* FlyBehavior.hpp */, 104 | B61A23281FFE8CF800FD7B18 /* FlyNoWay.cpp */, 105 | B61A23291FFE8CF800FD7B18 /* FlyNoWay.hpp */, 106 | B61A232B1FFE8E4E00FD7B18 /* FlyRocketPowered.cpp */, 107 | B61A232C1FFE8E4E00FD7B18 /* FlyRocketPowered.hpp */, 108 | B61A232E1FFE8EAA00FD7B18 /* FlyWithWings.cpp */, 109 | B61A232F1FFE8EAA00FD7B18 /* FlyWithWings.hpp */, 110 | B61A23311FFE94B300FD7B18 /* MallardDuck.cpp */, 111 | B61A23321FFE94B300FD7B18 /* MallardDuck.hpp */, 112 | B61A23341FFE94BF00FD7B18 /* ModelDuck.cpp */, 113 | B61A23351FFE94BF00FD7B18 /* ModelDuck.hpp */, 114 | B61A23371FFE94CC00FD7B18 /* MuteQuack.cpp */, 115 | B61A23381FFE94CC00FD7B18 /* MuteQuack.hpp */, 116 | B61A23431FFE991800FD7B18 /* Quack.cpp */, 117 | B61A23441FFE991800FD7B18 /* Quack.hpp */, 118 | B61A23231FFE866200FD7B18 /* QuackBehavior.hpp */, 119 | B61A233A1FFE94DA00FD7B18 /* RedHeadDuck.cpp */, 120 | B61A233B1FFE94DA00FD7B18 /* RedHeadDuck.hpp */, 121 | B61A233D1FFE94E300FD7B18 /* RubberDuck.cpp */, 122 | B61A233E1FFE94E300FD7B18 /* RubberDuck.hpp */, 123 | B61A23401FFE94F200FD7B18 /* Squeak.cpp */, 124 | B61A23411FFE94F200FD7B18 /* Squeak.hpp */, 125 | ); 126 | path = "Strategy Pattern"; 127 | sourceTree = ""; 128 | }; 129 | /* End PBXGroup section */ 130 | 131 | /* Begin PBXNativeTarget section */ 132 | B61A23141FFE859300FD7B18 /* Strategy Pattern */ = { 133 | isa = PBXNativeTarget; 134 | buildConfigurationList = B61A231C1FFE859300FD7B18 /* Build configuration list for PBXNativeTarget "Strategy Pattern" */; 135 | buildPhases = ( 136 | B61A23111FFE859300FD7B18 /* Sources */, 137 | B61A23121FFE859300FD7B18 /* Frameworks */, 138 | B61A23131FFE859300FD7B18 /* CopyFiles */, 139 | ); 140 | buildRules = ( 141 | ); 142 | dependencies = ( 143 | ); 144 | name = "Strategy Pattern"; 145 | productName = "Strategy Pattern"; 146 | productReference = B61A23151FFE859300FD7B18 /* Strategy Pattern */; 147 | productType = "com.apple.product-type.tool"; 148 | }; 149 | /* End PBXNativeTarget section */ 150 | 151 | /* Begin PBXProject section */ 152 | B61A230D1FFE859300FD7B18 /* Project object */ = { 153 | isa = PBXProject; 154 | attributes = { 155 | LastUpgradeCheck = 0930; 156 | ORGANIZATIONNAME = "Brian Arnold"; 157 | TargetAttributes = { 158 | B61A23141FFE859300FD7B18 = { 159 | CreatedOnToolsVersion = 9.2; 160 | ProvisioningStyle = Automatic; 161 | }; 162 | }; 163 | }; 164 | buildConfigurationList = B61A23101FFE859300FD7B18 /* Build configuration list for PBXProject "Strategy Pattern" */; 165 | compatibilityVersion = "Xcode 8.0"; 166 | developmentRegion = en; 167 | hasScannedForEncodings = 0; 168 | knownRegions = ( 169 | en, 170 | ); 171 | mainGroup = B61A230C1FFE859300FD7B18; 172 | productRefGroup = B61A23161FFE859300FD7B18 /* Products */; 173 | projectDirPath = ""; 174 | projectRoot = ""; 175 | targets = ( 176 | B61A23141FFE859300FD7B18 /* Strategy Pattern */, 177 | ); 178 | }; 179 | /* End PBXProject section */ 180 | 181 | /* Begin PBXSourcesBuildPhase section */ 182 | B61A23111FFE859300FD7B18 /* Sources */ = { 183 | isa = PBXSourcesBuildPhase; 184 | buildActionMask = 2147483647; 185 | files = ( 186 | B61A232A1FFE8CF800FD7B18 /* FlyNoWay.cpp in Sources */, 187 | B61A23271FFE885A00FD7B18 /* Duck.cpp in Sources */, 188 | B61A23361FFE94BF00FD7B18 /* ModelDuck.cpp in Sources */, 189 | B61A23451FFE991800FD7B18 /* Quack.cpp in Sources */, 190 | B61A23331FFE94B300FD7B18 /* MallardDuck.cpp in Sources */, 191 | B61A233F1FFE94E300FD7B18 /* RubberDuck.cpp in Sources */, 192 | B61A232D1FFE8E4E00FD7B18 /* FlyRocketPowered.cpp in Sources */, 193 | B61A23191FFE859300FD7B18 /* main.cpp in Sources */, 194 | B61A233C1FFE94DA00FD7B18 /* RedHeadDuck.cpp in Sources */, 195 | B61A23391FFE94CC00FD7B18 /* MuteQuack.cpp in Sources */, 196 | B61A23481FFEA63C00FD7B18 /* DecoyDuck.cpp in Sources */, 197 | B61A23421FFE94F200FD7B18 /* Squeak.cpp in Sources */, 198 | B61A23301FFE8EAA00FD7B18 /* FlyWithWings.cpp in Sources */, 199 | ); 200 | runOnlyForDeploymentPostprocessing = 0; 201 | }; 202 | /* End PBXSourcesBuildPhase section */ 203 | 204 | /* Begin XCBuildConfiguration section */ 205 | B61A231A1FFE859300FD7B18 /* Debug */ = { 206 | isa = XCBuildConfiguration; 207 | buildSettings = { 208 | ALWAYS_SEARCH_USER_PATHS = NO; 209 | CLANG_ANALYZER_NONNULL = YES; 210 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 211 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 212 | CLANG_CXX_LIBRARY = "libc++"; 213 | CLANG_ENABLE_MODULES = YES; 214 | CLANG_ENABLE_OBJC_ARC = YES; 215 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 216 | CLANG_WARN_BOOL_CONVERSION = YES; 217 | CLANG_WARN_COMMA = YES; 218 | CLANG_WARN_CONSTANT_CONVERSION = YES; 219 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 220 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 221 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 222 | CLANG_WARN_EMPTY_BODY = YES; 223 | CLANG_WARN_ENUM_CONVERSION = YES; 224 | CLANG_WARN_INFINITE_RECURSION = YES; 225 | CLANG_WARN_INT_CONVERSION = YES; 226 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 227 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 228 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 229 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 230 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 231 | CLANG_WARN_STRICT_PROTOTYPES = YES; 232 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 233 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 234 | CLANG_WARN_UNREACHABLE_CODE = YES; 235 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 236 | CODE_SIGN_IDENTITY = "-"; 237 | COPY_PHASE_STRIP = NO; 238 | DEBUG_INFORMATION_FORMAT = dwarf; 239 | ENABLE_STRICT_OBJC_MSGSEND = YES; 240 | ENABLE_TESTABILITY = YES; 241 | GCC_C_LANGUAGE_STANDARD = gnu11; 242 | GCC_DYNAMIC_NO_PIC = NO; 243 | GCC_NO_COMMON_BLOCKS = YES; 244 | GCC_OPTIMIZATION_LEVEL = 0; 245 | GCC_PREPROCESSOR_DEFINITIONS = ( 246 | "DEBUG=1", 247 | "$(inherited)", 248 | ); 249 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 250 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 251 | GCC_WARN_UNDECLARED_SELECTOR = YES; 252 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 253 | GCC_WARN_UNUSED_FUNCTION = YES; 254 | GCC_WARN_UNUSED_VARIABLE = YES; 255 | MACOSX_DEPLOYMENT_TARGET = 10.13; 256 | MTL_ENABLE_DEBUG_INFO = YES; 257 | ONLY_ACTIVE_ARCH = YES; 258 | SDKROOT = macosx; 259 | }; 260 | name = Debug; 261 | }; 262 | B61A231B1FFE859300FD7B18 /* Release */ = { 263 | isa = XCBuildConfiguration; 264 | buildSettings = { 265 | ALWAYS_SEARCH_USER_PATHS = NO; 266 | CLANG_ANALYZER_NONNULL = YES; 267 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 268 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 269 | CLANG_CXX_LIBRARY = "libc++"; 270 | CLANG_ENABLE_MODULES = YES; 271 | CLANG_ENABLE_OBJC_ARC = YES; 272 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 273 | CLANG_WARN_BOOL_CONVERSION = YES; 274 | CLANG_WARN_COMMA = YES; 275 | CLANG_WARN_CONSTANT_CONVERSION = YES; 276 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 277 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 278 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 279 | CLANG_WARN_EMPTY_BODY = YES; 280 | CLANG_WARN_ENUM_CONVERSION = YES; 281 | CLANG_WARN_INFINITE_RECURSION = YES; 282 | CLANG_WARN_INT_CONVERSION = YES; 283 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 284 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 285 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 286 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 287 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 288 | CLANG_WARN_STRICT_PROTOTYPES = YES; 289 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 290 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 291 | CLANG_WARN_UNREACHABLE_CODE = YES; 292 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 293 | CODE_SIGN_IDENTITY = "-"; 294 | COPY_PHASE_STRIP = NO; 295 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 296 | ENABLE_NS_ASSERTIONS = NO; 297 | ENABLE_STRICT_OBJC_MSGSEND = YES; 298 | GCC_C_LANGUAGE_STANDARD = gnu11; 299 | GCC_NO_COMMON_BLOCKS = YES; 300 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 301 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 302 | GCC_WARN_UNDECLARED_SELECTOR = YES; 303 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 304 | GCC_WARN_UNUSED_FUNCTION = YES; 305 | GCC_WARN_UNUSED_VARIABLE = YES; 306 | MACOSX_DEPLOYMENT_TARGET = 10.13; 307 | MTL_ENABLE_DEBUG_INFO = NO; 308 | SDKROOT = macosx; 309 | }; 310 | name = Release; 311 | }; 312 | B61A231D1FFE859300FD7B18 /* Debug */ = { 313 | isa = XCBuildConfiguration; 314 | buildSettings = { 315 | CLANG_ENABLE_OBJC_WEAK = YES; 316 | CODE_SIGN_STYLE = Automatic; 317 | PRODUCT_NAME = "$(TARGET_NAME)"; 318 | }; 319 | name = Debug; 320 | }; 321 | B61A231E1FFE859300FD7B18 /* Release */ = { 322 | isa = XCBuildConfiguration; 323 | buildSettings = { 324 | CLANG_ENABLE_OBJC_WEAK = YES; 325 | CODE_SIGN_STYLE = Automatic; 326 | PRODUCT_NAME = "$(TARGET_NAME)"; 327 | }; 328 | name = Release; 329 | }; 330 | /* End XCBuildConfiguration section */ 331 | 332 | /* Begin XCConfigurationList section */ 333 | B61A23101FFE859300FD7B18 /* Build configuration list for PBXProject "Strategy Pattern" */ = { 334 | isa = XCConfigurationList; 335 | buildConfigurations = ( 336 | B61A231A1FFE859300FD7B18 /* Debug */, 337 | B61A231B1FFE859300FD7B18 /* Release */, 338 | ); 339 | defaultConfigurationIsVisible = 0; 340 | defaultConfigurationName = Release; 341 | }; 342 | B61A231C1FFE859300FD7B18 /* Build configuration list for PBXNativeTarget "Strategy Pattern" */ = { 343 | isa = XCConfigurationList; 344 | buildConfigurations = ( 345 | B61A231D1FFE859300FD7B18 /* Debug */, 346 | B61A231E1FFE859300FD7B18 /* Release */, 347 | ); 348 | defaultConfigurationIsVisible = 0; 349 | defaultConfigurationName = Release; 350 | }; 351 | /* End XCConfigurationList section */ 352 | }; 353 | rootObject = B61A230D1FFE859300FD7B18 /* Project object */; 354 | } 355 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/DecoyDuck.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // DecoyDuck.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "DecoyDuck.hpp" 12 | 13 | #include "FlyNoWay.hpp" 14 | #include "MuteQuack.hpp" 15 | 16 | #include 17 | 18 | DecoyDuck::DecoyDuck() : 19 | Duck(std::make_unique(), std::make_unique()) { 20 | } 21 | 22 | void DecoyDuck::display() const { 23 | std::cout << "I'm just a decoy duck" << std::endl; 24 | } 25 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/DecoyDuck.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // DecoyDuck.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef DecoyDuck_hpp 12 | #define DecoyDuck_hpp 13 | 14 | #include "Duck.hpp" 15 | 16 | class DecoyDuck: public Duck { 17 | public: 18 | DecoyDuck(); 19 | 20 | void display() const override; 21 | }; 22 | 23 | 24 | #endif /* DecoyDuck_hpp */ 25 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/Duck.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Duck.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "Duck.hpp" 12 | 13 | #include "QuackBehavior.hpp" 14 | #include "FlyBehavior.hpp" 15 | 16 | #include 17 | 18 | Duck::Duck(std::unique_ptr flyBehavior, std::unique_ptr quackBehavior) : 19 | flyBehavior(std::move(flyBehavior)), 20 | quackBehavior(std::move(quackBehavior)) { 21 | } 22 | 23 | void Duck::performQuack() const { 24 | // Delegate to the behavior class. 25 | quackBehavior->quack(); 26 | } 27 | 28 | void Duck::swim() const { 29 | std::cout << "All ducks float, even decoys!" << std::endl; 30 | } 31 | 32 | void Duck::performFly() const { 33 | // Delegate to the behavior class. 34 | flyBehavior->fly(); 35 | } 36 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/Duck.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Duck.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef Duck_hpp 12 | #define Duck_hpp 13 | 14 | #include 15 | 16 | #include "FlyBehavior.hpp" 17 | #include "QuackBehavior.hpp" 18 | 19 | class Duck { 20 | // Declare two reference variables for the behavior interface types. All duck classes (in the same package) inherit these. 21 | public: 22 | // IN C++: prefer unique_ptr by default, shared_ptr when there are multiple references. 23 | std::unique_ptr flyBehavior; 24 | std::unique_ptr quackBehavior; 25 | 26 | Duck(std::unique_ptr flyBehavior, std::unique_ptr quackBehavior); 27 | 28 | // IN C++: a base class with virtual functions must also have a virtual destructor. 29 | virtual ~Duck() = default; 30 | 31 | void performQuack() const; 32 | 33 | void swim() const; 34 | 35 | virtual void display() const = 0; 36 | 37 | void performFly() const; 38 | }; 39 | 40 | #endif /* Duck_hpp */ 41 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/FlyBehavior.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // FlyBehavior.cpp 3 | // Strategy Pattern 4 | // 5 | // Created by Brian Arnold on 1/4/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "FlyBehavior.hpp" 10 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/FlyBehavior.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // FlyBehavior.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef FlyBehavior_hpp 12 | #define FlyBehavior_hpp 13 | 14 | /// FlyBehavior is an interface that all flying classes implement. All new flying classes just need to implement the fly() method. 15 | class FlyBehavior { 16 | public: 17 | virtual void fly() const = 0; 18 | 19 | // IN C++: a base class with virtual functions must also have a virtual destructor. 20 | virtual ~FlyBehavior() = default; 21 | }; 22 | 23 | #endif /* FlyBehavior_hpp */ 24 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/FlyNoWay.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // FlyNoWay.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "FlyNoWay.hpp" 12 | 13 | #include 14 | 15 | void FlyNoWay::fly() const { 16 | std::cout << "I can't fly!" << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/FlyNoWay.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // FlyNoWay.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef FlyNoWay_hpp 12 | #define FlyNoWay_hpp 13 | 14 | #include "FlyBehavior.hpp" 15 | 16 | /// And here's the implementation for all ducks that can't fly. 17 | class FlyNoWay: public FlyBehavior { 18 | public: 19 | void fly() const override; 20 | }; 21 | 22 | #endif /* FlyNoWay_hpp */ 23 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/FlyRocketPowered.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // FlyRocketPowered.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "FlyRocketPowered.hpp" 12 | 13 | #include 14 | 15 | void FlyRocketPowered::fly() const { 16 | std::cout << "I'm flying with a rocket!" << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/FlyRocketPowered.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // FlyRocketPowered.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef FlyRocketPowered_hpp 12 | #define FlyRocketPowered_hpp 13 | 14 | #include "FlyBehavior.hpp" 15 | 16 | // That's okay, we're creating a rocket-powered flying behavior. 17 | class FlyRocketPowered: public FlyBehavior { 18 | public: 19 | void fly() const override; 20 | }; 21 | 22 | #endif /* FlyRocketPowered_hpp */ 23 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/FlyWithWings.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // FlyWithWings.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "FlyWithWings.hpp" 12 | 13 | #include 14 | 15 | void FlyWithWings::fly() const { 16 | std::cout << "I'm flying!" << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/FlyWithWings.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // FlyWithWings.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef FlyWithWings_hpp 12 | #define FlyWithWings_hpp 13 | 14 | #include "FlyBehavior.hpp" 15 | 16 | // Here's the implementation of flying for all ducks that have wings. 17 | class FlyWithWings: public FlyBehavior { 18 | public: 19 | virtual void fly() const override; 20 | }; 21 | 22 | #endif /* FlyWithWings_hpp */ 23 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/MallardDuck.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // MallardDuck.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "MallardDuck.hpp" 12 | 13 | #include "FlyWithWings.hpp" 14 | #include "Quack.hpp" 15 | 16 | #include 17 | 18 | MallardDuck::MallardDuck() : 19 | Duck(std::make_unique(), std::make_unique()) { 20 | } 21 | 22 | void MallardDuck::display() const { 23 | std::cout << "I'm a real Mallard duck" << std::endl; 24 | } 25 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/MallardDuck.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // MallardDuck.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef MallardDuck_hpp 12 | #define MallardDuck_hpp 13 | 14 | #include "Duck.hpp" 15 | 16 | // A MallardDuck uses the Quack class to handle its quack, so when performQuack() is called, the responsibility for the quack is delegated to the Quack object and we get a real quack. 17 | // And it uses FlyWithWings as its FlyBehavior type. 18 | // Remember, MallardDuck inherits the quackBehavior and flyBehavior instance variables from class Duck. 19 | class MallardDuck: public Duck { 20 | public: 21 | MallardDuck(); 22 | 23 | void display() const override; 24 | }; 25 | 26 | #endif /* MallardDuck_hpp */ 27 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/ModelDuck.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ModelDuck.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "ModelDuck.hpp" 12 | 13 | #include "FlyNoWay.hpp" 14 | #include "Quack.hpp" 15 | 16 | #include 17 | 18 | ModelDuck::ModelDuck() : 19 | Duck(std::make_unique(), std::make_unique()) { 20 | } 21 | 22 | void ModelDuck::display() const { 23 | std::cout << "I'm a model duck" << std::endl; 24 | } 25 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/ModelDuck.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ModelDuck.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef ModelDuck_hpp 12 | #define ModelDuck_hpp 13 | 14 | #include "Duck.hpp" 15 | 16 | // Our model duck begins life grounded, without a way to fly. 17 | class ModelDuck: public Duck { 18 | public: 19 | ModelDuck(); 20 | 21 | void display() const override; 22 | }; 23 | 24 | #endif /* ModelDuck_hpp */ 25 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/MuteQuack.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // MuteQuack.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "MuteQuack.hpp" 12 | 13 | #include 14 | 15 | void MuteQuack::quack() const { 16 | std::cout << "<< Silence >>" << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/MuteQuack.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // MuteQuack.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef MuteQuack_hpp 12 | #define MuteQuack_hpp 13 | 14 | #include "QuackBehavior.hpp" 15 | 16 | // Quacks that make no sound at all. 17 | class MuteQuack: public QuackBehavior { 18 | public: 19 | void quack() const override; 20 | }; 21 | 22 | 23 | #endif /* MuteQuack_hpp */ 24 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/Quack.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Quack.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "Quack.hpp" 12 | 13 | #include 14 | 15 | void Quack::quack() const { 16 | std::cout << "Quack" << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/Quack.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Quack.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef Quack_hpp 12 | #define Quack_hpp 13 | 14 | #include "QuackBehavior.hpp" 15 | 16 | // Quacks that really quack. 17 | class Quack: public QuackBehavior { 18 | public: 19 | void quack() const override; 20 | }; 21 | 22 | #endif /* Quack_hpp */ 23 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/QuackBehavior.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // QuackBehavior.cpp 3 | // Strategy Pattern 4 | // 5 | // Created by Brian Arnold on 1/4/18. 6 | // Copyright © 2018 Brian Arnold. All rights reserved. 7 | // 8 | 9 | #include "QuackBehavior.hpp" 10 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/QuackBehavior.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // QuackBehavior.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef QuackBehavior_hpp 12 | #define QuackBehavior_hpp 13 | 14 | // Same thing here for the quack behavior, we have an interface that just includes a quack() method that needs to be implemented. 15 | class QuackBehavior { 16 | public: 17 | virtual void quack() const = 0; 18 | 19 | // IN C++: a base class with virtual functions must also have a virtual destructor. 20 | virtual ~QuackBehavior() = default; 21 | }; 22 | 23 | #endif /* QuackBehavior_hpp */ 24 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/RedHeadDuck.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // RedHeadDuck.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "RedHeadDuck.hpp" 12 | 13 | #include "FlyWithWings.hpp" 14 | #include "Quack.hpp" 15 | 16 | #include 17 | 18 | RedHeadDuck::RedHeadDuck() : 19 | Duck(std::make_unique(), std::make_unique()) { 20 | } 21 | 22 | void RedHeadDuck::display() const { 23 | std::cout << "I'm a real Red Headed duck" << std::endl; 24 | } 25 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/RedHeadDuck.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // RedHeadDuck.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef RedHeadDuck_hpp 12 | #define RedHeadDuck_hpp 13 | 14 | #include "Duck.hpp" 15 | 16 | class RedHeadDuck: public Duck { 17 | public: 18 | RedHeadDuck(); 19 | 20 | void display() const override; 21 | }; 22 | 23 | #endif /* RedHeadDuck_hpp */ 24 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/RubberDuck.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // RubberDuck.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "RubberDuck.hpp" 12 | 13 | #include "FlyNoWay.hpp" 14 | #include "Squeak.hpp" 15 | 16 | #include 17 | 18 | RubberDuck::RubberDuck() : 19 | Duck(std::make_unique(), std::make_unique()) { 20 | } 21 | 22 | void RubberDuck::display() const { 23 | std::cout << "I'm rubber duckie" << std::endl; 24 | } 25 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/RubberDuck.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // RubberDuck.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef RubberDuck_hpp 12 | #define RubberDuck_hpp 13 | 14 | #include "Duck.hpp" 15 | 16 | class RubberDuck: public Duck { 17 | public: 18 | RubberDuck(); 19 | 20 | void display() const override; 21 | }; 22 | 23 | #endif /* RubberDuck_hpp */ 24 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/Squeak.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Squeak.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "Squeak.hpp" 12 | 13 | #include 14 | 15 | void Squeak::quack() const { 16 | std::cout << "Squeak" << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/Squeak.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Squeak.hpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #ifndef Squeak_hpp 12 | #define Squeak_hpp 13 | 14 | #include "QuackBehavior.hpp" 15 | 16 | // Quacks that really squeak. 17 | class Squeak: public QuackBehavior { 18 | public: 19 | void quack() const override; 20 | }; 21 | 22 | #endif /* Squeak_hpp */ 23 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Strategy Pattern 4 | // 5 | // Based on "Head First Design Patterns," Freeman & Robson, O'Reilly. 6 | // 7 | // Created by Brian Arnold on 1/4/18. 8 | // Copyright © 2018 Brian Arnold. All rights reserved. 9 | // 10 | 11 | #include "MallardDuck.hpp" 12 | #include "RedHeadDuck.hpp" 13 | #include "DecoyDuck.hpp" 14 | #include "ModelDuck.hpp" 15 | #include "RubberDuck.hpp" 16 | 17 | #include "FlyRocketPowered.hpp" 18 | 19 | #include 20 | 21 | int main(int argc, const char * argv[]) { 22 | // IN C++: use stack-based classes when possible. 23 | MallardDuck mallard; 24 | mallard.display(); 25 | mallard.swim(); 26 | mallard.performQuack(); 27 | mallard.performFly(); 28 | 29 | std::cout << std::endl; 30 | 31 | RedHeadDuck redHead; 32 | redHead.display(); 33 | redHead.swim(); 34 | redHead.performQuack(); 35 | redHead.performFly(); 36 | 37 | std::cout << std::endl; 38 | 39 | DecoyDuck decoy; 40 | decoy.display(); 41 | decoy.swim(); 42 | decoy.performQuack(); 43 | decoy.performFly(); 44 | 45 | std::cout << std::endl; 46 | 47 | RubberDuck rubberDuckie; 48 | rubberDuckie.display(); 49 | rubberDuckie.swim(); 50 | rubberDuckie.performQuack(); 51 | rubberDuckie.performFly(); 52 | 53 | std::cout << std::endl; 54 | 55 | ModelDuck model; 56 | model.display(); 57 | model.swim(); 58 | model.performQuack(); 59 | // First call to performFly() delegates to the flyBehavior object set in the ModelDuck's constructor, which is a FlyNoWay instance. 60 | model.performFly(); 61 | // This invokes the model's inherited behavior setter method, and... voilà! The model suddenly has rocket-powered flying capability! 62 | model.flyBehavior = std::make_unique(); 63 | // If it worked, the model duck dynamically changed its flying behavior! You can't do THAT if the implementation lives inside the Duck class. 64 | model.performFly(); 65 | 66 | return 0; 67 | } 68 | --------------------------------------------------------------------------------