├── .DS_Store ├── .gitignore ├── Adapter Pattern ├── .DS_Store ├── Adapter Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Adapter Pattern │ ├── OldLibrary.swift │ ├── OldLibraryAdapter.swift │ ├── StoneLibrary.swift │ ├── StoneLibraryDB.swift │ ├── StoneLibrarySearchTool.swift │ └── main.swift ├── Chain Of Responsibility Pattern ├── Chain-Of-Responsibility-Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Chain-Of-Responsibility-Pattern │ ├── ConfirmPayment.swift │ ├── Transmitters.swift │ └── main.swift ├── Command Pattern ├── .DS_Store ├── Command Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Command Pattern │ └── main.swift ├── Decorator Pattern ├── .DS_Store ├── Decorator Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Decorator Pattern │ ├── CustomerAccount.swift │ ├── Purchase.swift │ ├── Services.swift │ └── main.swift ├── Facade Pattern ├── Facade Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Facade Pattern │ ├── Member.swift │ ├── Ship.swift │ ├── StoneFacade.swift │ ├── TreasureMap.swift │ └── main.swift ├── Factory Method Pattern ├── Factory Method Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Factory Method Pattern │ ├── ProductFactory.swift │ └── main.swift ├── Flyweight Pattern ├── Flyweight Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Flyweight Pattern │ ├── AppDelegate.swift │ ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ ├── Contents.json │ ├── bg-home.imageset │ │ ├── Contents.json │ │ └── bg-home.jpg │ ├── boo.imageset │ │ ├── Contents.json │ │ └── boo.png │ ├── mario.imageset │ │ ├── Contents.json │ │ └── mario.png │ ├── montyMole.imageset │ │ ├── Contents.json │ │ └── MontyMole.png │ ├── peach.imageset │ │ ├── Contents.json │ │ └── peach.png │ ├── shyGuy.imageset │ │ ├── Contents.json │ │ └── ShyGuy.png │ ├── star.imageset │ │ ├── Contents.json │ │ └── star.png │ ├── turtle-blue.imageset │ │ ├── Contents.json │ │ └── turtle-blue.png │ └── turtle-green.imageset │ │ ├── Contents.json │ │ └── turtle-green.png │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Character.swift │ ├── Flyweight.swift │ ├── FlyweightViewController.swift │ ├── FlyweightViewController.xib │ ├── HomeViewController.swift │ ├── HomeViewController.xib │ ├── Info.plist │ ├── NormalViewController.swift │ └── NormalViewController.xib ├── Mediator Pattern ├── Mediator Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Mediator Pattern │ ├── Car.swift │ ├── Mediator.swift │ ├── TestCar.swift │ └── main.swift ├── Observer Pattern ├── Observer Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Observer Pattern │ ├── Observer.swift │ ├── StoneBakery.swift │ ├── StoneFireStation.swift │ ├── StoneHospital.swift │ ├── StoneSchool.swift │ ├── WoodBakery.swift │ ├── WoodFireStation.swift │ ├── WoodHospital.swift │ ├── WoodSchool.swift │ └── main.swift ├── Prototype Pattern ├── Prototype Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Prototype Pattern │ ├── ReferenceTypeShop.swift │ ├── ValueTypeShop.swift │ └── main.swift ├── Proxy Pattern ├── Proxy-Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Proxy-Pattern │ ├── StoneBakery.swift │ ├── StoneChef.swift │ ├── StoneWaiter.swift │ ├── WoodBakery.swift │ └── main.swift ├── Strategy Pattern ├── Strategy Pattern.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Strategy Pattern │ ├── AdventureA.swift │ ├── AdventureB.swift │ ├── Strategy.swift │ └── main.swift └── Visitor Pattern ├── Visitor Pattern.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata └── Visitor Pattern ├── StoneMall.swift ├── Visitor.swift ├── WoodMall.swift └── main.swift /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/swift 2 | 3 | ### Swift ### 4 | # Xcode 5 | # 6 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 7 | 8 | ## Build generated 9 | build/ 10 | DerivedData/ 11 | 12 | ## Various settings 13 | *.pbxuser 14 | !default.pbxuser 15 | *.mode1v3 16 | !default.mode1v3 17 | *.mode2v3 18 | !default.mode2v3 19 | *.perspectivev3 20 | !default.perspectivev3 21 | xcuserdata/ 22 | 23 | ## Other 24 | *.moved-aside 25 | *.xcuserstate 26 | 27 | ## Obj-C/Swift specific 28 | *.hmap 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | .build/ 42 | 43 | # CocoaPods 44 | # 45 | # We recommend against adding the Pods directory to your .gitignore. However 46 | # you should judge for yourself, the pros and cons are mentioned at: 47 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 48 | # 49 | # Pods/ 50 | 51 | # Carthage 52 | # 53 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 54 | # Carthage/Checkouts 55 | 56 | Carthage/Build 57 | 58 | # fastlane 59 | # 60 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 61 | # screenshots whenever they are needed. 62 | # For more information about the recommended setup visit: 63 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 64 | 65 | fastlane/report.xml 66 | fastlane/Preview.html 67 | fastlane/screenshots 68 | fastlane/test_output 69 | 70 | # End of https://www.gitignore.io/api/swift 71 | -------------------------------------------------------------------------------- /Adapter Pattern/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/Adapter Pattern/.DS_Store -------------------------------------------------------------------------------- /Adapter Pattern/Adapter Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 18B573C71E2248B200147E13 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18B573C61E2248B200147E13 /* main.swift */; }; 11 | 18B573E51E2321E000147E13 /* StoneLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18B573E41E2321E000147E13 /* StoneLibrary.swift */; }; 12 | 18B573E71E23221C00147E13 /* StoneLibraryDB.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18B573E61E23221C00147E13 /* StoneLibraryDB.swift */; }; 13 | 18B573E91E2323ED00147E13 /* StoneLibrarySearchTool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18B573E81E2323ED00147E13 /* StoneLibrarySearchTool.swift */; }; 14 | 18B573EF1E2325B400147E13 /* OldLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18B573EE1E2325B400147E13 /* OldLibrary.swift */; }; 15 | 18B573F31E23293600147E13 /* OldLibraryAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18B573F21E23293600147E13 /* OldLibraryAdapter.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXCopyFilesBuildPhase section */ 19 | 18B573C11E2248B200147E13 /* CopyFiles */ = { 20 | isa = PBXCopyFilesBuildPhase; 21 | buildActionMask = 2147483647; 22 | dstPath = /usr/share/man/man1/; 23 | dstSubfolderSpec = 0; 24 | files = ( 25 | ); 26 | runOnlyForDeploymentPostprocessing = 1; 27 | }; 28 | /* End PBXCopyFilesBuildPhase section */ 29 | 30 | /* Begin PBXFileReference section */ 31 | 18B573C31E2248B200147E13 /* Adapter Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Adapter Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 32 | 18B573C61E2248B200147E13 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 33 | 18B573E41E2321E000147E13 /* StoneLibrary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoneLibrary.swift; sourceTree = ""; }; 34 | 18B573E61E23221C00147E13 /* StoneLibraryDB.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoneLibraryDB.swift; sourceTree = ""; }; 35 | 18B573E81E2323ED00147E13 /* StoneLibrarySearchTool.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoneLibrarySearchTool.swift; sourceTree = ""; }; 36 | 18B573EE1E2325B400147E13 /* OldLibrary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OldLibrary.swift; sourceTree = ""; }; 37 | 18B573F21E23293600147E13 /* OldLibraryAdapter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OldLibraryAdapter.swift; sourceTree = ""; }; 38 | /* End PBXFileReference section */ 39 | 40 | /* Begin PBXFrameworksBuildPhase section */ 41 | 18B573C01E2248B200147E13 /* Frameworks */ = { 42 | isa = PBXFrameworksBuildPhase; 43 | buildActionMask = 2147483647; 44 | files = ( 45 | ); 46 | runOnlyForDeploymentPostprocessing = 0; 47 | }; 48 | /* End PBXFrameworksBuildPhase section */ 49 | 50 | /* Begin PBXGroup section */ 51 | 18B573BA1E2248B200147E13 = { 52 | isa = PBXGroup; 53 | children = ( 54 | 18B573C51E2248B200147E13 /* Adapter Pattern */, 55 | 18B573C41E2248B200147E13 /* Products */, 56 | ); 57 | sourceTree = ""; 58 | }; 59 | 18B573C41E2248B200147E13 /* Products */ = { 60 | isa = PBXGroup; 61 | children = ( 62 | 18B573C31E2248B200147E13 /* Adapter Pattern */, 63 | ); 64 | name = Products; 65 | sourceTree = ""; 66 | }; 67 | 18B573C51E2248B200147E13 /* Adapter Pattern */ = { 68 | isa = PBXGroup; 69 | children = ( 70 | 18B573C61E2248B200147E13 /* main.swift */, 71 | 18B573DC1E2310A300147E13 /* StoneLibrary */, 72 | 18B573DD1E2310AE00147E13 /* Old Library */, 73 | ); 74 | path = "Adapter Pattern"; 75 | sourceTree = ""; 76 | }; 77 | 18B573DC1E2310A300147E13 /* StoneLibrary */ = { 78 | isa = PBXGroup; 79 | children = ( 80 | 18B573E41E2321E000147E13 /* StoneLibrary.swift */, 81 | 18B573E61E23221C00147E13 /* StoneLibraryDB.swift */, 82 | 18B573E81E2323ED00147E13 /* StoneLibrarySearchTool.swift */, 83 | ); 84 | name = StoneLibrary; 85 | sourceTree = ""; 86 | }; 87 | 18B573DD1E2310AE00147E13 /* Old Library */ = { 88 | isa = PBXGroup; 89 | children = ( 90 | 18B573EE1E2325B400147E13 /* OldLibrary.swift */, 91 | 18B573F21E23293600147E13 /* OldLibraryAdapter.swift */, 92 | ); 93 | name = "Old Library"; 94 | sourceTree = ""; 95 | }; 96 | /* End PBXGroup section */ 97 | 98 | /* Begin PBXNativeTarget section */ 99 | 18B573C21E2248B200147E13 /* Adapter Pattern */ = { 100 | isa = PBXNativeTarget; 101 | buildConfigurationList = 18B573CA1E2248B200147E13 /* Build configuration list for PBXNativeTarget "Adapter Pattern" */; 102 | buildPhases = ( 103 | 18B573BF1E2248B200147E13 /* Sources */, 104 | 18B573C01E2248B200147E13 /* Frameworks */, 105 | 18B573C11E2248B200147E13 /* CopyFiles */, 106 | ); 107 | buildRules = ( 108 | ); 109 | dependencies = ( 110 | ); 111 | name = "Adapter Pattern"; 112 | productName = "Adapter Pattern"; 113 | productReference = 18B573C31E2248B200147E13 /* Adapter Pattern */; 114 | productType = "com.apple.product-type.tool"; 115 | }; 116 | /* End PBXNativeTarget section */ 117 | 118 | /* Begin PBXProject section */ 119 | 18B573BB1E2248B200147E13 /* Project object */ = { 120 | isa = PBXProject; 121 | attributes = { 122 | LastSwiftUpdateCheck = 0820; 123 | LastUpgradeCheck = 0820; 124 | ORGANIZATIONNAME = Don; 125 | TargetAttributes = { 126 | 18B573C21E2248B200147E13 = { 127 | CreatedOnToolsVersion = 8.2.1; 128 | DevelopmentTeam = DT8726CBX4; 129 | ProvisioningStyle = Automatic; 130 | }; 131 | }; 132 | }; 133 | buildConfigurationList = 18B573BE1E2248B200147E13 /* Build configuration list for PBXProject "Adapter Pattern" */; 134 | compatibilityVersion = "Xcode 3.2"; 135 | developmentRegion = English; 136 | hasScannedForEncodings = 0; 137 | knownRegions = ( 138 | en, 139 | ); 140 | mainGroup = 18B573BA1E2248B200147E13; 141 | productRefGroup = 18B573C41E2248B200147E13 /* Products */; 142 | projectDirPath = ""; 143 | projectRoot = ""; 144 | targets = ( 145 | 18B573C21E2248B200147E13 /* Adapter Pattern */, 146 | ); 147 | }; 148 | /* End PBXProject section */ 149 | 150 | /* Begin PBXSourcesBuildPhase section */ 151 | 18B573BF1E2248B200147E13 /* Sources */ = { 152 | isa = PBXSourcesBuildPhase; 153 | buildActionMask = 2147483647; 154 | files = ( 155 | 18B573E51E2321E000147E13 /* StoneLibrary.swift in Sources */, 156 | 18B573F31E23293600147E13 /* OldLibraryAdapter.swift in Sources */, 157 | 18B573E71E23221C00147E13 /* StoneLibraryDB.swift in Sources */, 158 | 18B573EF1E2325B400147E13 /* OldLibrary.swift in Sources */, 159 | 18B573E91E2323ED00147E13 /* StoneLibrarySearchTool.swift in Sources */, 160 | 18B573C71E2248B200147E13 /* main.swift in Sources */, 161 | ); 162 | runOnlyForDeploymentPostprocessing = 0; 163 | }; 164 | /* End PBXSourcesBuildPhase section */ 165 | 166 | /* Begin XCBuildConfiguration section */ 167 | 18B573C81E2248B200147E13 /* Debug */ = { 168 | isa = XCBuildConfiguration; 169 | buildSettings = { 170 | ALWAYS_SEARCH_USER_PATHS = NO; 171 | CLANG_ANALYZER_NONNULL = YES; 172 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 173 | CLANG_CXX_LIBRARY = "libc++"; 174 | CLANG_ENABLE_MODULES = YES; 175 | CLANG_ENABLE_OBJC_ARC = YES; 176 | CLANG_WARN_BOOL_CONVERSION = YES; 177 | CLANG_WARN_CONSTANT_CONVERSION = YES; 178 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 179 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 180 | CLANG_WARN_EMPTY_BODY = YES; 181 | CLANG_WARN_ENUM_CONVERSION = YES; 182 | CLANG_WARN_INFINITE_RECURSION = YES; 183 | CLANG_WARN_INT_CONVERSION = YES; 184 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 185 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 186 | CLANG_WARN_UNREACHABLE_CODE = YES; 187 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 188 | CODE_SIGN_IDENTITY = "-"; 189 | COPY_PHASE_STRIP = NO; 190 | DEBUG_INFORMATION_FORMAT = dwarf; 191 | ENABLE_STRICT_OBJC_MSGSEND = YES; 192 | ENABLE_TESTABILITY = YES; 193 | GCC_C_LANGUAGE_STANDARD = gnu99; 194 | GCC_DYNAMIC_NO_PIC = NO; 195 | GCC_NO_COMMON_BLOCKS = YES; 196 | GCC_OPTIMIZATION_LEVEL = 0; 197 | GCC_PREPROCESSOR_DEFINITIONS = ( 198 | "DEBUG=1", 199 | "$(inherited)", 200 | ); 201 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 202 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 203 | GCC_WARN_UNDECLARED_SELECTOR = YES; 204 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 205 | GCC_WARN_UNUSED_FUNCTION = YES; 206 | GCC_WARN_UNUSED_VARIABLE = YES; 207 | MACOSX_DEPLOYMENT_TARGET = 10.12; 208 | MTL_ENABLE_DEBUG_INFO = YES; 209 | ONLY_ACTIVE_ARCH = YES; 210 | SDKROOT = macosx; 211 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 212 | }; 213 | name = Debug; 214 | }; 215 | 18B573C91E2248B200147E13 /* Release */ = { 216 | isa = XCBuildConfiguration; 217 | buildSettings = { 218 | ALWAYS_SEARCH_USER_PATHS = NO; 219 | CLANG_ANALYZER_NONNULL = YES; 220 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 221 | CLANG_CXX_LIBRARY = "libc++"; 222 | CLANG_ENABLE_MODULES = YES; 223 | CLANG_ENABLE_OBJC_ARC = YES; 224 | CLANG_WARN_BOOL_CONVERSION = YES; 225 | CLANG_WARN_CONSTANT_CONVERSION = YES; 226 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 227 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 228 | CLANG_WARN_EMPTY_BODY = YES; 229 | CLANG_WARN_ENUM_CONVERSION = YES; 230 | CLANG_WARN_INFINITE_RECURSION = YES; 231 | CLANG_WARN_INT_CONVERSION = YES; 232 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 233 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 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-with-dsym"; 239 | ENABLE_NS_ASSERTIONS = NO; 240 | ENABLE_STRICT_OBJC_MSGSEND = YES; 241 | GCC_C_LANGUAGE_STANDARD = gnu99; 242 | GCC_NO_COMMON_BLOCKS = YES; 243 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 244 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 245 | GCC_WARN_UNDECLARED_SELECTOR = YES; 246 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 247 | GCC_WARN_UNUSED_FUNCTION = YES; 248 | GCC_WARN_UNUSED_VARIABLE = YES; 249 | MACOSX_DEPLOYMENT_TARGET = 10.12; 250 | MTL_ENABLE_DEBUG_INFO = NO; 251 | SDKROOT = macosx; 252 | }; 253 | name = Release; 254 | }; 255 | 18B573CB1E2248B200147E13 /* Debug */ = { 256 | isa = XCBuildConfiguration; 257 | buildSettings = { 258 | DEVELOPMENT_TEAM = DT8726CBX4; 259 | PRODUCT_NAME = "$(TARGET_NAME)"; 260 | SWIFT_VERSION = 3.0; 261 | }; 262 | name = Debug; 263 | }; 264 | 18B573CC1E2248B200147E13 /* Release */ = { 265 | isa = XCBuildConfiguration; 266 | buildSettings = { 267 | DEVELOPMENT_TEAM = DT8726CBX4; 268 | PRODUCT_NAME = "$(TARGET_NAME)"; 269 | SWIFT_VERSION = 3.0; 270 | }; 271 | name = Release; 272 | }; 273 | /* End XCBuildConfiguration section */ 274 | 275 | /* Begin XCConfigurationList section */ 276 | 18B573BE1E2248B200147E13 /* Build configuration list for PBXProject "Adapter Pattern" */ = { 277 | isa = XCConfigurationList; 278 | buildConfigurations = ( 279 | 18B573C81E2248B200147E13 /* Debug */, 280 | 18B573C91E2248B200147E13 /* Release */, 281 | ); 282 | defaultConfigurationIsVisible = 0; 283 | defaultConfigurationName = Release; 284 | }; 285 | 18B573CA1E2248B200147E13 /* Build configuration list for PBXNativeTarget "Adapter Pattern" */ = { 286 | isa = XCConfigurationList; 287 | buildConfigurations = ( 288 | 18B573CB1E2248B200147E13 /* Debug */, 289 | 18B573CC1E2248B200147E13 /* Release */, 290 | ); 291 | defaultConfigurationIsVisible = 0; 292 | defaultConfigurationName = Release; 293 | }; 294 | /* End XCConfigurationList section */ 295 | }; 296 | rootObject = 18B573BB1E2248B200147E13 /* Project object */; 297 | } 298 | -------------------------------------------------------------------------------- /Adapter Pattern/Adapter Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Adapter Pattern/Adapter Pattern/OldLibrary.swift: -------------------------------------------------------------------------------- 1 | // 2 | // OldLibrary.swift 3 | // Adapter Pattern 4 | // 5 | // Created by Don on 2017/1/9. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class OldLibraryBook { 12 | fileprivate var name:String 13 | fileprivate var kind:String 14 | 15 | init(name:String, kind:String) { 16 | self.name = name 17 | self.kind = kind 18 | } 19 | 20 | func getName() -> String { 21 | return name 22 | } 23 | 24 | func getKind() -> String { 25 | return kind 26 | } 27 | 28 | } 29 | 30 | class OldLibraryDirectory { 31 | fileprivate var items:[String: OldLibraryBook] 32 | 33 | init() { 34 | items = ["gone girl": OldLibraryBook(name:"gone girl", kind:"novel"), 35 | "the martian" : OldLibraryBook(name: "the martian", kind: "novel") 36 | ] 37 | } 38 | 39 | func getItems() -> [String: OldLibraryBook] { 40 | return items 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Adapter Pattern/Adapter Pattern/OldLibraryAdapter.swift: -------------------------------------------------------------------------------- 1 | // 2 | // OldLibraryAdapter.swift 3 | // Adapter Pattern 4 | // 5 | // Created by Don on 2017/1/9. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class OldLibraryAdapter:StoneLibraryDataSource { 12 | 13 | fileprivate let directory:OldLibraryDirectory 14 | 15 | init() { 16 | directory = OldLibraryDirectory() 17 | } 18 | 19 | var books:[StoneBook] { 20 | return directory.getItems().values.map { item -> StoneBook in 21 | return StoneBook(name: item.getName(), category: item.getKind()) 22 | } 23 | } 24 | 25 | func searchByName(name:String) -> [StoneBook] { 26 | return createBooks(filter: {(oldBook:OldLibraryBook) -> Bool in 27 | return oldBook.getName().range(of: name) != nil 28 | }) 29 | } 30 | 31 | func searchByCategory(category:String) -> [StoneBook] { 32 | return createBooks(filter: {(oldBook:OldLibraryBook) -> Bool in 33 | return oldBook.getKind().range(of: category) != nil 34 | }) 35 | } 36 | 37 | // 查找出資料後,轉換成Book類型 38 | func createBooks(filter filterClosure:((OldLibraryBook) -> Bool)) -> [StoneBook] { 39 | var results = [StoneBook]() 40 | for item in directory.getItems().values { 41 | if filterClosure(item) { 42 | let aBook = StoneBook(name: item.getName(), category: item.getKind()) 43 | results.append(aBook) 44 | } 45 | } 46 | return results 47 | 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Adapter Pattern/Adapter Pattern/StoneLibrary.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StoneLibrary.swift 3 | // Adapter Pattern 4 | // 5 | // Created by Don on 2017/1/9. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | struct StoneBook { 12 | var name:String 13 | var category:String 14 | } 15 | 16 | protocol StoneLibraryDataSource { 17 | var books:[StoneBook] { get } 18 | func searchByName(name:String) -> [StoneBook] 19 | func searchByCategory(category:String) -> [StoneBook] 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Adapter Pattern/Adapter Pattern/StoneLibraryDB.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StoneLibraryDB.swift 3 | // Adapter Pattern 4 | // 5 | // Created by Don on 2017/1/9. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class StoneLibraryDB:StoneLibraryDataSource { 12 | var books = [StoneBook]() 13 | 14 | func searchByName(name:String) -> [StoneBook] { 15 | return search({ book -> Bool in 16 | return book.name.range(of: name) != nil 17 | }) 18 | } 19 | 20 | func searchByCategory(category:String) -> [StoneBook] { 21 | return search({ book -> Bool in 22 | return book.category.range(of: category) != nil 23 | }) 24 | } 25 | 26 | private func search(_ selector:((StoneBook) -> Bool)) -> [StoneBook] { 27 | var results = [StoneBook]() 28 | for book in books { 29 | if selector(book) { 30 | results.append(book) 31 | } 32 | } 33 | return results 34 | } 35 | 36 | } 37 | 38 | class ComputerScienceDB:StoneLibraryDB { 39 | 40 | override init() { 41 | super.init() 42 | books.append(StoneBook(name: "Code Complete", category: "computer science")) 43 | books.append(StoneBook(name: "Algorithm", category: "computer science")) 44 | } 45 | 46 | } 47 | 48 | class FruitDB:StoneLibraryDB { 49 | 50 | override init() { 51 | super.init() 52 | 53 | books.append(StoneBook(name: "apple", category: "fruit")) 54 | books.append(StoneBook(name: "orange", category: "fruit")) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Adapter Pattern/Adapter Pattern/StoneLibrarySearchTool.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StoneLibrarySearchTool.swift 3 | // Adapter Pattern 4 | // 5 | // Created by Don on 2017/1/9. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class StoneLibrarySearchTool { 12 | 13 | enum SearchType { 14 | case name 15 | case category 16 | } 17 | 18 | private var sources = [StoneLibraryDataSource]() 19 | 20 | var books:[StoneBook] { 21 | var results = [StoneBook]() 22 | for source in sources { 23 | results += source.books 24 | } 25 | return results 26 | 27 | } 28 | 29 | init(sources:[StoneLibraryDataSource]) { 30 | self.sources = sources 31 | } 32 | 33 | func search(text:String, type:SearchType) -> [StoneBook] { 34 | var results = [StoneBook]() 35 | for source in sources { 36 | results += type == .name ? source.searchByName(name: text) : source.searchByCategory(category: text) 37 | } 38 | return results 39 | 40 | } 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Adapter Pattern/Adapter Pattern/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Adapter Pattern 4 | // 5 | // Created by Don on 2017/1/8. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | let searchTool = StoneLibrarySearchTool(sources: [ComputerScienceDB(), FruitDB(), OldLibraryAdapter()]) 12 | 13 | print("== list ==") 14 | for book in searchTool.books { 15 | print(book.name) 16 | } 17 | 18 | print("== search ==") 19 | for book in searchTool.search(text: "computer science", type: .category) { 20 | print(book.name) 21 | } 22 | 23 | print("== search name ==") 24 | for book in searchTool.search(text: "a", type: .name) { 25 | print(book.name) 26 | } 27 | -------------------------------------------------------------------------------- /Chain Of Responsibility Pattern/Chain-Of-Responsibility-Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1A222ADB1E34574B00200673 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A222ADA1E34574B00200673 /* main.swift */; }; 11 | 1A222AE21E3457E700200673 /* ConfirmPayment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A222AE11E3457E700200673 /* ConfirmPayment.swift */; }; 12 | 1A222AE61E34661E00200673 /* Transmitters.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A222AE51E34661E00200673 /* Transmitters.swift */; }; 13 | /* End PBXBuildFile section */ 14 | 15 | /* Begin PBXCopyFilesBuildPhase section */ 16 | 1A222AD51E34574B00200673 /* CopyFiles */ = { 17 | isa = PBXCopyFilesBuildPhase; 18 | buildActionMask = 2147483647; 19 | dstPath = /usr/share/man/man1/; 20 | dstSubfolderSpec = 0; 21 | files = ( 22 | ); 23 | runOnlyForDeploymentPostprocessing = 1; 24 | }; 25 | /* End PBXCopyFilesBuildPhase section */ 26 | 27 | /* Begin PBXFileReference section */ 28 | 1A222AD71E34574B00200673 /* Chain-Of-Responsibility-Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Chain-Of-Responsibility-Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 29 | 1A222ADA1E34574B00200673 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 30 | 1A222AE11E3457E700200673 /* ConfirmPayment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfirmPayment.swift; sourceTree = ""; }; 31 | 1A222AE51E34661E00200673 /* Transmitters.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Transmitters.swift; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | 1A222AD41E34574B00200673 /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | 1A222ACE1E34574B00200673 = { 46 | isa = PBXGroup; 47 | children = ( 48 | 1A222AD91E34574B00200673 /* Chain-Of-Responsibility-Pattern */, 49 | 1A222AD81E34574B00200673 /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | 1A222AD81E34574B00200673 /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | 1A222AD71E34574B00200673 /* Chain-Of-Responsibility-Pattern */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | 1A222AD91E34574B00200673 /* Chain-Of-Responsibility-Pattern */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | 1A222ADA1E34574B00200673 /* main.swift */, 65 | 1A222AE11E3457E700200673 /* ConfirmPayment.swift */, 66 | 1A222AE51E34661E00200673 /* Transmitters.swift */, 67 | ); 68 | path = "Chain-Of-Responsibility-Pattern"; 69 | sourceTree = ""; 70 | }; 71 | /* End PBXGroup section */ 72 | 73 | /* Begin PBXNativeTarget section */ 74 | 1A222AD61E34574B00200673 /* Chain-Of-Responsibility-Pattern */ = { 75 | isa = PBXNativeTarget; 76 | buildConfigurationList = 1A222ADE1E34574B00200673 /* Build configuration list for PBXNativeTarget "Chain-Of-Responsibility-Pattern" */; 77 | buildPhases = ( 78 | 1A222AD31E34574B00200673 /* Sources */, 79 | 1A222AD41E34574B00200673 /* Frameworks */, 80 | 1A222AD51E34574B00200673 /* CopyFiles */, 81 | ); 82 | buildRules = ( 83 | ); 84 | dependencies = ( 85 | ); 86 | name = "Chain-Of-Responsibility-Pattern"; 87 | productName = "Chain-Of-Responsibility-Pattern"; 88 | productReference = 1A222AD71E34574B00200673 /* Chain-Of-Responsibility-Pattern */; 89 | productType = "com.apple.product-type.tool"; 90 | }; 91 | /* End PBXNativeTarget section */ 92 | 93 | /* Begin PBXProject section */ 94 | 1A222ACF1E34574B00200673 /* Project object */ = { 95 | isa = PBXProject; 96 | attributes = { 97 | LastSwiftUpdateCheck = 0820; 98 | LastUpgradeCheck = 0820; 99 | ORGANIZATIONNAME = "don chen"; 100 | TargetAttributes = { 101 | 1A222AD61E34574B00200673 = { 102 | CreatedOnToolsVersion = 8.2.1; 103 | DevelopmentTeam = DT8726CBX4; 104 | ProvisioningStyle = Automatic; 105 | }; 106 | }; 107 | }; 108 | buildConfigurationList = 1A222AD21E34574B00200673 /* Build configuration list for PBXProject "Chain-Of-Responsibility-Pattern" */; 109 | compatibilityVersion = "Xcode 3.2"; 110 | developmentRegion = English; 111 | hasScannedForEncodings = 0; 112 | knownRegions = ( 113 | en, 114 | ); 115 | mainGroup = 1A222ACE1E34574B00200673; 116 | productRefGroup = 1A222AD81E34574B00200673 /* Products */; 117 | projectDirPath = ""; 118 | projectRoot = ""; 119 | targets = ( 120 | 1A222AD61E34574B00200673 /* Chain-Of-Responsibility-Pattern */, 121 | ); 122 | }; 123 | /* End PBXProject section */ 124 | 125 | /* Begin PBXSourcesBuildPhase section */ 126 | 1A222AD31E34574B00200673 /* Sources */ = { 127 | isa = PBXSourcesBuildPhase; 128 | buildActionMask = 2147483647; 129 | files = ( 130 | 1A222AE61E34661E00200673 /* Transmitters.swift in Sources */, 131 | 1A222ADB1E34574B00200673 /* main.swift in Sources */, 132 | 1A222AE21E3457E700200673 /* ConfirmPayment.swift in Sources */, 133 | ); 134 | runOnlyForDeploymentPostprocessing = 0; 135 | }; 136 | /* End PBXSourcesBuildPhase section */ 137 | 138 | /* Begin XCBuildConfiguration section */ 139 | 1A222ADC1E34574B00200673 /* Debug */ = { 140 | isa = XCBuildConfiguration; 141 | buildSettings = { 142 | ALWAYS_SEARCH_USER_PATHS = NO; 143 | CLANG_ANALYZER_NONNULL = YES; 144 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 145 | CLANG_CXX_LIBRARY = "libc++"; 146 | CLANG_ENABLE_MODULES = YES; 147 | CLANG_ENABLE_OBJC_ARC = YES; 148 | CLANG_WARN_BOOL_CONVERSION = YES; 149 | CLANG_WARN_CONSTANT_CONVERSION = YES; 150 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 151 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 152 | CLANG_WARN_EMPTY_BODY = YES; 153 | CLANG_WARN_ENUM_CONVERSION = YES; 154 | CLANG_WARN_INFINITE_RECURSION = YES; 155 | CLANG_WARN_INT_CONVERSION = YES; 156 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 157 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 158 | CLANG_WARN_UNREACHABLE_CODE = YES; 159 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 160 | CODE_SIGN_IDENTITY = "-"; 161 | COPY_PHASE_STRIP = NO; 162 | DEBUG_INFORMATION_FORMAT = dwarf; 163 | ENABLE_STRICT_OBJC_MSGSEND = YES; 164 | ENABLE_TESTABILITY = YES; 165 | GCC_C_LANGUAGE_STANDARD = gnu99; 166 | GCC_DYNAMIC_NO_PIC = NO; 167 | GCC_NO_COMMON_BLOCKS = YES; 168 | GCC_OPTIMIZATION_LEVEL = 0; 169 | GCC_PREPROCESSOR_DEFINITIONS = ( 170 | "DEBUG=1", 171 | "$(inherited)", 172 | ); 173 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 174 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 175 | GCC_WARN_UNDECLARED_SELECTOR = YES; 176 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 177 | GCC_WARN_UNUSED_FUNCTION = YES; 178 | GCC_WARN_UNUSED_VARIABLE = YES; 179 | MACOSX_DEPLOYMENT_TARGET = 10.12; 180 | MTL_ENABLE_DEBUG_INFO = YES; 181 | ONLY_ACTIVE_ARCH = YES; 182 | SDKROOT = macosx; 183 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 184 | }; 185 | name = Debug; 186 | }; 187 | 1A222ADD1E34574B00200673 /* Release */ = { 188 | isa = XCBuildConfiguration; 189 | buildSettings = { 190 | ALWAYS_SEARCH_USER_PATHS = NO; 191 | CLANG_ANALYZER_NONNULL = YES; 192 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 193 | CLANG_CXX_LIBRARY = "libc++"; 194 | CLANG_ENABLE_MODULES = YES; 195 | CLANG_ENABLE_OBJC_ARC = YES; 196 | CLANG_WARN_BOOL_CONVERSION = YES; 197 | CLANG_WARN_CONSTANT_CONVERSION = YES; 198 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 199 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 200 | CLANG_WARN_EMPTY_BODY = YES; 201 | CLANG_WARN_ENUM_CONVERSION = YES; 202 | CLANG_WARN_INFINITE_RECURSION = YES; 203 | CLANG_WARN_INT_CONVERSION = YES; 204 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 205 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 206 | CLANG_WARN_UNREACHABLE_CODE = YES; 207 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 208 | CODE_SIGN_IDENTITY = "-"; 209 | COPY_PHASE_STRIP = NO; 210 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 211 | ENABLE_NS_ASSERTIONS = NO; 212 | ENABLE_STRICT_OBJC_MSGSEND = YES; 213 | GCC_C_LANGUAGE_STANDARD = gnu99; 214 | GCC_NO_COMMON_BLOCKS = YES; 215 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 216 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 217 | GCC_WARN_UNDECLARED_SELECTOR = YES; 218 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 219 | GCC_WARN_UNUSED_FUNCTION = YES; 220 | GCC_WARN_UNUSED_VARIABLE = YES; 221 | MACOSX_DEPLOYMENT_TARGET = 10.12; 222 | MTL_ENABLE_DEBUG_INFO = NO; 223 | SDKROOT = macosx; 224 | }; 225 | name = Release; 226 | }; 227 | 1A222ADF1E34574B00200673 /* Debug */ = { 228 | isa = XCBuildConfiguration; 229 | buildSettings = { 230 | DEVELOPMENT_TEAM = DT8726CBX4; 231 | PRODUCT_NAME = "$(TARGET_NAME)"; 232 | SWIFT_VERSION = 3.0; 233 | }; 234 | name = Debug; 235 | }; 236 | 1A222AE01E34574B00200673 /* Release */ = { 237 | isa = XCBuildConfiguration; 238 | buildSettings = { 239 | DEVELOPMENT_TEAM = DT8726CBX4; 240 | PRODUCT_NAME = "$(TARGET_NAME)"; 241 | SWIFT_VERSION = 3.0; 242 | }; 243 | name = Release; 244 | }; 245 | /* End XCBuildConfiguration section */ 246 | 247 | /* Begin XCConfigurationList section */ 248 | 1A222AD21E34574B00200673 /* Build configuration list for PBXProject "Chain-Of-Responsibility-Pattern" */ = { 249 | isa = XCConfigurationList; 250 | buildConfigurations = ( 251 | 1A222ADC1E34574B00200673 /* Debug */, 252 | 1A222ADD1E34574B00200673 /* Release */, 253 | ); 254 | defaultConfigurationIsVisible = 0; 255 | defaultConfigurationName = Release; 256 | }; 257 | 1A222ADE1E34574B00200673 /* Build configuration list for PBXNativeTarget "Chain-Of-Responsibility-Pattern" */ = { 258 | isa = XCConfigurationList; 259 | buildConfigurations = ( 260 | 1A222ADF1E34574B00200673 /* Debug */, 261 | 1A222AE01E34574B00200673 /* Release */, 262 | ); 263 | defaultConfigurationIsVisible = 0; 264 | }; 265 | /* End XCConfigurationList section */ 266 | }; 267 | rootObject = 1A222ACF1E34574B00200673 /* Project object */; 268 | } 269 | -------------------------------------------------------------------------------- /Chain Of Responsibility Pattern/Chain-Of-Responsibility-Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chain Of Responsibility Pattern/Chain-Of-Responsibility-Pattern/ConfirmPayment.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ConfirmPayment.swift 3 | // Chain-Of-Responsibility-Pattern 4 | // 5 | // Created by don chen on 2017/1/22. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | struct Payment { 12 | let name:String 13 | let amount:Int 14 | let usage:String 15 | } 16 | 17 | class TeamLeader { 18 | func confirm(payment:Payment) { 19 | if payment.amount <= 1000 { 20 | print("Team Leader 同意了 \(payment.name) 用來 \(payment.usage) 的費用報銷,金額為: \(payment.amount)") 21 | } else { 22 | print("Team Leader: 這個費用不該找我審核吧?") 23 | } 24 | 25 | } 26 | } 27 | 28 | class Chef { 29 | func confirm(payment:Payment) { 30 | if payment.amount > 1000 && payment.amount < 10000 { 31 | print("Chef 同意了 \(payment.name) 用來 \(payment.usage) 的費用報銷,金額為: \(payment.amount)") 32 | } else { 33 | print("Chef: 這個費用不該找我審核吧?") 34 | } 35 | 36 | } 37 | } 38 | 39 | class Boss { 40 | func confirm(payment:Payment) { 41 | if payment.amount > 10000 && payment.amount < 100000 { 42 | print("Boss 同意了 \(payment.name) 用來 \(payment.usage) 的費用報銷,金額為: \(payment.amount)") 43 | } else { 44 | print("Boss: 請問\(payment.name) 這個用來 \(payment.usage) 用途是?") 45 | } 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Chain Of Responsibility Pattern/Chain-Of-Responsibility-Pattern/Transmitters.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Transmitters.swift 3 | // Chain-Of-Responsibility-Pattern 4 | // 5 | // Created by don chen on 2017/1/22. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class Transmitter { 12 | var nextLink:Transmitter? 13 | 14 | required init() {} 15 | 16 | func confirm(payment:Payment) { 17 | if nextLink != nil { 18 | nextLink?.confirm(payment: payment) 19 | 20 | } else { 21 | print("抵達責任鏈尾端,似乎用來 \(payment.usage) 的費用 沒有人可以處理") 22 | 23 | } 24 | } 25 | 26 | class func createChain() -> Transmitter? { 27 | 28 | let transmitterClasses:[Transmitter.Type] = [ 29 | TeamLeaderTransmitter.self, 30 | ChefTransmitter.self, 31 | BossTransmitter.self 32 | ] 33 | 34 | var link:Transmitter? 35 | 36 | for tClass in transmitterClasses { 37 | let existingLink = link 38 | link = tClass.init() 39 | link?.nextLink = existingLink 40 | 41 | } 42 | 43 | return link 44 | } 45 | 46 | } 47 | 48 | class TeamLeaderTransmitter: Transmitter { 49 | 50 | override func confirm(payment:Payment) { 51 | if payment.amount <= 1000 { 52 | print("Team Leader 同意了 \(payment.name) 用來 \(payment.usage) 的費用報銷,金額為: \(payment.amount)") 53 | } else { 54 | super.confirm(payment: payment) 55 | } 56 | } 57 | } 58 | 59 | class ChefTransmitter: Transmitter { 60 | 61 | override func confirm(payment:Payment) { 62 | if payment.amount > 1000 && payment.amount < 10000 { 63 | print("Chef 同意了 \(payment.name) 用來 \(payment.usage) 的費用報銷,金額為: \(payment.amount)") 64 | 65 | } else { 66 | super.confirm(payment: payment) 67 | 68 | } 69 | } 70 | } 71 | 72 | class BossTransmitter: Transmitter { 73 | 74 | override func confirm(payment: Payment) { 75 | if payment.amount > 10000 && payment.amount < 100000 { 76 | print("Boss 同意了 \(payment.name) 用來 \(payment.usage) 的費用報銷,金額為: \(payment.amount)") 77 | 78 | } else { 79 | super.confirm(payment: payment) 80 | 81 | } 82 | } 83 | } 84 | 85 | -------------------------------------------------------------------------------- /Chain Of Responsibility Pattern/Chain-Of-Responsibility-Pattern/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Chain-Of-Responsibility-Pattern 4 | // 5 | // Created by don chen on 2017/1/22. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | let payments = [ 12 | Payment(name:"Don", amount:75000, usage:"購買麵包設備"), 13 | Payment(name:"Howard", amount:33000, usage:"購買高級調味料"), 14 | Payment(name:"Jerry", amount:7500, usage:"購買進口桿麵棍"), 15 | Payment(name:"Jack", amount:2500, usage:"購買麵粉"), 16 | Payment(name:"Oliver", amount:300, usage:"購買瓦斯"), 17 | Payment(name:"ES", amount:100000, usage:"購買生產線") 18 | ] 19 | 20 | let teamLeader = TeamLeader() 21 | let chef = Chef() 22 | let boss = Boss() 23 | 24 | for payment in payments { 25 | if payment.amount <= 1000 { 26 | teamLeader.confirm(payment: payment) 27 | 28 | } else if payment.amount < 10000 { 29 | chef.confirm(payment: payment) 30 | 31 | } else { 32 | boss.confirm(payment: payment) 33 | 34 | } 35 | } 36 | 37 | print("======== with chain of responsibility pattern ======") 38 | 39 | if let chain = Transmitter.createChain() { 40 | for payment in payments { 41 | chain.confirm(payment: payment) 42 | } 43 | } 44 | 45 | 46 | -------------------------------------------------------------------------------- /Command Pattern/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/Command Pattern/.DS_Store -------------------------------------------------------------------------------- /Command Pattern/Command Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 189A04B61E28656700C4C8EB /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 189A04B51E28656700C4C8EB /* main.swift */; }; 11 | /* End PBXBuildFile section */ 12 | 13 | /* Begin PBXCopyFilesBuildPhase section */ 14 | 189A04B01E28656700C4C8EB /* 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 | 189A04B21E28656700C4C8EB /* Command Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Command Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 27 | 189A04B51E28656700C4C8EB /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 28 | /* End PBXFileReference section */ 29 | 30 | /* Begin PBXFrameworksBuildPhase section */ 31 | 189A04AF1E28656700C4C8EB /* Frameworks */ = { 32 | isa = PBXFrameworksBuildPhase; 33 | buildActionMask = 2147483647; 34 | files = ( 35 | ); 36 | runOnlyForDeploymentPostprocessing = 0; 37 | }; 38 | /* End PBXFrameworksBuildPhase section */ 39 | 40 | /* Begin PBXGroup section */ 41 | 189A04A91E28656700C4C8EB = { 42 | isa = PBXGroup; 43 | children = ( 44 | 189A04B41E28656700C4C8EB /* Command Pattern */, 45 | 189A04B31E28656700C4C8EB /* Products */, 46 | ); 47 | sourceTree = ""; 48 | }; 49 | 189A04B31E28656700C4C8EB /* Products */ = { 50 | isa = PBXGroup; 51 | children = ( 52 | 189A04B21E28656700C4C8EB /* Command Pattern */, 53 | ); 54 | name = Products; 55 | sourceTree = ""; 56 | }; 57 | 189A04B41E28656700C4C8EB /* Command Pattern */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | 189A04B51E28656700C4C8EB /* main.swift */, 61 | ); 62 | path = "Command Pattern"; 63 | sourceTree = ""; 64 | }; 65 | /* End PBXGroup section */ 66 | 67 | /* Begin PBXNativeTarget section */ 68 | 189A04B11E28656700C4C8EB /* Command Pattern */ = { 69 | isa = PBXNativeTarget; 70 | buildConfigurationList = 189A04B91E28656700C4C8EB /* Build configuration list for PBXNativeTarget "Command Pattern" */; 71 | buildPhases = ( 72 | 189A04AE1E28656700C4C8EB /* Sources */, 73 | 189A04AF1E28656700C4C8EB /* Frameworks */, 74 | 189A04B01E28656700C4C8EB /* CopyFiles */, 75 | ); 76 | buildRules = ( 77 | ); 78 | dependencies = ( 79 | ); 80 | name = "Command Pattern"; 81 | productName = "Command Pattern"; 82 | productReference = 189A04B21E28656700C4C8EB /* Command Pattern */; 83 | productType = "com.apple.product-type.tool"; 84 | }; 85 | /* End PBXNativeTarget section */ 86 | 87 | /* Begin PBXProject section */ 88 | 189A04AA1E28656700C4C8EB /* Project object */ = { 89 | isa = PBXProject; 90 | attributes = { 91 | LastSwiftUpdateCheck = 0820; 92 | LastUpgradeCheck = 0820; 93 | ORGANIZATIONNAME = Don; 94 | TargetAttributes = { 95 | 189A04B11E28656700C4C8EB = { 96 | CreatedOnToolsVersion = 8.2.1; 97 | DevelopmentTeam = DT8726CBX4; 98 | ProvisioningStyle = Automatic; 99 | }; 100 | }; 101 | }; 102 | buildConfigurationList = 189A04AD1E28656700C4C8EB /* Build configuration list for PBXProject "Command Pattern" */; 103 | compatibilityVersion = "Xcode 3.2"; 104 | developmentRegion = English; 105 | hasScannedForEncodings = 0; 106 | knownRegions = ( 107 | en, 108 | ); 109 | mainGroup = 189A04A91E28656700C4C8EB; 110 | productRefGroup = 189A04B31E28656700C4C8EB /* Products */; 111 | projectDirPath = ""; 112 | projectRoot = ""; 113 | targets = ( 114 | 189A04B11E28656700C4C8EB /* Command Pattern */, 115 | ); 116 | }; 117 | /* End PBXProject section */ 118 | 119 | /* Begin PBXSourcesBuildPhase section */ 120 | 189A04AE1E28656700C4C8EB /* Sources */ = { 121 | isa = PBXSourcesBuildPhase; 122 | buildActionMask = 2147483647; 123 | files = ( 124 | 189A04B61E28656700C4C8EB /* main.swift in Sources */, 125 | ); 126 | runOnlyForDeploymentPostprocessing = 0; 127 | }; 128 | /* End PBXSourcesBuildPhase section */ 129 | 130 | /* Begin XCBuildConfiguration section */ 131 | 189A04B71E28656700C4C8EB /* Debug */ = { 132 | isa = XCBuildConfiguration; 133 | buildSettings = { 134 | ALWAYS_SEARCH_USER_PATHS = NO; 135 | CLANG_ANALYZER_NONNULL = YES; 136 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 137 | CLANG_CXX_LIBRARY = "libc++"; 138 | CLANG_ENABLE_MODULES = YES; 139 | CLANG_ENABLE_OBJC_ARC = YES; 140 | CLANG_WARN_BOOL_CONVERSION = YES; 141 | CLANG_WARN_CONSTANT_CONVERSION = YES; 142 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 143 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 144 | CLANG_WARN_EMPTY_BODY = YES; 145 | CLANG_WARN_ENUM_CONVERSION = YES; 146 | CLANG_WARN_INFINITE_RECURSION = YES; 147 | CLANG_WARN_INT_CONVERSION = YES; 148 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 149 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 150 | CLANG_WARN_UNREACHABLE_CODE = YES; 151 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 152 | CODE_SIGN_IDENTITY = "-"; 153 | COPY_PHASE_STRIP = NO; 154 | DEBUG_INFORMATION_FORMAT = dwarf; 155 | ENABLE_STRICT_OBJC_MSGSEND = YES; 156 | ENABLE_TESTABILITY = YES; 157 | GCC_C_LANGUAGE_STANDARD = gnu99; 158 | GCC_DYNAMIC_NO_PIC = NO; 159 | GCC_NO_COMMON_BLOCKS = YES; 160 | GCC_OPTIMIZATION_LEVEL = 0; 161 | GCC_PREPROCESSOR_DEFINITIONS = ( 162 | "DEBUG=1", 163 | "$(inherited)", 164 | ); 165 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 166 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 167 | GCC_WARN_UNDECLARED_SELECTOR = YES; 168 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 169 | GCC_WARN_UNUSED_FUNCTION = YES; 170 | GCC_WARN_UNUSED_VARIABLE = YES; 171 | MACOSX_DEPLOYMENT_TARGET = 10.12; 172 | MTL_ENABLE_DEBUG_INFO = YES; 173 | ONLY_ACTIVE_ARCH = YES; 174 | SDKROOT = macosx; 175 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 176 | }; 177 | name = Debug; 178 | }; 179 | 189A04B81E28656700C4C8EB /* Release */ = { 180 | isa = XCBuildConfiguration; 181 | buildSettings = { 182 | ALWAYS_SEARCH_USER_PATHS = NO; 183 | CLANG_ANALYZER_NONNULL = YES; 184 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 185 | CLANG_CXX_LIBRARY = "libc++"; 186 | CLANG_ENABLE_MODULES = YES; 187 | CLANG_ENABLE_OBJC_ARC = YES; 188 | CLANG_WARN_BOOL_CONVERSION = YES; 189 | CLANG_WARN_CONSTANT_CONVERSION = YES; 190 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 191 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 192 | CLANG_WARN_EMPTY_BODY = YES; 193 | CLANG_WARN_ENUM_CONVERSION = YES; 194 | CLANG_WARN_INFINITE_RECURSION = YES; 195 | CLANG_WARN_INT_CONVERSION = YES; 196 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 197 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 198 | CLANG_WARN_UNREACHABLE_CODE = YES; 199 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 200 | CODE_SIGN_IDENTITY = "-"; 201 | COPY_PHASE_STRIP = NO; 202 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 203 | ENABLE_NS_ASSERTIONS = NO; 204 | ENABLE_STRICT_OBJC_MSGSEND = YES; 205 | GCC_C_LANGUAGE_STANDARD = gnu99; 206 | GCC_NO_COMMON_BLOCKS = YES; 207 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 208 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 209 | GCC_WARN_UNDECLARED_SELECTOR = YES; 210 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 211 | GCC_WARN_UNUSED_FUNCTION = YES; 212 | GCC_WARN_UNUSED_VARIABLE = YES; 213 | MACOSX_DEPLOYMENT_TARGET = 10.12; 214 | MTL_ENABLE_DEBUG_INFO = NO; 215 | SDKROOT = macosx; 216 | }; 217 | name = Release; 218 | }; 219 | 189A04BA1E28656700C4C8EB /* Debug */ = { 220 | isa = XCBuildConfiguration; 221 | buildSettings = { 222 | DEVELOPMENT_TEAM = DT8726CBX4; 223 | PRODUCT_NAME = "$(TARGET_NAME)"; 224 | SWIFT_VERSION = 3.0; 225 | }; 226 | name = Debug; 227 | }; 228 | 189A04BB1E28656700C4C8EB /* Release */ = { 229 | isa = XCBuildConfiguration; 230 | buildSettings = { 231 | DEVELOPMENT_TEAM = DT8726CBX4; 232 | PRODUCT_NAME = "$(TARGET_NAME)"; 233 | SWIFT_VERSION = 3.0; 234 | }; 235 | name = Release; 236 | }; 237 | /* End XCBuildConfiguration section */ 238 | 239 | /* Begin XCConfigurationList section */ 240 | 189A04AD1E28656700C4C8EB /* Build configuration list for PBXProject "Command Pattern" */ = { 241 | isa = XCConfigurationList; 242 | buildConfigurations = ( 243 | 189A04B71E28656700C4C8EB /* Debug */, 244 | 189A04B81E28656700C4C8EB /* Release */, 245 | ); 246 | defaultConfigurationIsVisible = 0; 247 | defaultConfigurationName = Release; 248 | }; 249 | 189A04B91E28656700C4C8EB /* Build configuration list for PBXNativeTarget "Command Pattern" */ = { 250 | isa = XCConfigurationList; 251 | buildConfigurations = ( 252 | 189A04BA1E28656700C4C8EB /* Debug */, 253 | 189A04BB1E28656700C4C8EB /* Release */, 254 | ); 255 | defaultConfigurationIsVisible = 0; 256 | }; 257 | /* End XCConfigurationList section */ 258 | }; 259 | rootObject = 189A04AA1E28656700C4C8EB /* Project object */; 260 | } 261 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Command Pattern/Command Pattern/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Command Pattern 4 | // 5 | // Created by Don on 2017/1/13. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | -------------------------------------------------------------------------------- /Decorator Pattern/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/Decorator Pattern/.DS_Store -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1A4A71771E2B0A3000853458 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4A71761E2B0A3000853458 /* main.swift */; }; 11 | 1A4A717E1E2B0AB800853458 /* Purchase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4A717D1E2B0AB800853458 /* Purchase.swift */; }; 12 | 1A4A71801E2B0B0200853458 /* CustomerAccount.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4A717F1E2B0B0200853458 /* CustomerAccount.swift */; }; 13 | 1A4A71821E2B0E3200853458 /* Services.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4A71811E2B0E3200853458 /* Services.swift */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXCopyFilesBuildPhase section */ 17 | 1A4A71711E2B0A3000853458 /* 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 | 1A4A71731E2B0A3000853458 /* Decorator Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Decorator Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 30 | 1A4A71761E2B0A3000853458 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 31 | 1A4A717D1E2B0AB800853458 /* Purchase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Purchase.swift; sourceTree = ""; }; 32 | 1A4A717F1E2B0B0200853458 /* CustomerAccount.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomerAccount.swift; sourceTree = ""; }; 33 | 1A4A71811E2B0E3200853458 /* Services.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Services.swift; sourceTree = ""; }; 34 | /* End PBXFileReference section */ 35 | 36 | /* Begin PBXFrameworksBuildPhase section */ 37 | 1A4A71701E2B0A3000853458 /* Frameworks */ = { 38 | isa = PBXFrameworksBuildPhase; 39 | buildActionMask = 2147483647; 40 | files = ( 41 | ); 42 | runOnlyForDeploymentPostprocessing = 0; 43 | }; 44 | /* End PBXFrameworksBuildPhase section */ 45 | 46 | /* Begin PBXGroup section */ 47 | 1A4A716A1E2B0A3000853458 = { 48 | isa = PBXGroup; 49 | children = ( 50 | 1A4A71751E2B0A3000853458 /* Decorator Pattern */, 51 | 1A4A71741E2B0A3000853458 /* Products */, 52 | ); 53 | sourceTree = ""; 54 | }; 55 | 1A4A71741E2B0A3000853458 /* Products */ = { 56 | isa = PBXGroup; 57 | children = ( 58 | 1A4A71731E2B0A3000853458 /* Decorator Pattern */, 59 | ); 60 | name = Products; 61 | sourceTree = ""; 62 | }; 63 | 1A4A71751E2B0A3000853458 /* Decorator Pattern */ = { 64 | isa = PBXGroup; 65 | children = ( 66 | 1A4A71761E2B0A3000853458 /* main.swift */, 67 | 1A4A717D1E2B0AB800853458 /* Purchase.swift */, 68 | 1A4A717F1E2B0B0200853458 /* CustomerAccount.swift */, 69 | 1A4A71811E2B0E3200853458 /* Services.swift */, 70 | ); 71 | path = "Decorator Pattern"; 72 | sourceTree = ""; 73 | }; 74 | /* End PBXGroup section */ 75 | 76 | /* Begin PBXNativeTarget section */ 77 | 1A4A71721E2B0A3000853458 /* Decorator Pattern */ = { 78 | isa = PBXNativeTarget; 79 | buildConfigurationList = 1A4A717A1E2B0A3000853458 /* Build configuration list for PBXNativeTarget "Decorator Pattern" */; 80 | buildPhases = ( 81 | 1A4A716F1E2B0A3000853458 /* Sources */, 82 | 1A4A71701E2B0A3000853458 /* Frameworks */, 83 | 1A4A71711E2B0A3000853458 /* CopyFiles */, 84 | ); 85 | buildRules = ( 86 | ); 87 | dependencies = ( 88 | ); 89 | name = "Decorator Pattern"; 90 | productName = "Decorator Pattern"; 91 | productReference = 1A4A71731E2B0A3000853458 /* Decorator Pattern */; 92 | productType = "com.apple.product-type.tool"; 93 | }; 94 | /* End PBXNativeTarget section */ 95 | 96 | /* Begin PBXProject section */ 97 | 1A4A716B1E2B0A3000853458 /* Project object */ = { 98 | isa = PBXProject; 99 | attributes = { 100 | LastSwiftUpdateCheck = 0820; 101 | LastUpgradeCheck = 0820; 102 | ORGANIZATIONNAME = "don chen"; 103 | TargetAttributes = { 104 | 1A4A71721E2B0A3000853458 = { 105 | CreatedOnToolsVersion = 8.2.1; 106 | DevelopmentTeam = DT8726CBX4; 107 | ProvisioningStyle = Automatic; 108 | }; 109 | }; 110 | }; 111 | buildConfigurationList = 1A4A716E1E2B0A3000853458 /* Build configuration list for PBXProject "Decorator Pattern" */; 112 | compatibilityVersion = "Xcode 3.2"; 113 | developmentRegion = English; 114 | hasScannedForEncodings = 0; 115 | knownRegions = ( 116 | en, 117 | ); 118 | mainGroup = 1A4A716A1E2B0A3000853458; 119 | productRefGroup = 1A4A71741E2B0A3000853458 /* Products */; 120 | projectDirPath = ""; 121 | projectRoot = ""; 122 | targets = ( 123 | 1A4A71721E2B0A3000853458 /* Decorator Pattern */, 124 | ); 125 | }; 126 | /* End PBXProject section */ 127 | 128 | /* Begin PBXSourcesBuildPhase section */ 129 | 1A4A716F1E2B0A3000853458 /* Sources */ = { 130 | isa = PBXSourcesBuildPhase; 131 | buildActionMask = 2147483647; 132 | files = ( 133 | 1A4A717E1E2B0AB800853458 /* Purchase.swift in Sources */, 134 | 1A4A71821E2B0E3200853458 /* Services.swift in Sources */, 135 | 1A4A71771E2B0A3000853458 /* main.swift in Sources */, 136 | 1A4A71801E2B0B0200853458 /* CustomerAccount.swift in Sources */, 137 | ); 138 | runOnlyForDeploymentPostprocessing = 0; 139 | }; 140 | /* End PBXSourcesBuildPhase section */ 141 | 142 | /* Begin XCBuildConfiguration section */ 143 | 1A4A71781E2B0A3000853458 /* Debug */ = { 144 | isa = XCBuildConfiguration; 145 | buildSettings = { 146 | ALWAYS_SEARCH_USER_PATHS = NO; 147 | CLANG_ANALYZER_NONNULL = YES; 148 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 149 | CLANG_CXX_LIBRARY = "libc++"; 150 | CLANG_ENABLE_MODULES = YES; 151 | CLANG_ENABLE_OBJC_ARC = YES; 152 | CLANG_WARN_BOOL_CONVERSION = YES; 153 | CLANG_WARN_CONSTANT_CONVERSION = YES; 154 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 155 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 156 | CLANG_WARN_EMPTY_BODY = YES; 157 | CLANG_WARN_ENUM_CONVERSION = YES; 158 | CLANG_WARN_INFINITE_RECURSION = YES; 159 | CLANG_WARN_INT_CONVERSION = YES; 160 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 161 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 162 | CLANG_WARN_UNREACHABLE_CODE = YES; 163 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 164 | CODE_SIGN_IDENTITY = "-"; 165 | COPY_PHASE_STRIP = NO; 166 | DEBUG_INFORMATION_FORMAT = dwarf; 167 | ENABLE_STRICT_OBJC_MSGSEND = YES; 168 | ENABLE_TESTABILITY = YES; 169 | GCC_C_LANGUAGE_STANDARD = gnu99; 170 | GCC_DYNAMIC_NO_PIC = NO; 171 | GCC_NO_COMMON_BLOCKS = YES; 172 | GCC_OPTIMIZATION_LEVEL = 0; 173 | GCC_PREPROCESSOR_DEFINITIONS = ( 174 | "DEBUG=1", 175 | "$(inherited)", 176 | ); 177 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 178 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 179 | GCC_WARN_UNDECLARED_SELECTOR = YES; 180 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 181 | GCC_WARN_UNUSED_FUNCTION = YES; 182 | GCC_WARN_UNUSED_VARIABLE = YES; 183 | MACOSX_DEPLOYMENT_TARGET = 10.12; 184 | MTL_ENABLE_DEBUG_INFO = YES; 185 | ONLY_ACTIVE_ARCH = YES; 186 | SDKROOT = macosx; 187 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 188 | }; 189 | name = Debug; 190 | }; 191 | 1A4A71791E2B0A3000853458 /* Release */ = { 192 | isa = XCBuildConfiguration; 193 | buildSettings = { 194 | ALWAYS_SEARCH_USER_PATHS = NO; 195 | CLANG_ANALYZER_NONNULL = YES; 196 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 197 | CLANG_CXX_LIBRARY = "libc++"; 198 | CLANG_ENABLE_MODULES = YES; 199 | CLANG_ENABLE_OBJC_ARC = YES; 200 | CLANG_WARN_BOOL_CONVERSION = YES; 201 | CLANG_WARN_CONSTANT_CONVERSION = YES; 202 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 203 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 204 | CLANG_WARN_EMPTY_BODY = YES; 205 | CLANG_WARN_ENUM_CONVERSION = YES; 206 | CLANG_WARN_INFINITE_RECURSION = YES; 207 | CLANG_WARN_INT_CONVERSION = YES; 208 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 209 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 210 | CLANG_WARN_UNREACHABLE_CODE = YES; 211 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 212 | CODE_SIGN_IDENTITY = "-"; 213 | COPY_PHASE_STRIP = NO; 214 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 215 | ENABLE_NS_ASSERTIONS = NO; 216 | ENABLE_STRICT_OBJC_MSGSEND = YES; 217 | GCC_C_LANGUAGE_STANDARD = gnu99; 218 | GCC_NO_COMMON_BLOCKS = YES; 219 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 220 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 221 | GCC_WARN_UNDECLARED_SELECTOR = YES; 222 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 223 | GCC_WARN_UNUSED_FUNCTION = YES; 224 | GCC_WARN_UNUSED_VARIABLE = YES; 225 | MACOSX_DEPLOYMENT_TARGET = 10.12; 226 | MTL_ENABLE_DEBUG_INFO = NO; 227 | SDKROOT = macosx; 228 | }; 229 | name = Release; 230 | }; 231 | 1A4A717B1E2B0A3000853458 /* Debug */ = { 232 | isa = XCBuildConfiguration; 233 | buildSettings = { 234 | DEVELOPMENT_TEAM = DT8726CBX4; 235 | PRODUCT_NAME = "$(TARGET_NAME)"; 236 | SWIFT_VERSION = 3.0; 237 | }; 238 | name = Debug; 239 | }; 240 | 1A4A717C1E2B0A3000853458 /* Release */ = { 241 | isa = XCBuildConfiguration; 242 | buildSettings = { 243 | DEVELOPMENT_TEAM = DT8726CBX4; 244 | PRODUCT_NAME = "$(TARGET_NAME)"; 245 | SWIFT_VERSION = 3.0; 246 | }; 247 | name = Release; 248 | }; 249 | /* End XCBuildConfiguration section */ 250 | 251 | /* Begin XCConfigurationList section */ 252 | 1A4A716E1E2B0A3000853458 /* Build configuration list for PBXProject "Decorator Pattern" */ = { 253 | isa = XCConfigurationList; 254 | buildConfigurations = ( 255 | 1A4A71781E2B0A3000853458 /* Debug */, 256 | 1A4A71791E2B0A3000853458 /* Release */, 257 | ); 258 | defaultConfigurationIsVisible = 0; 259 | defaultConfigurationName = Release; 260 | }; 261 | 1A4A717A1E2B0A3000853458 /* Build configuration list for PBXNativeTarget "Decorator Pattern" */ = { 262 | isa = XCConfigurationList; 263 | buildConfigurations = ( 264 | 1A4A717B1E2B0A3000853458 /* Debug */, 265 | 1A4A717C1E2B0A3000853458 /* Release */, 266 | ); 267 | defaultConfigurationIsVisible = 0; 268 | }; 269 | /* End XCConfigurationList section */ 270 | }; 271 | rootObject = 1A4A716B1E2B0A3000853458 /* Project object */; 272 | } 273 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/CustomerAccount.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CustomerAccount.swift 3 | // Decorator Pattern 4 | // 5 | // Created by don chen on 2017/1/15. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class CustomerAccount { 12 | 13 | let name:String 14 | var purchases = [Purchase]() 15 | 16 | init(name:String) { 17 | self.name = name 18 | } 19 | 20 | func addPurchase(purchase:Purchase) { 21 | self.purchases.append(purchase) 22 | } 23 | 24 | func showAccount() { 25 | var total:Float = 0 26 | print("購物車中包含:") 27 | for item in purchases { 28 | total += item.totalPrice 29 | print("商品 \(item.description) 價格 \(formatCurrencyString(number: item.totalPrice))") 30 | } 31 | } 32 | 33 | func formatCurrencyString(number:Float) -> String { 34 | let formatter = NumberFormatter() 35 | formatter.numberStyle = .currency 36 | return formatter.string(from: NSNumber(value: number))! 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Purchase.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Purchase.swift 3 | // Decorator Pattern 4 | // 5 | // Created by don chen on 2017/1/15. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | 12 | class Purchase { 13 | private let product:String 14 | private let price:Float 15 | 16 | init(product:String, price:Float) { 17 | self.product = product 18 | self.price = price 19 | 20 | } 21 | 22 | var description:String { 23 | return product 24 | } 25 | 26 | var totalPrice:Float { 27 | return price 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/Services.swift: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // Services.swift 4 | // Decorator Pattern 5 | // 6 | // Created by don chen on 2017/1/15. 7 | // Copyright © 2017年 don chen. All rights reserved. 8 | // 9 | 10 | import Foundation 11 | 12 | class PurchaseWithDelivery: BasePurchaseDecorator { 13 | override var description:String { 14 | return "\(super.description) + 運送" 15 | } 16 | 17 | override var totalPrice:Float { 18 | return super.totalPrice + 5 19 | } 20 | } 21 | 22 | class PurchaseWithGiftWrap: BasePurchaseDecorator { 23 | override var description:String { 24 | return "\(super.description) + 禮盒包裝" 25 | } 26 | 27 | override var totalPrice:Float { 28 | return super.totalPrice + 7.5 29 | } 30 | } 31 | 32 | class PurchaseWithCard: BasePurchaseDecorator { 33 | override var description:String { 34 | return "\(super.description) + 卡片" 35 | } 36 | 37 | override var totalPrice: Float { 38 | return super.totalPrice + 3.5 39 | } 40 | } 41 | 42 | class BasePurchaseDecorator: Purchase { 43 | private let wrappedPurchase: Purchase 44 | 45 | init(purchase:Purchase) { 46 | wrappedPurchase = purchase 47 | super.init(product: purchase.description, price: purchase.totalPrice) 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Decorator Pattern/Decorator Pattern/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Decorator Pattern 4 | // 5 | // Created by don chen on 2017/1/15. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | let account = CustomerAccount(name: "Don") 12 | 13 | account.addPurchase(purchase: Purchase(product: "Sandwich", price: 10)) 14 | account.addPurchase(purchase: Purchase(product: "Coffee", price: 20)) 15 | 16 | account.addPurchase(purchase: PurchaseWithGiftWrap(purchase: PurchaseWithDelivery(purchase: Purchase(product: "Chocolate", price: 30)))) 17 | 18 | account.showAccount() 19 | -------------------------------------------------------------------------------- /Facade Pattern/Facade Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 18FE0D101E28DAB3006EE8C6 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18FE0D0F1E28DAB3006EE8C6 /* main.swift */; }; 11 | 18FE0D211E29C515006EE8C6 /* TreasureMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18FE0D201E29C515006EE8C6 /* TreasureMap.swift */; }; 12 | 18FE0D231E29C51B006EE8C6 /* Ship.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18FE0D221E29C51B006EE8C6 /* Ship.swift */; }; 13 | 18FE0D251E29C51F006EE8C6 /* Member.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18FE0D241E29C51F006EE8C6 /* Member.swift */; }; 14 | 18FE0D271E29C59F006EE8C6 /* StoneFacade.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18FE0D261E29C59F006EE8C6 /* StoneFacade.swift */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXCopyFilesBuildPhase section */ 18 | 18FE0D0A1E28DAB2006EE8C6 /* CopyFiles */ = { 19 | isa = PBXCopyFilesBuildPhase; 20 | buildActionMask = 2147483647; 21 | dstPath = /usr/share/man/man1/; 22 | dstSubfolderSpec = 0; 23 | files = ( 24 | ); 25 | runOnlyForDeploymentPostprocessing = 1; 26 | }; 27 | /* End PBXCopyFilesBuildPhase section */ 28 | 29 | /* Begin PBXFileReference section */ 30 | 18FE0D0C1E28DAB3006EE8C6 /* Facade Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Facade Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 31 | 18FE0D0F1E28DAB3006EE8C6 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 32 | 18FE0D201E29C515006EE8C6 /* TreasureMap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TreasureMap.swift; sourceTree = ""; }; 33 | 18FE0D221E29C51B006EE8C6 /* Ship.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Ship.swift; sourceTree = ""; }; 34 | 18FE0D241E29C51F006EE8C6 /* Member.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Member.swift; sourceTree = ""; }; 35 | 18FE0D261E29C59F006EE8C6 /* StoneFacade.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoneFacade.swift; sourceTree = ""; }; 36 | /* End PBXFileReference section */ 37 | 38 | /* Begin PBXFrameworksBuildPhase section */ 39 | 18FE0D091E28DAB2006EE8C6 /* Frameworks */ = { 40 | isa = PBXFrameworksBuildPhase; 41 | buildActionMask = 2147483647; 42 | files = ( 43 | ); 44 | runOnlyForDeploymentPostprocessing = 0; 45 | }; 46 | /* End PBXFrameworksBuildPhase section */ 47 | 48 | /* Begin PBXGroup section */ 49 | 18FE0D031E28DAB2006EE8C6 = { 50 | isa = PBXGroup; 51 | children = ( 52 | 18FE0D0E1E28DAB3006EE8C6 /* Facade Pattern */, 53 | 18FE0D0D1E28DAB3006EE8C6 /* Products */, 54 | ); 55 | sourceTree = ""; 56 | }; 57 | 18FE0D0D1E28DAB3006EE8C6 /* Products */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | 18FE0D0C1E28DAB3006EE8C6 /* Facade Pattern */, 61 | ); 62 | name = Products; 63 | sourceTree = ""; 64 | }; 65 | 18FE0D0E1E28DAB3006EE8C6 /* Facade Pattern */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | 18FE0D0F1E28DAB3006EE8C6 /* main.swift */, 69 | 18FE0D201E29C515006EE8C6 /* TreasureMap.swift */, 70 | 18FE0D221E29C51B006EE8C6 /* Ship.swift */, 71 | 18FE0D241E29C51F006EE8C6 /* Member.swift */, 72 | 18FE0D261E29C59F006EE8C6 /* StoneFacade.swift */, 73 | ); 74 | path = "Facade Pattern"; 75 | sourceTree = ""; 76 | }; 77 | /* End PBXGroup section */ 78 | 79 | /* Begin PBXNativeTarget section */ 80 | 18FE0D0B1E28DAB2006EE8C6 /* Facade Pattern */ = { 81 | isa = PBXNativeTarget; 82 | buildConfigurationList = 18FE0D131E28DAB3006EE8C6 /* Build configuration list for PBXNativeTarget "Facade Pattern" */; 83 | buildPhases = ( 84 | 18FE0D081E28DAB2006EE8C6 /* Sources */, 85 | 18FE0D091E28DAB2006EE8C6 /* Frameworks */, 86 | 18FE0D0A1E28DAB2006EE8C6 /* CopyFiles */, 87 | ); 88 | buildRules = ( 89 | ); 90 | dependencies = ( 91 | ); 92 | name = "Facade Pattern"; 93 | productName = "Facade Pattern"; 94 | productReference = 18FE0D0C1E28DAB3006EE8C6 /* Facade Pattern */; 95 | productType = "com.apple.product-type.tool"; 96 | }; 97 | /* End PBXNativeTarget section */ 98 | 99 | /* Begin PBXProject section */ 100 | 18FE0D041E28DAB2006EE8C6 /* Project object */ = { 101 | isa = PBXProject; 102 | attributes = { 103 | LastSwiftUpdateCheck = 0820; 104 | LastUpgradeCheck = 0820; 105 | ORGANIZATIONNAME = Don; 106 | TargetAttributes = { 107 | 18FE0D0B1E28DAB2006EE8C6 = { 108 | CreatedOnToolsVersion = 8.2.1; 109 | DevelopmentTeam = DT8726CBX4; 110 | ProvisioningStyle = Automatic; 111 | }; 112 | }; 113 | }; 114 | buildConfigurationList = 18FE0D071E28DAB2006EE8C6 /* Build configuration list for PBXProject "Facade Pattern" */; 115 | compatibilityVersion = "Xcode 3.2"; 116 | developmentRegion = English; 117 | hasScannedForEncodings = 0; 118 | knownRegions = ( 119 | en, 120 | ); 121 | mainGroup = 18FE0D031E28DAB2006EE8C6; 122 | productRefGroup = 18FE0D0D1E28DAB3006EE8C6 /* Products */; 123 | projectDirPath = ""; 124 | projectRoot = ""; 125 | targets = ( 126 | 18FE0D0B1E28DAB2006EE8C6 /* Facade Pattern */, 127 | ); 128 | }; 129 | /* End PBXProject section */ 130 | 131 | /* Begin PBXSourcesBuildPhase section */ 132 | 18FE0D081E28DAB2006EE8C6 /* Sources */ = { 133 | isa = PBXSourcesBuildPhase; 134 | buildActionMask = 2147483647; 135 | files = ( 136 | 18FE0D231E29C51B006EE8C6 /* Ship.swift in Sources */, 137 | 18FE0D101E28DAB3006EE8C6 /* main.swift in Sources */, 138 | 18FE0D271E29C59F006EE8C6 /* StoneFacade.swift in Sources */, 139 | 18FE0D251E29C51F006EE8C6 /* Member.swift in Sources */, 140 | 18FE0D211E29C515006EE8C6 /* TreasureMap.swift in Sources */, 141 | ); 142 | runOnlyForDeploymentPostprocessing = 0; 143 | }; 144 | /* End PBXSourcesBuildPhase section */ 145 | 146 | /* Begin XCBuildConfiguration section */ 147 | 18FE0D111E28DAB3006EE8C6 /* Debug */ = { 148 | isa = XCBuildConfiguration; 149 | buildSettings = { 150 | ALWAYS_SEARCH_USER_PATHS = NO; 151 | CLANG_ANALYZER_NONNULL = YES; 152 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 153 | CLANG_CXX_LIBRARY = "libc++"; 154 | CLANG_ENABLE_MODULES = YES; 155 | CLANG_ENABLE_OBJC_ARC = YES; 156 | CLANG_WARN_BOOL_CONVERSION = YES; 157 | CLANG_WARN_CONSTANT_CONVERSION = YES; 158 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 159 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 160 | CLANG_WARN_EMPTY_BODY = YES; 161 | CLANG_WARN_ENUM_CONVERSION = YES; 162 | CLANG_WARN_INFINITE_RECURSION = YES; 163 | CLANG_WARN_INT_CONVERSION = YES; 164 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 165 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 166 | CLANG_WARN_UNREACHABLE_CODE = YES; 167 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 168 | CODE_SIGN_IDENTITY = "-"; 169 | COPY_PHASE_STRIP = NO; 170 | DEBUG_INFORMATION_FORMAT = dwarf; 171 | ENABLE_STRICT_OBJC_MSGSEND = YES; 172 | ENABLE_TESTABILITY = YES; 173 | GCC_C_LANGUAGE_STANDARD = gnu99; 174 | GCC_DYNAMIC_NO_PIC = NO; 175 | GCC_NO_COMMON_BLOCKS = YES; 176 | GCC_OPTIMIZATION_LEVEL = 0; 177 | GCC_PREPROCESSOR_DEFINITIONS = ( 178 | "DEBUG=1", 179 | "$(inherited)", 180 | ); 181 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 182 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 183 | GCC_WARN_UNDECLARED_SELECTOR = YES; 184 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 185 | GCC_WARN_UNUSED_FUNCTION = YES; 186 | GCC_WARN_UNUSED_VARIABLE = YES; 187 | MACOSX_DEPLOYMENT_TARGET = 10.12; 188 | MTL_ENABLE_DEBUG_INFO = YES; 189 | ONLY_ACTIVE_ARCH = YES; 190 | SDKROOT = macosx; 191 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 192 | }; 193 | name = Debug; 194 | }; 195 | 18FE0D121E28DAB3006EE8C6 /* Release */ = { 196 | isa = XCBuildConfiguration; 197 | buildSettings = { 198 | ALWAYS_SEARCH_USER_PATHS = NO; 199 | CLANG_ANALYZER_NONNULL = YES; 200 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 201 | CLANG_CXX_LIBRARY = "libc++"; 202 | CLANG_ENABLE_MODULES = YES; 203 | CLANG_ENABLE_OBJC_ARC = YES; 204 | CLANG_WARN_BOOL_CONVERSION = YES; 205 | CLANG_WARN_CONSTANT_CONVERSION = YES; 206 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 207 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 208 | CLANG_WARN_EMPTY_BODY = YES; 209 | CLANG_WARN_ENUM_CONVERSION = YES; 210 | CLANG_WARN_INFINITE_RECURSION = YES; 211 | CLANG_WARN_INT_CONVERSION = YES; 212 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 213 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 214 | CLANG_WARN_UNREACHABLE_CODE = YES; 215 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 216 | CODE_SIGN_IDENTITY = "-"; 217 | COPY_PHASE_STRIP = NO; 218 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 219 | ENABLE_NS_ASSERTIONS = NO; 220 | ENABLE_STRICT_OBJC_MSGSEND = YES; 221 | GCC_C_LANGUAGE_STANDARD = gnu99; 222 | GCC_NO_COMMON_BLOCKS = YES; 223 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 224 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 225 | GCC_WARN_UNDECLARED_SELECTOR = YES; 226 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 227 | GCC_WARN_UNUSED_FUNCTION = YES; 228 | GCC_WARN_UNUSED_VARIABLE = YES; 229 | MACOSX_DEPLOYMENT_TARGET = 10.12; 230 | MTL_ENABLE_DEBUG_INFO = NO; 231 | SDKROOT = macosx; 232 | }; 233 | name = Release; 234 | }; 235 | 18FE0D141E28DAB3006EE8C6 /* Debug */ = { 236 | isa = XCBuildConfiguration; 237 | buildSettings = { 238 | DEVELOPMENT_TEAM = DT8726CBX4; 239 | PRODUCT_NAME = "$(TARGET_NAME)"; 240 | SWIFT_VERSION = 3.0; 241 | }; 242 | name = Debug; 243 | }; 244 | 18FE0D151E28DAB3006EE8C6 /* Release */ = { 245 | isa = XCBuildConfiguration; 246 | buildSettings = { 247 | DEVELOPMENT_TEAM = DT8726CBX4; 248 | PRODUCT_NAME = "$(TARGET_NAME)"; 249 | SWIFT_VERSION = 3.0; 250 | }; 251 | name = Release; 252 | }; 253 | /* End XCBuildConfiguration section */ 254 | 255 | /* Begin XCConfigurationList section */ 256 | 18FE0D071E28DAB2006EE8C6 /* Build configuration list for PBXProject "Facade Pattern" */ = { 257 | isa = XCConfigurationList; 258 | buildConfigurations = ( 259 | 18FE0D111E28DAB3006EE8C6 /* Debug */, 260 | 18FE0D121E28DAB3006EE8C6 /* Release */, 261 | ); 262 | defaultConfigurationIsVisible = 0; 263 | defaultConfigurationName = Release; 264 | }; 265 | 18FE0D131E28DAB3006EE8C6 /* Build configuration list for PBXNativeTarget "Facade Pattern" */ = { 266 | isa = XCConfigurationList; 267 | buildConfigurations = ( 268 | 18FE0D141E28DAB3006EE8C6 /* Debug */, 269 | 18FE0D151E28DAB3006EE8C6 /* Release */, 270 | ); 271 | defaultConfigurationIsVisible = 0; 272 | }; 273 | /* End XCConfigurationList section */ 274 | }; 275 | rootObject = 18FE0D041E28DAB2006EE8C6 /* Project object */; 276 | } 277 | -------------------------------------------------------------------------------- /Facade Pattern/Facade Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Facade Pattern/Facade Pattern/Member.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Member.swift 3 | // Facade Pattern 4 | // 5 | // Created by Don on 2017/1/14. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class Member { 12 | enum Actions { 13 | case digForGold 14 | case digForSilver 15 | case digForBronze 16 | } 17 | 18 | func performAction(action:Actions, callback:(Int) -> Void) { 19 | var prizeValue = 0 20 | switch action { 21 | case .digForGold: 22 | prizeValue = 5000 23 | 24 | case .digForSilver: 25 | prizeValue = 2000 26 | 27 | case .digForBronze: 28 | prizeValue = 1000 29 | 30 | } 31 | 32 | callback(prizeValue) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Facade Pattern/Facade Pattern/Ship.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Ship.swift 3 | // Facade Pattern 4 | // 5 | // Created by Don on 2017/1/14. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class Ship { 12 | struct ShipLocation { 13 | let NorthSouth:Int 14 | let EastWest:Int 15 | } 16 | 17 | var currentPosition:ShipLocation 18 | 19 | init() { 20 | currentPosition = ShipLocation(NorthSouth: 20, EastWest: 20) 21 | } 22 | 23 | func moveToLocation(location:ShipLocation, callback:(ShipLocation) -> Void) { 24 | currentPosition = location 25 | callback(currentPosition) 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Facade Pattern/Facade Pattern/StoneFacade.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StoneFacade.swift 3 | // Facade Pattern 4 | // 5 | // Created by Don on 2017/1/14. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | enum TreasureTypes { 12 | case gold 13 | case silver 14 | case bronze 15 | } 16 | 17 | class StoneFacade { 18 | private let map = TreasureMap() 19 | private let ship = Ship() 20 | private let member = Member() 21 | 22 | func getTreasure(type: TreasureTypes) -> Int { 23 | var prizeAmount = 0 24 | 25 | var treasureMapType:TreasureMap.Tresures 26 | var memberActionType:Member.Actions 27 | 28 | switch(type) { 29 | case .gold: 30 | treasureMapType = .gold 31 | memberActionType = .digForGold 32 | 33 | case .silver: 34 | treasureMapType = .silver 35 | memberActionType = .digForSilver 36 | 37 | case .bronze: 38 | treasureMapType = .bronze 39 | memberActionType = .digForBronze 40 | } 41 | 42 | let treasureLocation = map.findTresure(type: treasureMapType) 43 | 44 | // convert from map to ship coordinates 45 | let sequence:[Character] = ["A", "B", "C", "D", "E", "F", "G"] 46 | let eastWestPos = sequence.index(of: treasureLocation.gridLetter) 47 | let shipTarget = Ship.ShipLocation(NorthSouth: Int(treasureLocation.gridNumber), EastWest: eastWestPos!) 48 | 49 | // move ship 50 | ship.moveToLocation(location: shipTarget, callback: { location in 51 | self.member.performAction(action: memberActionType, callback: { prize in 52 | prizeAmount = prize 53 | 54 | }) 55 | }) 56 | 57 | return prizeAmount 58 | 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Facade Pattern/Facade Pattern/TreasureMap.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TreasureMap.swift 3 | // Facade Pattern 4 | // 5 | // Created by Don on 2017/1/14. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class TreasureMap { 12 | 13 | enum Tresures { 14 | case gold 15 | case silver 16 | case bronze 17 | } 18 | 19 | struct MapLocation { 20 | let gridLetter: Character 21 | let gridNumber: Int 22 | } 23 | 24 | func findTresure(type:Tresures) -> MapLocation { 25 | switch type { 26 | case .gold: 27 | return MapLocation(gridLetter: "A", gridNumber: 0) 28 | 29 | case .silver: 30 | return MapLocation(gridLetter: "F", gridNumber: 17) 31 | 32 | case .bronze: 33 | return MapLocation(gridLetter: "O", gridNumber: 40) 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Facade Pattern/Facade Pattern/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Facade Pattern 4 | // 5 | // Created by Don on 2017/1/13. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | let map = TreasureMap() 12 | let ship = Ship() 13 | let member = Member() 14 | 15 | let treasureLocation = map.findTresure(type: TreasureMap.Tresures.gold) 16 | 17 | // convert from map to ship coordinates 18 | let sequence:[Character] = ["A", "B", "C", "D", "E", "F", "G"] 19 | let eastWestPos = sequence.index(of: treasureLocation.gridLetter) 20 | let shipTarget = Ship.ShipLocation(NorthSouth: Int(treasureLocation.gridNumber), EastWest: eastWestPos!) 21 | 22 | 23 | // relocate ship 24 | ship.moveToLocation(location: shipTarget, callback: {location in 25 | member.performAction(action: .digForGold, callback: { prize in 26 | print("挖到了 Gold 價值 \(prize) ") 27 | }) 28 | }) 29 | 30 | 31 | 32 | let facade = StoneFacade() 33 | let prize = facade.getTreasure(type: .gold) 34 | print("挖到了 Gold 價值 \(prize)") 35 | 36 | -------------------------------------------------------------------------------- /Factory Method Pattern/Factory Method Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1A10A3591E6A635800837EBD /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A10A3581E6A635800837EBD /* main.swift */; }; 11 | 1A10A3601E6A6E1600837EBD /* ProductFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A10A35F1E6A6E1600837EBD /* ProductFactory.swift */; }; 12 | /* End PBXBuildFile section */ 13 | 14 | /* Begin PBXCopyFilesBuildPhase section */ 15 | 1A10A3531E6A635800837EBD /* CopyFiles */ = { 16 | isa = PBXCopyFilesBuildPhase; 17 | buildActionMask = 2147483647; 18 | dstPath = /usr/share/man/man1/; 19 | dstSubfolderSpec = 0; 20 | files = ( 21 | ); 22 | runOnlyForDeploymentPostprocessing = 1; 23 | }; 24 | /* End PBXCopyFilesBuildPhase section */ 25 | 26 | /* Begin PBXFileReference section */ 27 | 1A10A3551E6A635800837EBD /* Factory Method Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Factory Method Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 28 | 1A10A3581E6A635800837EBD /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 29 | 1A10A35F1E6A6E1600837EBD /* ProductFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProductFactory.swift; sourceTree = ""; }; 30 | /* End PBXFileReference section */ 31 | 32 | /* Begin PBXFrameworksBuildPhase section */ 33 | 1A10A3521E6A635800837EBD /* Frameworks */ = { 34 | isa = PBXFrameworksBuildPhase; 35 | buildActionMask = 2147483647; 36 | files = ( 37 | ); 38 | runOnlyForDeploymentPostprocessing = 0; 39 | }; 40 | /* End PBXFrameworksBuildPhase section */ 41 | 42 | /* Begin PBXGroup section */ 43 | 1A10A34C1E6A635800837EBD = { 44 | isa = PBXGroup; 45 | children = ( 46 | 1A10A3571E6A635800837EBD /* Factory Method Pattern */, 47 | 1A10A3561E6A635800837EBD /* Products */, 48 | ); 49 | sourceTree = ""; 50 | }; 51 | 1A10A3561E6A635800837EBD /* Products */ = { 52 | isa = PBXGroup; 53 | children = ( 54 | 1A10A3551E6A635800837EBD /* Factory Method Pattern */, 55 | ); 56 | name = Products; 57 | sourceTree = ""; 58 | }; 59 | 1A10A3571E6A635800837EBD /* Factory Method Pattern */ = { 60 | isa = PBXGroup; 61 | children = ( 62 | 1A10A3581E6A635800837EBD /* main.swift */, 63 | 1A10A35F1E6A6E1600837EBD /* ProductFactory.swift */, 64 | ); 65 | path = "Factory Method Pattern"; 66 | sourceTree = ""; 67 | }; 68 | /* End PBXGroup section */ 69 | 70 | /* Begin PBXNativeTarget section */ 71 | 1A10A3541E6A635800837EBD /* Factory Method Pattern */ = { 72 | isa = PBXNativeTarget; 73 | buildConfigurationList = 1A10A35C1E6A635800837EBD /* Build configuration list for PBXNativeTarget "Factory Method Pattern" */; 74 | buildPhases = ( 75 | 1A10A3511E6A635800837EBD /* Sources */, 76 | 1A10A3521E6A635800837EBD /* Frameworks */, 77 | 1A10A3531E6A635800837EBD /* CopyFiles */, 78 | ); 79 | buildRules = ( 80 | ); 81 | dependencies = ( 82 | ); 83 | name = "Factory Method Pattern"; 84 | productName = "Factory Method Pattern"; 85 | productReference = 1A10A3551E6A635800837EBD /* Factory Method Pattern */; 86 | productType = "com.apple.product-type.tool"; 87 | }; 88 | /* End PBXNativeTarget section */ 89 | 90 | /* Begin PBXProject section */ 91 | 1A10A34D1E6A635800837EBD /* Project object */ = { 92 | isa = PBXProject; 93 | attributes = { 94 | LastSwiftUpdateCheck = 0820; 95 | LastUpgradeCheck = 0820; 96 | ORGANIZATIONNAME = "Don Chen"; 97 | TargetAttributes = { 98 | 1A10A3541E6A635800837EBD = { 99 | CreatedOnToolsVersion = 8.2.1; 100 | DevelopmentTeam = DT8726CBX4; 101 | ProvisioningStyle = Automatic; 102 | }; 103 | }; 104 | }; 105 | buildConfigurationList = 1A10A3501E6A635800837EBD /* Build configuration list for PBXProject "Factory Method Pattern" */; 106 | compatibilityVersion = "Xcode 3.2"; 107 | developmentRegion = English; 108 | hasScannedForEncodings = 0; 109 | knownRegions = ( 110 | en, 111 | ); 112 | mainGroup = 1A10A34C1E6A635800837EBD; 113 | productRefGroup = 1A10A3561E6A635800837EBD /* Products */; 114 | projectDirPath = ""; 115 | projectRoot = ""; 116 | targets = ( 117 | 1A10A3541E6A635800837EBD /* Factory Method Pattern */, 118 | ); 119 | }; 120 | /* End PBXProject section */ 121 | 122 | /* Begin PBXSourcesBuildPhase section */ 123 | 1A10A3511E6A635800837EBD /* Sources */ = { 124 | isa = PBXSourcesBuildPhase; 125 | buildActionMask = 2147483647; 126 | files = ( 127 | 1A10A3601E6A6E1600837EBD /* ProductFactory.swift in Sources */, 128 | 1A10A3591E6A635800837EBD /* main.swift in Sources */, 129 | ); 130 | runOnlyForDeploymentPostprocessing = 0; 131 | }; 132 | /* End PBXSourcesBuildPhase section */ 133 | 134 | /* Begin XCBuildConfiguration section */ 135 | 1A10A35A1E6A635800837EBD /* Debug */ = { 136 | isa = XCBuildConfiguration; 137 | buildSettings = { 138 | ALWAYS_SEARCH_USER_PATHS = NO; 139 | CLANG_ANALYZER_NONNULL = YES; 140 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 141 | CLANG_CXX_LIBRARY = "libc++"; 142 | CLANG_ENABLE_MODULES = YES; 143 | CLANG_ENABLE_OBJC_ARC = YES; 144 | CLANG_WARN_BOOL_CONVERSION = YES; 145 | CLANG_WARN_CONSTANT_CONVERSION = YES; 146 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 147 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 148 | CLANG_WARN_EMPTY_BODY = YES; 149 | CLANG_WARN_ENUM_CONVERSION = YES; 150 | CLANG_WARN_INFINITE_RECURSION = YES; 151 | CLANG_WARN_INT_CONVERSION = YES; 152 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 153 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 154 | CLANG_WARN_UNREACHABLE_CODE = YES; 155 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 156 | CODE_SIGN_IDENTITY = "-"; 157 | COPY_PHASE_STRIP = NO; 158 | DEBUG_INFORMATION_FORMAT = dwarf; 159 | ENABLE_STRICT_OBJC_MSGSEND = YES; 160 | ENABLE_TESTABILITY = YES; 161 | GCC_C_LANGUAGE_STANDARD = gnu99; 162 | GCC_DYNAMIC_NO_PIC = NO; 163 | GCC_NO_COMMON_BLOCKS = YES; 164 | GCC_OPTIMIZATION_LEVEL = 0; 165 | GCC_PREPROCESSOR_DEFINITIONS = ( 166 | "DEBUG=1", 167 | "$(inherited)", 168 | ); 169 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 170 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 171 | GCC_WARN_UNDECLARED_SELECTOR = YES; 172 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 173 | GCC_WARN_UNUSED_FUNCTION = YES; 174 | GCC_WARN_UNUSED_VARIABLE = YES; 175 | MACOSX_DEPLOYMENT_TARGET = 10.12; 176 | MTL_ENABLE_DEBUG_INFO = YES; 177 | ONLY_ACTIVE_ARCH = YES; 178 | SDKROOT = macosx; 179 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 180 | }; 181 | name = Debug; 182 | }; 183 | 1A10A35B1E6A635800837EBD /* Release */ = { 184 | isa = XCBuildConfiguration; 185 | buildSettings = { 186 | ALWAYS_SEARCH_USER_PATHS = NO; 187 | CLANG_ANALYZER_NONNULL = YES; 188 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 189 | CLANG_CXX_LIBRARY = "libc++"; 190 | CLANG_ENABLE_MODULES = YES; 191 | CLANG_ENABLE_OBJC_ARC = YES; 192 | CLANG_WARN_BOOL_CONVERSION = YES; 193 | CLANG_WARN_CONSTANT_CONVERSION = YES; 194 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 195 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 196 | CLANG_WARN_EMPTY_BODY = YES; 197 | CLANG_WARN_ENUM_CONVERSION = YES; 198 | CLANG_WARN_INFINITE_RECURSION = YES; 199 | CLANG_WARN_INT_CONVERSION = YES; 200 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 201 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 202 | CLANG_WARN_UNREACHABLE_CODE = YES; 203 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 204 | CODE_SIGN_IDENTITY = "-"; 205 | COPY_PHASE_STRIP = NO; 206 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 207 | ENABLE_NS_ASSERTIONS = NO; 208 | ENABLE_STRICT_OBJC_MSGSEND = YES; 209 | GCC_C_LANGUAGE_STANDARD = gnu99; 210 | GCC_NO_COMMON_BLOCKS = YES; 211 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 212 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 213 | GCC_WARN_UNDECLARED_SELECTOR = YES; 214 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 215 | GCC_WARN_UNUSED_FUNCTION = YES; 216 | GCC_WARN_UNUSED_VARIABLE = YES; 217 | MACOSX_DEPLOYMENT_TARGET = 10.12; 218 | MTL_ENABLE_DEBUG_INFO = NO; 219 | SDKROOT = macosx; 220 | }; 221 | name = Release; 222 | }; 223 | 1A10A35D1E6A635800837EBD /* Debug */ = { 224 | isa = XCBuildConfiguration; 225 | buildSettings = { 226 | DEVELOPMENT_TEAM = DT8726CBX4; 227 | PRODUCT_NAME = "$(TARGET_NAME)"; 228 | SWIFT_VERSION = 3.0; 229 | }; 230 | name = Debug; 231 | }; 232 | 1A10A35E1E6A635800837EBD /* Release */ = { 233 | isa = XCBuildConfiguration; 234 | buildSettings = { 235 | DEVELOPMENT_TEAM = DT8726CBX4; 236 | PRODUCT_NAME = "$(TARGET_NAME)"; 237 | SWIFT_VERSION = 3.0; 238 | }; 239 | name = Release; 240 | }; 241 | /* End XCBuildConfiguration section */ 242 | 243 | /* Begin XCConfigurationList section */ 244 | 1A10A3501E6A635800837EBD /* Build configuration list for PBXProject "Factory Method Pattern" */ = { 245 | isa = XCConfigurationList; 246 | buildConfigurations = ( 247 | 1A10A35A1E6A635800837EBD /* Debug */, 248 | 1A10A35B1E6A635800837EBD /* Release */, 249 | ); 250 | defaultConfigurationIsVisible = 0; 251 | defaultConfigurationName = Release; 252 | }; 253 | 1A10A35C1E6A635800837EBD /* Build configuration list for PBXNativeTarget "Factory Method Pattern" */ = { 254 | isa = XCConfigurationList; 255 | buildConfigurations = ( 256 | 1A10A35D1E6A635800837EBD /* Debug */, 257 | 1A10A35E1E6A635800837EBD /* Release */, 258 | ); 259 | defaultConfigurationIsVisible = 0; 260 | }; 261 | /* End XCConfigurationList section */ 262 | }; 263 | rootObject = 1A10A34D1E6A635800837EBD /* Project object */; 264 | } 265 | -------------------------------------------------------------------------------- /Factory Method Pattern/Factory Method Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Factory Method Pattern/Factory Method Pattern/ProductFactory.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ProductFactory.swift 3 | // Factory Method Pattern 4 | // 5 | // Created by don chen on 2017/3/4. 6 | // Copyright © 2017年 Don Chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | enum BoxType { 12 | case smallBox 13 | case bigBox 14 | case largeBox 15 | } 16 | 17 | class ProductFactory { 18 | static let shared = ProductFactory() 19 | private init(){} 20 | 21 | func createProduct(biscuitCount:Int) -> Product { 22 | switch biscuitCount { 23 | case 0...4: 24 | return SmallProduct(biscuitCount: biscuitCount) 25 | case 6...10: 26 | return BigProduct(biscuitCount: biscuitCount) 27 | default: 28 | return LargeProduct(biscuitCount: biscuitCount) 29 | } 30 | } 31 | 32 | } 33 | 34 | class Product { 35 | var name:String 36 | var box:BoxType 37 | var price:Float 38 | 39 | init(name:String, box:BoxType, biscuitCount:Int) { 40 | let price = Float(biscuitCount) * 5.0 41 | 42 | self.name = name 43 | self.box = box 44 | self.price = price 45 | } 46 | } 47 | 48 | class SmallProduct:Product { 49 | init(biscuitCount:Int) { 50 | super.init(name: "Small Product", box: .smallBox, biscuitCount: biscuitCount) 51 | 52 | } 53 | } 54 | 55 | class BigProduct:Product { 56 | init(biscuitCount:Int) { 57 | super.init(name: "Big Product", box: .bigBox, biscuitCount: biscuitCount) 58 | 59 | } 60 | } 61 | 62 | class LargeProduct:Product { 63 | init(biscuitCount:Int) { 64 | super.init(name: "Large Product", box: .largeBox, biscuitCount: biscuitCount) 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Factory Method Pattern/Factory Method Pattern/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Factory Method Pattern 4 | // 5 | // Created by don chen on 2017/3/4. 6 | // Copyright © 2017年 Don Chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | let orderList = [1,3,5,7,9,12,15,18] 12 | 13 | let factory = ProductFactory.shared 14 | var products = [Product]() 15 | for order in orderList { 16 | let product = factory.createProduct(biscuitCount: order) 17 | products.append(product) 18 | } 19 | 20 | for product in products { 21 | print(product.name + " - Price: " + "\(product.price)") 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Flyweight Pattern 4 | // 5 | // Created by Don on 2017/1/7. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | 19 | let homeVC = HomeViewController(nibName: "HomeViewController", bundle: nil) 20 | let NC = UINavigationController(rootViewController: homeVC) 21 | window?.rootViewController = NC 22 | 23 | return true 24 | } 25 | 26 | func applicationWillResignActive(_ application: UIApplication) { 27 | // 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. 28 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 29 | } 30 | 31 | func applicationDidEnterBackground(_ application: UIApplication) { 32 | // 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. 33 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 34 | } 35 | 36 | func applicationWillEnterForeground(_ application: UIApplication) { 37 | // 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. 38 | } 39 | 40 | func applicationDidBecomeActive(_ application: UIApplication) { 41 | // 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. 42 | } 43 | 44 | func applicationWillTerminate(_ application: UIApplication) { 45 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 46 | } 47 | 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight 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 | "info" : { 45 | "version" : 1, 46 | "author" : "xcode" 47 | } 48 | } -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/bg-home.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "bg-home.jpg", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/bg-home.imageset/bg-home.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/Flyweight Pattern/Flyweight Pattern/Assets.xcassets/bg-home.imageset/bg-home.jpg -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/boo.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "boo.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/boo.imageset/boo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/Flyweight Pattern/Flyweight Pattern/Assets.xcassets/boo.imageset/boo.png -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/mario.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "mario.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/mario.imageset/mario.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/Flyweight Pattern/Flyweight Pattern/Assets.xcassets/mario.imageset/mario.png -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/montyMole.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "MontyMole.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/montyMole.imageset/MontyMole.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/Flyweight Pattern/Flyweight Pattern/Assets.xcassets/montyMole.imageset/MontyMole.png -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/peach.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "peach.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/peach.imageset/peach.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/Flyweight Pattern/Flyweight Pattern/Assets.xcassets/peach.imageset/peach.png -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/shyGuy.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "ShyGuy.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/shyGuy.imageset/ShyGuy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/Flyweight Pattern/Flyweight Pattern/Assets.xcassets/shyGuy.imageset/ShyGuy.png -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/star.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "star.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/star.imageset/star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/Flyweight Pattern/Flyweight Pattern/Assets.xcassets/star.imageset/star.png -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/turtle-blue.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "turtle-blue.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/turtle-blue.imageset/turtle-blue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/Flyweight Pattern/Flyweight Pattern/Assets.xcassets/turtle-blue.imageset/turtle-blue.png -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/turtle-green.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "turtle-green.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Assets.xcassets/turtle-green.imageset/turtle-green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamdon/Swift-Design-Pattern/ddb76a6dc5120ebbf8bf3c0e4b1789307606b0ce/Flyweight Pattern/Flyweight Pattern/Assets.xcassets/turtle-green.imageset/turtle-green.png -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight 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 | 27 | 28 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight 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 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Character.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Character.swift 3 | // Flyweight Pattern 4 | // 5 | // Created by Don on 2017/1/8. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class Character: UIImageView { 12 | 13 | override func draw(_ rect: CGRect) { 14 | // Drawing code 15 | image?.draw(in: rect) 16 | } 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Flyweight.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Flyweight.swift 3 | // Flyweight Pattern 4 | // 5 | // Created by Don on 2017/1/8. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | 12 | enum CharacterType:Int { 13 | case mario 14 | case montyMole 15 | case peach 16 | case shyguy 17 | case star 18 | case turtleBlue 19 | case turtleGreen 20 | case boo 21 | } 22 | 23 | class FlyweightFactory { 24 | var characterImages = [CharacterType:UIImage]() 25 | 26 | func createFlyweight(type:CharacterType) -> Character { 27 | 28 | var image = characterImages[type] 29 | if image == nil { 30 | switch type { 31 | case .mario: 32 | image = UIImage(named: "mario") 33 | characterImages[type] = image 34 | 35 | case .montyMole: 36 | image = UIImage(named: "montyMole") 37 | characterImages[type] = image 38 | 39 | case .peach: 40 | image = UIImage(named: "peach") 41 | characterImages[type] = image 42 | 43 | case .shyguy: 44 | image = UIImage(named: "shyguy") 45 | characterImages[type] = image 46 | 47 | case .star: 48 | image = UIImage(named: "star") 49 | characterImages[type] = image 50 | 51 | case .turtleBlue: 52 | image = UIImage(named: "turtle-blue") 53 | characterImages[type] = image 54 | 55 | case .turtleGreen: 56 | image = UIImage(named: "turtle-green") 57 | characterImages[type] = image 58 | 59 | case .boo: 60 | image = UIImage(named: "boo") 61 | characterImages[type] = image 62 | } 63 | } 64 | 65 | 66 | 67 | return Character(image: image) 68 | } 69 | 70 | 71 | } 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/FlyweightViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // FlyweightViewController.swift 3 | // Flyweight Pattern 4 | // 5 | // Created by Don on 2017/1/7. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class FlyweightViewController: UIViewController { 12 | 13 | let factory = FlyweightFactory() 14 | 15 | override func viewDidLoad() { 16 | super.viewDidLoad() 17 | 18 | } 19 | 20 | override func didReceiveMemoryWarning() { 21 | super.didReceiveMemoryWarning() 22 | // Dispose of any resources that can be recreated. 23 | } 24 | 25 | override func viewWillAppear(_ animated: Bool) { 26 | navigationController?.navigationBar.isHidden = true 27 | 28 | } 29 | 30 | override func viewWillDisappear(_ animated: Bool) { 31 | navigationController?.navigationBar.isHidden = false 32 | } 33 | 34 | 35 | override func viewDidAppear(_ animated: Bool) { 36 | for _ in 0...50000 { 37 | showWorld() 38 | } 39 | } 40 | 41 | func showWorld() { 42 | let screenBounds = UIScreen.main.bounds 43 | let screenWidth = screenBounds.width 44 | let screenHeight = screenBounds.height 45 | let minWidth = CGFloat(20) 46 | 47 | let randomX = CGFloat(arc4random_uniform(UInt32(screenWidth))) 48 | let randomY = CGFloat(arc4random_uniform(UInt32(screenHeight))) 49 | let randomWidth = CGFloat(max(CGFloat(arc4random_uniform(50)), minWidth)) 50 | 51 | // imageView 52 | let imageFrame = CGRect(x: randomX, y: randomY, width: randomWidth, height: randomWidth) 53 | 54 | let randomType = Int(arc4random_uniform(8)) 55 | let character = factory.createFlyweight(type: CharacterType(rawValue:randomType)!) 56 | character.frame = imageFrame 57 | 58 | view.addSubview(character) 59 | 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/FlyweightViewController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/HomeViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // HomeViewController.swift 3 | // Flyweight Pattern 4 | // 5 | // Created by Don on 2017/1/7. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class HomeViewController: UIViewController { 12 | 13 | @IBOutlet var nomalButton: UIButton! 14 | @IBOutlet var flyweightButton: UIButton! 15 | 16 | override func viewDidLoad() { 17 | super.viewDidLoad() 18 | 19 | title = "Flyweight Pattern" 20 | } 21 | 22 | override func didReceiveMemoryWarning() { 23 | super.didReceiveMemoryWarning() 24 | // Dispose of any resources that can be recreated. 25 | } 26 | 27 | @IBAction func didTapNormal(_ sender: Any) { 28 | let VC = NormalViewController(nibName: "NormalViewController", bundle: nil) 29 | navigationController?.pushViewController(VC, animated: true) 30 | } 31 | 32 | @IBAction func didTapFlyweight(_ sender: Any) { 33 | let VC = FlyweightViewController(nibName: "FlyweightViewController", bundle: nil) 34 | navigationController?.pushViewController(VC, animated: true) 35 | } 36 | 37 | @IBAction func didTapFlyweight2(_ sender: Any) { 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/HomeViewController.xib: -------------------------------------------------------------------------------- 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 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 47 | 60 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 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 | 38 | 39 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/NormalViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // NormalViewController.swift 3 | // Flyweight Pattern 4 | // 5 | // Created by Don on 2017/1/7. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class NormalViewController: UIViewController { 12 | override var prefersStatusBarHidden: Bool { 13 | return true 14 | } 15 | 16 | override func viewDidLoad() { 17 | super.viewDidLoad() 18 | 19 | // Do any additional setup after loading the view. 20 | } 21 | 22 | override func viewWillAppear(_ animated: Bool) { 23 | navigationController?.navigationBar.isHidden = true 24 | 25 | } 26 | 27 | override func viewWillDisappear(_ animated: Bool) { 28 | navigationController?.navigationBar.isHidden = false 29 | } 30 | 31 | override func viewDidAppear(_ animated: Bool) { 32 | for _ in 0...50000 { 33 | showWorld() 34 | } 35 | } 36 | 37 | func showWorld() { 38 | let screenBounds = UIScreen.main.bounds 39 | let screenWidth = screenBounds.width 40 | let screenHeight = screenBounds.height 41 | let minWidth = CGFloat(20) 42 | 43 | let randomX = CGFloat(arc4random_uniform(UInt32(screenWidth))) 44 | let randomY = CGFloat(arc4random_uniform(UInt32(screenHeight))) 45 | let randomWidth = CGFloat(max(CGFloat(arc4random_uniform(50)), minWidth)) 46 | 47 | // imageView 48 | let imageFrame = CGRect(x: randomX, y: randomY, width: randomWidth, height: randomWidth) 49 | let imageView = UIImageView(frame: imageFrame) 50 | imageView.contentMode = .scaleAspectFit 51 | 52 | // image 53 | let imageRow = Int(arc4random_uniform(8)) 54 | if let image = getImage(row:imageRow) { 55 | imageView.image = image 56 | } 57 | 58 | view.addSubview(imageView) 59 | 60 | } 61 | 62 | func getImage(row:Int) -> UIImage? { 63 | if let imageType = CharacterType(rawValue: row) { 64 | switch imageType { 65 | case .mario: 66 | return UIImage(named: "mario") 67 | 68 | case .montyMole: 69 | return UIImage(named: "MontyMole") 70 | 71 | case .peach: 72 | return UIImage(named: "peach") 73 | 74 | case .shyguy: 75 | return UIImage(named: "ShyGuy") 76 | 77 | case .star: 78 | return UIImage(named: "star") 79 | 80 | case .turtleBlue: 81 | return UIImage(named: "turtle-blue") 82 | 83 | case .turtleGreen: 84 | return UIImage(named: "turtle-green") 85 | 86 | case .boo: 87 | return UIImage(named: "boo") 88 | 89 | } 90 | } else { 91 | return nil 92 | 93 | } 94 | 95 | 96 | } 97 | 98 | 99 | 100 | 101 | } 102 | -------------------------------------------------------------------------------- /Flyweight Pattern/Flyweight Pattern/NormalViewController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Mediator Pattern/Mediator Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1870623F1E2688C3004327D6 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1870623E1E2688C3004327D6 /* main.swift */; }; 11 | 187062491E271C56004327D6 /* TestCar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 187062481E271C56004327D6 /* TestCar.swift */; }; 12 | 1870624C1E272635004327D6 /* Car.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1870624B1E272635004327D6 /* Car.swift */; }; 13 | 1870624E1E272FA6004327D6 /* Mediator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1870624D1E272FA6004327D6 /* Mediator.swift */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXCopyFilesBuildPhase section */ 17 | 187062391E2688C2004327D6 /* 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 | 1870623B1E2688C3004327D6 /* Mediator Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Mediator Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 30 | 1870623E1E2688C3004327D6 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 31 | 187062481E271C56004327D6 /* TestCar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestCar.swift; sourceTree = ""; }; 32 | 1870624B1E272635004327D6 /* Car.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Car.swift; sourceTree = ""; }; 33 | 1870624D1E272FA6004327D6 /* Mediator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Mediator.swift; sourceTree = ""; }; 34 | /* End PBXFileReference section */ 35 | 36 | /* Begin PBXFrameworksBuildPhase section */ 37 | 187062381E2688C2004327D6 /* Frameworks */ = { 38 | isa = PBXFrameworksBuildPhase; 39 | buildActionMask = 2147483647; 40 | files = ( 41 | ); 42 | runOnlyForDeploymentPostprocessing = 0; 43 | }; 44 | /* End PBXFrameworksBuildPhase section */ 45 | 46 | /* Begin PBXGroup section */ 47 | 187062321E2688C2004327D6 = { 48 | isa = PBXGroup; 49 | children = ( 50 | 1870623D1E2688C3004327D6 /* Mediator Pattern */, 51 | 1870623C1E2688C3004327D6 /* Products */, 52 | ); 53 | sourceTree = ""; 54 | }; 55 | 1870623C1E2688C3004327D6 /* Products */ = { 56 | isa = PBXGroup; 57 | children = ( 58 | 1870623B1E2688C3004327D6 /* Mediator Pattern */, 59 | ); 60 | name = Products; 61 | sourceTree = ""; 62 | }; 63 | 1870623D1E2688C3004327D6 /* Mediator Pattern */ = { 64 | isa = PBXGroup; 65 | children = ( 66 | 1870623E1E2688C3004327D6 /* main.swift */, 67 | 187062451E2688C9004327D6 /* Test Way */, 68 | 1870624A1E272627004327D6 /* New Way */, 69 | ); 70 | path = "Mediator Pattern"; 71 | sourceTree = ""; 72 | }; 73 | 187062451E2688C9004327D6 /* Test Way */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | 187062481E271C56004327D6 /* TestCar.swift */, 77 | ); 78 | name = "Test Way"; 79 | sourceTree = ""; 80 | }; 81 | 1870624A1E272627004327D6 /* New Way */ = { 82 | isa = PBXGroup; 83 | children = ( 84 | 1870624B1E272635004327D6 /* Car.swift */, 85 | 1870624D1E272FA6004327D6 /* Mediator.swift */, 86 | ); 87 | name = "New Way"; 88 | sourceTree = ""; 89 | }; 90 | /* End PBXGroup section */ 91 | 92 | /* Begin PBXNativeTarget section */ 93 | 1870623A1E2688C2004327D6 /* Mediator Pattern */ = { 94 | isa = PBXNativeTarget; 95 | buildConfigurationList = 187062421E2688C3004327D6 /* Build configuration list for PBXNativeTarget "Mediator Pattern" */; 96 | buildPhases = ( 97 | 187062371E2688C2004327D6 /* Sources */, 98 | 187062381E2688C2004327D6 /* Frameworks */, 99 | 187062391E2688C2004327D6 /* CopyFiles */, 100 | ); 101 | buildRules = ( 102 | ); 103 | dependencies = ( 104 | ); 105 | name = "Mediator Pattern"; 106 | productName = "Mediator Pattern"; 107 | productReference = 1870623B1E2688C3004327D6 /* Mediator Pattern */; 108 | productType = "com.apple.product-type.tool"; 109 | }; 110 | /* End PBXNativeTarget section */ 111 | 112 | /* Begin PBXProject section */ 113 | 187062331E2688C2004327D6 /* Project object */ = { 114 | isa = PBXProject; 115 | attributes = { 116 | LastSwiftUpdateCheck = 0820; 117 | LastUpgradeCheck = 0820; 118 | ORGANIZATIONNAME = Don; 119 | TargetAttributes = { 120 | 1870623A1E2688C2004327D6 = { 121 | CreatedOnToolsVersion = 8.2.1; 122 | DevelopmentTeam = DT8726CBX4; 123 | ProvisioningStyle = Automatic; 124 | }; 125 | }; 126 | }; 127 | buildConfigurationList = 187062361E2688C2004327D6 /* Build configuration list for PBXProject "Mediator Pattern" */; 128 | compatibilityVersion = "Xcode 3.2"; 129 | developmentRegion = English; 130 | hasScannedForEncodings = 0; 131 | knownRegions = ( 132 | en, 133 | ); 134 | mainGroup = 187062321E2688C2004327D6; 135 | productRefGroup = 1870623C1E2688C3004327D6 /* Products */; 136 | projectDirPath = ""; 137 | projectRoot = ""; 138 | targets = ( 139 | 1870623A1E2688C2004327D6 /* Mediator Pattern */, 140 | ); 141 | }; 142 | /* End PBXProject section */ 143 | 144 | /* Begin PBXSourcesBuildPhase section */ 145 | 187062371E2688C2004327D6 /* Sources */ = { 146 | isa = PBXSourcesBuildPhase; 147 | buildActionMask = 2147483647; 148 | files = ( 149 | 187062491E271C56004327D6 /* TestCar.swift in Sources */, 150 | 1870623F1E2688C3004327D6 /* main.swift in Sources */, 151 | 1870624E1E272FA6004327D6 /* Mediator.swift in Sources */, 152 | 1870624C1E272635004327D6 /* Car.swift in Sources */, 153 | ); 154 | runOnlyForDeploymentPostprocessing = 0; 155 | }; 156 | /* End PBXSourcesBuildPhase section */ 157 | 158 | /* Begin XCBuildConfiguration section */ 159 | 187062401E2688C3004327D6 /* Debug */ = { 160 | isa = XCBuildConfiguration; 161 | buildSettings = { 162 | ALWAYS_SEARCH_USER_PATHS = NO; 163 | CLANG_ANALYZER_NONNULL = YES; 164 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 165 | CLANG_CXX_LIBRARY = "libc++"; 166 | CLANG_ENABLE_MODULES = YES; 167 | CLANG_ENABLE_OBJC_ARC = YES; 168 | CLANG_WARN_BOOL_CONVERSION = YES; 169 | CLANG_WARN_CONSTANT_CONVERSION = YES; 170 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 171 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 172 | CLANG_WARN_EMPTY_BODY = YES; 173 | CLANG_WARN_ENUM_CONVERSION = YES; 174 | CLANG_WARN_INFINITE_RECURSION = YES; 175 | CLANG_WARN_INT_CONVERSION = YES; 176 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 177 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 178 | CLANG_WARN_UNREACHABLE_CODE = YES; 179 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 180 | CODE_SIGN_IDENTITY = "-"; 181 | COPY_PHASE_STRIP = NO; 182 | DEBUG_INFORMATION_FORMAT = dwarf; 183 | ENABLE_STRICT_OBJC_MSGSEND = YES; 184 | ENABLE_TESTABILITY = YES; 185 | GCC_C_LANGUAGE_STANDARD = gnu99; 186 | GCC_DYNAMIC_NO_PIC = NO; 187 | GCC_NO_COMMON_BLOCKS = YES; 188 | GCC_OPTIMIZATION_LEVEL = 0; 189 | GCC_PREPROCESSOR_DEFINITIONS = ( 190 | "DEBUG=1", 191 | "$(inherited)", 192 | ); 193 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 194 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 195 | GCC_WARN_UNDECLARED_SELECTOR = YES; 196 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 197 | GCC_WARN_UNUSED_FUNCTION = YES; 198 | GCC_WARN_UNUSED_VARIABLE = YES; 199 | MACOSX_DEPLOYMENT_TARGET = 10.12; 200 | MTL_ENABLE_DEBUG_INFO = YES; 201 | ONLY_ACTIVE_ARCH = YES; 202 | SDKROOT = macosx; 203 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 204 | }; 205 | name = Debug; 206 | }; 207 | 187062411E2688C3004327D6 /* Release */ = { 208 | isa = XCBuildConfiguration; 209 | buildSettings = { 210 | ALWAYS_SEARCH_USER_PATHS = NO; 211 | CLANG_ANALYZER_NONNULL = YES; 212 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 213 | CLANG_CXX_LIBRARY = "libc++"; 214 | CLANG_ENABLE_MODULES = YES; 215 | CLANG_ENABLE_OBJC_ARC = YES; 216 | CLANG_WARN_BOOL_CONVERSION = YES; 217 | CLANG_WARN_CONSTANT_CONVERSION = YES; 218 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 219 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 220 | CLANG_WARN_EMPTY_BODY = YES; 221 | CLANG_WARN_ENUM_CONVERSION = YES; 222 | CLANG_WARN_INFINITE_RECURSION = YES; 223 | CLANG_WARN_INT_CONVERSION = YES; 224 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 225 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 226 | CLANG_WARN_UNREACHABLE_CODE = YES; 227 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 228 | CODE_SIGN_IDENTITY = "-"; 229 | COPY_PHASE_STRIP = NO; 230 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 231 | ENABLE_NS_ASSERTIONS = NO; 232 | ENABLE_STRICT_OBJC_MSGSEND = YES; 233 | GCC_C_LANGUAGE_STANDARD = gnu99; 234 | GCC_NO_COMMON_BLOCKS = YES; 235 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 236 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 237 | GCC_WARN_UNDECLARED_SELECTOR = YES; 238 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 239 | GCC_WARN_UNUSED_FUNCTION = YES; 240 | GCC_WARN_UNUSED_VARIABLE = YES; 241 | MACOSX_DEPLOYMENT_TARGET = 10.12; 242 | MTL_ENABLE_DEBUG_INFO = NO; 243 | SDKROOT = macosx; 244 | }; 245 | name = Release; 246 | }; 247 | 187062431E2688C3004327D6 /* Debug */ = { 248 | isa = XCBuildConfiguration; 249 | buildSettings = { 250 | DEVELOPMENT_TEAM = DT8726CBX4; 251 | PRODUCT_NAME = "$(TARGET_NAME)"; 252 | SWIFT_VERSION = 3.0; 253 | }; 254 | name = Debug; 255 | }; 256 | 187062441E2688C3004327D6 /* Release */ = { 257 | isa = XCBuildConfiguration; 258 | buildSettings = { 259 | DEVELOPMENT_TEAM = DT8726CBX4; 260 | PRODUCT_NAME = "$(TARGET_NAME)"; 261 | SWIFT_VERSION = 3.0; 262 | }; 263 | name = Release; 264 | }; 265 | /* End XCBuildConfiguration section */ 266 | 267 | /* Begin XCConfigurationList section */ 268 | 187062361E2688C2004327D6 /* Build configuration list for PBXProject "Mediator Pattern" */ = { 269 | isa = XCConfigurationList; 270 | buildConfigurations = ( 271 | 187062401E2688C3004327D6 /* Debug */, 272 | 187062411E2688C3004327D6 /* Release */, 273 | ); 274 | defaultConfigurationIsVisible = 0; 275 | defaultConfigurationName = Release; 276 | }; 277 | 187062421E2688C3004327D6 /* Build configuration list for PBXNativeTarget "Mediator Pattern" */ = { 278 | isa = XCConfigurationList; 279 | buildConfigurations = ( 280 | 187062431E2688C3004327D6 /* Debug */, 281 | 187062441E2688C3004327D6 /* Release */, 282 | ); 283 | defaultConfigurationIsVisible = 0; 284 | }; 285 | /* End XCConfigurationList section */ 286 | }; 287 | rootObject = 187062331E2688C2004327D6 /* Project object */; 288 | } 289 | -------------------------------------------------------------------------------- /Mediator Pattern/Mediator Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Mediator Pattern/Mediator Pattern/Car.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Car.swift 3 | // Mediator Pattern 4 | // 5 | // Created by Don on 2017/1/12. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | struct Position { 12 | var x:Int 13 | } 14 | 15 | class Car: Peer { 16 | var name:String 17 | var currentPosition:Position 18 | var mediator:Mediator 19 | 20 | init(name:String, pos:Position, mediator: Mediator) { 21 | self.name = name 22 | self.currentPosition = pos 23 | self.mediator = mediator 24 | 25 | mediator.registerPeer(peer: self) 26 | } 27 | 28 | func checkIsOtherCarsTooClose(position: Position) -> Bool { 29 | return abs(position.x - self.currentPosition.x) < 5 30 | } 31 | 32 | func changePosition(newPosition:Position) { 33 | self.currentPosition = newPosition 34 | print("\(name) 移動到了 \(self.currentPosition.x)") 35 | 36 | if(mediator.changePosition(peer: self, pos: self.currentPosition)) { 37 | print("\(name) 太靠近其他車了,慢一點)") 38 | } 39 | } 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Mediator Pattern/Mediator Pattern/Mediator.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Mediator.swift 3 | // Mediator Pattern 4 | // 5 | // Created by Don on 2017/1/12. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | protocol Peer { 12 | var name:String {get} 13 | func checkIsOtherCarsTooClose(position:Position) -> Bool 14 | } 15 | 16 | protocol Mediator { 17 | func registerPeer(peer:Peer) 18 | func unregisterPeer(peer:Peer) 19 | func changePosition(peer:Peer, pos:Position) -> Bool 20 | } 21 | 22 | class CarMediator: Mediator { 23 | private var peers:[String:Peer] 24 | 25 | init() { 26 | peers = [String:Peer]() 27 | 28 | } 29 | 30 | func registerPeer(peer: Peer) { 31 | self.peers[peer.name] = peer 32 | } 33 | 34 | func unregisterPeer(peer: Peer) { 35 | self.peers.removeValue(forKey: peer.name) 36 | } 37 | 38 | func changePosition(peer: Peer, pos: Position) -> Bool { 39 | for storedPeer in peers.values { 40 | if peer.name != storedPeer.name && storedPeer.checkIsOtherCarsTooClose(position: pos) { 41 | return true 42 | } 43 | } 44 | return false 45 | } 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Mediator Pattern/Mediator Pattern/TestCar.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TestCar.swift 3 | // Mediator Pattern 4 | // 5 | // Created by Don on 2017/1/12. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | struct TestPosition { 12 | var x:Int 13 | } 14 | 15 | class TestCar { 16 | var carName:String 17 | var currentPosition:TestPosition 18 | private var otherCars:[TestCar] 19 | 20 | init(carName:String, position:TestPosition) { 21 | self.carName = carName 22 | self.currentPosition = position 23 | self.otherCars = [TestCar]() 24 | } 25 | 26 | // 加入關注的汽車對象 27 | func addCarsInArea(cars:TestCar...) { 28 | for car in cars { 29 | otherCars.append(car) 30 | } 31 | } 32 | 33 | // 當有車輛改變位置時,檢查是不是太靠近了。 34 | func checkIsOtherCarsTooClose(car:TestCar) -> Bool { 35 | return abs(currentPosition.x - car.currentPosition.x) < 5 36 | } 37 | 38 | // 更新目前車輛的位置 39 | func changePosition(newPosition:TestPosition) { 40 | self.currentPosition = newPosition 41 | 42 | for car in otherCars { 43 | if car.checkIsOtherCarsTooClose(car: self) { 44 | print("\(carName), 慢一點,太靠近 \(car.carName) 了") 45 | } 46 | } 47 | print("\(carName) 移動到了位置 \(currentPosition.x)") 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Mediator Pattern/Mediator Pattern/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Mediator Pattern 4 | // 5 | // Created by Don on 2017/1/11. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | 12 | // 舊系統測試 13 | //let stoneTestCar = TestCar(carName: "Stone Car", position: TestPosition(x: 0)) 14 | //let woodTestCar = TestCar(carName: "Wood Car", position: TestPosition(x:10)) 15 | //let ironTestCar = TestCar(carName: "Iron Car", position: TestPosition(x:20)) 16 | //let waterTestCar = TestCar(carName: "Water Car", position: TestPosition(x:30)) 17 | // 18 | //stoneTestCar.addCarsInArea(cars: woodTestCar, ironTestCar, waterTestCar) 19 | //woodTestCar.addCarsInArea(cars: stoneTestCar, ironTestCar, waterTestCar) 20 | //ironTestCar.addCarsInArea(cars: stoneTestCar, woodTestCar, waterTestCar) 21 | //waterTestCar.addCarsInArea(cars: stoneTestCar, woodTestCar, ironTestCar) 22 | // 23 | // 24 | //woodTestCar.changePosition(newPosition: TestPosition(x: 17)) 25 | 26 | 27 | // 加入 中介者模式後的測試 28 | let mediator:Mediator = CarMediator() 29 | 30 | let stoneCar = Car(name: "Stone Car", pos: Position(x:0), mediator: mediator) 31 | let woodCar = Car(name: "Wood Car", pos: Position(x:10), mediator: mediator) 32 | let ironCar = Car(name: "Iron Car", pos: Position(x:20),mediator: mediator) 33 | let waterCar = Car(name: "Water Car", pos: Position(x:30), mediator: mediator) 34 | 35 | woodCar.changePosition(newPosition: Position(x: 17)) 36 | 37 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/Observer.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Observer.swift 3 | // Observer Pattern 4 | // 5 | // Created by Don on 2017/1/10. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | protocol Observer:class { 12 | func notify(item:String) 13 | } 14 | 15 | protocol Subject { 16 | func addObservers(observers:Observer...) 17 | func removeObserver(observer:Observer) 18 | func sendNotifications(item:String) 19 | } 20 | 21 | class SubjectBase: Subject { 22 | private var observers = [Observer]() 23 | 24 | func addObservers(observers:Observer...) { 25 | for item in observers { 26 | self.observers.append(item) 27 | } 28 | } 29 | 30 | func removeObserver(observer:Observer) { 31 | self.observers = self.observers.filter{ $0 !== observer } 32 | } 33 | 34 | // to announce today's special 35 | func sendNotifications(item:String) { 36 | for observer in observers { 37 | observer.notify(item: item) 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/StoneBakery.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StoneBakery.swift 3 | // Observer Pattern 4 | // 5 | // Created by Don on 2017/1/10. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class StoneBakery:SubjectBase { 12 | func announceTodaySpecial(item:String) { 13 | sendNotifications(item: item) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/StoneFireStation.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StoneFireStation.swift 3 | // Observer Pattern 4 | // 5 | // Created by Don on 2017/1/10. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class StoneFireStation:Observer { 12 | func notify(item: String) { 13 | notifyFiremanTodaySpecial(item: item) 14 | } 15 | 16 | func notifyFiremanTodaySpecial(item:String) { 17 | print("notified fireman today's special \(item)") 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/StoneHospital.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StoneHospital.swift 3 | // Observer Pattern 4 | // 5 | // Created by Don on 2017/1/10. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class StoneHospital:Observer { 12 | func notify(item: String) { 13 | notifyDoctorsTodaySpecial(item: item) 14 | } 15 | 16 | func notifyDoctorsTodaySpecial(item:String) { 17 | print("notified doctors today's special \(item)") 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/StoneSchool.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StoneSchool.swift 3 | // Observer Pattern 4 | // 5 | // Created by Don on 2017/1/10. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class StoneSchool:Observer { 12 | func notify(item: String) { 13 | notifyStudentsTodaySpecial(item: item) 14 | } 15 | 16 | func notifyStudentsTodaySpecial(item:String) { 17 | print("notified students today's special is \(item)") 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/WoodBakery.swift: -------------------------------------------------------------------------------- 1 | // 2 | // WoodBakery.swift 3 | // Observer Pattern 4 | // 5 | // Created by Don on 2017/1/10. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class WoodBakery { 12 | private let school = WoodSchool() 13 | private let fireStation = WoodFireStation() 14 | private let hospital = WoodHospital() 15 | 16 | func announceTodaySpecial(item:String) { 17 | school.notifyStudentsTodaySpecial(item: item) 18 | fireStation.notifyFiremanTodaySpecial(item: item) 19 | hospital.notifyDoctorsTodaySpecial(item: item) 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/WoodFireStation.swift: -------------------------------------------------------------------------------- 1 | // 2 | // WoodFireStation.swift 3 | // Observer Pattern 4 | // 5 | // Created by Don on 2017/1/10. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class WoodFireStation { 12 | func notifyFiremanTodaySpecial(item:String) { 13 | print("notified fireman today's special \(item)") 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/WoodHospital.swift: -------------------------------------------------------------------------------- 1 | // 2 | // WoodHospital.swift 3 | // Observer Pattern 4 | // 5 | // Created by Don on 2017/1/10. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class WoodHospital { 12 | func notifyDoctorsTodaySpecial(item:String) { 13 | print("notified doctors today's special \(item)") 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/WoodSchool.swift: -------------------------------------------------------------------------------- 1 | // 2 | // WoodSchool.swift 3 | // Observer Pattern 4 | // 5 | // Created by Don on 2017/1/10. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class WoodSchool { 12 | func notifyStudentsTodaySpecial(item:String) { 13 | print("notified students today's special is \(item)") 14 | } 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /Observer Pattern/Observer Pattern/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Observer Pattern 4 | // 5 | // Created by Don on 2017/1/9. 6 | // Copyright © 2017年 Don. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | print("==== Wood City ====") 12 | let woodBakery = WoodBakery() 13 | woodBakery.announceTodaySpecial(item: "donutes") 14 | 15 | print("==== Stone City ====") 16 | let school = StoneSchool() 17 | let fireStation = StoneFireStation() 18 | let hospital = StoneFireStation() 19 | 20 | let bakery = StoneBakery() 21 | bakery.addObservers(observers: school, fireStation, hospital) 22 | bakery.sendNotifications(item: "French bread") 23 | 24 | print("----- remove fireStation -----") 25 | bakery.removeObserver(observer: fireStation) 26 | bakery.sendNotifications(item: "baguette") 27 | 28 | 29 | 30 | var numbers = [1,2,3,4,5,6] 31 | var newNumbers = numbers.filter{ $0 == 2 } 32 | print(newNumbers) 33 | -------------------------------------------------------------------------------- /Prototype Pattern/Prototype Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1A899B1E1E3DB3560086ADDC /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A899B1D1E3DB3560086ADDC /* main.swift */; }; 11 | 1A899B251E3DBAB00086ADDC /* ValueTypeShop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A899B241E3DBAB00086ADDC /* ValueTypeShop.swift */; }; 12 | 1A899B271E3DBAB70086ADDC /* ReferenceTypeShop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A899B261E3DBAB70086ADDC /* ReferenceTypeShop.swift */; }; 13 | /* End PBXBuildFile section */ 14 | 15 | /* Begin PBXCopyFilesBuildPhase section */ 16 | 1A899B181E3DB3560086ADDC /* CopyFiles */ = { 17 | isa = PBXCopyFilesBuildPhase; 18 | buildActionMask = 2147483647; 19 | dstPath = /usr/share/man/man1/; 20 | dstSubfolderSpec = 0; 21 | files = ( 22 | ); 23 | runOnlyForDeploymentPostprocessing = 1; 24 | }; 25 | /* End PBXCopyFilesBuildPhase section */ 26 | 27 | /* Begin PBXFileReference section */ 28 | 1A899B1A1E3DB3560086ADDC /* Prototype Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Prototype Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 29 | 1A899B1D1E3DB3560086ADDC /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 30 | 1A899B241E3DBAB00086ADDC /* ValueTypeShop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueTypeShop.swift; sourceTree = ""; }; 31 | 1A899B261E3DBAB70086ADDC /* ReferenceTypeShop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReferenceTypeShop.swift; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | 1A899B171E3DB3560086ADDC /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | 1A899B111E3DB3560086ADDC = { 46 | isa = PBXGroup; 47 | children = ( 48 | 1A899B1C1E3DB3560086ADDC /* Prototype Pattern */, 49 | 1A899B1B1E3DB3560086ADDC /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | 1A899B1B1E3DB3560086ADDC /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | 1A899B1A1E3DB3560086ADDC /* Prototype Pattern */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | 1A899B1C1E3DB3560086ADDC /* Prototype Pattern */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | 1A899B1D1E3DB3560086ADDC /* main.swift */, 65 | 1A899B241E3DBAB00086ADDC /* ValueTypeShop.swift */, 66 | 1A899B261E3DBAB70086ADDC /* ReferenceTypeShop.swift */, 67 | ); 68 | path = "Prototype Pattern"; 69 | sourceTree = ""; 70 | }; 71 | /* End PBXGroup section */ 72 | 73 | /* Begin PBXNativeTarget section */ 74 | 1A899B191E3DB3560086ADDC /* Prototype Pattern */ = { 75 | isa = PBXNativeTarget; 76 | buildConfigurationList = 1A899B211E3DB3560086ADDC /* Build configuration list for PBXNativeTarget "Prototype Pattern" */; 77 | buildPhases = ( 78 | 1A899B161E3DB3560086ADDC /* Sources */, 79 | 1A899B171E3DB3560086ADDC /* Frameworks */, 80 | 1A899B181E3DB3560086ADDC /* CopyFiles */, 81 | ); 82 | buildRules = ( 83 | ); 84 | dependencies = ( 85 | ); 86 | name = "Prototype Pattern"; 87 | productName = "Prototype Pattern"; 88 | productReference = 1A899B1A1E3DB3560086ADDC /* Prototype Pattern */; 89 | productType = "com.apple.product-type.tool"; 90 | }; 91 | /* End PBXNativeTarget section */ 92 | 93 | /* Begin PBXProject section */ 94 | 1A899B121E3DB3560086ADDC /* Project object */ = { 95 | isa = PBXProject; 96 | attributes = { 97 | LastSwiftUpdateCheck = 0820; 98 | LastUpgradeCheck = 0820; 99 | ORGANIZATIONNAME = "Don Chen"; 100 | TargetAttributes = { 101 | 1A899B191E3DB3560086ADDC = { 102 | CreatedOnToolsVersion = 8.2.1; 103 | DevelopmentTeam = DT8726CBX4; 104 | ProvisioningStyle = Automatic; 105 | }; 106 | }; 107 | }; 108 | buildConfigurationList = 1A899B151E3DB3560086ADDC /* Build configuration list for PBXProject "Prototype Pattern" */; 109 | compatibilityVersion = "Xcode 3.2"; 110 | developmentRegion = English; 111 | hasScannedForEncodings = 0; 112 | knownRegions = ( 113 | en, 114 | ); 115 | mainGroup = 1A899B111E3DB3560086ADDC; 116 | productRefGroup = 1A899B1B1E3DB3560086ADDC /* Products */; 117 | projectDirPath = ""; 118 | projectRoot = ""; 119 | targets = ( 120 | 1A899B191E3DB3560086ADDC /* Prototype Pattern */, 121 | ); 122 | }; 123 | /* End PBXProject section */ 124 | 125 | /* Begin PBXSourcesBuildPhase section */ 126 | 1A899B161E3DB3560086ADDC /* Sources */ = { 127 | isa = PBXSourcesBuildPhase; 128 | buildActionMask = 2147483647; 129 | files = ( 130 | 1A899B271E3DBAB70086ADDC /* ReferenceTypeShop.swift in Sources */, 131 | 1A899B1E1E3DB3560086ADDC /* main.swift in Sources */, 132 | 1A899B251E3DBAB00086ADDC /* ValueTypeShop.swift in Sources */, 133 | ); 134 | runOnlyForDeploymentPostprocessing = 0; 135 | }; 136 | /* End PBXSourcesBuildPhase section */ 137 | 138 | /* Begin XCBuildConfiguration section */ 139 | 1A899B1F1E3DB3560086ADDC /* Debug */ = { 140 | isa = XCBuildConfiguration; 141 | buildSettings = { 142 | ALWAYS_SEARCH_USER_PATHS = NO; 143 | CLANG_ANALYZER_NONNULL = YES; 144 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 145 | CLANG_CXX_LIBRARY = "libc++"; 146 | CLANG_ENABLE_MODULES = YES; 147 | CLANG_ENABLE_OBJC_ARC = YES; 148 | CLANG_WARN_BOOL_CONVERSION = YES; 149 | CLANG_WARN_CONSTANT_CONVERSION = YES; 150 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 151 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 152 | CLANG_WARN_EMPTY_BODY = YES; 153 | CLANG_WARN_ENUM_CONVERSION = YES; 154 | CLANG_WARN_INFINITE_RECURSION = YES; 155 | CLANG_WARN_INT_CONVERSION = YES; 156 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 157 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 158 | CLANG_WARN_UNREACHABLE_CODE = YES; 159 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 160 | CODE_SIGN_IDENTITY = "-"; 161 | COPY_PHASE_STRIP = NO; 162 | DEBUG_INFORMATION_FORMAT = dwarf; 163 | ENABLE_STRICT_OBJC_MSGSEND = YES; 164 | ENABLE_TESTABILITY = YES; 165 | GCC_C_LANGUAGE_STANDARD = gnu99; 166 | GCC_DYNAMIC_NO_PIC = NO; 167 | GCC_NO_COMMON_BLOCKS = YES; 168 | GCC_OPTIMIZATION_LEVEL = 0; 169 | GCC_PREPROCESSOR_DEFINITIONS = ( 170 | "DEBUG=1", 171 | "$(inherited)", 172 | ); 173 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 174 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 175 | GCC_WARN_UNDECLARED_SELECTOR = YES; 176 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 177 | GCC_WARN_UNUSED_FUNCTION = YES; 178 | GCC_WARN_UNUSED_VARIABLE = YES; 179 | MACOSX_DEPLOYMENT_TARGET = 10.12; 180 | MTL_ENABLE_DEBUG_INFO = YES; 181 | ONLY_ACTIVE_ARCH = YES; 182 | SDKROOT = macosx; 183 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 184 | }; 185 | name = Debug; 186 | }; 187 | 1A899B201E3DB3560086ADDC /* Release */ = { 188 | isa = XCBuildConfiguration; 189 | buildSettings = { 190 | ALWAYS_SEARCH_USER_PATHS = NO; 191 | CLANG_ANALYZER_NONNULL = YES; 192 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 193 | CLANG_CXX_LIBRARY = "libc++"; 194 | CLANG_ENABLE_MODULES = YES; 195 | CLANG_ENABLE_OBJC_ARC = YES; 196 | CLANG_WARN_BOOL_CONVERSION = YES; 197 | CLANG_WARN_CONSTANT_CONVERSION = YES; 198 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 199 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 200 | CLANG_WARN_EMPTY_BODY = YES; 201 | CLANG_WARN_ENUM_CONVERSION = YES; 202 | CLANG_WARN_INFINITE_RECURSION = YES; 203 | CLANG_WARN_INT_CONVERSION = YES; 204 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 205 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 206 | CLANG_WARN_UNREACHABLE_CODE = YES; 207 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 208 | CODE_SIGN_IDENTITY = "-"; 209 | COPY_PHASE_STRIP = NO; 210 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 211 | ENABLE_NS_ASSERTIONS = NO; 212 | ENABLE_STRICT_OBJC_MSGSEND = YES; 213 | GCC_C_LANGUAGE_STANDARD = gnu99; 214 | GCC_NO_COMMON_BLOCKS = YES; 215 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 216 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 217 | GCC_WARN_UNDECLARED_SELECTOR = YES; 218 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 219 | GCC_WARN_UNUSED_FUNCTION = YES; 220 | GCC_WARN_UNUSED_VARIABLE = YES; 221 | MACOSX_DEPLOYMENT_TARGET = 10.12; 222 | MTL_ENABLE_DEBUG_INFO = NO; 223 | SDKROOT = macosx; 224 | }; 225 | name = Release; 226 | }; 227 | 1A899B221E3DB3560086ADDC /* Debug */ = { 228 | isa = XCBuildConfiguration; 229 | buildSettings = { 230 | DEVELOPMENT_TEAM = DT8726CBX4; 231 | PRODUCT_NAME = "$(TARGET_NAME)"; 232 | SWIFT_VERSION = 3.0; 233 | }; 234 | name = Debug; 235 | }; 236 | 1A899B231E3DB3560086ADDC /* Release */ = { 237 | isa = XCBuildConfiguration; 238 | buildSettings = { 239 | DEVELOPMENT_TEAM = DT8726CBX4; 240 | PRODUCT_NAME = "$(TARGET_NAME)"; 241 | SWIFT_VERSION = 3.0; 242 | }; 243 | name = Release; 244 | }; 245 | /* End XCBuildConfiguration section */ 246 | 247 | /* Begin XCConfigurationList section */ 248 | 1A899B151E3DB3560086ADDC /* Build configuration list for PBXProject "Prototype Pattern" */ = { 249 | isa = XCConfigurationList; 250 | buildConfigurations = ( 251 | 1A899B1F1E3DB3560086ADDC /* Debug */, 252 | 1A899B201E3DB3560086ADDC /* Release */, 253 | ); 254 | defaultConfigurationIsVisible = 0; 255 | defaultConfigurationName = Release; 256 | }; 257 | 1A899B211E3DB3560086ADDC /* Build configuration list for PBXNativeTarget "Prototype Pattern" */ = { 258 | isa = XCConfigurationList; 259 | buildConfigurations = ( 260 | 1A899B221E3DB3560086ADDC /* Debug */, 261 | 1A899B231E3DB3560086ADDC /* Release */, 262 | ); 263 | defaultConfigurationIsVisible = 0; 264 | }; 265 | /* End XCConfigurationList section */ 266 | }; 267 | rootObject = 1A899B121E3DB3560086ADDC /* Project object */; 268 | } 269 | -------------------------------------------------------------------------------- /Prototype Pattern/Prototype Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Prototype Pattern/Prototype Pattern/ReferenceTypeShop.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ReferenceTypeShop.swift 3 | // Prototype Pattern 4 | // 5 | // Created by don chen on 2017/1/29. 6 | // Copyright © 2017年 Don Chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class ReferenceTypeShop { 12 | var name:String 13 | var place:String 14 | 15 | init(name:String, place:String) { 16 | self.name = name 17 | self.place = place 18 | } 19 | func printDetails() { 20 | print("\(name) is at \(place)") 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Prototype Pattern/Prototype Pattern/ValueTypeShop.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ValueTypeShop.swift 3 | // Prototype Pattern 4 | // 5 | // Created by don chen on 2017/1/29. 6 | // Copyright © 2017年 Don Chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | struct ValueTypeShop { 12 | var name:String 13 | var place:String 14 | 15 | func printDetails() { 16 | print("\(name) is at \(place)") 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Prototype Pattern/Prototype Pattern/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Prototype Pattern 4 | // 5 | // Created by don chen on 2017/1/29. 6 | // Copyright © 2017年 Don Chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | print("value type copy") 12 | let valueTypeShopA = ValueTypeShop(name: "Shop A", place: "West") 13 | 14 | var valueTypeShopB = valueTypeShopA 15 | valueTypeShopB.name = "Shop B" 16 | valueTypeShopB.place = "West" 17 | 18 | valueTypeShopA.printDetails() 19 | valueTypeShopB.printDetails() 20 | 21 | print("---------") 22 | print("reference type copy") 23 | // 如果使用reference type, Swift將會創建一個指向該對象的引用而不是複製這個對象。 24 | let referenceShopC = ReferenceTypeShop(name: "Shop C", place: "West") 25 | 26 | let referenceShopD = referenceShopC 27 | referenceShopD.name = "Shop C" 28 | referenceShopD.place = "West" 29 | 30 | referenceShopC.printDetails() 31 | referenceShopD.printDetails() 32 | -------------------------------------------------------------------------------- /Proxy Pattern/Proxy-Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1A0719CD1E31A2DF008A6E4F /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A0719CC1E31A2DF008A6E4F /* main.swift */; }; 11 | 1A0719D41E31A48A008A6E4F /* WoodBakery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A0719D31E31A48A008A6E4F /* WoodBakery.swift */; }; 12 | 1A0719D71E31A8BE008A6E4F /* StoneBakery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A0719D61E31A8BE008A6E4F /* StoneBakery.swift */; }; 13 | 1A0719D91E31A8DE008A6E4F /* StoneChef.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A0719D81E31A8DE008A6E4F /* StoneChef.swift */; }; 14 | 1A0719DB1E31A8EE008A6E4F /* StoneWaiter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A0719DA1E31A8EE008A6E4F /* StoneWaiter.swift */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXCopyFilesBuildPhase section */ 18 | 1A0719C71E31A2DF008A6E4F /* CopyFiles */ = { 19 | isa = PBXCopyFilesBuildPhase; 20 | buildActionMask = 2147483647; 21 | dstPath = /usr/share/man/man1/; 22 | dstSubfolderSpec = 0; 23 | files = ( 24 | ); 25 | runOnlyForDeploymentPostprocessing = 1; 26 | }; 27 | /* End PBXCopyFilesBuildPhase section */ 28 | 29 | /* Begin PBXFileReference section */ 30 | 1A0719C91E31A2DF008A6E4F /* Proxy-Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Proxy-Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 31 | 1A0719CC1E31A2DF008A6E4F /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 32 | 1A0719D31E31A48A008A6E4F /* WoodBakery.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WoodBakery.swift; sourceTree = ""; }; 33 | 1A0719D61E31A8BE008A6E4F /* StoneBakery.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoneBakery.swift; sourceTree = ""; }; 34 | 1A0719D81E31A8DE008A6E4F /* StoneChef.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoneChef.swift; sourceTree = ""; }; 35 | 1A0719DA1E31A8EE008A6E4F /* StoneWaiter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoneWaiter.swift; sourceTree = ""; }; 36 | /* End PBXFileReference section */ 37 | 38 | /* Begin PBXFrameworksBuildPhase section */ 39 | 1A0719C61E31A2DF008A6E4F /* Frameworks */ = { 40 | isa = PBXFrameworksBuildPhase; 41 | buildActionMask = 2147483647; 42 | files = ( 43 | ); 44 | runOnlyForDeploymentPostprocessing = 0; 45 | }; 46 | /* End PBXFrameworksBuildPhase section */ 47 | 48 | /* Begin PBXGroup section */ 49 | 1A0719C01E31A2DF008A6E4F = { 50 | isa = PBXGroup; 51 | children = ( 52 | 1A0719CB1E31A2DF008A6E4F /* Proxy-Pattern */, 53 | 1A0719CA1E31A2DF008A6E4F /* Products */, 54 | ); 55 | sourceTree = ""; 56 | }; 57 | 1A0719CA1E31A2DF008A6E4F /* Products */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | 1A0719C91E31A2DF008A6E4F /* Proxy-Pattern */, 61 | ); 62 | name = Products; 63 | sourceTree = ""; 64 | }; 65 | 1A0719CB1E31A2DF008A6E4F /* Proxy-Pattern */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | 1A0719CC1E31A2DF008A6E4F /* main.swift */, 69 | 1A0719D31E31A48A008A6E4F /* WoodBakery.swift */, 70 | 1A0719D51E31A8B4008A6E4F /* StoneBakery */, 71 | ); 72 | path = "Proxy-Pattern"; 73 | sourceTree = ""; 74 | }; 75 | 1A0719D51E31A8B4008A6E4F /* StoneBakery */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | 1A0719D61E31A8BE008A6E4F /* StoneBakery.swift */, 79 | 1A0719D81E31A8DE008A6E4F /* StoneChef.swift */, 80 | 1A0719DA1E31A8EE008A6E4F /* StoneWaiter.swift */, 81 | ); 82 | name = StoneBakery; 83 | sourceTree = ""; 84 | }; 85 | /* End PBXGroup section */ 86 | 87 | /* Begin PBXNativeTarget section */ 88 | 1A0719C81E31A2DF008A6E4F /* Proxy-Pattern */ = { 89 | isa = PBXNativeTarget; 90 | buildConfigurationList = 1A0719D01E31A2DF008A6E4F /* Build configuration list for PBXNativeTarget "Proxy-Pattern" */; 91 | buildPhases = ( 92 | 1A0719C51E31A2DF008A6E4F /* Sources */, 93 | 1A0719C61E31A2DF008A6E4F /* Frameworks */, 94 | 1A0719C71E31A2DF008A6E4F /* CopyFiles */, 95 | ); 96 | buildRules = ( 97 | ); 98 | dependencies = ( 99 | ); 100 | name = "Proxy-Pattern"; 101 | productName = "Proxy-Pattern"; 102 | productReference = 1A0719C91E31A2DF008A6E4F /* Proxy-Pattern */; 103 | productType = "com.apple.product-type.tool"; 104 | }; 105 | /* End PBXNativeTarget section */ 106 | 107 | /* Begin PBXProject section */ 108 | 1A0719C11E31A2DF008A6E4F /* Project object */ = { 109 | isa = PBXProject; 110 | attributes = { 111 | LastSwiftUpdateCheck = 0820; 112 | LastUpgradeCheck = 0820; 113 | ORGANIZATIONNAME = "don chen"; 114 | TargetAttributes = { 115 | 1A0719C81E31A2DF008A6E4F = { 116 | CreatedOnToolsVersion = 8.2.1; 117 | DevelopmentTeam = DT8726CBX4; 118 | ProvisioningStyle = Automatic; 119 | }; 120 | }; 121 | }; 122 | buildConfigurationList = 1A0719C41E31A2DF008A6E4F /* Build configuration list for PBXProject "Proxy-Pattern" */; 123 | compatibilityVersion = "Xcode 3.2"; 124 | developmentRegion = English; 125 | hasScannedForEncodings = 0; 126 | knownRegions = ( 127 | en, 128 | ); 129 | mainGroup = 1A0719C01E31A2DF008A6E4F; 130 | productRefGroup = 1A0719CA1E31A2DF008A6E4F /* Products */; 131 | projectDirPath = ""; 132 | projectRoot = ""; 133 | targets = ( 134 | 1A0719C81E31A2DF008A6E4F /* Proxy-Pattern */, 135 | ); 136 | }; 137 | /* End PBXProject section */ 138 | 139 | /* Begin PBXSourcesBuildPhase section */ 140 | 1A0719C51E31A2DF008A6E4F /* Sources */ = { 141 | isa = PBXSourcesBuildPhase; 142 | buildActionMask = 2147483647; 143 | files = ( 144 | 1A0719D41E31A48A008A6E4F /* WoodBakery.swift in Sources */, 145 | 1A0719D91E31A8DE008A6E4F /* StoneChef.swift in Sources */, 146 | 1A0719CD1E31A2DF008A6E4F /* main.swift in Sources */, 147 | 1A0719DB1E31A8EE008A6E4F /* StoneWaiter.swift in Sources */, 148 | 1A0719D71E31A8BE008A6E4F /* StoneBakery.swift in Sources */, 149 | ); 150 | runOnlyForDeploymentPostprocessing = 0; 151 | }; 152 | /* End PBXSourcesBuildPhase section */ 153 | 154 | /* Begin XCBuildConfiguration section */ 155 | 1A0719CE1E31A2DF008A6E4F /* Debug */ = { 156 | isa = XCBuildConfiguration; 157 | buildSettings = { 158 | ALWAYS_SEARCH_USER_PATHS = NO; 159 | CLANG_ANALYZER_NONNULL = YES; 160 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 161 | CLANG_CXX_LIBRARY = "libc++"; 162 | CLANG_ENABLE_MODULES = YES; 163 | CLANG_ENABLE_OBJC_ARC = YES; 164 | CLANG_WARN_BOOL_CONVERSION = YES; 165 | CLANG_WARN_CONSTANT_CONVERSION = YES; 166 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 167 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 168 | CLANG_WARN_EMPTY_BODY = YES; 169 | CLANG_WARN_ENUM_CONVERSION = YES; 170 | CLANG_WARN_INFINITE_RECURSION = YES; 171 | CLANG_WARN_INT_CONVERSION = YES; 172 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 173 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 174 | CLANG_WARN_UNREACHABLE_CODE = YES; 175 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 176 | CODE_SIGN_IDENTITY = "-"; 177 | COPY_PHASE_STRIP = NO; 178 | DEBUG_INFORMATION_FORMAT = dwarf; 179 | ENABLE_STRICT_OBJC_MSGSEND = YES; 180 | ENABLE_TESTABILITY = YES; 181 | GCC_C_LANGUAGE_STANDARD = gnu99; 182 | GCC_DYNAMIC_NO_PIC = NO; 183 | GCC_NO_COMMON_BLOCKS = YES; 184 | GCC_OPTIMIZATION_LEVEL = 0; 185 | GCC_PREPROCESSOR_DEFINITIONS = ( 186 | "DEBUG=1", 187 | "$(inherited)", 188 | ); 189 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 190 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 191 | GCC_WARN_UNDECLARED_SELECTOR = YES; 192 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 193 | GCC_WARN_UNUSED_FUNCTION = YES; 194 | GCC_WARN_UNUSED_VARIABLE = YES; 195 | MACOSX_DEPLOYMENT_TARGET = 10.12; 196 | MTL_ENABLE_DEBUG_INFO = YES; 197 | ONLY_ACTIVE_ARCH = YES; 198 | SDKROOT = macosx; 199 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 200 | }; 201 | name = Debug; 202 | }; 203 | 1A0719CF1E31A2DF008A6E4F /* Release */ = { 204 | isa = XCBuildConfiguration; 205 | buildSettings = { 206 | ALWAYS_SEARCH_USER_PATHS = NO; 207 | CLANG_ANALYZER_NONNULL = YES; 208 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 209 | CLANG_CXX_LIBRARY = "libc++"; 210 | CLANG_ENABLE_MODULES = YES; 211 | CLANG_ENABLE_OBJC_ARC = YES; 212 | CLANG_WARN_BOOL_CONVERSION = YES; 213 | CLANG_WARN_CONSTANT_CONVERSION = YES; 214 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 215 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 216 | CLANG_WARN_EMPTY_BODY = YES; 217 | CLANG_WARN_ENUM_CONVERSION = YES; 218 | CLANG_WARN_INFINITE_RECURSION = YES; 219 | CLANG_WARN_INT_CONVERSION = YES; 220 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 221 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 222 | CLANG_WARN_UNREACHABLE_CODE = YES; 223 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 224 | CODE_SIGN_IDENTITY = "-"; 225 | COPY_PHASE_STRIP = NO; 226 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 227 | ENABLE_NS_ASSERTIONS = NO; 228 | ENABLE_STRICT_OBJC_MSGSEND = YES; 229 | GCC_C_LANGUAGE_STANDARD = gnu99; 230 | GCC_NO_COMMON_BLOCKS = YES; 231 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 232 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 233 | GCC_WARN_UNDECLARED_SELECTOR = YES; 234 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 235 | GCC_WARN_UNUSED_FUNCTION = YES; 236 | GCC_WARN_UNUSED_VARIABLE = YES; 237 | MACOSX_DEPLOYMENT_TARGET = 10.12; 238 | MTL_ENABLE_DEBUG_INFO = NO; 239 | SDKROOT = macosx; 240 | }; 241 | name = Release; 242 | }; 243 | 1A0719D11E31A2DF008A6E4F /* Debug */ = { 244 | isa = XCBuildConfiguration; 245 | buildSettings = { 246 | DEVELOPMENT_TEAM = DT8726CBX4; 247 | PRODUCT_NAME = "$(TARGET_NAME)"; 248 | SWIFT_VERSION = 3.0; 249 | }; 250 | name = Debug; 251 | }; 252 | 1A0719D21E31A2DF008A6E4F /* Release */ = { 253 | isa = XCBuildConfiguration; 254 | buildSettings = { 255 | DEVELOPMENT_TEAM = DT8726CBX4; 256 | PRODUCT_NAME = "$(TARGET_NAME)"; 257 | SWIFT_VERSION = 3.0; 258 | }; 259 | name = Release; 260 | }; 261 | /* End XCBuildConfiguration section */ 262 | 263 | /* Begin XCConfigurationList section */ 264 | 1A0719C41E31A2DF008A6E4F /* Build configuration list for PBXProject "Proxy-Pattern" */ = { 265 | isa = XCConfigurationList; 266 | buildConfigurations = ( 267 | 1A0719CE1E31A2DF008A6E4F /* Debug */, 268 | 1A0719CF1E31A2DF008A6E4F /* Release */, 269 | ); 270 | defaultConfigurationIsVisible = 0; 271 | defaultConfigurationName = Release; 272 | }; 273 | 1A0719D01E31A2DF008A6E4F /* Build configuration list for PBXNativeTarget "Proxy-Pattern" */ = { 274 | isa = XCConfigurationList; 275 | buildConfigurations = ( 276 | 1A0719D11E31A2DF008A6E4F /* Debug */, 277 | 1A0719D21E31A2DF008A6E4F /* Release */, 278 | ); 279 | defaultConfigurationIsVisible = 0; 280 | }; 281 | /* End XCConfigurationList section */ 282 | }; 283 | rootObject = 1A0719C11E31A2DF008A6E4F /* Project object */; 284 | } 285 | -------------------------------------------------------------------------------- /Proxy Pattern/Proxy-Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Proxy Pattern/Proxy-Pattern/StoneBakery.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StoneBakery.swift 3 | // Proxy-Pattern 4 | // 5 | // Created by don chen on 2017/1/20. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | struct StoneSpecial { 12 | var name:String 13 | var price:Float 14 | var amount:Int 15 | 16 | } 17 | 18 | protocol StoneBakeryDataSource { 19 | func todaySpecial() -> [StoneSpecial] 20 | } 21 | 22 | protocol StoneBakeryDelegate { 23 | func didOrderItem(name:String, amount:Int) 24 | } 25 | 26 | class StoneBakery { 27 | var name:String 28 | var dataSource:StoneBakeryDataSource? 29 | var delegate:StoneBakeryDelegate? 30 | 31 | init(name:String) { 32 | self.name = name 33 | 34 | let chef = StoneChef() 35 | dataSource = chef 36 | 37 | let waiter = StoneWaiter() 38 | delegate = waiter 39 | 40 | } 41 | 42 | func showTodaySpecial() { 43 | print("\(name) 今日特色:") 44 | 45 | let items = getTodaySpecial() 46 | 47 | for item in items { 48 | print("\(item.name), 價格 \(item.price), 數量 \(item.amount)") 49 | } 50 | } 51 | 52 | func getTodaySpecial() -> [StoneSpecial] { 53 | var items = [StoneSpecial]() 54 | if let list = dataSource?.todaySpecial() { 55 | items = list 56 | } 57 | 58 | return items 59 | } 60 | 61 | func orderItem(name:String,amount:Int) { 62 | delegate?.didOrderItem(name: name, amount: amount) 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Proxy Pattern/Proxy-Pattern/StoneChef.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StoneChef.swift 3 | // Proxy-Pattern 4 | // 5 | // Created by don chen on 2017/1/20. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class StoneChef:StoneBakeryDataSource { 12 | 13 | func todaySpecial() -> [StoneSpecial] { 14 | var items = [StoneSpecial]() 15 | items.append(StoneSpecial(name: "法國麵包", price: 180, amount: 20)) 16 | items.append(StoneSpecial(name: "蒜蓉麵包", price: 70, amount: 15)) 17 | items.append(StoneSpecial(name: "太陽餅", price: 85, amount: 10)) 18 | items.append(StoneSpecial(name: "草莓大福", price: 400, amount: 5)) 19 | return items 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Proxy Pattern/Proxy-Pattern/StoneWaiter.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StoneWaiter.swift 3 | // Proxy-Pattern 4 | // 5 | // Created by don chen on 2017/1/20. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class StoneWaiter:StoneBakeryDelegate { 12 | 13 | func didOrderItem(name: String, amount: Int) { 14 | print("客人下單:\(amount) 份 \(name)") 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Proxy Pattern/Proxy-Pattern/WoodBakery.swift: -------------------------------------------------------------------------------- 1 | // 2 | // WoodBakery.swift 3 | // Proxy-Pattern 4 | // 5 | // Created by don chen on 2017/1/20. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | struct WoodSpecial { 12 | var name:String 13 | var price:Float 14 | var amount:Int 15 | 16 | } 17 | 18 | class WoodBakery { 19 | var name:String 20 | 21 | init(name:String) { 22 | self.name = name 23 | } 24 | 25 | func showTodaySpecial() { 26 | print("\(name) 今日特色:") 27 | let items = getTodaySpecial() 28 | for item in items { 29 | print("\(item.name), 價格 \(item.price), 數量 \(item.amount)") 30 | } 31 | } 32 | 33 | func orderItem(name:String,amount:Int) { 34 | print("有客人下單:\(amount) 份 \(name)") 35 | } 36 | 37 | private func getTodaySpecial() -> [WoodSpecial] { 38 | var items = [WoodSpecial]() 39 | items.append(WoodSpecial(name: "法國麵包", price: 180, amount: 20)) 40 | items.append(WoodSpecial(name: "蒜蓉麵包", price: 70, amount: 15)) 41 | items.append(WoodSpecial(name: "太陽餅", price: 85, amount: 10)) 42 | items.append(WoodSpecial(name: "草莓大福", price: 400, amount: 5)) 43 | return items 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Proxy Pattern/Proxy-Pattern/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Proxy-Pattern 4 | // 5 | // Created by don chen on 2017/1/20. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | let woodBakery = WoodBakery(name: "Wood 1號店") 12 | woodBakery.showTodaySpecial() 13 | 14 | print("-----------------------") 15 | 16 | woodBakery.orderItem(name: "法國麵包", amount: 5) 17 | woodBakery.orderItem(name: "太陽餅", amount: 75) 18 | woodBakery.orderItem(name: "草莓大福", amount: 100) 19 | 20 | print("-----------------------") 21 | 22 | let stoneBakery = StoneBakery(name: "Stone 1號店") 23 | stoneBakery.showTodaySpecial() 24 | 25 | print("-----------------------") 26 | 27 | stoneBakery.orderItem(name: "法國麵包", amount: 10) 28 | stoneBakery.orderItem(name: "太陽餅", amount: 120) 29 | stoneBakery.orderItem(name: "草莓大福", amount: 330) 30 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1A899AE51E3A34830086ADDC /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A899AE41E3A34830086ADDC /* main.swift */; }; 11 | 1A899AF01E3A393C0086ADDC /* AdventureA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A899AEF1E3A393C0086ADDC /* AdventureA.swift */; }; 12 | 1A899AF31E3A44F00086ADDC /* AdventureB.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A899AF21E3A44F00086ADDC /* AdventureB.swift */; }; 13 | 1A899AF51E3A45010086ADDC /* Strategy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A899AF41E3A45010086ADDC /* Strategy.swift */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXCopyFilesBuildPhase section */ 17 | 1A899ADF1E3A34830086ADDC /* 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 | 1A899AE11E3A34830086ADDC /* Strategy Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Strategy Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 30 | 1A899AE41E3A34830086ADDC /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 31 | 1A899AEF1E3A393C0086ADDC /* AdventureA.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AdventureA.swift; sourceTree = ""; }; 32 | 1A899AF21E3A44F00086ADDC /* AdventureB.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AdventureB.swift; sourceTree = ""; }; 33 | 1A899AF41E3A45010086ADDC /* Strategy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Strategy.swift; sourceTree = ""; }; 34 | /* End PBXFileReference section */ 35 | 36 | /* Begin PBXFrameworksBuildPhase section */ 37 | 1A899ADE1E3A34830086ADDC /* Frameworks */ = { 38 | isa = PBXFrameworksBuildPhase; 39 | buildActionMask = 2147483647; 40 | files = ( 41 | ); 42 | runOnlyForDeploymentPostprocessing = 0; 43 | }; 44 | /* End PBXFrameworksBuildPhase section */ 45 | 46 | /* Begin PBXGroup section */ 47 | 1A899AD81E3A34830086ADDC = { 48 | isa = PBXGroup; 49 | children = ( 50 | 1A899AE31E3A34830086ADDC /* Strategy Pattern */, 51 | 1A899AE21E3A34830086ADDC /* Products */, 52 | ); 53 | sourceTree = ""; 54 | }; 55 | 1A899AE21E3A34830086ADDC /* Products */ = { 56 | isa = PBXGroup; 57 | children = ( 58 | 1A899AE11E3A34830086ADDC /* Strategy Pattern */, 59 | ); 60 | name = Products; 61 | sourceTree = ""; 62 | }; 63 | 1A899AE31E3A34830086ADDC /* Strategy Pattern */ = { 64 | isa = PBXGroup; 65 | children = ( 66 | 1A899AE41E3A34830086ADDC /* main.swift */, 67 | 1A899AEE1E3A39290086ADDC /* AdventureA */, 68 | 1A899AF11E3A44E00086ADDC /* AdventureB */, 69 | ); 70 | path = "Strategy Pattern"; 71 | sourceTree = ""; 72 | }; 73 | 1A899AEE1E3A39290086ADDC /* AdventureA */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | 1A899AEF1E3A393C0086ADDC /* AdventureA.swift */, 77 | ); 78 | name = AdventureA; 79 | sourceTree = ""; 80 | }; 81 | 1A899AF11E3A44E00086ADDC /* AdventureB */ = { 82 | isa = PBXGroup; 83 | children = ( 84 | 1A899AF21E3A44F00086ADDC /* AdventureB.swift */, 85 | 1A899AF41E3A45010086ADDC /* Strategy.swift */, 86 | ); 87 | name = AdventureB; 88 | sourceTree = ""; 89 | }; 90 | /* End PBXGroup section */ 91 | 92 | /* Begin PBXNativeTarget section */ 93 | 1A899AE01E3A34830086ADDC /* Strategy Pattern */ = { 94 | isa = PBXNativeTarget; 95 | buildConfigurationList = 1A899AE81E3A34830086ADDC /* Build configuration list for PBXNativeTarget "Strategy Pattern" */; 96 | buildPhases = ( 97 | 1A899ADD1E3A34830086ADDC /* Sources */, 98 | 1A899ADE1E3A34830086ADDC /* Frameworks */, 99 | 1A899ADF1E3A34830086ADDC /* CopyFiles */, 100 | ); 101 | buildRules = ( 102 | ); 103 | dependencies = ( 104 | ); 105 | name = "Strategy Pattern"; 106 | productName = "Strategy Pattern"; 107 | productReference = 1A899AE11E3A34830086ADDC /* Strategy Pattern */; 108 | productType = "com.apple.product-type.tool"; 109 | }; 110 | /* End PBXNativeTarget section */ 111 | 112 | /* Begin PBXProject section */ 113 | 1A899AD91E3A34830086ADDC /* Project object */ = { 114 | isa = PBXProject; 115 | attributes = { 116 | LastSwiftUpdateCheck = 0820; 117 | LastUpgradeCheck = 0820; 118 | ORGANIZATIONNAME = "don chen"; 119 | TargetAttributes = { 120 | 1A899AE01E3A34830086ADDC = { 121 | CreatedOnToolsVersion = 8.2.1; 122 | DevelopmentTeam = DT8726CBX4; 123 | ProvisioningStyle = Automatic; 124 | }; 125 | }; 126 | }; 127 | buildConfigurationList = 1A899ADC1E3A34830086ADDC /* Build configuration list for PBXProject "Strategy Pattern" */; 128 | compatibilityVersion = "Xcode 3.2"; 129 | developmentRegion = English; 130 | hasScannedForEncodings = 0; 131 | knownRegions = ( 132 | en, 133 | ); 134 | mainGroup = 1A899AD81E3A34830086ADDC; 135 | productRefGroup = 1A899AE21E3A34830086ADDC /* Products */; 136 | projectDirPath = ""; 137 | projectRoot = ""; 138 | targets = ( 139 | 1A899AE01E3A34830086ADDC /* Strategy Pattern */, 140 | ); 141 | }; 142 | /* End PBXProject section */ 143 | 144 | /* Begin PBXSourcesBuildPhase section */ 145 | 1A899ADD1E3A34830086ADDC /* Sources */ = { 146 | isa = PBXSourcesBuildPhase; 147 | buildActionMask = 2147483647; 148 | files = ( 149 | 1A899AF51E3A45010086ADDC /* Strategy.swift in Sources */, 150 | 1A899AF01E3A393C0086ADDC /* AdventureA.swift in Sources */, 151 | 1A899AF31E3A44F00086ADDC /* AdventureB.swift in Sources */, 152 | 1A899AE51E3A34830086ADDC /* main.swift in Sources */, 153 | ); 154 | runOnlyForDeploymentPostprocessing = 0; 155 | }; 156 | /* End PBXSourcesBuildPhase section */ 157 | 158 | /* Begin XCBuildConfiguration section */ 159 | 1A899AE61E3A34830086ADDC /* Debug */ = { 160 | isa = XCBuildConfiguration; 161 | buildSettings = { 162 | ALWAYS_SEARCH_USER_PATHS = NO; 163 | CLANG_ANALYZER_NONNULL = YES; 164 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 165 | CLANG_CXX_LIBRARY = "libc++"; 166 | CLANG_ENABLE_MODULES = YES; 167 | CLANG_ENABLE_OBJC_ARC = YES; 168 | CLANG_WARN_BOOL_CONVERSION = YES; 169 | CLANG_WARN_CONSTANT_CONVERSION = YES; 170 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 171 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 172 | CLANG_WARN_EMPTY_BODY = YES; 173 | CLANG_WARN_ENUM_CONVERSION = YES; 174 | CLANG_WARN_INFINITE_RECURSION = YES; 175 | CLANG_WARN_INT_CONVERSION = YES; 176 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 177 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 178 | CLANG_WARN_UNREACHABLE_CODE = YES; 179 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 180 | CODE_SIGN_IDENTITY = "-"; 181 | COPY_PHASE_STRIP = NO; 182 | DEBUG_INFORMATION_FORMAT = dwarf; 183 | ENABLE_STRICT_OBJC_MSGSEND = YES; 184 | ENABLE_TESTABILITY = YES; 185 | GCC_C_LANGUAGE_STANDARD = gnu99; 186 | GCC_DYNAMIC_NO_PIC = NO; 187 | GCC_NO_COMMON_BLOCKS = YES; 188 | GCC_OPTIMIZATION_LEVEL = 0; 189 | GCC_PREPROCESSOR_DEFINITIONS = ( 190 | "DEBUG=1", 191 | "$(inherited)", 192 | ); 193 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 194 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 195 | GCC_WARN_UNDECLARED_SELECTOR = YES; 196 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 197 | GCC_WARN_UNUSED_FUNCTION = YES; 198 | GCC_WARN_UNUSED_VARIABLE = YES; 199 | MACOSX_DEPLOYMENT_TARGET = 10.12; 200 | MTL_ENABLE_DEBUG_INFO = YES; 201 | ONLY_ACTIVE_ARCH = YES; 202 | SDKROOT = macosx; 203 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 204 | }; 205 | name = Debug; 206 | }; 207 | 1A899AE71E3A34830086ADDC /* Release */ = { 208 | isa = XCBuildConfiguration; 209 | buildSettings = { 210 | ALWAYS_SEARCH_USER_PATHS = NO; 211 | CLANG_ANALYZER_NONNULL = YES; 212 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 213 | CLANG_CXX_LIBRARY = "libc++"; 214 | CLANG_ENABLE_MODULES = YES; 215 | CLANG_ENABLE_OBJC_ARC = YES; 216 | CLANG_WARN_BOOL_CONVERSION = YES; 217 | CLANG_WARN_CONSTANT_CONVERSION = YES; 218 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 219 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 220 | CLANG_WARN_EMPTY_BODY = YES; 221 | CLANG_WARN_ENUM_CONVERSION = YES; 222 | CLANG_WARN_INFINITE_RECURSION = YES; 223 | CLANG_WARN_INT_CONVERSION = YES; 224 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 225 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 226 | CLANG_WARN_UNREACHABLE_CODE = YES; 227 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 228 | CODE_SIGN_IDENTITY = "-"; 229 | COPY_PHASE_STRIP = NO; 230 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 231 | ENABLE_NS_ASSERTIONS = NO; 232 | ENABLE_STRICT_OBJC_MSGSEND = YES; 233 | GCC_C_LANGUAGE_STANDARD = gnu99; 234 | GCC_NO_COMMON_BLOCKS = YES; 235 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 236 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 237 | GCC_WARN_UNDECLARED_SELECTOR = YES; 238 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 239 | GCC_WARN_UNUSED_FUNCTION = YES; 240 | GCC_WARN_UNUSED_VARIABLE = YES; 241 | MACOSX_DEPLOYMENT_TARGET = 10.12; 242 | MTL_ENABLE_DEBUG_INFO = NO; 243 | SDKROOT = macosx; 244 | }; 245 | name = Release; 246 | }; 247 | 1A899AE91E3A34830086ADDC /* Debug */ = { 248 | isa = XCBuildConfiguration; 249 | buildSettings = { 250 | DEVELOPMENT_TEAM = DT8726CBX4; 251 | PRODUCT_NAME = "$(TARGET_NAME)"; 252 | SWIFT_VERSION = 3.0; 253 | }; 254 | name = Debug; 255 | }; 256 | 1A899AEA1E3A34830086ADDC /* Release */ = { 257 | isa = XCBuildConfiguration; 258 | buildSettings = { 259 | DEVELOPMENT_TEAM = DT8726CBX4; 260 | PRODUCT_NAME = "$(TARGET_NAME)"; 261 | SWIFT_VERSION = 3.0; 262 | }; 263 | name = Release; 264 | }; 265 | /* End XCBuildConfiguration section */ 266 | 267 | /* Begin XCConfigurationList section */ 268 | 1A899ADC1E3A34830086ADDC /* Build configuration list for PBXProject "Strategy Pattern" */ = { 269 | isa = XCConfigurationList; 270 | buildConfigurations = ( 271 | 1A899AE61E3A34830086ADDC /* Debug */, 272 | 1A899AE71E3A34830086ADDC /* Release */, 273 | ); 274 | defaultConfigurationIsVisible = 0; 275 | defaultConfigurationName = Release; 276 | }; 277 | 1A899AE81E3A34830086ADDC /* Build configuration list for PBXNativeTarget "Strategy Pattern" */ = { 278 | isa = XCConfigurationList; 279 | buildConfigurations = ( 280 | 1A899AE91E3A34830086ADDC /* Debug */, 281 | 1A899AEA1E3A34830086ADDC /* Release */, 282 | ); 283 | defaultConfigurationIsVisible = 0; 284 | }; 285 | /* End XCConfigurationList section */ 286 | }; 287 | rootObject = 1A899AD91E3A34830086ADDC /* Project object */; 288 | } 289 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/AdventureA.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AdventureA.swift 3 | // Strategy Pattern 4 | // 5 | // Created by don chen on 2017/1/26. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class AdventureA { 12 | private let name:String 13 | 14 | init(adventureName:String) { 15 | name = adventureName 16 | } 17 | 18 | func attack() { 19 | print("冒險者 \(name) 進行了 普通攻擊") 20 | } 21 | 22 | func rockAttack() { 23 | print("冒險者 \(name) 進行了 石頭拋射攻擊") 24 | } 25 | 26 | func waterAttack() { 27 | print("冒險者 \(name) 進行了 噴水攻擊") 28 | } 29 | 30 | func rushAttack() { 31 | print("冒險者 \(name) 進行了 衝撞攻擊") 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/AdventureB.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AdventureB.swift 3 | // Strategy Pattern 4 | // 5 | // Created by don chen on 2017/1/26. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class AdventureB { 12 | private let name:String 13 | 14 | init(name:String) { 15 | self.name = name 16 | } 17 | 18 | func attackWith(strategy:Strategy?) { 19 | if strategy != nil { 20 | let way = strategy!.attack() 21 | print("冒險者 \(name) 進行了 \(way) 攻擊") 22 | } else { 23 | print("冒險者 \(name) 進行了 普通 攻擊") 24 | } 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/Strategy.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Strategy.swift 3 | // Strategy Pattern 4 | // 5 | // Created by don chen on 2017/1/26. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | protocol Strategy { 12 | func attack() -> String 13 | } 14 | 15 | class RushStrategy:Strategy { 16 | func attack() -> String { 17 | return "衝撞" 18 | } 19 | } 20 | 21 | class RockStrategy:Strategy { 22 | func attack() -> String { 23 | return "石頭拋射" 24 | } 25 | } 26 | 27 | class WaterStrategy:Strategy { 28 | func attack() -> String { 29 | return "噴水" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Strategy Pattern/Strategy Pattern/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Strategy Pattern 4 | // 5 | // Created by don chen on 2017/1/26. 6 | // Copyright © 2017年 don chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | let adventureA = AdventureA(adventureName: "Adventure A") 12 | adventureA.attack() 13 | 14 | 15 | 16 | let adventureB = AdventureB(name: "Adventure B") 17 | 18 | // 不是用策略 19 | adventureB.attackWith(strategy: nil) 20 | 21 | // 使用rock Strategy 22 | let rockStrategy = RockStrategy() 23 | adventureB.attackWith(strategy: rockStrategy) 24 | -------------------------------------------------------------------------------- /Visitor Pattern/Visitor Pattern.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1A899B031E3AFB930086ADDC /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A899B021E3AFB930086ADDC /* main.swift */; }; 11 | 1A899B0B1E3AFCBF0086ADDC /* WoodMall.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A899B0A1E3AFCBF0086ADDC /* WoodMall.swift */; }; 12 | 1A899B0E1E3B076D0086ADDC /* StoneMall.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A899B0D1E3B076D0086ADDC /* StoneMall.swift */; }; 13 | 1A899B101E3B07AF0086ADDC /* Visitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A899B0F1E3B07AF0086ADDC /* Visitor.swift */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXCopyFilesBuildPhase section */ 17 | 1A899AFD1E3AFB930086ADDC /* 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 | 1A899AFF1E3AFB930086ADDC /* Visitor Pattern */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Visitor Pattern"; sourceTree = BUILT_PRODUCTS_DIR; }; 30 | 1A899B021E3AFB930086ADDC /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 31 | 1A899B0A1E3AFCBF0086ADDC /* WoodMall.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WoodMall.swift; sourceTree = ""; }; 32 | 1A899B0D1E3B076D0086ADDC /* StoneMall.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoneMall.swift; sourceTree = ""; }; 33 | 1A899B0F1E3B07AF0086ADDC /* Visitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Visitor.swift; sourceTree = ""; }; 34 | /* End PBXFileReference section */ 35 | 36 | /* Begin PBXFrameworksBuildPhase section */ 37 | 1A899AFC1E3AFB930086ADDC /* Frameworks */ = { 38 | isa = PBXFrameworksBuildPhase; 39 | buildActionMask = 2147483647; 40 | files = ( 41 | ); 42 | runOnlyForDeploymentPostprocessing = 0; 43 | }; 44 | /* End PBXFrameworksBuildPhase section */ 45 | 46 | /* Begin PBXGroup section */ 47 | 1A899AF61E3AFB930086ADDC = { 48 | isa = PBXGroup; 49 | children = ( 50 | 1A899B011E3AFB930086ADDC /* Visitor Pattern */, 51 | 1A899B001E3AFB930086ADDC /* Products */, 52 | ); 53 | sourceTree = ""; 54 | }; 55 | 1A899B001E3AFB930086ADDC /* Products */ = { 56 | isa = PBXGroup; 57 | children = ( 58 | 1A899AFF1E3AFB930086ADDC /* Visitor Pattern */, 59 | ); 60 | name = Products; 61 | sourceTree = ""; 62 | }; 63 | 1A899B011E3AFB930086ADDC /* Visitor Pattern */ = { 64 | isa = PBXGroup; 65 | children = ( 66 | 1A899B021E3AFB930086ADDC /* main.swift */, 67 | 1A899B091E3AFC590086ADDC /* Wood Mall */, 68 | 1A899B0C1E3B074E0086ADDC /* Stone Mall */, 69 | ); 70 | path = "Visitor Pattern"; 71 | sourceTree = ""; 72 | }; 73 | 1A899B091E3AFC590086ADDC /* Wood Mall */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | 1A899B0A1E3AFCBF0086ADDC /* WoodMall.swift */, 77 | ); 78 | name = "Wood Mall"; 79 | sourceTree = ""; 80 | }; 81 | 1A899B0C1E3B074E0086ADDC /* Stone Mall */ = { 82 | isa = PBXGroup; 83 | children = ( 84 | 1A899B0D1E3B076D0086ADDC /* StoneMall.swift */, 85 | 1A899B0F1E3B07AF0086ADDC /* Visitor.swift */, 86 | ); 87 | name = "Stone Mall"; 88 | sourceTree = ""; 89 | }; 90 | /* End PBXGroup section */ 91 | 92 | /* Begin PBXNativeTarget section */ 93 | 1A899AFE1E3AFB930086ADDC /* Visitor Pattern */ = { 94 | isa = PBXNativeTarget; 95 | buildConfigurationList = 1A899B061E3AFB930086ADDC /* Build configuration list for PBXNativeTarget "Visitor Pattern" */; 96 | buildPhases = ( 97 | 1A899AFB1E3AFB930086ADDC /* Sources */, 98 | 1A899AFC1E3AFB930086ADDC /* Frameworks */, 99 | 1A899AFD1E3AFB930086ADDC /* CopyFiles */, 100 | ); 101 | buildRules = ( 102 | ); 103 | dependencies = ( 104 | ); 105 | name = "Visitor Pattern"; 106 | productName = "Visitor Pattern"; 107 | productReference = 1A899AFF1E3AFB930086ADDC /* Visitor Pattern */; 108 | productType = "com.apple.product-type.tool"; 109 | }; 110 | /* End PBXNativeTarget section */ 111 | 112 | /* Begin PBXProject section */ 113 | 1A899AF71E3AFB930086ADDC /* Project object */ = { 114 | isa = PBXProject; 115 | attributes = { 116 | LastSwiftUpdateCheck = 0820; 117 | LastUpgradeCheck = 0820; 118 | ORGANIZATIONNAME = "Don Chen"; 119 | TargetAttributes = { 120 | 1A899AFE1E3AFB930086ADDC = { 121 | CreatedOnToolsVersion = 8.2.1; 122 | DevelopmentTeam = DT8726CBX4; 123 | ProvisioningStyle = Automatic; 124 | }; 125 | }; 126 | }; 127 | buildConfigurationList = 1A899AFA1E3AFB930086ADDC /* Build configuration list for PBXProject "Visitor Pattern" */; 128 | compatibilityVersion = "Xcode 3.2"; 129 | developmentRegion = English; 130 | hasScannedForEncodings = 0; 131 | knownRegions = ( 132 | en, 133 | ); 134 | mainGroup = 1A899AF61E3AFB930086ADDC; 135 | productRefGroup = 1A899B001E3AFB930086ADDC /* Products */; 136 | projectDirPath = ""; 137 | projectRoot = ""; 138 | targets = ( 139 | 1A899AFE1E3AFB930086ADDC /* Visitor Pattern */, 140 | ); 141 | }; 142 | /* End PBXProject section */ 143 | 144 | /* Begin PBXSourcesBuildPhase section */ 145 | 1A899AFB1E3AFB930086ADDC /* Sources */ = { 146 | isa = PBXSourcesBuildPhase; 147 | buildActionMask = 2147483647; 148 | files = ( 149 | 1A899B101E3B07AF0086ADDC /* Visitor.swift in Sources */, 150 | 1A899B0B1E3AFCBF0086ADDC /* WoodMall.swift in Sources */, 151 | 1A899B0E1E3B076D0086ADDC /* StoneMall.swift in Sources */, 152 | 1A899B031E3AFB930086ADDC /* main.swift in Sources */, 153 | ); 154 | runOnlyForDeploymentPostprocessing = 0; 155 | }; 156 | /* End PBXSourcesBuildPhase section */ 157 | 158 | /* Begin XCBuildConfiguration section */ 159 | 1A899B041E3AFB930086ADDC /* Debug */ = { 160 | isa = XCBuildConfiguration; 161 | buildSettings = { 162 | ALWAYS_SEARCH_USER_PATHS = NO; 163 | CLANG_ANALYZER_NONNULL = YES; 164 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 165 | CLANG_CXX_LIBRARY = "libc++"; 166 | CLANG_ENABLE_MODULES = YES; 167 | CLANG_ENABLE_OBJC_ARC = YES; 168 | CLANG_WARN_BOOL_CONVERSION = YES; 169 | CLANG_WARN_CONSTANT_CONVERSION = YES; 170 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 171 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 172 | CLANG_WARN_EMPTY_BODY = YES; 173 | CLANG_WARN_ENUM_CONVERSION = YES; 174 | CLANG_WARN_INFINITE_RECURSION = YES; 175 | CLANG_WARN_INT_CONVERSION = YES; 176 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 177 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 178 | CLANG_WARN_UNREACHABLE_CODE = YES; 179 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 180 | CODE_SIGN_IDENTITY = "-"; 181 | COPY_PHASE_STRIP = NO; 182 | DEBUG_INFORMATION_FORMAT = dwarf; 183 | ENABLE_STRICT_OBJC_MSGSEND = YES; 184 | ENABLE_TESTABILITY = YES; 185 | GCC_C_LANGUAGE_STANDARD = gnu99; 186 | GCC_DYNAMIC_NO_PIC = NO; 187 | GCC_NO_COMMON_BLOCKS = YES; 188 | GCC_OPTIMIZATION_LEVEL = 0; 189 | GCC_PREPROCESSOR_DEFINITIONS = ( 190 | "DEBUG=1", 191 | "$(inherited)", 192 | ); 193 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 194 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 195 | GCC_WARN_UNDECLARED_SELECTOR = YES; 196 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 197 | GCC_WARN_UNUSED_FUNCTION = YES; 198 | GCC_WARN_UNUSED_VARIABLE = YES; 199 | MACOSX_DEPLOYMENT_TARGET = 10.12; 200 | MTL_ENABLE_DEBUG_INFO = YES; 201 | ONLY_ACTIVE_ARCH = YES; 202 | SDKROOT = macosx; 203 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 204 | }; 205 | name = Debug; 206 | }; 207 | 1A899B051E3AFB930086ADDC /* Release */ = { 208 | isa = XCBuildConfiguration; 209 | buildSettings = { 210 | ALWAYS_SEARCH_USER_PATHS = NO; 211 | CLANG_ANALYZER_NONNULL = YES; 212 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 213 | CLANG_CXX_LIBRARY = "libc++"; 214 | CLANG_ENABLE_MODULES = YES; 215 | CLANG_ENABLE_OBJC_ARC = YES; 216 | CLANG_WARN_BOOL_CONVERSION = YES; 217 | CLANG_WARN_CONSTANT_CONVERSION = YES; 218 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 219 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 220 | CLANG_WARN_EMPTY_BODY = YES; 221 | CLANG_WARN_ENUM_CONVERSION = YES; 222 | CLANG_WARN_INFINITE_RECURSION = YES; 223 | CLANG_WARN_INT_CONVERSION = YES; 224 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 225 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 226 | CLANG_WARN_UNREACHABLE_CODE = YES; 227 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 228 | CODE_SIGN_IDENTITY = "-"; 229 | COPY_PHASE_STRIP = NO; 230 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 231 | ENABLE_NS_ASSERTIONS = NO; 232 | ENABLE_STRICT_OBJC_MSGSEND = YES; 233 | GCC_C_LANGUAGE_STANDARD = gnu99; 234 | GCC_NO_COMMON_BLOCKS = YES; 235 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 236 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 237 | GCC_WARN_UNDECLARED_SELECTOR = YES; 238 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 239 | GCC_WARN_UNUSED_FUNCTION = YES; 240 | GCC_WARN_UNUSED_VARIABLE = YES; 241 | MACOSX_DEPLOYMENT_TARGET = 10.12; 242 | MTL_ENABLE_DEBUG_INFO = NO; 243 | SDKROOT = macosx; 244 | }; 245 | name = Release; 246 | }; 247 | 1A899B071E3AFB930086ADDC /* Debug */ = { 248 | isa = XCBuildConfiguration; 249 | buildSettings = { 250 | DEVELOPMENT_TEAM = DT8726CBX4; 251 | PRODUCT_NAME = "$(TARGET_NAME)"; 252 | SWIFT_VERSION = 3.0; 253 | }; 254 | name = Debug; 255 | }; 256 | 1A899B081E3AFB930086ADDC /* Release */ = { 257 | isa = XCBuildConfiguration; 258 | buildSettings = { 259 | DEVELOPMENT_TEAM = DT8726CBX4; 260 | PRODUCT_NAME = "$(TARGET_NAME)"; 261 | SWIFT_VERSION = 3.0; 262 | }; 263 | name = Release; 264 | }; 265 | /* End XCBuildConfiguration section */ 266 | 267 | /* Begin XCConfigurationList section */ 268 | 1A899AFA1E3AFB930086ADDC /* Build configuration list for PBXProject "Visitor Pattern" */ = { 269 | isa = XCConfigurationList; 270 | buildConfigurations = ( 271 | 1A899B041E3AFB930086ADDC /* Debug */, 272 | 1A899B051E3AFB930086ADDC /* Release */, 273 | ); 274 | defaultConfigurationIsVisible = 0; 275 | defaultConfigurationName = Release; 276 | }; 277 | 1A899B061E3AFB930086ADDC /* Build configuration list for PBXNativeTarget "Visitor Pattern" */ = { 278 | isa = XCConfigurationList; 279 | buildConfigurations = ( 280 | 1A899B071E3AFB930086ADDC /* Debug */, 281 | 1A899B081E3AFB930086ADDC /* Release */, 282 | ); 283 | defaultConfigurationIsVisible = 0; 284 | }; 285 | /* End XCConfigurationList section */ 286 | }; 287 | rootObject = 1A899AF71E3AFB930086ADDC /* Project object */; 288 | } 289 | -------------------------------------------------------------------------------- /Visitor Pattern/Visitor Pattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Visitor Pattern/Visitor Pattern/StoneMall.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StoneMall.swift 3 | // Visitor Pattern 4 | // 5 | // Created by don chen on 2017/1/27. 6 | // Copyright © 2017年 Don Chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class StoneComputerShop: StoneShop { 12 | let revenue:Float 13 | 14 | init(revenue:Float) { 15 | self.revenue = revenue 16 | } 17 | 18 | func accept(visitor: Visitor) { 19 | visitor.visit(shop: self) 20 | } 21 | } 22 | 23 | class StoneBicycleShop: StoneShop { 24 | let revenue:Float 25 | 26 | init(revenue:Float) { 27 | self.revenue = revenue 28 | } 29 | 30 | func accept(visitor: Visitor) { 31 | visitor.visit(shop: self) 32 | } 33 | } 34 | 35 | class StoneFurnitureShop: StoneShop { 36 | let revenue:Float 37 | 38 | init(revenue:Float) { 39 | self.revenue = revenue 40 | } 41 | 42 | func accept(visitor: Visitor) { 43 | visitor.visit(shop: self) 44 | } 45 | } 46 | 47 | class StoneMall { 48 | let shops:[StoneShop] 49 | 50 | init(shops:StoneShop...) { 51 | self.shops = shops 52 | } 53 | 54 | // 上下文類,通過accept方法,將訪問者傳遞給對象 55 | func accept(visitor: Visitor) { 56 | for shop in shops { 57 | shop.accept(visitor: visitor) 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Visitor Pattern/Visitor Pattern/Visitor.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Visitor.swift 3 | // Visitor Pattern 4 | // 5 | // Created by don chen on 2017/1/27. 6 | // Copyright © 2017年 Don Chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | protocol StoneShop { 12 | func accept(visitor:Visitor) 13 | } 14 | 15 | protocol Visitor { 16 | func visit(shop:StoneComputerShop) 17 | func visit(shop:StoneBicycleShop) 18 | func visit(shop:StoneFurnitureShop) 19 | 20 | } 21 | 22 | /* 23 | * 抽成規則 24 | * WoodComputerShop 5% 25 | * WoodBicycleShop 15% 26 | * WoodFurnitureShop 25% 27 | * 28 | */ 29 | class ShopVisitor: Visitor { 30 | var totalRevenue:Float = 0 31 | 32 | func visit(shop: StoneComputerShop) { 33 | totalRevenue += shop.revenue * 0.05 34 | } 35 | 36 | func visit(shop: StoneBicycleShop) { 37 | totalRevenue += shop.revenue * 0.15 38 | } 39 | 40 | func visit(shop: StoneFurnitureShop) { 41 | totalRevenue += shop.revenue * 0.25 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Visitor Pattern/Visitor Pattern/WoodMall.swift: -------------------------------------------------------------------------------- 1 | // 2 | // WoodMall.swift 3 | // Visitor Pattern 4 | // 5 | // Created by don chen on 2017/1/27. 6 | // Copyright © 2017年 Don Chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class WoodComputerShop { 12 | let revenue:Float 13 | 14 | init(revenue:Float) { 15 | self.revenue = revenue 16 | } 17 | } 18 | 19 | class WoodBicycleShop { 20 | let revenue:Float 21 | 22 | init(revenue:Float) { 23 | self.revenue = revenue 24 | } 25 | } 26 | 27 | class WoodFurnitureShop { 28 | let revenue:Float 29 | 30 | init(revenue:Float) { 31 | self.revenue = revenue 32 | } 33 | } 34 | 35 | /* 36 | * 抽成規則 37 | * WoodComputerShop 5% 38 | * WoodBicycleShop 15% 39 | * WoodFurnitureShop 25% 40 | * 41 | */ 42 | class WoodMall { 43 | let shops:[Any] 44 | 45 | init(shops:Any...) { 46 | self.shops = shops 47 | } 48 | 49 | func calculateRevenue() -> Float { 50 | return shops.reduce(0, { total, shop in 51 | if let computerShop = shop as? WoodComputerShop { 52 | return total + computerShop.revenue * 0.05 53 | 54 | } else if let bicycleShop = shop as? WoodBicycleShop { 55 | return total + bicycleShop.revenue * 0.15 56 | 57 | } else if let furnitureShop = shop as? WoodFurnitureShop { 58 | return total + furnitureShop.revenue * 0.25 59 | 60 | } else { 61 | return total 62 | } 63 | 64 | }) 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Visitor Pattern/Visitor Pattern/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Visitor Pattern 4 | // 5 | // Created by don chen on 2017/1/27. 6 | // Copyright © 2017年 Don Chen. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | let woodComputerShop = WoodComputerShop(revenue: 8000) 12 | let woodBicycleShop = WoodBicycleShop(revenue: 7500) 13 | let woodFurnitureShop = WoodFurnitureShop(revenue: 15000) 14 | 15 | let woodMall = WoodMall(shops: woodComputerShop, woodBicycleShop, woodFurnitureShop) 16 | let woodRevenue = woodMall.calculateRevenue() 17 | print("Wood Mall 一共盈利 \(woodRevenue)") 18 | 19 | print("-------------") 20 | let stoneComputerShop = StoneComputerShop(revenue: 8000) 21 | let stoneBicycleShop = StoneBicycleShop(revenue: 7500) 22 | let stoneFurnitureShop = StoneFurnitureShop(revenue: 15000) 23 | 24 | let stoneMall = StoneMall(shops: stoneComputerShop, stoneBicycleShop, stoneFurnitureShop) 25 | let shopVisitor = ShopVisitor() 26 | stoneMall.accept(visitor: shopVisitor) 27 | print("Stone Mall 一共盈利 \(shopVisitor.totalRevenue)") 28 | --------------------------------------------------------------------------------