├── .gitattributes ├── LICENSE ├── README.md ├── Swift Factory Design Pattern.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── softwaretesting.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── softwaretesting.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ └── xcschememanagement.plist └── Swift Factory Design Pattern ├── AppDelegate.swift ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── ShapeFactory.swift └── ViewController.swift /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Simon Ng 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Design pattern in Swift: Factory Method 2 | 3 | For the full tutorial, please visit the following link: 4 | 5 | https://www.appcoda.com/design-pattern-creational/ 6 | -------------------------------------------------------------------------------- /Swift Factory Design Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 02D0F70820FA659F009EF440 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02D0F70720FA659F009EF440 /* AppDelegate.swift */; }; 11 | 02D0F70A20FA659F009EF440 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02D0F70920FA659F009EF440 /* ViewController.swift */; }; 12 | 02D0F70D20FA659F009EF440 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 02D0F70B20FA659F009EF440 /* Main.storyboard */; }; 13 | 02D0F70F20FA659F009EF440 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 02D0F70E20FA659F009EF440 /* Assets.xcassets */; }; 14 | 02D0F71220FA659F009EF440 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 02D0F71020FA659F009EF440 /* LaunchScreen.storyboard */; }; 15 | 02D0F71A20FA72A5009EF440 /* ShapeFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02D0F71920FA72A5009EF440 /* ShapeFactory.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 02D0F70420FA659F009EF440 /* Swift Factory Design Pattern.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Swift Factory Design Pattern.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | 02D0F70720FA659F009EF440 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 21 | 02D0F70920FA659F009EF440 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 22 | 02D0F70C20FA659F009EF440 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 23 | 02D0F70E20FA659F009EF440 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | 02D0F71120FA659F009EF440 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 25 | 02D0F71320FA659F009EF440 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 26 | 02D0F71920FA72A5009EF440 /* ShapeFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShapeFactory.swift; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | 02D0F70120FA659F009EF440 /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | 02D0F6FB20FA659F009EF440 = { 41 | isa = PBXGroup; 42 | children = ( 43 | 02D0F70620FA659F009EF440 /* Swift Factory Design Pattern */, 44 | 02D0F70520FA659F009EF440 /* Products */, 45 | ); 46 | sourceTree = ""; 47 | }; 48 | 02D0F70520FA659F009EF440 /* Products */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | 02D0F70420FA659F009EF440 /* Swift Factory Design Pattern.app */, 52 | ); 53 | name = Products; 54 | sourceTree = ""; 55 | }; 56 | 02D0F70620FA659F009EF440 /* Swift Factory Design Pattern */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | 02D0F70720FA659F009EF440 /* AppDelegate.swift */, 60 | 02D0F70920FA659F009EF440 /* ViewController.swift */, 61 | 02D0F71920FA72A5009EF440 /* ShapeFactory.swift */, 62 | 02D0F70B20FA659F009EF440 /* Main.storyboard */, 63 | 02D0F70E20FA659F009EF440 /* Assets.xcassets */, 64 | 02D0F71020FA659F009EF440 /* LaunchScreen.storyboard */, 65 | 02D0F71320FA659F009EF440 /* Info.plist */, 66 | ); 67 | path = "Swift Factory Design Pattern"; 68 | sourceTree = ""; 69 | }; 70 | /* End PBXGroup section */ 71 | 72 | /* Begin PBXNativeTarget section */ 73 | 02D0F70320FA659F009EF440 /* Swift Factory Design Pattern */ = { 74 | isa = PBXNativeTarget; 75 | buildConfigurationList = 02D0F71620FA659F009EF440 /* Build configuration list for PBXNativeTarget "Swift Factory Design Pattern" */; 76 | buildPhases = ( 77 | 02D0F70020FA659F009EF440 /* Sources */, 78 | 02D0F70120FA659F009EF440 /* Frameworks */, 79 | 02D0F70220FA659F009EF440 /* Resources */, 80 | ); 81 | buildRules = ( 82 | ); 83 | dependencies = ( 84 | ); 85 | name = "Swift Factory Design Pattern"; 86 | productName = "Swift Factory Design Pattern"; 87 | productReference = 02D0F70420FA659F009EF440 /* Swift Factory Design Pattern.app */; 88 | productType = "com.apple.product-type.application"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | 02D0F6FC20FA659F009EF440 /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | LastSwiftUpdateCheck = 0920; 97 | LastUpgradeCheck = 0920; 98 | ORGANIZATIONNAME = "Andrew L. Jaffee"; 99 | TargetAttributes = { 100 | 02D0F70320FA659F009EF440 = { 101 | CreatedOnToolsVersion = 9.2; 102 | ProvisioningStyle = Automatic; 103 | }; 104 | }; 105 | }; 106 | buildConfigurationList = 02D0F6FF20FA659F009EF440 /* Build configuration list for PBXProject "Swift Factory Design Pattern" */; 107 | compatibilityVersion = "Xcode 8.0"; 108 | developmentRegion = en; 109 | hasScannedForEncodings = 0; 110 | knownRegions = ( 111 | en, 112 | Base, 113 | ); 114 | mainGroup = 02D0F6FB20FA659F009EF440; 115 | productRefGroup = 02D0F70520FA659F009EF440 /* Products */; 116 | projectDirPath = ""; 117 | projectRoot = ""; 118 | targets = ( 119 | 02D0F70320FA659F009EF440 /* Swift Factory Design Pattern */, 120 | ); 121 | }; 122 | /* End PBXProject section */ 123 | 124 | /* Begin PBXResourcesBuildPhase section */ 125 | 02D0F70220FA659F009EF440 /* Resources */ = { 126 | isa = PBXResourcesBuildPhase; 127 | buildActionMask = 2147483647; 128 | files = ( 129 | 02D0F71220FA659F009EF440 /* LaunchScreen.storyboard in Resources */, 130 | 02D0F70F20FA659F009EF440 /* Assets.xcassets in Resources */, 131 | 02D0F70D20FA659F009EF440 /* Main.storyboard in Resources */, 132 | ); 133 | runOnlyForDeploymentPostprocessing = 0; 134 | }; 135 | /* End PBXResourcesBuildPhase section */ 136 | 137 | /* Begin PBXSourcesBuildPhase section */ 138 | 02D0F70020FA659F009EF440 /* Sources */ = { 139 | isa = PBXSourcesBuildPhase; 140 | buildActionMask = 2147483647; 141 | files = ( 142 | 02D0F71A20FA72A5009EF440 /* ShapeFactory.swift in Sources */, 143 | 02D0F70A20FA659F009EF440 /* ViewController.swift in Sources */, 144 | 02D0F70820FA659F009EF440 /* AppDelegate.swift in Sources */, 145 | ); 146 | runOnlyForDeploymentPostprocessing = 0; 147 | }; 148 | /* End PBXSourcesBuildPhase section */ 149 | 150 | /* Begin PBXVariantGroup section */ 151 | 02D0F70B20FA659F009EF440 /* Main.storyboard */ = { 152 | isa = PBXVariantGroup; 153 | children = ( 154 | 02D0F70C20FA659F009EF440 /* Base */, 155 | ); 156 | name = Main.storyboard; 157 | sourceTree = ""; 158 | }; 159 | 02D0F71020FA659F009EF440 /* LaunchScreen.storyboard */ = { 160 | isa = PBXVariantGroup; 161 | children = ( 162 | 02D0F71120FA659F009EF440 /* Base */, 163 | ); 164 | name = LaunchScreen.storyboard; 165 | sourceTree = ""; 166 | }; 167 | /* End PBXVariantGroup section */ 168 | 169 | /* Begin XCBuildConfiguration section */ 170 | 02D0F71420FA659F009EF440 /* Debug */ = { 171 | isa = XCBuildConfiguration; 172 | buildSettings = { 173 | ALWAYS_SEARCH_USER_PATHS = NO; 174 | CLANG_ANALYZER_NONNULL = YES; 175 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 176 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 177 | CLANG_CXX_LIBRARY = "libc++"; 178 | CLANG_ENABLE_MODULES = YES; 179 | CLANG_ENABLE_OBJC_ARC = YES; 180 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 181 | CLANG_WARN_BOOL_CONVERSION = YES; 182 | CLANG_WARN_COMMA = YES; 183 | CLANG_WARN_CONSTANT_CONVERSION = YES; 184 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 185 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 186 | CLANG_WARN_EMPTY_BODY = YES; 187 | CLANG_WARN_ENUM_CONVERSION = YES; 188 | CLANG_WARN_INFINITE_RECURSION = YES; 189 | CLANG_WARN_INT_CONVERSION = YES; 190 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 191 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 192 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 193 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 194 | CLANG_WARN_STRICT_PROTOTYPES = YES; 195 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 196 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 197 | CLANG_WARN_UNREACHABLE_CODE = YES; 198 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 199 | CODE_SIGN_IDENTITY = "iPhone Developer"; 200 | COPY_PHASE_STRIP = NO; 201 | DEBUG_INFORMATION_FORMAT = dwarf; 202 | ENABLE_STRICT_OBJC_MSGSEND = YES; 203 | ENABLE_TESTABILITY = YES; 204 | GCC_C_LANGUAGE_STANDARD = gnu11; 205 | GCC_DYNAMIC_NO_PIC = NO; 206 | GCC_NO_COMMON_BLOCKS = YES; 207 | GCC_OPTIMIZATION_LEVEL = 0; 208 | GCC_PREPROCESSOR_DEFINITIONS = ( 209 | "DEBUG=1", 210 | "$(inherited)", 211 | ); 212 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 213 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 214 | GCC_WARN_UNDECLARED_SELECTOR = YES; 215 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 216 | GCC_WARN_UNUSED_FUNCTION = YES; 217 | GCC_WARN_UNUSED_VARIABLE = YES; 218 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 219 | MTL_ENABLE_DEBUG_INFO = YES; 220 | ONLY_ACTIVE_ARCH = YES; 221 | SDKROOT = iphoneos; 222 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 223 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 224 | }; 225 | name = Debug; 226 | }; 227 | 02D0F71520FA659F009EF440 /* Release */ = { 228 | isa = XCBuildConfiguration; 229 | buildSettings = { 230 | ALWAYS_SEARCH_USER_PATHS = NO; 231 | CLANG_ANALYZER_NONNULL = YES; 232 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 233 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 234 | CLANG_CXX_LIBRARY = "libc++"; 235 | CLANG_ENABLE_MODULES = YES; 236 | CLANG_ENABLE_OBJC_ARC = YES; 237 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 238 | CLANG_WARN_BOOL_CONVERSION = YES; 239 | CLANG_WARN_COMMA = YES; 240 | CLANG_WARN_CONSTANT_CONVERSION = YES; 241 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 242 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 243 | CLANG_WARN_EMPTY_BODY = YES; 244 | CLANG_WARN_ENUM_CONVERSION = YES; 245 | CLANG_WARN_INFINITE_RECURSION = YES; 246 | CLANG_WARN_INT_CONVERSION = YES; 247 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 248 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 249 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 250 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 251 | CLANG_WARN_STRICT_PROTOTYPES = YES; 252 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 253 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 254 | CLANG_WARN_UNREACHABLE_CODE = YES; 255 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 256 | CODE_SIGN_IDENTITY = "iPhone Developer"; 257 | COPY_PHASE_STRIP = NO; 258 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 259 | ENABLE_NS_ASSERTIONS = NO; 260 | ENABLE_STRICT_OBJC_MSGSEND = YES; 261 | GCC_C_LANGUAGE_STANDARD = gnu11; 262 | GCC_NO_COMMON_BLOCKS = YES; 263 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 264 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 265 | GCC_WARN_UNDECLARED_SELECTOR = YES; 266 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 267 | GCC_WARN_UNUSED_FUNCTION = YES; 268 | GCC_WARN_UNUSED_VARIABLE = YES; 269 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 270 | MTL_ENABLE_DEBUG_INFO = NO; 271 | SDKROOT = iphoneos; 272 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 273 | VALIDATE_PRODUCT = YES; 274 | }; 275 | name = Release; 276 | }; 277 | 02D0F71720FA659F009EF440 /* Debug */ = { 278 | isa = XCBuildConfiguration; 279 | buildSettings = { 280 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 281 | CODE_SIGN_STYLE = Automatic; 282 | DEVELOPMENT_TEAM = ""; 283 | INFOPLIST_FILE = "Swift Factory Design Pattern/Info.plist"; 284 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 285 | PRODUCT_BUNDLE_IDENTIFIER = "com.yourRealDomainName.Swift-Factory-Design-Pattern"; 286 | PRODUCT_NAME = "$(TARGET_NAME)"; 287 | SWIFT_VERSION = 4.0; 288 | TARGETED_DEVICE_FAMILY = "1,2"; 289 | }; 290 | name = Debug; 291 | }; 292 | 02D0F71820FA659F009EF440 /* Release */ = { 293 | isa = XCBuildConfiguration; 294 | buildSettings = { 295 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 296 | CODE_SIGN_STYLE = Automatic; 297 | DEVELOPMENT_TEAM = ""; 298 | INFOPLIST_FILE = "Swift Factory Design Pattern/Info.plist"; 299 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 300 | PRODUCT_BUNDLE_IDENTIFIER = "com.yourRealDomainName.Swift-Factory-Design-Pattern"; 301 | PRODUCT_NAME = "$(TARGET_NAME)"; 302 | SWIFT_VERSION = 4.0; 303 | TARGETED_DEVICE_FAMILY = "1,2"; 304 | }; 305 | name = Release; 306 | }; 307 | /* End XCBuildConfiguration section */ 308 | 309 | /* Begin XCConfigurationList section */ 310 | 02D0F6FF20FA659F009EF440 /* Build configuration list for PBXProject "Swift Factory Design Pattern" */ = { 311 | isa = XCConfigurationList; 312 | buildConfigurations = ( 313 | 02D0F71420FA659F009EF440 /* Debug */, 314 | 02D0F71520FA659F009EF440 /* Release */, 315 | ); 316 | defaultConfigurationIsVisible = 0; 317 | defaultConfigurationName = Release; 318 | }; 319 | 02D0F71620FA659F009EF440 /* Build configuration list for PBXNativeTarget "Swift Factory Design Pattern" */ = { 320 | isa = XCConfigurationList; 321 | buildConfigurations = ( 322 | 02D0F71720FA659F009EF440 /* Debug */, 323 | 02D0F71820FA659F009EF440 /* Release */, 324 | ); 325 | defaultConfigurationIsVisible = 0; 326 | defaultConfigurationName = Release; 327 | }; 328 | /* End XCConfigurationList section */ 329 | }; 330 | rootObject = 02D0F6FC20FA659F009EF440 /* Project object */; 331 | } 332 | -------------------------------------------------------------------------------- /Swift Factory Design Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Swift Factory Design Pattern.xcodeproj/project.xcworkspace/xcuserdata/softwaretesting.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/appcoda/FactoryMethodInSwift/3fc49b9f3185547afb33da681d66b49f0c684827/Swift Factory Design Pattern.xcodeproj/project.xcworkspace/xcuserdata/softwaretesting.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Swift Factory Design Pattern.xcodeproj/xcuserdata/softwaretesting.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /Swift Factory Design Pattern.xcodeproj/xcuserdata/softwaretesting.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Swift Factory Design Pattern.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Swift Factory Design Pattern/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Swift Factory Design Pattern 4 | // 5 | // Created by Andrew L. Jaffee on 7/14/18. 6 | // 7 | /* 8 | 9 | Copyright (c) 2017-2018 Andrew L. Jaffee, microIT Infrastructure, LLC, and 10 | iosbrain.com. 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy 13 | of this software and associated documentation files (the "Software"), to deal 14 | in the Software without restriction, including without limitation the rights 15 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | copies of the Software, and to permit persons to whom the Software is 17 | furnished to do so, subject to the following conditions: 18 | 19 | The above copyright notice and this permission notice shall be included in all 20 | copies or substantial portions of the Software. 21 | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 | SOFTWARE. 29 | 30 | */ 31 | 32 | import UIKit 33 | 34 | @UIApplicationMain 35 | class AppDelegate: UIResponder, UIApplicationDelegate { 36 | 37 | var window: UIWindow? 38 | 39 | 40 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 41 | // Override point for customization after application launch. 42 | return true 43 | } 44 | 45 | func applicationWillResignActive(_ application: UIApplication) { 46 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 47 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 48 | } 49 | 50 | func applicationDidEnterBackground(_ application: UIApplication) { 51 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 52 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 53 | } 54 | 55 | func applicationWillEnterForeground(_ application: UIApplication) { 56 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 57 | } 58 | 59 | func applicationDidBecomeActive(_ application: UIApplication) { 60 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 61 | } 62 | 63 | func applicationWillTerminate(_ application: UIApplication) { 64 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 65 | } 66 | 67 | 68 | } 69 | 70 | -------------------------------------------------------------------------------- /Swift Factory Design Pattern/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | } 88 | ], 89 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /Swift Factory Design Pattern/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Swift Factory Design Pattern/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 31 | 38 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Swift Factory Design Pattern/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Swift Factory Design Pattern/ShapeFactory.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ShapeFactory.swift 3 | // Swift Factory Design Pattern 4 | // 5 | // Created by Andrew L. Jaffee on 7/14/18. 6 | // 7 | /* 8 | 9 | Copyright (c) 2017-2018 Andrew L. Jaffee, microIT Infrastructure, LLC, and 10 | iosbrain.com. 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy 13 | of this software and associated documentation files (the "Software"), to deal 14 | in the Software without restriction, including without limitation the rights 15 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | copies of the Software, and to permit persons to whom the Software is 17 | furnished to do so, subject to the following conditions: 18 | 19 | The above copyright notice and this permission notice shall be included in all 20 | copies or substantial portions of the Software. 21 | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 | SOFTWARE. 29 | 30 | */ 31 | 32 | import Foundation 33 | 34 | import UIKit 35 | 36 | // these values have been pre-selected by 37 | // the graphics and design teams 38 | let defaultHeight = 200 39 | let defaultColor = UIColor.blue 40 | 41 | protocol HelperViewFactoryProtocol { 42 | 43 | func configure() 44 | func position() 45 | func display() 46 | var height: Int { get } 47 | var view: UIView { get } 48 | var parentView: UIView { get } 49 | 50 | } 51 | 52 | fileprivate class Square: HelperViewFactoryProtocol { 53 | 54 | let height: Int 55 | let parentView: UIView 56 | var view: UIView 57 | 58 | init(height: Int = defaultHeight, parentView: UIView) { 59 | 60 | self.height = height 61 | self.parentView = parentView 62 | view = UIView() 63 | 64 | } 65 | 66 | func configure() { 67 | 68 | let frame = CGRect(x: 0, y: 0, width: height, height: height) 69 | view.frame = frame 70 | view.backgroundColor = defaultColor 71 | 72 | } 73 | 74 | func position() { 75 | 76 | view.center = parentView.center 77 | 78 | } 79 | 80 | func display() { 81 | 82 | configure() 83 | position() 84 | parentView.addSubview(view) 85 | 86 | } 87 | 88 | } // end class Square 89 | 90 | fileprivate class Circle : Square { 91 | 92 | override func configure() { 93 | 94 | super.configure() 95 | 96 | view.layer.cornerRadius = view.frame.width / 2 97 | view.layer.masksToBounds = true 98 | 99 | } 100 | 101 | } // end class Circle 102 | 103 | fileprivate class Rectangle : Square { 104 | 105 | override func configure() { 106 | 107 | let frame = CGRect(x: 0, y: 0, width: height + height/2, height: height) 108 | view.frame = frame 109 | view.backgroundColor = UIColor.blue 110 | 111 | } 112 | 113 | } // end class Rectangle 114 | 115 | enum Shapes { 116 | 117 | case square 118 | case circle 119 | case rectangle 120 | 121 | } 122 | 123 | class ShapeFactory { 124 | 125 | let parentView: UIView 126 | 127 | init(parentView: UIView) { 128 | 129 | self.parentView = parentView 130 | 131 | } 132 | 133 | func create(as shape: Shapes) -> HelperViewFactoryProtocol { 134 | 135 | switch shape { 136 | 137 | case .square: 138 | 139 | let square = Square(parentView: parentView) 140 | return square 141 | 142 | case .circle: 143 | 144 | let circle = Circle(parentView: parentView) 145 | return circle 146 | 147 | case .rectangle: 148 | 149 | let rectangle = Rectangle(parentView: parentView) 150 | return rectangle 151 | 152 | } 153 | 154 | } // end func display 155 | 156 | } // end class ShapeFactory 157 | 158 | // Public factory method to display shapes. 159 | func createShape(_ shape: Shapes, on view: UIView) { 160 | 161 | let shapeFactory = ShapeFactory(parentView: view) 162 | shapeFactory.create(as: shape).display() 163 | 164 | } 165 | 166 | // Alternative public factory method to display shapes. 167 | // Technically, the factory method should return one of 168 | // a number of related classes. 169 | func getShape(_ shape: Shapes, on view: UIView) -> HelperViewFactoryProtocol { 170 | 171 | let shapeFactory = ShapeFactory(parentView: view) 172 | return shapeFactory.create(as: shape) 173 | 174 | } 175 | -------------------------------------------------------------------------------- /Swift Factory Design Pattern/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // Swift Factory Design Pattern 4 | // 5 | // Created by Andrew L. Jaffee on 7/14/18. 6 | // 7 | /* 8 | 9 | Copyright (c) 2017-2018 Andrew L. Jaffee, microIT Infrastructure, LLC, and 10 | iosbrain.com. 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy 13 | of this software and associated documentation files (the "Software"), to deal 14 | in the Software without restriction, including without limitation the rights 15 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | copies of the Software, and to permit persons to whom the Software is 17 | furnished to do so, subject to the following conditions: 18 | 19 | The above copyright notice and this permission notice shall be included in all 20 | copies or substantial portions of the Software. 21 | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 | SOFTWARE. 29 | 30 | */ 31 | 32 | import UIKit 33 | 34 | class ViewController: UIViewController { 35 | 36 | override func viewDidLoad() { 37 | super.viewDidLoad() 38 | // Do any additional setup after loading the view, typically from a nib. 39 | 40 | } 41 | 42 | override func didReceiveMemoryWarning() { 43 | super.didReceiveMemoryWarning() 44 | // Dispose of any resources that can be recreated. 45 | } 46 | 47 | @IBAction func drawCircle(_ sender: Any) { 48 | 49 | // just draw the shape 50 | createShape(.circle, on: view) 51 | 52 | } 53 | 54 | @IBAction func drawSquare(_ sender: Any) { 55 | 56 | // just draw the shape 57 | createShape(.square, on: view) 58 | 59 | } 60 | 61 | @IBAction func drawRectangle(_ sender: Any) { 62 | 63 | // actually get an object from the factory 64 | // and use it to draw the shape 65 | let rectangle = getShape(.rectangle, on: view) 66 | rectangle.display() 67 | 68 | } 69 | 70 | } // end class ViewController 71 | 72 | --------------------------------------------------------------------------------