├── .gitignore ├── DesignModeDemo ├── DesignModeDemo.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist ├── DesignModeDemo │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ ├── Contents.json │ │ ├── File.imageset │ │ │ ├── Contents.json │ │ │ └── File@2x.png │ │ └── Folder.imageset │ │ │ ├── Contents.json │ │ │ └── Folder@2x.png │ ├── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ ├── Info.plist │ ├── ViewController.h │ ├── ViewController.m │ ├── main.m │ ├── 创建型模式 │ │ ├── 单例模式 │ │ │ ├── SingletonManager.h │ │ │ ├── SingletonManager.m │ │ │ ├── SingletonPatternViewController.h │ │ │ └── SingletonPatternViewController.m │ │ ├── 原型模式 │ │ │ ├── BaseCopyObject.h │ │ │ ├── BaseCopyObject.m │ │ │ ├── ClassModel.h │ │ │ ├── ClassModel.m │ │ │ ├── PrototypePatternViewController.h │ │ │ ├── PrototypePatternViewController.m │ │ │ ├── StudentModel.h │ │ │ └── StudentModel.m │ │ ├── 工厂方法模式 │ │ │ ├── BallpointPen.h │ │ │ ├── BallpointPen.m │ │ │ ├── FactoryPatternViewController.h │ │ │ ├── FactoryPatternViewController.m │ │ │ ├── Pen.h │ │ │ ├── Pen.m │ │ │ ├── PenFactory.h │ │ │ ├── PenFactory.m │ │ │ ├── Pencil.h │ │ │ └── Pencil.m │ │ ├── 抽象工厂模式 │ │ │ ├── AbstractFactoryPatternImg.jpeg │ │ │ ├── AbstractFactoryPatternViewController.h │ │ │ ├── AbstractFactoryPatternViewController.m │ │ │ ├── CircleHeaderFactory.h │ │ │ ├── CircleHeaderFactory.m │ │ │ ├── CircleView.h │ │ │ ├── CircleView.m │ │ │ ├── HeaderImgFactory.h │ │ │ ├── HeaderImgFactory.m │ │ │ ├── SquareHeaderFactory.h │ │ │ ├── SquareHeaderFactory.m │ │ │ ├── SquareView.h │ │ │ └── SquareView.m │ │ └── 构建者模式 │ │ │ ├── BuilderViewController.h │ │ │ ├── BuilderViewController.m │ │ │ ├── CarBuilder.h │ │ │ ├── Director.h │ │ │ ├── Director.m │ │ │ └── 车场 │ │ │ ├── Car.h │ │ │ ├── Car.m │ │ │ ├── CarCompanyBuilder.h │ │ │ ├── CarCompanyBuilder.m │ │ │ └── CarCompanyProtocol.h │ ├── 结构型模式 │ │ ├── 享元模式 │ │ │ ├── Flower.h │ │ │ ├── Flower.m │ │ │ ├── FlowerFactory.h │ │ │ ├── FlowerFactory.m │ │ │ ├── FlyweightPatternViewController.h │ │ │ └── FlyweightPatternViewController.m │ │ ├── 代理模式 │ │ │ ├── DelegatePatternViewController.h │ │ │ ├── DelegatePatternViewController.m │ │ │ ├── Person.h │ │ │ └── Person.m │ │ ├── 外观模式 │ │ │ ├── CabDriver.h │ │ │ ├── CabDriver.m │ │ │ ├── FacadeCar.h │ │ │ ├── FacadeCar.m │ │ │ ├── FacadePatternViewController.h │ │ │ ├── FacadePatternViewController.m │ │ │ ├── Taximeter.h │ │ │ └── Taximeter.m │ │ ├── 桥接模式 │ │ │ ├── BridgePatternViewController.h │ │ │ ├── BridgePatternViewController.m │ │ │ ├── 空调 │ │ │ │ ├── AirConditioner.h │ │ │ │ ├── AirConditioner.m │ │ │ │ ├── GeliAirConditioner.h │ │ │ │ ├── GeliAirConditioner.m │ │ │ │ ├── HaierAirConditioner.h │ │ │ │ └── HaierAirConditioner.m │ │ │ └── 遥控器 │ │ │ │ ├── DirectionRemote.h │ │ │ │ ├── DirectionRemote.m │ │ │ │ ├── Remote.h │ │ │ │ ├── Remote.m │ │ │ │ ├── TemperatureRemote.h │ │ │ │ └── TemperatureRemote.m │ │ ├── 组合模式 │ │ │ ├── CompositePatternViewController.h │ │ │ ├── CompositePatternViewController.m │ │ │ ├── File.h │ │ │ ├── File.m │ │ │ ├── FileCell.h │ │ │ ├── FileCell.m │ │ │ ├── FileViewController.h │ │ │ ├── FileViewController.m │ │ │ ├── UIView+SetRect.h │ │ │ ├── UIView+SetRect.m │ │ │ └── 文件夹系统.png │ │ ├── 装饰器模式 │ │ │ ├── CheatGameDecotor.h │ │ │ ├── CheatGameDecotor.m │ │ │ ├── CoinGameDecorator.h │ │ │ ├── CoinGameDecorator.m │ │ │ ├── DecoratorPatternViewController.h │ │ │ ├── DecoratorPatternViewController.m │ │ │ ├── GameDecortor.h │ │ │ ├── GameDecortor.m │ │ │ ├── GameLoL+Cheat.h │ │ │ ├── GameLoL+Cheat.m │ │ │ ├── GameLoL+Coin.h │ │ │ ├── GameLoL+Coin.m │ │ │ ├── GameLoL.h │ │ │ └── GameLoL.m │ │ └── 适配器模式 │ │ │ ├── AdapterViewController.h │ │ │ ├── AdapterViewController.m │ │ │ ├── 案例1 │ │ │ ├── AdapterDemo1ViewController.h │ │ │ ├── AdapterDemo1ViewController.m │ │ │ ├── AdapterUSD.h │ │ │ ├── AdapterUSD.m │ │ │ ├── MoneyAdapterProtocol.h │ │ │ ├── 对象适配器 │ │ │ │ ├── AdapterManager.h │ │ │ │ └── AdapterManager.m │ │ │ └── 类适配器 │ │ │ │ ├── AdapterCNY.h │ │ │ │ └── AdapterCNY.m │ │ │ └── 案例2 │ │ │ ├── AdapterDemo2ViewController.h │ │ │ ├── AdapterDemo2ViewController.m │ │ │ ├── ColorView.h │ │ │ ├── ColorView.m │ │ │ ├── ModelOne.h │ │ │ ├── ModelOne.m │ │ │ ├── ModelOneAdapter.h │ │ │ ├── ModelOneAdapter.m │ │ │ ├── ModelTwo.h │ │ │ ├── ModelTwo.m │ │ │ ├── ModelTwoAdapter.h │ │ │ ├── ModelTwoAdapter.m │ │ │ └── UserInfoProtocol.h │ └── 行为型模式 │ │ ├── 中介者模式 │ │ ├── Colleague.h │ │ ├── Colleague.m │ │ ├── ConcreteColleague.h │ │ ├── ConcreteColleague.m │ │ ├── ConcreteMediator.h │ │ ├── ConcreteMediator.m │ │ ├── Mediator.h │ │ ├── Mediator.m │ │ ├── MediatorPatternViewController.h │ │ └── MediatorPatternViewController.m │ │ ├── 命令模式 │ │ ├── CommandPatternViewController.h │ │ ├── CommandPatternViewController.m │ │ ├── CommandProtocol.h │ │ ├── DarkerCommand.h │ │ ├── DarkerCommand.m │ │ ├── Invoker.h │ │ ├── Invoker.m │ │ ├── LighterCommand.h │ │ ├── LighterCommand.m │ │ ├── Receiver.h │ │ └── Receiver.m │ │ ├── 备忘录模式 │ │ ├── Apple.h │ │ ├── Apple.m │ │ ├── FastCoder │ │ │ ├── FastCoder.h │ │ │ ├── FastCoder.m │ │ │ ├── NSObject+MementoCenter.h │ │ │ └── NSObject+MementoCenter.m │ │ ├── MementoCenter.h │ │ ├── MementoCenter.m │ │ ├── MementoPatternViewController.h │ │ ├── MementoPatternViewController.m │ │ └── MementoProtocol.h │ │ ├── 模版方法模式 │ │ ├── Basketball.h │ │ ├── Basketball.m │ │ ├── Football.h │ │ ├── Football.m │ │ ├── Game.h │ │ ├── Game.m │ │ ├── TemplatePatternViewController.h │ │ └── TemplatePatternViewController.m │ │ ├── 状态模式 │ │ ├── BuyState.h │ │ ├── BuyState.m │ │ ├── Celling.h │ │ ├── Celling.m │ │ ├── CellingState.h │ │ ├── ChoosingState.h │ │ ├── ChoosingState.m │ │ ├── ShipmentState.h │ │ ├── ShipmentState.m │ │ ├── StatePatternViewController.h │ │ └── StatePatternViewController.m │ │ ├── 策略模式 │ │ ├── Context.h │ │ ├── Context.m │ │ ├── OperationAdd.h │ │ ├── OperationAdd.m │ │ ├── OperationMultiply.h │ │ ├── OperationMultiply.m │ │ ├── OperationSubStract.h │ │ ├── OperationSubStract.m │ │ ├── Strategy.h │ │ ├── StrategyPatternViewController.h │ │ └── StrategyPatternViewController.m │ │ ├── 观察者模式 │ │ ├── ObserverPatternViewController.h │ │ ├── ObserverPatternViewController.m │ │ ├── SubscibeCenter.h │ │ ├── SubscibeCenter.m │ │ └── SubscibeProtocol.h │ │ ├── 解释器模式 │ │ ├── InterpreterPatternViewController.h │ │ ├── InterpreterPatternViewController.m │ │ └── 四则运算 │ │ │ ├── AdditionExpression.h │ │ │ ├── AdditionExpression.m │ │ │ ├── ArithmeticExpression.h │ │ │ ├── Calculator.h │ │ │ ├── Calculator.m │ │ │ ├── NumExpression.h │ │ │ ├── NumExpression.m │ │ │ ├── OperatorExpression.h │ │ │ ├── OperatorExpression.m │ │ │ ├── ReduceExpression.h │ │ │ └── ReduceExpression.m │ │ ├── 访问者模式 │ │ ├── AccountBook.h │ │ ├── AccountBook.m │ │ ├── AccountBookViewer.h │ │ ├── Bill.h │ │ ├── Boss.h │ │ ├── Boss.m │ │ ├── CAP.h │ │ ├── CAP.m │ │ ├── ConsumeBill.h │ │ ├── ConsumeBill.m │ │ ├── IncomeBill.h │ │ ├── IncomeBill.m │ │ ├── VisitorPatternViewController.h │ │ └── VisitorPatternViewController.m │ │ ├── 责任链模式 │ │ ├── ChainOfResponsibilityPatternViewController.h │ │ ├── ChainOfResponsibilityPatternViewController.m │ │ ├── ChairmanHandler.h │ │ ├── ChairmanHandler.m │ │ ├── GroupLeaderHandler.h │ │ ├── GroupLeaderHandler.m │ │ ├── Handler.h │ │ ├── Handler.m │ │ ├── ManagerHandler.h │ │ ├── ManagerHandler.m │ │ ├── Order.h │ │ └── Order.m │ │ └── 迭代器模式 │ │ ├── IteratorPatternViewController.h │ │ ├── IteratorPatternViewController.m │ │ ├── LinkIterator.h │ │ ├── LinkIterator.m │ │ ├── LinkListProtocol.h │ │ ├── LinkedList.h │ │ ├── LinkedList.m │ │ ├── Node.h │ │ └── Node.m └── demo.png ├── DesignPatternIntroduce.md ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/12. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/12. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "ViewController.h" 11 | 12 | @interface AppDelegate () 13 | 14 | @end 15 | 16 | @implementation AppDelegate 17 | 18 | 19 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 20 | // Override point for customization after application launch. 21 | 22 | ViewController *_mainVC = [[ViewController alloc] init]; 23 | UINavigationController *naiv = [[UINavigationController alloc] initWithRootViewController:_mainVC]; 24 | self.window.rootViewController = naiv; 25 | return YES; 26 | } 27 | 28 | 29 | - (void)applicationWillResignActive:(UIApplication *)application { 30 | // 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. 31 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 32 | } 33 | 34 | 35 | - (void)applicationDidEnterBackground:(UIApplication *)application { 36 | // 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. 37 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 38 | } 39 | 40 | 41 | - (void)applicationWillEnterForeground:(UIApplication *)application { 42 | // 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. 43 | } 44 | 45 | 46 | - (void)applicationDidBecomeActive:(UIApplication *)application { 47 | // 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. 48 | } 49 | 50 | 51 | - (void)applicationWillTerminate:(UIApplication *)application { 52 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 53 | } 54 | 55 | 56 | @end 57 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/Assets.xcassets/File.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "File@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/Assets.xcassets/File.imageset/File@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WiKi123/DesignPattern/b6300815a52f99c30f1f050b0b3433d3eadeba32/DesignModeDemo/DesignModeDemo/Assets.xcassets/File.imageset/File@2x.png -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/Assets.xcassets/Folder.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "Folder@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/Assets.xcassets/Folder.imageset/Folder@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WiKi123/DesignPattern/b6300815a52f99c30f1f050b0b3433d3eadeba32/DesignModeDemo/DesignModeDemo/Assets.xcassets/Folder.imageset/Folder@2x.png -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/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 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/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 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/12. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/12. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | 10 | #define HEADER_HEIGHT ((AFTER_IOS7)?64:44) 11 | #define AFTER_IOS7 ([[[UIDevice currentDevice] systemVersion] intValue] >= 7) 12 | #define APP_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width 13 | #define APP_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height 14 | 15 | 16 | #import "ViewController.h" 17 | 18 | 19 | @interface ViewController () 20 | 21 | @property (nonatomic,strong) UITableView *tbView; 22 | @property (nonatomic,strong) NSArray *designModeArray; 23 | @property (nonatomic,strong) NSDictionary *modeVCDic; 24 | 25 | @end 26 | 27 | @implementation ViewController 28 | 29 | - (void)viewDidLoad { 30 | [super viewDidLoad]; 31 | self.title = @"24种设计模式-DEMO"; 32 | self.view.backgroundColor = [UIColor whiteColor]; 33 | [self.view addSubview:self.tbView]; 34 | 35 | // Do any additional setup after loading the view, typically from a nib. 36 | } 37 | 38 | #pragma mark - delegate & dataSource; 39 | 40 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 41 | 42 | return self.designModeArray.count; 43 | } 44 | 45 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 46 | 47 | 48 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 49 | if (cell == nil) { 50 | cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 51 | cell.selectionStyle = UITableViewCellSelectionStyleNone; 52 | } 53 | 54 | cell.textLabel.text = [self.designModeArray objectAtIndex:indexPath.row]; 55 | if (indexPath.row < 5) { 56 | cell.backgroundColor = [UIColor redColor]; 57 | }else if(indexPath.row < 12){ 58 | cell.backgroundColor = [UIColor greenColor]; 59 | }else{ 60 | cell.backgroundColor = [UIColor blueColor]; 61 | } 62 | 63 | return cell; 64 | } 65 | 66 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 67 | 68 | return 50; 69 | 70 | } 71 | 72 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 73 | 74 | 75 | NSString *type = [self.designModeArray objectAtIndex:indexPath.row]; 76 | NSLog(@"\n\n=================设计模式:%@=================\n\n",type); 77 | NSString *vcName = [self.modeVCDic objectForKey:type]; 78 | UIViewController *subViewController = [[NSClassFromString(vcName) alloc] init]; 79 | subViewController.title = type; 80 | subViewController.view.backgroundColor = [UIColor whiteColor]; 81 | [self.navigationController pushViewController:subViewController animated:YES]; 82 | 83 | } 84 | 85 | #pragma mark - getters and setters; 86 | 87 | - (UITableView *)tbView{ 88 | 89 | if (_tbView == nil) { 90 | // CGRect tbRect = CGRectMake(0, 0, APP_SCREEN_WIDTH, APP_SCREEN_HEIGHT - HEADER_HEIGHT); 91 | _tbView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];; 92 | _tbView.delegate = self; 93 | _tbView.dataSource = self; 94 | _tbView.tableFooterView = [[UIView alloc] init]; 95 | } 96 | return _tbView; 97 | } 98 | 99 | 100 | - (NSArray *)designModeArray{ 101 | 102 | if (_designModeArray == nil) { 103 | _designModeArray = @[@"工厂方法模式",@"抽象工厂模式",@"单例模式",@"构建者模式",@"原型模式", 104 | 105 | @"适配器模式",@"装饰器模式",@"代理模式",@"外观模式",@"桥接模式",@"组合模式", 106 | @"享元模式", 107 | 108 | @"策略模式",@"模板模式",@"观察者模式",@"中介者模式",@"迭代器模式",@"责任链模式", 109 | @"命令模式",@"备忘录模式",@"状态模式",@"访问者模式",@"解释器模式"]; 110 | } 111 | return _designModeArray; 112 | 113 | } 114 | 115 | - (NSDictionary *)modeVCDic{ 116 | 117 | if (_modeVCDic == nil) { 118 | 119 | _modeVCDic = @{ 120 | @"工厂方法模式":@"FactoryPatternViewController", 121 | @"抽象工厂模式":@"AbstractFactoryPatternViewController", 122 | @"单例模式":@"SingletonPatternViewController", 123 | @"构建者模式":@"BuilderViewController", 124 | @"原型模式":@"PrototypePatternViewController", 125 | 126 | @"适配器模式":@"AdapterViewController", 127 | @"装饰器模式":@"DecoratorPatternViewController", 128 | @"代理模式":@"DelegatePatternViewController", 129 | @"外观模式":@"FacadePatternViewController", 130 | @"桥接模式":@"BridgePatternViewController", 131 | @"组合模式":@"CompositePatternViewController", 132 | @"享元模式":@"FlyweightPatternViewController", 133 | 134 | @"策略模式":@"StrategyPatternViewController", 135 | @"模板模式":@"TemplatePatternViewController", 136 | @"观察者模式":@"ObserverPatternViewController", 137 | @"中介者模式":@"MediatorPatternViewController", 138 | @"迭代器模式":@"IteratorPatternViewController", 139 | @"责任链模式":@"ChainOfResponsibilityPatternViewController", 140 | @"命令模式":@"CommandPatternViewController", 141 | @"备忘录模式":@"MementoPatternViewController", 142 | @"状态模式":@"StatePatternViewController", 143 | @"访问者模式":@"VisitorPatternViewController", 144 | @"解释器模式":@"InterpreterPatternViewController" 145 | 146 | }; 147 | } 148 | return _modeVCDic; 149 | 150 | } 151 | 152 | @end 153 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/12. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/单例模式/SingletonManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // SingletonManager.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SingletonManager : NSObject 12 | 13 | + (instancetype)shareManager; 14 | 15 | - (void)makeSureIsSingleton; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/单例模式/SingletonManager.m: -------------------------------------------------------------------------------- 1 | // 2 | // SingletonManager.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "SingletonManager.h" 10 | 11 | static SingletonManager *manager = nil; 12 | 13 | @implementation SingletonManager 14 | 15 | + (instancetype)shareManager { 16 | 17 | static dispatch_once_t onceToken; 18 | dispatch_once(&onceToken, ^{ 19 | manager = [[SingletonManager alloc] initSingletonManager]; 20 | }); 21 | return manager; 22 | } 23 | 24 | 25 | - (instancetype)initSingletonManager { 26 | self = [super init]; 27 | if (self) { 28 | } 29 | return self; 30 | } 31 | 32 | 33 | - (instancetype)init{ 34 | self = [super init]; 35 | if (self) { 36 | NSLog(@"请使用单例SingletonManager初始化"); 37 | assert(NO); 38 | } 39 | return self; 40 | } 41 | 42 | - (void)makeSureIsSingleton { 43 | 44 | NSLog(@"Current method --------- %@",NSStringFromSelector(_cmd)); 45 | } 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/单例模式/SingletonPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // SingletonPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SingletonPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/单例模式/SingletonPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // SingletonPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "SingletonPatternViewController.h" 10 | #import "SingletonManager.h" 11 | 12 | @interface SingletonPatternViewController () 13 | 14 | @end 15 | 16 | @implementation SingletonPatternViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | // Do any additional setup after loading the view. 21 | 22 | 23 | [[SingletonManager shareManager] makeSureIsSingleton]; 24 | 25 | //[[SingletonManager alloc] init]; 26 | 27 | } 28 | 29 | - (void)didReceiveMemoryWarning { 30 | [super didReceiveMemoryWarning]; 31 | // Dispose of any resources that can be recreated. 32 | } 33 | 34 | /* 35 | #pragma mark - Navigation 36 | 37 | // In a storyboard-based application, you will often want to do a little preparation before navigation 38 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 39 | // Get the new view controller using [segue destinationViewController]. 40 | // Pass the selected object to the new view controller. 41 | } 42 | */ 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/原型模式/BaseCopyObject.h: -------------------------------------------------------------------------------- 1 | // 2 | // BaseCopyObject.h 3 | // NSCopingDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface BaseCopyObject : NSObject 12 | 13 | // 子类不要重载 14 | - (id)copyWithZone:(NSZone *)zone; 15 | 16 | // 子类去实现 17 | - (void)copyOperationWithObject:(id)object; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/原型模式/BaseCopyObject.m: -------------------------------------------------------------------------------- 1 | // 2 | // BaseCopyObject.m 3 | // NSCopingDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "BaseCopyObject.h" 10 | 11 | @implementation BaseCopyObject 12 | 13 | - (id)copyWithZone:(NSZone *)zone { 14 | BaseCopyObject *copyObject = [[self class] allocWithZone:zone]; 15 | // 赋值操作 16 | [self copyOperationWithObject:copyObject]; 17 | 18 | return copyObject; 19 | } 20 | 21 | - (void)copyOperationWithObject:(id)object { 22 | } 23 | 24 | @end 25 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/原型模式/ClassModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // ClassModel.h 3 | // NSCopingDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "BaseCopyObject.h" 10 | 11 | @interface ClassModel : BaseCopyObject 12 | 13 | @property (nonatomic, copy) NSString *className; 14 | @property (nonatomic, copy) NSArray *students; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/原型模式/ClassModel.m: -------------------------------------------------------------------------------- 1 | // 2 | // ClassModel.m 3 | // NSCopingDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "ClassModel.h" 10 | 11 | @implementation ClassModel 12 | - (void)copyOperationWithObject:(ClassModel *)object { 13 | object.className = self.className; 14 | // object.students = self.students; 15 | 16 | // 深拷贝 17 | object.students = [[NSArray alloc] initWithArray:self.students copyItems:YES]; 18 | 19 | 20 | } 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/原型模式/PrototypePatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // PrototypePatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface PrototypePatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/原型模式/PrototypePatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // PrototypePatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "PrototypePatternViewController.h" 10 | #import "StudentModel.h" 11 | #import "ClassModel.h" 12 | 13 | @interface PrototypePatternViewController () 14 | 15 | @end 16 | 17 | @implementation PrototypePatternViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | // Do any additional setup after loading the view. 22 | 23 | 24 | StudentModel *stu1 = [StudentModel new]; 25 | stu1.name = @"张三"; 26 | 27 | StudentModel *stu2 = stu1.copy; 28 | 29 | // classModel 30 | ClassModel *class1 = [[ClassModel alloc] init]; 31 | class1.className = @"ban ji 1"; 32 | class1.students = @[stu1, stu2]; 33 | 34 | ClassModel *class2 = class1.copy; 35 | NSLog(@"%@ ----%@", class1, class2); 36 | 37 | NSLog(@"%@", class1.students); 38 | NSLog(@"%@", class2.students); 39 | 40 | } 41 | 42 | - (void)didReceiveMemoryWarning { 43 | [super didReceiveMemoryWarning]; 44 | // Dispose of any resources that can be recreated. 45 | } 46 | 47 | /* 48 | #pragma mark - Navigation 49 | 50 | // In a storyboard-based application, you will often want to do a little preparation before navigation 51 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 52 | // Get the new view controller using [segue destinationViewController]. 53 | // Pass the selected object to the new view controller. 54 | } 55 | */ 56 | 57 | @end 58 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/原型模式/StudentModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // StudentModel.h 3 | // NSCopingDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "BaseCopyObject.h" 10 | 11 | @interface StudentModel : BaseCopyObject 12 | 13 | @property (nonatomic, copy) NSString *name; 14 | @property (nonatomic, copy) NSString *age; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/原型模式/StudentModel.m: -------------------------------------------------------------------------------- 1 | // 2 | // StudentModel.m 3 | // NSCopingDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "StudentModel.h" 10 | 11 | @implementation StudentModel 12 | 13 | - (void)copyOperationWithObject:(StudentModel *)object { 14 | object.name = self.name; 15 | object.age = self.age; 16 | } 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/工厂方法模式/BallpointPen.h: -------------------------------------------------------------------------------- 1 | // 2 | // BallpointPen.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Pen.h" 10 | 11 | @interface BallpointPen : Pen 12 | 13 | - (void)lookLikeFeeling; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/工厂方法模式/BallpointPen.m: -------------------------------------------------------------------------------- 1 | // 2 | // BallpointPen.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "BallpointPen.h" 10 | 11 | @implementation BallpointPen 12 | 13 | - (void)ifHeavy{ 14 | 15 | NSLog(@"圆珠笔比较轻"); 16 | } 17 | 18 | - (void)ifExpensive{ 19 | 20 | NSLog(@"圆珠笔价格适中"); 21 | } 22 | 23 | - (void)lookLikeFeeling{ 24 | 25 | NSLog(@"很有feel"); 26 | } 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/工厂方法模式/FactoryPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // FactoryPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface FactoryPatternViewController : UIViewController 12 | 13 | @end 14 | 15 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/工厂方法模式/FactoryPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // FactoryPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "FactoryPatternViewController.h" 10 | #import "PenFactory.h" 11 | #import "Pencil.h" 12 | #import "BallpointPen.h" 13 | 14 | @interface FactoryPatternViewController () 15 | 16 | @end 17 | 18 | @implementation FactoryPatternViewController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | // Do any additional setup after loading the view. 23 | 24 | 25 | /* 26 | 工厂方法 27 | 1.通过类继承创建抽象产品 28 | 2.创建一种产品 29 | 3.子类化创建并重写工厂方法来创建新产品 30 | 31 | 工厂方法: 多个产品抽象 32 | */ 33 | 34 | // 35 | Pen *pen = [PenFactory penFactory:PenTypePencil]; 36 | [pen ifHeavy]; 37 | [pen ifExpensive]; 38 | 39 | // 40 | [(Pencil *)pen useFeeling]; 41 | 42 | // 43 | BallpointPen *ballPen = (BallpointPen *)[PenFactory penFactory:PenTypeBallpointPen]; 44 | [ballPen lookLikeFeeling]; 45 | 46 | } 47 | 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/工厂方法模式/Pen.h: -------------------------------------------------------------------------------- 1 | // 2 | // Pen.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface Pen : NSObject 12 | 13 | - (void)ifHeavy; 14 | 15 | - (void)ifExpensive; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/工厂方法模式/Pen.m: -------------------------------------------------------------------------------- 1 | // 2 | // Pen.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Pen.h" 10 | 11 | @implementation Pen 12 | 13 | - (void)ifHeavy{ 14 | 15 | 16 | } 17 | 18 | - (void)ifExpensive{ 19 | 20 | 21 | } 22 | 23 | @end 24 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/工厂方法模式/PenFactory.h: -------------------------------------------------------------------------------- 1 | // 2 | // PenFactory.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Pen.h" 11 | 12 | typedef enum : NSUInteger { 13 | PenTypePencil, 14 | PenTypeBallpointPen, 15 | PenTypeNone, 16 | } PenType; 17 | 18 | @interface PenFactory : NSObject 19 | 20 | + (Pen *)penFactory:(PenType)penType; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/工厂方法模式/PenFactory.m: -------------------------------------------------------------------------------- 1 | // 2 | // PenFactory.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "PenFactory.h" 10 | #import "Pencil.h" 11 | #import "BallpointPen.h" 12 | 13 | @implementation PenFactory 14 | 15 | + (Pen *)penFactory:(PenType)penType{ 16 | 17 | Pen *pen = nil; 18 | 19 | switch (penType) { 20 | case PenTypePencil: 21 | pen = [[Pencil alloc] init]; 22 | break; 23 | case PenTypeBallpointPen: 24 | pen = [[BallpointPen alloc]init]; 25 | default: 26 | break; 27 | } 28 | return pen; 29 | 30 | } 31 | @end 32 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/工厂方法模式/Pencil.h: -------------------------------------------------------------------------------- 1 | // 2 | // Pencil.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Pen.h" 10 | 11 | 12 | @interface Pencil : Pen 13 | 14 | - (void)useFeeling; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/工厂方法模式/Pencil.m: -------------------------------------------------------------------------------- 1 | // 2 | // Pencil.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Pencil.h" 10 | 11 | @implementation Pencil 12 | 13 | - (void)ifHeavy{ 14 | 15 | NSLog(@"铅笔很轻"); 16 | } 17 | 18 | - (void)ifExpensive{ 19 | 20 | NSLog(@"铅笔很便宜"); 21 | 22 | } 23 | 24 | - (void)useFeeling{ 25 | 26 | NSLog(@"铅笔写错字容易修改"); 27 | 28 | } 29 | 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/AbstractFactoryPatternImg.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WiKi123/DesignPattern/b6300815a52f99c30f1f050b0b3433d3eadeba32/DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/AbstractFactoryPatternImg.jpeg -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/AbstractFactoryPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // AbstractFactoryPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AbstractFactoryPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/AbstractFactoryPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // AbstractFactoryPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "AbstractFactoryPatternViewController.h" 10 | #import "CircleHeaderFactory.h" 11 | #import "SquareHeaderFactory.h" 12 | 13 | @interface AbstractFactoryPatternViewController () 14 | 15 | @end 16 | 17 | @implementation AbstractFactoryPatternViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | // Do any additional setup after loading the view. 22 | 23 | 24 | /* 25 | 抽象工厂 26 | 1. 通过对象组合创建抽象产品 27 | 2. 创建多个系列产品 28 | 3. 必须修改父类的接口才能支持新的产品 29 | 30 | 抽象工厂: 是对工厂抽象 31 | */ 32 | 33 | UIView *imgV = [CircleHeaderFactory creatHeaderImgView]; 34 | [self.view addSubview:imgV]; 35 | 36 | 37 | UIView *img1 = [SquareHeaderFactory creatHeaderImgView]; 38 | [self.view addSubview:img1]; 39 | } 40 | 41 | @end 42 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/CircleHeaderFactory.h: -------------------------------------------------------------------------------- 1 | // 2 | // CircleHeaderFactory.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "HeaderImgFactory.h" 10 | 11 | @interface CircleHeaderFactory : HeaderImgFactory 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/CircleHeaderFactory.m: -------------------------------------------------------------------------------- 1 | // 2 | // CircleHeaderFactory.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "CircleHeaderFactory.h" 10 | #import "CircleView.h" 11 | 12 | @implementation CircleHeaderFactory 13 | 14 | + (UIView *)creatHeaderImgView { 15 | return [[CircleView alloc] init]; 16 | } 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/CircleView.h: -------------------------------------------------------------------------------- 1 | // 2 | // CircleView.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface CircleView : UIImageView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/CircleView.m: -------------------------------------------------------------------------------- 1 | // 2 | // CircleView.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "CircleView.h" 10 | 11 | @implementation CircleView 12 | 13 | - (instancetype)init{ 14 | self = [super init]; 15 | if (self) { 16 | 17 | self.clipsToBounds = YES; 18 | self.frame = CGRectMake(60, 100, 100, 100); 19 | self.layer.cornerRadius = 50; 20 | self.image = [UIImage imageNamed:@"AbstractFactoryPatternImg.jpeg"]; 21 | } 22 | return self; 23 | 24 | } 25 | @end 26 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/HeaderImgFactory.h: -------------------------------------------------------------------------------- 1 | // 2 | // HeaderImgFactory.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface HeaderImgFactory : NSObject 13 | 14 | + (UIView *)creatHeaderImgView; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/HeaderImgFactory.m: -------------------------------------------------------------------------------- 1 | // 2 | // HeaderImgFactory.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "HeaderImgFactory.h" 10 | 11 | @implementation HeaderImgFactory 12 | 13 | + (UIView *)creatHeaderImgView { 14 | return nil; 15 | } 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/SquareHeaderFactory.h: -------------------------------------------------------------------------------- 1 | // 2 | // SquareHeaderFactory.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "HeaderImgFactory.h" 10 | 11 | @interface SquareHeaderFactory : HeaderImgFactory 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/SquareHeaderFactory.m: -------------------------------------------------------------------------------- 1 | // 2 | // SquareHeaderFactory.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "SquareHeaderFactory.h" 10 | #import "SquareView.h" 11 | 12 | @implementation SquareHeaderFactory 13 | 14 | + (UIView *)creatHeaderImgView { 15 | return [[SquareView alloc] init];; 16 | } 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/SquareView.h: -------------------------------------------------------------------------------- 1 | // 2 | // SquareView.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SquareView : UIImageView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/抽象工厂模式/SquareView.m: -------------------------------------------------------------------------------- 1 | // 2 | // SquareView.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "SquareView.h" 10 | 11 | @implementation SquareView 12 | 13 | - (instancetype)init{ 14 | self = [super init]; 15 | if (self) { 16 | 17 | self.frame = CGRectMake(40, 240, 120, 120); 18 | self.image = [UIImage imageNamed:@"AbstractFactoryPatternImg.jpeg"]; 19 | } 20 | return self; 21 | 22 | } 23 | @end 24 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/构建者模式/BuilderViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // BuilderViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/12. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface BuilderViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/构建者模式/BuilderViewController.m: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 比如: 4 | 我们想要一辆车,在VC中我们不管如何实现的,最后给我车就OK。 5 | */ 6 | 7 | 8 | #import "BuilderViewController.h" 9 | #import "Director.h" 10 | #import "CarCompanyBuilder.h" 11 | 12 | @interface BuilderViewController () 13 | 14 | @end 15 | 16 | @implementation BuilderViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | // Do any additional setup after loading the view. 21 | 22 | //第一种构建方式 23 | id build = [[CarCompanyBuilder alloc] init]; 24 | 25 | Director *director = [[Director alloc] initWithBuilder:build]; 26 | 27 | id car = [director constructWithBody:@"法拉利F1车身" andEngine:@"8缸发动机" andTyre:@"华勤轮胎"]; 28 | 29 | NSLog(@"car1 = %@",car); 30 | 31 | 32 | //第二种构建方式 33 | id build2 = [[CarCompanyBuilder alloc] init]; 34 | 35 | id car2 = [[[[build2 buildCarBody:@"五菱宏光"] buildEngine:@"4缸发动机"] buildTyre:@"防滑轮胎"] getCar]; 36 | 37 | NSLog(@"car2 = %@",car2); 38 | 39 | 40 | } 41 | 42 | - (void)didReceiveMemoryWarning { 43 | [super didReceiveMemoryWarning]; 44 | // Dispose of any resources that can be recreated. 45 | } 46 | 47 | 48 | @end 49 | 50 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/构建者模式/CarBuilder.h: -------------------------------------------------------------------------------- 1 | // 2 | // CarBuilder.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "CarCompanyProtocol.h" 11 | 12 | @protocol CarBuilder 13 | 14 | @required 15 | 16 | - (id)buildCarBody:(NSString *)carBody; 17 | 18 | - (id)buildEngine:(NSString *)engine; 19 | 20 | - (id)buildTyre:(NSString *)tyre; 21 | 22 | - (id)getCar; 23 | 24 | @end 25 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/构建者模式/Director.h: -------------------------------------------------------------------------------- 1 | // 2 | // Director.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "CarBuilder.h" 11 | 12 | 13 | @interface Director : NSObject 14 | 15 | - (instancetype)initWithBuilder:(id)builder; 16 | 17 | //获取想要的车 18 | - (id)constructWithBody:(NSString *)carBody 19 | andEngine:(NSString *)engine 20 | andTyre:(NSString *)tyre; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/构建者模式/Director.m: -------------------------------------------------------------------------------- 1 | // 2 | // Director.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Director.h" 10 | 11 | @interface Director() 12 | 13 | @property (nonatomic,strong) id carBuilder; 14 | 15 | @end 16 | 17 | 18 | @implementation Director 19 | 20 | - (instancetype)initWithBuilder:(id)builder{ 21 | 22 | self = [super init]; 23 | if (self) { 24 | _carBuilder = builder; 25 | } 26 | return self; 27 | } 28 | 29 | - (id)constructWithBody:(NSString *)carBody 30 | andEngine:(NSString *)engine 31 | andTyre:(NSString *)tyre{ 32 | 33 | return [[[[_carBuilder buildCarBody:carBody] buildEngine:engine] buildTyre:tyre] getCar]; 34 | 35 | } 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/构建者模式/车场/Car.h: -------------------------------------------------------------------------------- 1 | // 2 | // CarCompany.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "CarCompanyProtocol.h" 11 | 12 | @interface Car : NSObject 13 | 14 | @property (nonatomic,strong,readonly) NSString *carBody; 15 | @property (nonatomic,strong,readonly) NSString *engine; 16 | @property (nonatomic,strong,readonly) NSString *tyre; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/构建者模式/车场/Car.m: -------------------------------------------------------------------------------- 1 | // 2 | // CarCompany.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Car.h" 10 | 11 | @interface Car () 12 | 13 | @property (nonatomic,strong,readwrite) NSString *carBody; 14 | @property (nonatomic,strong,readwrite) NSString *engine; 15 | @property (nonatomic,strong,readwrite) NSString *tyre; 16 | 17 | @end 18 | 19 | @implementation Car 20 | 21 | #pragma mark - CarCompanyProtocol 22 | 23 | - (void)buildCarBody:(NSString *)carBody { 24 | 25 | NSLog(@"制造车身:%@",carBody); 26 | self.carBody = carBody; 27 | } 28 | 29 | - (void)buildEngine:(NSString *)engine { 30 | 31 | NSLog(@"制造发动机:%@",engine); 32 | self.engine = engine; 33 | } 34 | 35 | - (void)buildTyre:(NSString *)tyre { 36 | 37 | NSLog(@"制造轮胎:%@",tyre); 38 | self.tyre = tyre; 39 | } 40 | 41 | @end 42 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/构建者模式/车场/CarCompanyBuilder.h: -------------------------------------------------------------------------------- 1 | // 2 | // CarBuilder.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "CarBuilder.h" 11 | 12 | @interface CarCompanyBuilder : NSObject 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/构建者模式/车场/CarCompanyBuilder.m: -------------------------------------------------------------------------------- 1 | // 2 | // CarBuilder.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "CarCompanyBuilder.h" 10 | #import "Car.h" 11 | 12 | @interface CarCompanyBuilder () 13 | 14 | @property (nonatomic, strong) id car; //Car *car; Car遵守协议CarCompanyProtocol 15 | 16 | @end 17 | 18 | @implementation CarCompanyBuilder 19 | 20 | - (instancetype)init{ 21 | 22 | self = [super init]; 23 | if (self) { 24 | _car = [[Car alloc] init]; 25 | } 26 | return self; 27 | } 28 | 29 | 30 | #pragma mark - CarBuilder 31 | 32 | - (id)buildCarBody:(NSString *)carBody { 33 | 34 | [_car buildCarBody:carBody]; 35 | return self; 36 | } 37 | 38 | - (id)buildEngine:(NSString *)engine { 39 | 40 | [_car buildEngine:engine]; 41 | return self; 42 | } 43 | 44 | - (id)buildTyre:(NSString *)tyre { 45 | 46 | [_car buildTyre:tyre]; 47 | return self; 48 | } 49 | 50 | - (id)getCar { 51 | 52 | return _car; 53 | } 54 | 55 | @end 56 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/创建型模式/构建者模式/车场/CarCompanyProtocol.h: -------------------------------------------------------------------------------- 1 | // 2 | // CarCompanyProtocol.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | 12 | @protocol CarCompanyProtocol 13 | 14 | @required 15 | 16 | - (void)buildCarBody:(NSString *)carBody; 17 | 18 | - (void)buildEngine:(NSString *)engine; 19 | 20 | - (void)buildTyre:(NSString *)tyre; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/享元模式/Flower.h: -------------------------------------------------------------------------------- 1 | // 2 | // Flower.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface Flower : NSObject 12 | 13 | @property (nonatomic,strong) NSString *color; 14 | @property (nonatomic,strong) NSString *name; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/享元模式/Flower.m: -------------------------------------------------------------------------------- 1 | // 2 | // Flower.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Flower.h" 10 | 11 | @implementation Flower 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/享元模式/FlowerFactory.h: -------------------------------------------------------------------------------- 1 | // 2 | // FlowerFactory.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Flower.h" 11 | 12 | typedef enum : NSUInteger { 13 | FlowerTypeRed, 14 | FlowerTypeBlue, 15 | FlowerTypeWhite, 16 | kTotalNumberFlower // 用于计数的. index = 3 = type number 17 | } FlowerType; 18 | 19 | @interface FlowerFactory : NSObject 20 | 21 | - (Flower *)flowerWithType:(FlowerType)flowerType; 22 | 23 | @property (nonatomic, strong) NSMutableDictionary *flowerPools; 24 | 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/享元模式/FlowerFactory.m: -------------------------------------------------------------------------------- 1 | // 2 | // FlowerFactory.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "FlowerFactory.h" 10 | 11 | @implementation FlowerFactory 12 | 13 | - (Flower *)flowerWithType:(FlowerType)flowerType{ 14 | 15 | if (self.flowerPools == nil) { 16 | self.flowerPools = [NSMutableDictionary dictionaryWithCapacity:kTotalNumberFlower]; 17 | } 18 | 19 | Flower *flower = [self.flowerPools objectForKey:[NSNumber numberWithInteger:flowerType]]; 20 | 21 | if (flower == nil) { 22 | 23 | flower = [[Flower alloc] init]; 24 | switch (flowerType) { 25 | case FlowerTypeRed: 26 | flower.color = @"红色"; 27 | flower.name = @"红玫瑰"; 28 | break; 29 | 30 | case FlowerTypeBlue: 31 | flower.color = @"蓝色"; 32 | flower.name = @"普罗旺斯"; 33 | break; 34 | case FlowerTypeWhite: 35 | flower.color = @"白色"; 36 | flower.name = @"白百何"; 37 | default: 38 | break; 39 | } 40 | 41 | [self.flowerPools setObject:flower forKey:[NSNumber numberWithInt:flowerType]]; 42 | } 43 | 44 | return flower; 45 | } 46 | 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/享元模式/FlyweightPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // FlyweightPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface FlyweightPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/享元模式/FlyweightPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // FlyweightPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "FlyweightPatternViewController.h" 10 | #import "FlowerFactory.h" 11 | 12 | 13 | @interface FlyweightPatternViewController () 14 | 15 | @end 16 | 17 | @implementation FlyweightPatternViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | // Do any additional setup after loading the view. 22 | 23 | 24 | /* 25 | cell的复用机制其实就是享元模式 26 | */ 27 | 28 | FlowerFactory *factory = [[FlowerFactory alloc] init]; 29 | 30 | NSMutableArray *mutArr = [[NSMutableArray alloc] init]; 31 | 32 | for (int i=0; i<5; i++) { 33 | 34 | FlowerType flowerType = arc4random_uniform(kTotalNumberFlower); 35 | 36 | // 使用缓存池工厂方法来调用.类型是随机的 37 | 38 | Flower *flower = [factory flowerWithType:flowerType]; 39 | // 简单的创建方式 40 | [mutArr addObject:flower]; 41 | } 42 | 43 | } 44 | 45 | 46 | @end 47 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/代理模式/DelegatePatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // DelegatePatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface DelegatePatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/代理模式/DelegatePatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // DelegatePatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "DelegatePatternViewController.h" 10 | #import "Person.h" 11 | 12 | @interface DelegatePatternViewController () 13 | 14 | @end 15 | 16 | @implementation DelegatePatternViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | // Do any additional setup after loading the view. 21 | 22 | Person *koala = [[Person alloc] init]; 23 | koala.delegate = self; 24 | [koala run:5]; 25 | 26 | } 27 | 28 | - (void)person:(Person *)person runSpeed:(NSInteger)speed { 29 | 30 | NSLog(@"spped is %d",speed); 31 | 32 | } 33 | 34 | 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/代理模式/Person.h: -------------------------------------------------------------------------------- 1 | // 2 | // Person.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | @class Person; 11 | 12 | @protocol PersonDelegate 13 | 14 | @optional 15 | 16 | - (void)person:(Person *)person runSpeed:(NSInteger)speed; 17 | 18 | @end 19 | 20 | 21 | @interface Person : NSObject 22 | 23 | @property (nonatomic,weak) id delegate; 24 | 25 | - (void)run:(NSInteger)speed; 26 | 27 | @end 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/代理模式/Person.m: -------------------------------------------------------------------------------- 1 | // 2 | // Person.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Person.h" 10 | 11 | @implementation Person 12 | 13 | - (void)run:(NSInteger)speed { 14 | 15 | if (self.delegate && [self.delegate respondsToSelector:@selector(person:runSpeed:)]) { 16 | 17 | [self.delegate person:self runSpeed:speed * 1.25]; 18 | } 19 | 20 | } 21 | 22 | 23 | @end 24 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/外观模式/CabDriver.h: -------------------------------------------------------------------------------- 1 | // 2 | // CabDriver.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface CabDriver : NSObject 12 | 13 | // 到达指定的位置 14 | + (void)driveToLocation:(NSString *)location; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/外观模式/CabDriver.m: -------------------------------------------------------------------------------- 1 | // 2 | // CabDriver.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "CabDriver.h" 10 | #import "FacadeCar.h" 11 | #import "Taximeter.h" 12 | 13 | @implementation CabDriver 14 | 15 | + (void)driveToLocation:(NSString *)location { 16 | // 启动计价器 17 | Taximeter *taximeter = [[Taximeter alloc] init]; 18 | [taximeter start]; 19 | 20 | // 驾驶汽车 21 | FacadeCar *car = [[FacadeCar alloc] init]; 22 | [car releaseBrakes]; // 松刹车 23 | [car pressAccelerator]; // 踩油门 24 | 25 | // 到达位置的操作 26 | [car releaseAccelerator]; 27 | [car pressBrakes]; 28 | [taximeter stop]; 29 | 30 | NSLog(@"%@已经到达",location); 31 | } 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/外观模式/FacadeCar.h: -------------------------------------------------------------------------------- 1 | // 2 | // FacadeCar.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface FacadeCar : NSObject 12 | 13 | - (void)releaseBrakes; /**< 松刹车 */ 14 | 15 | - (void)pressBrakes; /**< 踩刹车 */ 16 | 17 | - (void)pressAccelerator; /**< 踩油门 */ 18 | 19 | - (void)releaseAccelerator; /**< 松油门 */ 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/外观模式/FacadeCar.m: -------------------------------------------------------------------------------- 1 | // 2 | // FacadeCar.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "FacadeCar.h" 10 | 11 | @implementation FacadeCar 12 | 13 | - (void)releaseBrakes { 14 | NSLog(@"松刹车"); 15 | } 16 | 17 | - (void)pressBrakes { 18 | NSLog(@"踩刹车"); 19 | } 20 | 21 | - (void)pressAccelerator { 22 | NSLog(@"踩油门"); 23 | } 24 | 25 | - (void)releaseAccelerator { 26 | NSLog(@"松油门"); 27 | } 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/外观模式/FacadePatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // FacadePatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface FacadePatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/外观模式/FacadePatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // FacadePatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "FacadePatternViewController.h" 10 | #import "CabDriver.h" 11 | 12 | @interface FacadePatternViewController () 13 | 14 | @end 15 | 16 | @implementation FacadePatternViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | // Do any additional setup after loading the view. 21 | 22 | [CabDriver driveToLocation:@"帝都"]; 23 | 24 | 25 | } 26 | 27 | 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/外观模式/Taximeter.h: -------------------------------------------------------------------------------- 1 | // 2 | // Taximeter.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface Taximeter : NSObject 12 | 13 | - (void)start; /**< 开 */ 14 | 15 | - (void)stop; /**< 停止 */ 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/外观模式/Taximeter.m: -------------------------------------------------------------------------------- 1 | // 2 | // Taximeter.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Taximeter.h" 10 | 11 | @implementation Taximeter 12 | - (void)start { 13 | NSLog(@"开启"); 14 | } 15 | 16 | - (void)stop { 17 | NSLog(@"停止 "); 18 | } 19 | @end 20 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/BridgePatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // BridgePatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface BridgePatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/BridgePatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // BridgePatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "BridgePatternViewController.h" 10 | #import "DirectionRemote.h" 11 | #import "TemperatureRemote.h" 12 | #import "HaierAirConditioner.h" 13 | #import "GeliAirConditioner.h" 14 | 15 | @interface BridgePatternViewController () 16 | 17 | @end 18 | 19 | @implementation BridgePatternViewController 20 | 21 | - (void)viewDidLoad { 22 | [super viewDidLoad]; 23 | // Do any additional setup after loading the view. 24 | 25 | /* 26 | 桥接模式的特点: 27 | 1. 将依赖具体的实现,改为依赖抽象. 得到松耦合的状态 28 | 2. 分离了接口和实现部分 29 | 3. 提高了扩展性. 30 | 31 | 32 | Ps:热空气上升冷空气下降,所以热风朝下吹 33 | */ 34 | 35 | 36 | //海尔空调 37 | HaierAirConditioner *haierAirConditioner = [[HaierAirConditioner alloc] init]; 38 | 39 | //控制风向 40 | DirectionRemote *directionRemote = [[DirectionRemote alloc] init]; 41 | //让海尔空调往上吹风 42 | directionRemote.airConditioner = haierAirConditioner; 43 | [directionRemote up]; 44 | 45 | //控制温度 46 | TemperatureRemote *temperatureRemote = [[TemperatureRemote alloc] init]; 47 | //让海尔空调更冷 48 | temperatureRemote.airConditioner = haierAirConditioner; 49 | [temperatureRemote colder]; 50 | 51 | //===================================================================================== 52 | 53 | //让格力空调往下吹热风 54 | GeliAirConditioner *geliAirConditioner = [[GeliAirConditioner alloc] init]; 55 | directionRemote.airConditioner = geliAirConditioner; 56 | [directionRemote down]; 57 | temperatureRemote.airConditioner = geliAirConditioner; 58 | [temperatureRemote warmer]; 59 | 60 | 61 | 62 | } 63 | 64 | 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/空调/AirConditioner.h: -------------------------------------------------------------------------------- 1 | // 2 | // AirConditioner.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AirConditioner : NSObject 12 | 13 | 14 | //空调接收命令 15 | - (void)loadCommand:(NSString *)command; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/空调/AirConditioner.m: -------------------------------------------------------------------------------- 1 | // 2 | // AirConditioner.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "AirConditioner.h" 10 | 11 | @implementation AirConditioner 12 | 13 | - (void)loadCommand:(NSString *)command { 14 | 15 | } 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/空调/GeliAirConditioner.h: -------------------------------------------------------------------------------- 1 | // 2 | // GeliAirConditioner.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "AirConditioner.h" 10 | 11 | @interface GeliAirConditioner : AirConditioner 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/空调/GeliAirConditioner.m: -------------------------------------------------------------------------------- 1 | // 2 | // GeliAirConditioner.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "GeliAirConditioner.h" 10 | 11 | @implementation GeliAirConditioner 12 | 13 | - (void)loadCommand:(NSString *)command { 14 | 15 | NSLog(@"格力空调接受到命令:-----%@",command); 16 | } 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/空调/HaierAirConditioner.h: -------------------------------------------------------------------------------- 1 | // 2 | // HaierAirConditioner.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "AirConditioner.h" 10 | 11 | @interface HaierAirConditioner : AirConditioner 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/空调/HaierAirConditioner.m: -------------------------------------------------------------------------------- 1 | // 2 | // HaierAirConditioner.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "HaierAirConditioner.h" 10 | 11 | @implementation HaierAirConditioner 12 | 13 | - (void)loadCommand:(NSString *)command { 14 | 15 | NSLog(@"海尔空调接受到命令:-----%@",command); 16 | 17 | } 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/遥控器/DirectionRemote.h: -------------------------------------------------------------------------------- 1 | // 2 | // DirectionRemote.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Remote.h" 11 | 12 | @interface DirectionRemote : Remote 13 | 14 | - (void)up; 15 | 16 | - (void)down; 17 | 18 | - (void)left; 19 | 20 | - (void)right; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/遥控器/DirectionRemote.m: -------------------------------------------------------------------------------- 1 | // 2 | // DirectionRemote.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "DirectionRemote.h" 10 | 11 | @implementation DirectionRemote 12 | 13 | - (void)up { 14 | [super setCommand:@"up"]; 15 | } 16 | 17 | - (void)down { 18 | 19 | [super setCommand:@"down"]; 20 | } 21 | 22 | - (void)left { 23 | 24 | [super setCommand:@"left"]; 25 | } 26 | 27 | - (void)right { 28 | [super setCommand:@"right"]; 29 | } 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/遥控器/Remote.h: -------------------------------------------------------------------------------- 1 | // 2 | // Remote.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AirConditioner.h" 11 | 12 | @interface Remote : NSObject 13 | 14 | @property (nonatomic,strong) AirConditioner *airConditioner; 15 | 16 | //遥控器给空调发送指令 17 | - (void)setCommand:(NSString *)command; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/遥控器/Remote.m: -------------------------------------------------------------------------------- 1 | // 2 | // Remote.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Remote.h" 10 | 11 | @implementation Remote 12 | 13 | - (void)setCommand:(NSString *)command { 14 | 15 | [self.airConditioner loadCommand:command]; 16 | } 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/遥控器/TemperatureRemote.h: -------------------------------------------------------------------------------- 1 | // 2 | // TemperatureRemote.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Remote.h" 11 | 12 | @interface TemperatureRemote : Remote 13 | 14 | - (void)warmer; 15 | 16 | - (void)colder; 17 | 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/桥接模式/遥控器/TemperatureRemote.m: -------------------------------------------------------------------------------- 1 | // 2 | // TemperatureRemote.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/13. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "TemperatureRemote.h" 10 | 11 | @implementation TemperatureRemote 12 | 13 | - (void)warmer { 14 | [super setCommand:@"warmer"]; 15 | } 16 | 17 | - (void)colder { 18 | [super setCommand:@"colder"]; 19 | } 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/组合模式/CompositePatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // CompositePatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface CompositePatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/组合模式/CompositePatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // CompositePatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "CompositePatternViewController.h" 10 | #import "FileCell.h" 11 | #import "File.h" 12 | 13 | @interface CompositePatternViewController () 14 | 15 | @property (nonatomic, strong) UITableView *tableView; 16 | @property (nonatomic, strong) File *root; 17 | 18 | @end 19 | 20 | @implementation CompositePatternViewController 21 | 22 | - (void)viewDidLoad { 23 | [super viewDidLoad]; 24 | 25 | self.title = @"Root"; 26 | 27 | // 1.创建根节点 28 | self.root = [File fileWithFileType:kFolder fileName:@"root"]; 29 | 30 | // 2.创建第一级文件 31 | File *folder_A_1 = [File fileWithFileType:kFolder fileName:@"Folder_A_1"]; 32 | File *file_A_1 = [File fileWithFileType:kFile fileName:@"File_A_1"]; 33 | File *file_A_2 = [File fileWithFileType:kFile fileName:@"File_A_2"]; 34 | File *file_A_3 = [File fileWithFileType:kFile fileName:@"File_A_3"]; 35 | 36 | 37 | // 3.创建第二级文件 38 | File *folder_B_1 = [File fileWithFileType:kFolder fileName:@"Folder_B_1"]; 39 | File *file_B_1 = [File fileWithFileType:kFile fileName:@"File_B_1"]; 40 | File *file_B_2 = [File fileWithFileType:kFile fileName:@"File_B_2"]; 41 | File *folder_B_2 = [File fileWithFileType:kFolder fileName:@"Folder_B_2"]; 42 | 43 | // 4.创建第二级文件 44 | File *folder_C_1 = [File fileWithFileType:kFolder fileName:@"Folder_C_1"]; 45 | File *file_C_1 = [File fileWithFileType:kFile fileName:@"File_C_1"]; 46 | File *file_C_2 = [File fileWithFileType:kFile fileName:@"File_C_2"]; 47 | 48 | 49 | [self.root addFile:folder_A_1]; 50 | [self.root addFile:file_A_1]; 51 | [self.root addFile:file_A_2]; 52 | [self.root addFile:file_A_3]; 53 | 54 | [folder_A_1 addFile:folder_B_1]; 55 | [folder_A_1 addFile:file_B_1]; 56 | [folder_A_1 addFile:file_B_2]; 57 | [folder_A_1 addFile:folder_B_2]; 58 | 59 | [folder_B_1 addFile:folder_C_1]; 60 | [folder_B_1 addFile:file_C_1]; 61 | [folder_B_1 addFile:file_C_2]; 62 | 63 | 64 | [self setTableView]; 65 | } 66 | 67 | #pragma mark - tableView相关 68 | - (void)setTableView { 69 | 70 | self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; 71 | 72 | self.tableView.delegate = self; 73 | self.tableView.dataSource = self; 74 | self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 75 | 76 | [self.tableView registerClass:[FileCell class] forCellReuseIdentifier:@"fileCell"]; 77 | 78 | [self.view addSubview:self.tableView]; 79 | } 80 | 81 | #pragma mark - UITableViewDataSource 82 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 83 | 84 | return self.root.childFiles.count; 85 | } 86 | 87 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 88 | 89 | FileCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fileCell"]; 90 | cell.indexPath = indexPath; 91 | cell.tableView = tableView; 92 | cell.controller = self; 93 | 94 | // 传入节点File 95 | cell.data = self.root.childFiles[indexPath.row]; 96 | 97 | [cell loadContent]; 98 | 99 | return cell; 100 | } 101 | 102 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 103 | 104 | return 60.f; 105 | } 106 | 107 | @end 108 | 109 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/组合模式/File.h: -------------------------------------------------------------------------------- 1 | // 2 | // File.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | typedef enum :NSUInteger { 12 | kFile,// 文件 13 | kFolder, // 文件夹 14 | 15 | }EFile; 16 | 17 | @interface File : NSObject 18 | 19 | // 文件夹或文件名, 根据枚举来区分 20 | @property (nonatomic, copy) NSString *name; 21 | 22 | @property (nonatomic, assign) EFile fileType; /**< 文件类型 */ 23 | 24 | // 集合 25 | @property (nonatomic, strong) NSMutableArray *childFiles; 26 | 27 | // 添加文件到集合 28 | - (void)addFile:(File *)file; 29 | 30 | // 初始化的方法 31 | + (instancetype)fileWithFileType:(EFile)fileType fileName:(NSString *)name; 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/组合模式/File.m: -------------------------------------------------------------------------------- 1 | // 2 | // File.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "File.h" 10 | 11 | @implementation File 12 | 13 | - (instancetype)init 14 | { 15 | self = [super init]; 16 | if (self) { 17 | self.childFiles = [NSMutableArray array]; 18 | } 19 | return self; 20 | } 21 | 22 | // 添加文件到集合 23 | - (void)addFile:(File *)file { 24 | [self.childFiles addObject:file]; 25 | } 26 | 27 | // 初始化的方法 28 | + (instancetype)fileWithFileType:(EFile)fileType fileName:(NSString *)name { 29 | File *file = [[self alloc] init]; 30 | file.fileType = fileType; 31 | file.name = name; 32 | 33 | return file; 34 | } 35 | 36 | 37 | 38 | @end 39 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/组合模式/FileCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // FileCell.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface FileCell : UITableViewCell 12 | 13 | @property (nonatomic, weak) id data; 14 | @property (nonatomic, weak) NSIndexPath *indexPath; 15 | @property (nonatomic, weak) UITableView *tableView; 16 | @property (nonatomic, weak) UIViewController *controller; 17 | 18 | - (void)loadContent; 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/组合模式/FileCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // FileCell.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "FileCell.h" 10 | #import "UIView+SetRect.h" 11 | #import "File.h" 12 | #import "FileViewController.h" 13 | 14 | @interface FileCell () 15 | 16 | @property (nonatomic, strong) UIImageView *fileImageView; 17 | @property (nonatomic, strong) UIImageView *folderImageView; 18 | @property (nonatomic, strong) UILabel *iconNameLabel; 19 | @property (nonatomic, strong) UIButton *button; 20 | 21 | @property (nonatomic, strong) UILabel *nameLabel; 22 | 23 | @end 24 | 25 | @implementation FileCell 26 | 27 | - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 28 | 29 | if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 30 | 31 | self.selectionStyle = UITableViewCellSelectionStyleNone; 32 | 33 | [self buildView]; 34 | } 35 | 36 | return self; 37 | } 38 | 39 | - (void)buildView { 40 | 41 | UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 79.5f, 500, 0.5f)]; 42 | line.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.25f]; 43 | [self addSubview:line]; 44 | 45 | self.fileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"File"]]; 46 | self.folderImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Folder"]]; 47 | self.fileImageView.center = CGPointMake(40, 50); 48 | self.folderImageView.center = CGPointMake(40, 50); 49 | [self addSubview:self.folderImageView]; 50 | [self addSubview:self.fileImageView]; 51 | 52 | self.iconNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 13, 80, 20)]; 53 | self.iconNameLabel.font = [UIFont fontWithName:@"AppleSDGothicNeo-Light" size:12.f]; 54 | self.iconNameLabel.textAlignment = NSTextAlignmentCenter; 55 | [self addSubview:self.iconNameLabel]; 56 | 57 | self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 10, 200, 60)]; 58 | self.nameLabel.font = [UIFont fontWithName:@"Avenir-Book" size:12.f]; 59 | [self addSubview:self.nameLabel]; 60 | 61 | self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 500, 80)]; 62 | [self.button addTarget:self 63 | action:@selector(buttonEvent) 64 | forControlEvents:UIControlEventTouchUpInside]; 65 | [self addSubview:self.button]; 66 | } 67 | 68 | - (void)buttonEvent { 69 | File *file = self.data; 70 | 71 | if (file.fileType == kFolder) { 72 | FileViewController *fvc = [[FileViewController alloc] init]; 73 | fvc.rootFile = file; 74 | [self.controller.navigationController pushViewController:fvc animated:YES]; 75 | } 76 | 77 | NSLog(@"----%@-", self.indexPath); 78 | } 79 | 80 | // 切换到文件夹状态 81 | - (void)changeToFolderState { 82 | 83 | self.fileImageView.hidden = YES; 84 | self.folderImageView.hidden = NO; 85 | self.iconNameLabel.textColor = [UIColor blackColor]; 86 | self.nameLabel.textColor = [UIColor blackColor]; 87 | self.iconNameLabel.text = @"Folder"; 88 | } 89 | 90 | - (void)loadContent { 91 | // 1.获取数据 92 | File *file = self.data; 93 | 94 | // 2. 文件名等于文件名 95 | self.nameLabel.text = file.name; 96 | 97 | // 3.判断文件类型与文件夹的类型 98 | if (file.fileType == kFolder) { 99 | // 3.1切换文件夹图标 100 | [self changeToFolderState]; 101 | 102 | } else if (file.fileType == kFile) { 103 | // 3.2切换到文件图标 104 | [self changeToFileState]; 105 | } 106 | } 107 | 108 | 109 | // 切换到文件状态 110 | - (void)changeToFileState { 111 | 112 | self.fileImageView.hidden = NO; 113 | self.folderImageView.hidden = YES; 114 | self.iconNameLabel.textColor = [UIColor grayColor]; 115 | self.nameLabel.textColor = [UIColor grayColor]; 116 | self.iconNameLabel.text = @"File"; 117 | } 118 | 119 | 120 | @end 121 | 122 | 123 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/组合模式/FileViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // FileViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "File.h" 11 | 12 | @interface FileViewController : UIViewController 13 | 14 | // 根节点 15 | @property (nonatomic, strong) File *rootFile; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/组合模式/FileViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // FileViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "FileViewController.h" 10 | #import "FileCell.h" 11 | 12 | @interface FileViewController () 13 | 14 | @property (nonatomic, strong) UITableView *tableView; 15 | 16 | @end 17 | 18 | @implementation FileViewController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | // Do any additional setup after loading the view. 23 | self.title = self.rootFile.name; 24 | [self initTableView]; 25 | 26 | } 27 | 28 | - (void)initTableView { 29 | 30 | self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; 31 | 32 | self.tableView.delegate = self; 33 | self.tableView.dataSource = self; 34 | self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 35 | 36 | [self.tableView registerClass:[FileCell class] forCellReuseIdentifier:@"fileCell"]; 37 | 38 | [self.view addSubview:self.tableView]; 39 | } 40 | 41 | #pragma mark - UITableViewDataSource 42 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 43 | 44 | return self.rootFile.childFiles.count; 45 | } 46 | 47 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 48 | 49 | FileCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fileCell"]; 50 | cell.indexPath = indexPath; 51 | cell.tableView = tableView; 52 | cell.controller = self; 53 | 54 | // 传入节点File 55 | cell.data = self.rootFile.childFiles[indexPath.row]; 56 | 57 | [cell loadContent]; 58 | 59 | return cell; 60 | } 61 | 62 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 63 | 64 | return 60.f; 65 | } 66 | 67 | 68 | @end 69 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/组合模式/UIView+SetRect.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+SetRect.h 3 | // 文件组合 4 | // 5 | // Created by James on 2017/12/15. 6 | // Copyright © 2017年 TZ. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIView (SetRect) 12 | // Frame 13 | @property (nonatomic) CGPoint viewOrigin; 14 | @property (nonatomic) CGSize viewSize; 15 | 16 | // Frame Origin 17 | @property (nonatomic) CGFloat x; 18 | @property (nonatomic) CGFloat y; 19 | 20 | // Frame Size 21 | @property (nonatomic) CGFloat width; 22 | @property (nonatomic) CGFloat height; 23 | 24 | // Frame Borders 25 | @property (nonatomic) CGFloat top; 26 | @property (nonatomic) CGFloat left; 27 | @property (nonatomic) CGFloat bottom; 28 | @property (nonatomic) CGFloat right; 29 | 30 | // Center Point 31 | #if !IS_IOS_DEVICE 32 | @property (nonatomic) CGPoint center; 33 | #endif 34 | @property (nonatomic) CGFloat centerX; 35 | @property (nonatomic) CGFloat centerY; 36 | 37 | // Middle Point 38 | @property (nonatomic, readonly) CGPoint middlePoint; 39 | @property (nonatomic, readonly) CGFloat middleX; 40 | @property (nonatomic, readonly) CGFloat middleY; 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/组合模式/UIView+SetRect.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+SetRect.m 3 | // 文件组合 4 | // 5 | // Created by James on 2017/12/15. 6 | // Copyright © 2017年 TZ. All rights reserved. 7 | // 8 | 9 | #import "UIView+SetRect.h" 10 | 11 | @implementation UIView (SetRect) 12 | #pragma mark Frame 13 | 14 | - (CGPoint)viewOrigin 15 | { 16 | return self.frame.origin; 17 | } 18 | 19 | - (void)setViewOrigin:(CGPoint)newOrigin 20 | { 21 | CGRect newFrame = self.frame; 22 | newFrame.origin = newOrigin; 23 | self.frame = newFrame; 24 | } 25 | 26 | - (CGSize)viewSize 27 | { 28 | return self.frame.size; 29 | } 30 | 31 | - (void)setViewSize:(CGSize)newSize 32 | { 33 | CGRect newFrame = self.frame; 34 | newFrame.size = newSize; 35 | self.frame = newFrame; 36 | } 37 | 38 | 39 | #pragma mark Frame Origin 40 | 41 | - (CGFloat)x 42 | { 43 | return self.frame.origin.x; 44 | } 45 | 46 | - (void)setX:(CGFloat)newX 47 | { 48 | CGRect newFrame = self.frame; 49 | newFrame.origin.x = newX; 50 | self.frame = newFrame; 51 | } 52 | 53 | - (CGFloat)y 54 | { 55 | return self.frame.origin.y; 56 | } 57 | 58 | - (void)setY:(CGFloat)newY 59 | { 60 | CGRect newFrame = self.frame; 61 | newFrame.origin.y = newY; 62 | self.frame = newFrame; 63 | } 64 | 65 | 66 | #pragma mark Frame Size 67 | 68 | - (CGFloat)height 69 | { 70 | return self.frame.size.height; 71 | } 72 | 73 | - (void)setHeight:(CGFloat)newHeight 74 | { 75 | CGRect newFrame = self.frame; 76 | newFrame.size.height = newHeight; 77 | self.frame = newFrame; 78 | } 79 | 80 | - (CGFloat)width 81 | { 82 | return self.frame.size.width; 83 | } 84 | 85 | - (void)setWidth:(CGFloat)newWidth 86 | { 87 | CGRect newFrame = self.frame; 88 | newFrame.size.width = newWidth; 89 | self.frame = newFrame; 90 | } 91 | 92 | 93 | #pragma mark Frame Borders 94 | 95 | - (CGFloat)left 96 | { 97 | return self.x; 98 | } 99 | 100 | - (void)setLeft:(CGFloat)left 101 | { 102 | self.x = left; 103 | } 104 | 105 | - (CGFloat)right 106 | { 107 | return self.frame.origin.x + self.frame.size.width; 108 | } 109 | 110 | - (void)setRight:(CGFloat)right 111 | { 112 | self.x = right - self.width; 113 | } 114 | 115 | - (CGFloat)top 116 | { 117 | return self.y; 118 | } 119 | 120 | - (void)setTop:(CGFloat)top 121 | { 122 | self.y = top; 123 | } 124 | 125 | - (CGFloat)bottom 126 | { 127 | return self.frame.origin.y + self.frame.size.height; 128 | } 129 | 130 | - (void)setBottom:(CGFloat)bottom 131 | { 132 | self.y = bottom - self.height; 133 | } 134 | 135 | 136 | #pragma mark Center Point 137 | 138 | #if !IS_IOS_DEVICE 139 | - (CGPoint)center 140 | { 141 | return CGPointMake(self.left + self.middleX, self.top + self.middleY); 142 | } 143 | 144 | - (void)setCenter:(CGPoint)newCenter 145 | { 146 | self.left = newCenter.x - self.middleX; 147 | self.top = newCenter.y - self.middleY; 148 | } 149 | #endif 150 | 151 | - (CGFloat)centerX 152 | { 153 | return self.center.x; 154 | } 155 | 156 | - (void)setCenterX:(CGFloat)newCenterX 157 | { 158 | self.center = CGPointMake(newCenterX, self.center.y); 159 | } 160 | 161 | - (CGFloat)centerY 162 | { 163 | return self.center.y; 164 | } 165 | 166 | - (void)setCenterY:(CGFloat)newCenterY 167 | { 168 | self.center = CGPointMake(self.center.x, newCenterY); 169 | } 170 | 171 | 172 | #pragma mark Middle Point 173 | 174 | - (CGPoint)middlePobbbbbint 175 | { 176 | return CGPointMake(self.middleX, self.middleY); 177 | } 178 | 179 | - (CGFloat)middleX 180 | { 181 | return self.width / 2; 182 | } 183 | 184 | - (CGFloat)middleY 185 | { 186 | return self.height / 2; 187 | } 188 | 189 | @end 190 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/组合模式/文件夹系统.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WiKi123/DesignPattern/b6300815a52f99c30f1f050b0b3433d3eadeba32/DesignModeDemo/DesignModeDemo/结构型模式/组合模式/文件夹系统.png -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/CheatGameDecotor.h: -------------------------------------------------------------------------------- 1 | // 2 | // CheatGameDecotor.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | 10 | #import 11 | #import "GameDecortor.h" 12 | 13 | @interface CheatGameDecotor : GameDecortor 14 | 15 | // 作弊器 16 | - (void)cheat; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/CheatGameDecotor.m: -------------------------------------------------------------------------------- 1 | // 2 | // CheatGameDecotor.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | 10 | #import "CheatGameDecotor.h" 11 | 12 | @implementation CheatGameDecotor 13 | 14 | - (void)cheat { 15 | // 作弊操作 16 | [self up]; 17 | [self up]; 18 | 19 | [self down]; 20 | [self down]; 21 | 22 | [self commandA]; 23 | [self commandA]; 24 | } 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/CoinGameDecorator.h: -------------------------------------------------------------------------------- 1 | // 2 | // CoinGameDecorator.h 3 | // GameComponetDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "GameDecortor.h" 10 | 11 | @interface CoinGameDecorator : GameDecortor 12 | 13 | // 游戏币 14 | @property (nonatomic, assign) NSInteger coin; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/CoinGameDecorator.m: -------------------------------------------------------------------------------- 1 | // 2 | // CoinGameDecorator.m 3 | // GameComponetDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "CoinGameDecorator.h" 10 | 11 | @implementation CoinGameDecorator 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/DecoratorPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // DecoratorPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface DecoratorPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/DecoratorPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // DecoratorPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "DecoratorPatternViewController.h" 10 | #import "GameLoL.h" 11 | #import "GameDecortor.h" 12 | #import "CheatGameDecotor.h" 13 | #import "GameLoL+Cheat.h" 14 | #import "GameLoL+Coin.h" 15 | 16 | @interface DecoratorPatternViewController () 17 | 18 | @end 19 | 20 | @implementation DecoratorPatternViewController 21 | 22 | - (void)viewDidLoad { 23 | [super viewDidLoad]; 24 | // Do any additional setup after loading the view. 25 | 26 | 27 | GameLoL *gameLoL = [[GameLoL alloc] init]; 28 | [gameLoL up]; 29 | [gameLoL cheat]; 30 | 31 | gameLoL.coin = 10; 32 | NSLog(@"coin %ld", (long)gameLoL.coin); 33 | 34 | 35 | GameDecortor *gameDecortor = [[GameDecortor alloc] init]; 36 | [gameDecortor up]; 37 | 38 | // 调用作弊器 39 | CheatGameDecotor *cheatGameDecotor = [[CheatGameDecotor alloc] init]; 40 | [cheatGameDecotor cheat]; 41 | } 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/GameDecortor.h: -------------------------------------------------------------------------------- 1 | // 2 | // GameDecortor.h 3 | // GameComponetDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "GameLoL.h" 11 | 12 | @interface GameDecortor : NSObject 13 | // 上下左右 14 | - (void)up; 15 | - (void)down; 16 | - (void)left; 17 | - (void)right; 18 | 19 | // 选择与开始的操作 20 | - (void)select; 21 | - (void)start; 22 | 23 | // 按钮 24 | - (void)commandA; 25 | - (void)commandB; 26 | - (void)commandC; 27 | - (void)commandD; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/GameDecortor.m: -------------------------------------------------------------------------------- 1 | // 2 | // GameDecortor.m 3 | // GameComponetDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "GameDecortor.h" 10 | 11 | @interface GameDecortor () 12 | @property (nonatomic, strong) GameLoL *gameLoL; 13 | @end 14 | 15 | @implementation GameDecortor 16 | 17 | - (instancetype)init { 18 | self = [super init]; 19 | if (self) { 20 | self.gameLoL = [[GameLoL alloc] init]; 21 | } 22 | return self; 23 | } 24 | 25 | // GameLoL里面所有的方法 26 | - (void)up { 27 | [self.gameLoL up]; 28 | } 29 | 30 | - (void)down { 31 | [self.gameLoL down]; 32 | } 33 | 34 | - (void)left { 35 | [self.gameLoL left]; 36 | } 37 | 38 | - (void)right { 39 | [self.gameLoL right]; 40 | } 41 | 42 | - (void)select { 43 | [self.gameLoL select]; 44 | } 45 | 46 | - (void)start { 47 | [self.gameLoL start]; 48 | } 49 | 50 | - (void)commandA { 51 | [self.gameLoL commandA]; 52 | } 53 | 54 | - (void)commandB { 55 | [self.gameLoL commandB]; 56 | } 57 | 58 | - (void)commandC { 59 | [self.gameLoL commandC]; 60 | } 61 | 62 | - (void)commandY { 63 | [self.gameLoL commandD]; 64 | } 65 | @end 66 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/GameLoL+Cheat.h: -------------------------------------------------------------------------------- 1 | // 2 | // GameLoL+Cheat.h 3 | // GameComponetDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "GameLoL.h" 10 | 11 | @interface GameLoL (Cheat) 12 | // 作弊器 13 | - (void)cheat; 14 | 15 | //- (void)up; 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/GameLoL+Cheat.m: -------------------------------------------------------------------------------- 1 | // 2 | // GameLoL+Cheat.m 3 | // GameComponetDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "GameLoL+Cheat.h" 10 | 11 | @implementation GameLoL (Cheat) 12 | 13 | - (void)cheat { 14 | // 作弊操作 15 | [self up]; 16 | [self up]; 17 | 18 | [self down]; 19 | [self down]; 20 | 21 | [self commandA]; 22 | [self commandA]; 23 | } 24 | 25 | //- (void)up { 26 | // NSLog(@"LOLOLOLOL123123"); 27 | //} 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/GameLoL+Coin.h: -------------------------------------------------------------------------------- 1 | // 2 | // GameLoL+Coin.h 3 | // GameComponetDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "GameLoL.h" 10 | 11 | @interface GameLoL (Coin) 12 | // 游戏币 13 | @property (nonatomic, assign) NSInteger coin; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/GameLoL+Coin.m: -------------------------------------------------------------------------------- 1 | // 2 | // GameLoL+Coin.m 3 | // GameComponetDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "GameLoL+Coin.h" 10 | #import 11 | 12 | static const NSString *_coinStr = @"_coinStr"; 13 | @implementation GameLoL (Coin) 14 | 15 | - (void)setCoin:(NSInteger)coin { 16 | objc_setAssociatedObject(self, (__bridge const void *)_coinStr, @(coin), OBJC_ASSOCIATION_ASSIGN); 17 | 18 | } 19 | 20 | - (NSInteger)coin { 21 | NSNumber *number = objc_getAssociatedObject(self, (__bridge const void *)_coinStr); 22 | 23 | return number.integerValue; 24 | } 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/GameLoL.h: -------------------------------------------------------------------------------- 1 | // 2 | // GameLoL.h 3 | // GameComponetDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface GameLoL : NSObject 12 | 13 | // 上下左右 14 | - (void)up; 15 | - (void)down; 16 | - (void)left; 17 | - (void)right; 18 | 19 | // 选择与开始的操作 20 | - (void)select; 21 | - (void)start; 22 | 23 | // 按钮 24 | - (void)commandA; 25 | - (void)commandB; 26 | - (void)commandC; 27 | - (void)commandD; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/装饰器模式/GameLoL.m: -------------------------------------------------------------------------------- 1 | // 2 | // GameLoL.m 3 | // GameComponetDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "GameLoL.h" 10 | 11 | @implementation GameLoL 12 | 13 | - (void)up { 14 | NSLog(@"up"); 15 | } 16 | 17 | - (void)down { 18 | NSLog(@"down"); 19 | } 20 | 21 | - (void)left { 22 | NSLog(@"left"); 23 | } 24 | 25 | - (void)right { 26 | NSLog(@"right"); 27 | } 28 | 29 | - (void)select { 30 | NSLog(@"select"); 31 | } 32 | 33 | - (void)start { 34 | NSLog(@"start"); 35 | } 36 | 37 | - (void)commandA { 38 | NSLog(@"commandA"); 39 | } 40 | 41 | - (void)commandB { 42 | NSLog(@"commandB"); 43 | } 44 | 45 | - (void)commandC { 46 | NSLog(@"commandC"); 47 | } 48 | 49 | - (void)commandD { 50 | NSLog(@"commandD"); 51 | } 52 | 53 | @end 54 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/AdapterViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // AdapterViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AdapterViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/AdapterViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // AdapterViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | /* 10 | 适配器模式: 1.目标协议 2.适配者 3.适配器 11 | 1.类适配器: 适配器是继承咋们适配者的 12 | 2.对象适配器: 适配器是引用咱们的适配者的 13 | 14 | 桥接模式跟适配器的区别: 15 | 适配器: 改变已有的两个接口, 让他们相兼容. (组件之间的适配, 音视频方面, SDK跟自己写的代码之间适配) 16 | 桥接模式:分离抽象化和实现类. 两者的接口可以不同,主要是在于分离 17 | 18 | 桥接模式先有桥, 才有两端的实现. 19 | 适配器模式是先有两边的, 才有适配器 20 | */ 21 | 22 | 23 | #import "AdapterViewController.h" 24 | 25 | @interface AdapterViewController () 26 | 27 | @property (nonatomic, strong) NSArray *array; 28 | @property (nonatomic, strong) UITableView *tbView; 29 | @property (nonatomic, strong) NSDictionary *modeVCDic; 30 | 31 | @end 32 | 33 | @implementation AdapterViewController 34 | 35 | - (void)viewDidLoad { 36 | [super viewDidLoad]; 37 | 38 | self.array = @[@"案例1",@"案例2"]; 39 | [self.view addSubview:self.tbView]; 40 | 41 | // Do any additional setup after loading the view, typically from a nib. 42 | } 43 | 44 | #pragma mark - delegate & dataSource; 45 | 46 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 47 | 48 | return self.array.count; 49 | } 50 | 51 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 52 | 53 | 54 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 55 | if (cell == nil) { 56 | cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 57 | cell.selectionStyle = UITableViewCellSelectionStyleNone; 58 | } 59 | 60 | cell.textLabel.text = [self.array objectAtIndex:indexPath.row]; 61 | 62 | return cell; 63 | } 64 | 65 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 66 | 67 | return 50; 68 | 69 | } 70 | 71 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 72 | 73 | NSString *type = [self.array objectAtIndex:indexPath.row]; 74 | NSString *vcName = [self.modeVCDic objectForKey:type]; 75 | UIViewController *subViewController = [[NSClassFromString(vcName) alloc] init]; 76 | subViewController.title = type; 77 | subViewController.view.backgroundColor = [UIColor whiteColor]; 78 | [self.navigationController pushViewController:subViewController animated:YES]; 79 | 80 | } 81 | 82 | #pragma mark - getters and setters; 83 | 84 | - (UITableView *)tbView{ 85 | 86 | if (_tbView == nil) { 87 | _tbView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];; 88 | _tbView.delegate = self; 89 | _tbView.dataSource = self; 90 | _tbView.tableFooterView = [[UIView alloc] init]; 91 | } 92 | return _tbView; 93 | } 94 | 95 | 96 | - (NSDictionary *)modeVCDic{ 97 | 98 | if (_modeVCDic == nil) { 99 | 100 | _modeVCDic = @{ 101 | @"案例1":@"AdapterDemo1ViewController", 102 | @"案例2":@"AdapterDemo2ViewController" 103 | }; 104 | } 105 | return _modeVCDic; 106 | } 107 | 108 | @end 109 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例1/AdapterDemo1ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // AdapterDemo1ViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AdapterDemo1ViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例1/AdapterDemo1ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // AdapterDemo1ViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "AdapterDemo1ViewController.h" 10 | #import "AdapterCNY.h" 11 | #import "AdapterManager.h" 12 | #import "AdapterUSD.h" 13 | 14 | @interface AdapterDemo1ViewController () 15 | 16 | @end 17 | 18 | @implementation AdapterDemo1ViewController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | // Do any additional setup after loading the view. 23 | 24 | /* 25 | 把100美元转换成人民币. 26 | 27 | 类适配器:通过继承来适配两个接口 28 | 对象适配器:不继承被适配者,他们是一个关联关系,相当于引用了这个类 29 | */ 30 | 31 | //类适配器 32 | AdapterCNY *adapterCNY = [[AdapterCNY alloc] init]; 33 | float cny = [adapterCNY getCNY]; 34 | NSLog(@"cny is %f",cny); 35 | 36 | 37 | //对象适配器 38 | AdapterManager *manager = [[AdapterManager alloc] initWithAdapter:[AdapterUSD new]]; 39 | float cny2 = [manager getCNY]; 40 | NSLog(@"cny2 is %f",cny2); 41 | } 42 | 43 | 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例1/AdapterUSD.h: -------------------------------------------------------------------------------- 1 | // 2 | // AdapterUSD.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AdapterUSD : NSObject 12 | 13 | - (float)getUSD; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例1/AdapterUSD.m: -------------------------------------------------------------------------------- 1 | // 2 | // AdapterUSD.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "AdapterUSD.h" 10 | 11 | @implementation AdapterUSD 12 | 13 | - (float)getUSD { 14 | 15 | return 100.00f; 16 | } 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例1/MoneyAdapterProtocol.h: -------------------------------------------------------------------------------- 1 | // 2 | // MoneyAdapterProtocol.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @protocol MoneyAdapterProtocol 12 | 13 | - (float) getCNY; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例1/对象适配器/AdapterManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // AdapterManager.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "MoneyAdapterProtocol.h" 11 | #import "AdapterUSD.h" 12 | 13 | @interface AdapterManager : NSObject 14 | 15 | - (instancetype)initWithAdapter:(AdapterUSD *)adapter; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例1/对象适配器/AdapterManager.m: -------------------------------------------------------------------------------- 1 | // 2 | // AdapterManager.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "AdapterManager.h" 10 | 11 | @interface AdapterManager () 12 | 13 | @property (nonatomic,strong) AdapterUSD *adapterUSD; 14 | 15 | @end 16 | 17 | 18 | @implementation AdapterManager 19 | 20 | - (instancetype)initWithAdapter:(AdapterUSD *)adapter { 21 | self = [super init]; 22 | if (self) { 23 | _adapterUSD = adapter; 24 | } 25 | return self; 26 | } 27 | 28 | 29 | - (float)getCNY { 30 | 31 | return [self.adapterUSD getUSD] * 6.6; 32 | } 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例1/类适配器/AdapterCNY.h: -------------------------------------------------------------------------------- 1 | // 2 | // AdapterCNY.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "AdapterUSD.h" 10 | #import "MoneyAdapterProtocol.h" 11 | 12 | @interface AdapterCNY : AdapterUSD 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例1/类适配器/AdapterCNY.m: -------------------------------------------------------------------------------- 1 | // 2 | // AdapterCNY.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "AdapterCNY.h" 10 | 11 | @implementation AdapterCNY 12 | 13 | - (float)getCNY { 14 | 15 | 16 | return [self getUSD] * 6.6f; 17 | } 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/AdapterDemo2ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // AdapterDemo2ViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AdapterDemo2ViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/AdapterDemo2ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // AdapterDemo2ViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "AdapterDemo2ViewController.h" 10 | #import "ColorView.h" 11 | #import "ModelOne.h" 12 | #import "ModelTwo.h" 13 | #import "ModelOneAdapter.h" 14 | #import "ModelTwoAdapter.h" 15 | 16 | @interface AdapterDemo2ViewController () 17 | 18 | @end 19 | 20 | @implementation AdapterDemo2ViewController 21 | 22 | - (void)viewDidLoad { 23 | [super viewDidLoad]; 24 | // Do any additional setup after loading the view. 25 | 26 | ColorView *colorView = [[ColorView alloc] init]; 27 | 28 | ModelOne *model = [[ModelOne alloc] init]; 29 | model.userName = @"张三"; 30 | model.age = @"18"; 31 | 32 | 33 | ModelTwo *modle2 = [[ModelTwo alloc] init]; 34 | modle2.firstName = @"四"; 35 | modle2.lastName = @"李"; 36 | modle2.age = 23; 37 | 38 | ModelOneAdapter *oneAdapter = [[ModelOneAdapter alloc] initWithModel:model]; 39 | [colorView loadModel:oneAdapter]; 40 | 41 | ModelTwoAdapter *twoAdapter = [[ModelTwoAdapter alloc] initWithModel:modle2]; 42 | [colorView loadModel:twoAdapter]; 43 | 44 | } 45 | 46 | 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/ColorView.h: -------------------------------------------------------------------------------- 1 | // 2 | // ColorView.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "UserInfoProtocol.h" 11 | 12 | @interface ColorView : NSObject 13 | 14 | - (void)loadModel:(id)model; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/ColorView.m: -------------------------------------------------------------------------------- 1 | // 2 | // ColorView.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "ColorView.h" 10 | 11 | @implementation ColorView 12 | 13 | - (void)loadModel:(id )model { 14 | 15 | NSLog(@"UI展示用户姓名:%@,展示用户年龄:%@", [model getName], model.getAge); 16 | } 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/ModelOne.h: -------------------------------------------------------------------------------- 1 | // 2 | // ModelOne.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ModelOne : NSObject 12 | 13 | @property (nonatomic,strong) NSString *userName; 14 | 15 | @property (nonatomic,strong) NSString *age; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/ModelOne.m: -------------------------------------------------------------------------------- 1 | // 2 | // ModelOne.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "ModelOne.h" 10 | 11 | @implementation ModelOne 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/ModelOneAdapter.h: -------------------------------------------------------------------------------- 1 | // 2 | // ModelOneAdapter.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "UserInfoProtocol.h" 11 | #import "ModelOne.h" 12 | 13 | @interface ModelOneAdapter : NSObject 14 | 15 | - (instancetype)initWithModel:(ModelOne *)model; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/ModelOneAdapter.m: -------------------------------------------------------------------------------- 1 | // 2 | // ModelOneAdapter.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "ModelOneAdapter.h" 10 | 11 | @interface ModelOneAdapter () 12 | 13 | @property (nonatomic, strong) ModelOne *model; /**< 输入的模型数据 */ 14 | 15 | @end 16 | 17 | @implementation ModelOneAdapter 18 | 19 | - (instancetype)initWithModel:(ModelOne *)model { 20 | 21 | self = [super init]; 22 | if (self) { 23 | self.model = model; 24 | } 25 | return self; 26 | } 27 | 28 | #pragma mark - UserInfoProtocol 29 | 30 | - (NSString *)getName { 31 | 32 | return self.model.userName; 33 | } 34 | 35 | - (NSString *)getAge { 36 | 37 | return self.model.age; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/ModelTwo.h: -------------------------------------------------------------------------------- 1 | // 2 | // ModelTwo.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ModelTwo : NSObject 12 | 13 | @property (nonatomic,strong) NSString *firstName; 14 | 15 | @property (nonatomic,strong) NSString *lastName; 16 | 17 | @property (nonatomic,assign) NSInteger age; 18 | 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/ModelTwo.m: -------------------------------------------------------------------------------- 1 | // 2 | // ModelTwo.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "ModelTwo.h" 10 | 11 | @implementation ModelTwo 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/ModelTwoAdapter.h: -------------------------------------------------------------------------------- 1 | // 2 | // ModelTwoAdapter.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "UserInfoProtocol.h" 11 | #import "ModelTwo.h" 12 | 13 | @interface ModelTwoAdapter : NSObject 14 | 15 | - (instancetype)initWithModel:(ModelTwo *)model; 16 | 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/ModelTwoAdapter.m: -------------------------------------------------------------------------------- 1 | // 2 | // ModelTwoAdapter.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "ModelTwoAdapter.h" 10 | 11 | @interface ModelTwoAdapter () 12 | 13 | @property (nonatomic, strong) ModelTwo *model; /**< 输入的模型数据 */ 14 | 15 | @end 16 | 17 | @implementation ModelTwoAdapter 18 | 19 | - (instancetype)initWithModel:(ModelTwo *)model { 20 | 21 | self = [super init]; 22 | if (self) { 23 | self.model = model; 24 | } 25 | return self; 26 | } 27 | 28 | 29 | #pragma mark - UserInfoProtocol 30 | 31 | - (NSString *)getName { 32 | 33 | return [NSString stringWithFormat:@"%@ %@",self.model.lastName,self.model.firstName]; 34 | } 35 | 36 | - (NSString *)getAge { 37 | 38 | return [NSString stringWithFormat:@"%ld",(long)self.model.age]; 39 | } 40 | 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/结构型模式/适配器模式/案例2/UserInfoProtocol.h: -------------------------------------------------------------------------------- 1 | // 2 | // UserInfoProtocol.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @protocol UserInfoProtocol 12 | 13 | @required 14 | 15 | - (NSString *)getName; 16 | 17 | - (NSString *)getAge; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/中介者模式/Colleague.h: -------------------------------------------------------------------------------- 1 | // 2 | // Colleague.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | @class Colleague; 11 | 12 | @protocol ColleagueDelegate 13 | 14 | - (void)colleagueChoose:(Colleague *)event; 15 | 16 | @end 17 | 18 | 19 | @interface Colleague : NSObject 20 | 21 | @property (nonatomic,weak) id delegate; 22 | 23 | @end 24 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/中介者模式/Colleague.m: -------------------------------------------------------------------------------- 1 | // 2 | // Colleague.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Colleague.h" 10 | 11 | @implementation Colleague 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/中介者模式/ConcreteColleague.h: -------------------------------------------------------------------------------- 1 | // 2 | // ConcreteColleague.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Colleague.h" 10 | 11 | @interface ConcreteColleague : Colleague 12 | 13 | @property (nonatomic,assign) NSInteger roomSize; 14 | 15 | - (void)chooseRoomSizeChanged:(NSInteger)roomSize; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/中介者模式/ConcreteColleague.m: -------------------------------------------------------------------------------- 1 | // 2 | // ConcreteColleague.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "ConcreteColleague.h" 10 | 11 | @implementation ConcreteColleague 12 | 13 | - (void)chooseRoomSizeChanged:(NSInteger)roomSize { 14 | 15 | self.roomSize = roomSize; 16 | if (self.delegate && [self.delegate respondsToSelector:@selector(colleagueChoose:)]) { 17 | [self.delegate colleagueChoose:self]; 18 | } 19 | 20 | } 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/中介者模式/ConcreteMediator.h: -------------------------------------------------------------------------------- 1 | // 2 | // ConcreteMediator.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Mediator.h" 10 | #import "ConcreteColleague.h" 11 | 12 | @interface ConcreteMediator : Mediator 13 | 14 | @property (nonatomic, strong) ConcreteColleague *colleagueA; 15 | @property (nonatomic, strong) ConcreteColleague *colleagueB; 16 | 17 | // 查看信息 18 | - (NSDictionary *)values; 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/中介者模式/ConcreteMediator.m: -------------------------------------------------------------------------------- 1 | // 2 | // ConcreteMediator.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "ConcreteMediator.h" 10 | 11 | @implementation ConcreteMediator 12 | 13 | // 主要用来查看打印信息. 14 | - (NSDictionary *)values { 15 | return @{@"A":@(self.colleagueA.roomSize), 16 | @"B":@(self.colleagueB.roomSize), 17 | }; 18 | } 19 | 20 | #pragma mark -ColleagueDelegate 21 | 22 | - (void)colleagueChoose:(Colleague *)event { 23 | 24 | if ([event isEqual:self.colleagueA]) { 25 | 26 | self.colleagueA.roomSize = self.colleagueA.roomSize * 0.9f; 27 | 28 | }else{ 29 | 30 | self.colleagueB.roomSize = self.colleagueB.roomSize * 0.85; 31 | } 32 | } 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/中介者模式/Mediator.h: -------------------------------------------------------------------------------- 1 | // 2 | // Mediator.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Colleague.h" 11 | 12 | @interface Mediator : NSObject 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/中介者模式/Mediator.m: -------------------------------------------------------------------------------- 1 | // 2 | // Mediator.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Mediator.h" 10 | 11 | @implementation Mediator 12 | 13 | - (void)colleagueChoose:(Colleague *)event { 14 | 15 | } 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/中介者模式/MediatorPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // MediatorPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MediatorPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/中介者模式/MediatorPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // MediatorPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/17. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "MediatorPatternViewController.h" 10 | #import "ConcreteMediator.h" 11 | #import "ConcreteColleague.h" 12 | 13 | @interface MediatorPatternViewController () 14 | 15 | @end 16 | 17 | @implementation MediatorPatternViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | // Do any additional setup after loading the view. 22 | 23 | //中介报价 24 | ConcreteMediator *mediator = [ConcreteMediator new]; 25 | 26 | //卖房者A 27 | ConcreteColleague *colleagueA = [ConcreteColleague new]; 28 | mediator.colleagueA = colleagueA; 29 | colleagueA.delegate = mediator; 30 | [colleagueA chooseRoomSizeChanged:80]; 31 | 32 | 33 | //卖房者B 34 | ConcreteColleague *colleagueB = [ConcreteColleague new]; 35 | mediator.colleagueB = colleagueB; 36 | colleagueB.delegate = mediator; 37 | [colleagueB chooseRoomSizeChanged:120]; 38 | 39 | NSLog(@"所有卖房者去公摊后的实际面积:%@", [mediator values]); 40 | 41 | } 42 | 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/命令模式/CommandPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // CommandPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface CommandPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/命令模式/CommandPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // CommandPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "CommandPatternViewController.h" 10 | #import "Receiver.h" 11 | #import "Invoker.h" 12 | #import "DarkerCommand.h" 13 | #import "LighterCommand.h" 14 | 15 | 16 | @interface CommandPatternViewController () 17 | 18 | @property (nonatomic, strong) UIButton *lightBtn; 19 | @property (nonatomic, strong) UIButton *darkBtn; 20 | @property (nonatomic, strong) UIButton *backBtn; 21 | @property (nonatomic, strong) Receiver *receiver; 22 | 23 | @end 24 | 25 | @implementation CommandPatternViewController 26 | 27 | - (void)viewDidLoad { 28 | [super viewDidLoad]; 29 | // Do any additional setup after loading the view. 30 | 31 | self.view.backgroundColor = [UIColor whiteColor]; 32 | [self.view addSubview:self.lightBtn]; 33 | [self.view addSubview:self.darkBtn]; 34 | [self.view addSubview:self.backBtn]; 35 | 36 | // 接收器 37 | self.receiver = [[Receiver alloc] init]; 38 | [self.receiver setReceiverView:self.view]; 39 | } 40 | 41 | #pragma mark - private 42 | 43 | - (void)lightClick:(id)sender{ 44 | 45 | LighterCommand *command = [[LighterCommand alloc] initWithReceiver:self.receiver paramter:0.1]; 46 | [[Invoker sharedInstance] addAndExcute:command]; 47 | } 48 | 49 | - (void)darkClick:(id)sender{ 50 | 51 | DarkerCommand *command = [[DarkerCommand alloc] initWithReceiver:self.receiver paramter:0.1]; 52 | [[Invoker sharedInstance] addAndExcute:command]; 53 | } 54 | 55 | - (void)backClick:(id)sender{ 56 | 57 | [[Invoker sharedInstance] goBack]; 58 | 59 | } 60 | 61 | #pragma mark - setter & getter 62 | 63 | - (UIButton *)lightBtn { 64 | 65 | if (_lightBtn == nil) { 66 | _lightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 67 | _lightBtn.backgroundColor = [UIColor blackColor]; 68 | _lightBtn.titleLabel.textColor = [UIColor whiteColor]; 69 | _lightBtn.frame = CGRectMake(10, 200, 80, 50); 70 | [_lightBtn setTitle:@"变亮" forState:UIControlStateNormal]; 71 | [_lightBtn addTarget:self action:@selector(lightClick:) forControlEvents:UIControlEventTouchUpInside]; 72 | } 73 | return _lightBtn; 74 | } 75 | 76 | - (UIButton *)darkBtn { 77 | 78 | if (_darkBtn == nil) { 79 | _darkBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 80 | _darkBtn.backgroundColor = [UIColor blackColor]; 81 | _darkBtn.titleLabel.textColor = [UIColor whiteColor]; 82 | _darkBtn.frame = CGRectMake(120, 200, 80, 50); 83 | [_darkBtn setTitle:@"变暗" forState:UIControlStateNormal]; 84 | [_darkBtn addTarget:self action:@selector(darkClick:) forControlEvents:UIControlEventTouchUpInside]; 85 | } 86 | return _darkBtn; 87 | } 88 | 89 | - (UIButton *)backBtn { 90 | 91 | if (_backBtn == nil) { 92 | _backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 93 | _backBtn.backgroundColor = [UIColor blackColor]; 94 | _backBtn.titleLabel.textColor = [UIColor whiteColor]; 95 | _backBtn.frame = CGRectMake(250, 200, 80, 50); 96 | [_backBtn setTitle:@"撤销" forState:UIControlStateNormal]; 97 | [_backBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside]; 98 | } 99 | return _backBtn; 100 | } 101 | @end 102 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/命令模式/CommandProtocol.h: -------------------------------------------------------------------------------- 1 | // 2 | // CommandProtocol.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @protocol CommandProtocol 12 | 13 | // 执行命令 14 | - (void)excute; 15 | 16 | // 撤销命令 17 | - (void)backExcute; 18 | 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/命令模式/DarkerCommand.h: -------------------------------------------------------------------------------- 1 | // 2 | // DarkerCommand.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "CommandProtocol.h" 11 | #import "Receiver.h" 12 | 13 | @interface DarkerCommand : NSObject 14 | 15 | // 绑定接收器 16 | - (instancetype)initWithReceiver:(Receiver *)receiver paramter:(CGFloat)paramter; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/命令模式/DarkerCommand.m: -------------------------------------------------------------------------------- 1 | // 2 | // DarkerCommand.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "DarkerCommand.h" 10 | 11 | @interface DarkerCommand () 12 | @property (nonatomic, strong) Receiver *receiver; 13 | @property (nonatomic, assign) CGFloat paramter; 14 | 15 | @end 16 | 17 | @implementation DarkerCommand 18 | 19 | - (instancetype)initWithReceiver:(Receiver *)receiver paramter:(CGFloat)paramter { 20 | self = [super init]; 21 | if (self) { 22 | 23 | self.receiver = receiver; 24 | self.paramter = paramter; 25 | } 26 | return self; 27 | } 28 | 29 | // 执行命令 30 | - (void)excute { 31 | [self.receiver mackeDarker:self.paramter]; 32 | } 33 | 34 | // 撤销命令 35 | - (void)backExcute { 36 | [self.receiver mackeLighter:self.paramter]; 37 | } 38 | 39 | @end 40 | 41 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/命令模式/Invoker.h: -------------------------------------------------------------------------------- 1 | // 2 | // Invoker.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "CommandProtocol.h" 11 | 12 | @interface Invoker : NSObject 13 | 14 | + (instancetype)sharedInstance; 15 | 16 | // 回退指令 17 | - (void)goBack; 18 | 19 | // 添加操作指令 20 | - (void)addAndExcute:(id )command; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/命令模式/Invoker.m: -------------------------------------------------------------------------------- 1 | // 2 | // Invoker.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Invoker.h" 10 | 11 | @interface Invoker () 12 | @property (nonatomic, strong) NSMutableArray *array; // 存储操作的 13 | @end 14 | 15 | @implementation Invoker 16 | 17 | + (instancetype)sharedInstance 18 | { 19 | static Invoker *sharedInstance = nil; 20 | static dispatch_once_t onceToken; 21 | dispatch_once(&onceToken, ^{ 22 | 23 | sharedInstance = [[self alloc] init]; 24 | sharedInstance.array = [NSMutableArray array]; 25 | 26 | }); 27 | return sharedInstance; 28 | } 29 | 30 | // 回退指令 31 | - (void)goBack { 32 | // 1.获取数组中的最后一个操作 33 | id command = self.array.lastObject; 34 | 35 | // 2.操作调用,撤销的步骤 36 | [command backExcute]; 37 | 38 | // 3.删除最后操作 39 | [self.array removeLastObject]; 40 | } 41 | 42 | // 添加操作指令 43 | - (void)addAndExcute:(id )command { 44 | // 1.把操作添加到数组 45 | [self.array addObject:command]; 46 | 47 | // 2.让操作调用实现的协议方法 48 | [command excute]; 49 | } 50 | @end 51 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/命令模式/LighterCommand.h: -------------------------------------------------------------------------------- 1 | // 2 | // LighterCommand.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "CommandProtocol.h" 11 | #import "Receiver.h" 12 | 13 | @interface LighterCommand : NSObject 14 | 15 | // 绑定接收器 16 | - (instancetype)initWithReceiver:(Receiver *)receiver paramter:(CGFloat)paramter; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/命令模式/LighterCommand.m: -------------------------------------------------------------------------------- 1 | // 2 | // LighterCommand.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "LighterCommand.h" 10 | 11 | @interface LighterCommand () 12 | @property (nonatomic, strong) Receiver *receiver; 13 | @property (nonatomic, assign) CGFloat paramter; 14 | 15 | @end 16 | 17 | @implementation LighterCommand 18 | 19 | - (instancetype)initWithReceiver:(Receiver *)receiver paramter:(CGFloat)paramter { 20 | 21 | self = [super init]; 22 | if (self) { 23 | 24 | self.receiver = receiver; 25 | self.paramter = paramter; 26 | } 27 | return self; 28 | } 29 | 30 | // 执行命令 31 | - (void)excute { 32 | [self.receiver mackeLighter:self.paramter]; 33 | } 34 | 35 | // 撤销命令 36 | - (void)backExcute { 37 | [self.receiver mackeDarker:self.paramter]; 38 | } 39 | 40 | @end 41 | 42 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/命令模式/Receiver.h: -------------------------------------------------------------------------------- 1 | // 2 | // Receiver.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface Receiver : NSObject{ 13 | CGFloat _white; // 白的 14 | CGFloat _alpha; // 透明度 15 | } 16 | 17 | // 接收者的view 18 | @property (nonatomic, strong) UIView *receiverView; 19 | 20 | // 变暗的接口 21 | - (void)mackeDarker:(CGFloat)pamameter; 22 | 23 | // 变亮的接口 24 | - (void)mackeLighter:(CGFloat)pamameter; 25 | 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/命令模式/Receiver.m: -------------------------------------------------------------------------------- 1 | // 2 | // Receiver.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Receiver.h" 10 | 11 | @implementation Receiver 12 | 13 | - (void)setReceiverView:(UIView *)receiverView { 14 | // 获取数值 15 | _receiverView = receiverView; 16 | UIColor *color = _receiverView.backgroundColor; 17 | 18 | [color getWhite:&_white alpha:&_alpha]; 19 | } 20 | 21 | // 变暗的接口 22 | - (void)mackeDarker:(CGFloat)pamameter { 23 | _white -= pamameter; 24 | // 设置取值范围 25 | _white = MAX(0, _white); 26 | 27 | // 设置背景色 28 | _receiverView.backgroundColor = [UIColor colorWithWhite:_white alpha:_alpha]; 29 | } 30 | 31 | // 变亮的接口 32 | - (void)mackeLighter:(CGFloat)pamameter { 33 | _white += pamameter; 34 | // 设置取值范围 35 | _white = MIN(1, _white); 36 | 37 | // 设置背景色 38 | _receiverView.backgroundColor = [UIColor colorWithWhite:_white alpha:_alpha]; 39 | } 40 | 41 | @end 42 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/备忘录模式/Apple.h: -------------------------------------------------------------------------------- 1 | // 2 | // Apple.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "MementoProtocol.h" 11 | 12 | @interface Apple : NSObject 13 | 14 | @property (nonatomic, copy) NSString *name; 15 | @property (nonatomic, strong) NSNumber *age; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/备忘录模式/Apple.m: -------------------------------------------------------------------------------- 1 | // 2 | // Apple.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Apple.h" 10 | 11 | @implementation Apple 12 | 13 | - (id)currentState { 14 | 15 | return @{ @"name":self.name, 16 | @"age" : self.age 17 | }; 18 | } 19 | 20 | - (void)recoverFromState:(id)state { 21 | NSDictionary *data = state; 22 | self.name = data[@"name"]; 23 | self.age = data[@"age"]; 24 | } 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/备忘录模式/FastCoder/FastCoder.h: -------------------------------------------------------------------------------- 1 | // 2 | // FastCoding.h 3 | // 4 | // Version 3.2.1 5 | // 6 | // Created by Nick Lockwood on 09/12/2013. 7 | // Copyright (c) 2013 Charcoal Design 8 | // 9 | // Distributed under the permissive zlib License 10 | // Get the latest version from here: 11 | // 12 | // https://github.com/nicklockwood/FastCoding 13 | // 14 | // This software is provided 'as-is', without any express or implied 15 | // warranty. In no event will the authors be held liable for any damages 16 | // arising from the use of this software. 17 | // 18 | // Permission is granted to anyone to use this software for any purpose, 19 | // including commercial applications, and to alter it and redistribute it 20 | // freely, subject to the following restrictions: 21 | // 22 | // 1. The origin of this software must not be misrepresented; you must not 23 | // claim that you wrote the original software. If you use this software 24 | // in a product, an acknowledgment in the product documentation would be 25 | // appreciated but is not required. 26 | // 27 | // 2. Altered source versions must be plainly marked as such, and must not be 28 | // misrepresented as being the original software. 29 | // 30 | // 3. This notice may not be removed or altered from any source distribution. 31 | // 32 | 33 | 34 | #import 35 | 36 | 37 | extern NSString *const FastCodingException; 38 | 39 | 40 | @interface NSObject (FastCoding) 41 | 42 | + (NSArray *)fastCodingKeys; 43 | - (id)awakeAfterFastCoding; 44 | - (Class)classForFastCoding; 45 | - (BOOL)preferFastCoding; 46 | 47 | @end 48 | 49 | 50 | @interface FastCoder : NSObject 51 | 52 | + (id)objectWithData:(NSData *)data; 53 | + (id)propertyListWithData:(NSData *)data; 54 | + (NSData *)dataWithRootObject:(id)object; 55 | 56 | @end -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/备忘录模式/FastCoder/NSObject+MementoCenter.h: -------------------------------------------------------------------------------- 1 | // 2 | // MementoCenter.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSObject (MementoCenter) 12 | 13 | // 存储状态 14 | - (void)saveStateWithKey:(NSString *)key; 15 | 16 | // 恢复状态 17 | - (void)recoverFromStateWithKey:(NSString *)key; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/备忘录模式/FastCoder/NSObject+MementoCenter.m: -------------------------------------------------------------------------------- 1 | // 2 | // MementoCenter.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "NSObject+MementoCenter.h" 10 | #import "MementoCenter.h" 11 | 12 | @implementation NSObject (MementoCenter) 13 | 14 | - (void)saveStateWithKey:(NSString *)key { 15 | id obj = (id )self; 16 | 17 | if ([obj respondsToSelector:@selector(currentState)]) { 18 | [MementoCenter saveMementoObject:obj withKey:key]; 19 | } 20 | } 21 | 22 | - (void)recoverFromStateWithKey:(NSString *)key { 23 | id state = [MementoCenter mementoObjectWithKey:key]; 24 | 25 | id obj = (id )self; 26 | 27 | if ([obj respondsToSelector:@selector(recoverFromStateWithKey:)]) { 28 | [obj recoverFromState:state]; 29 | } 30 | } 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/备忘录模式/MementoCenter.h: -------------------------------------------------------------------------------- 1 | // 2 | // MementorCenter.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "MementoProtocol.h" 11 | 12 | @interface MementoCenter : NSObject 13 | 14 | // 存对象的状态 15 | + (void)saveMementoObject:(id )object withKey:(NSString *)key; 16 | 17 | // 取出对象 18 | + (id)mementoObjectWithKey:(NSString *)key; 19 | 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/备忘录模式/MementoCenter.m: -------------------------------------------------------------------------------- 1 | // 2 | // MementoCenter.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "MementoCenter.h" 10 | #import "FastCoder.h" 11 | 12 | 13 | @implementation MementoCenter 14 | 15 | // 存对象的状态 16 | + (void)saveMementoObject:(id )object withKey:(NSString *)key { 17 | 18 | id data = [object currentState]; 19 | // 转化data为NSData 20 | NSData *tmpData = [FastCoder dataWithRootObject:data]; 21 | 22 | if (tmpData) { 23 | [[NSUserDefaults standardUserDefaults] setObject:tmpData forKey:key]; 24 | } 25 | } 26 | 27 | // 取出对象 28 | + (id)mementoObjectWithKey:(NSString *)key { 29 | id data = nil; 30 | 31 | NSData *tmpData = [[NSUserDefaults standardUserDefaults] objectForKey:key]; 32 | 33 | if (tmpData) { 34 | // 解码 35 | data = [FastCoder objectWithData:tmpData]; 36 | } 37 | 38 | return data; 39 | } 40 | 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/备忘录模式/MementoPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // MementoPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MementoPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/备忘录模式/MementoPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // MementoPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "MementoPatternViewController.h" 10 | #import "MementoCenter.h" 11 | #import "Apple.h" 12 | #import "NSObject+MementoCenter.h" 13 | 14 | @interface MementoPatternViewController () 15 | 16 | @end 17 | 18 | @implementation MementoPatternViewController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | // Do any additional setup after loading the view. 23 | 24 | 25 | Apple *apple = [[Apple alloc] init]; 26 | apple.name = @"富士苹果"; 27 | apple.age = @(10); 28 | 29 | 30 | //方法一: 31 | 32 | // 存 33 | [MementoCenter saveMementoObject:apple withKey:@"Apple"]; 34 | 35 | // 取 36 | [apple recoverFromState:[MementoCenter mementoObjectWithKey:@"Apple"]]; 37 | 38 | NSLog(@"name: %@ age:%@", apple.name, apple.age); 39 | 40 | //方法二:(优化) 41 | 42 | [apple saveStateWithKey:@"Apples"]; 43 | [apple recoverFromStateWithKey:@"Apples"]; 44 | 45 | NSLog(@"name: %@ age:%@", apple.name, apple.age); 46 | 47 | } 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/备忘录模式/MementoProtocol.h: -------------------------------------------------------------------------------- 1 | // 2 | // MementoProtocol.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/23. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @protocol MementoProtocol 12 | 13 | - (id)currentState; /**< 获取状态 */ 14 | 15 | - (void)recoverFromState:(id)state; /**< 恢复状态 */ 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/模版方法模式/Basketball.h: -------------------------------------------------------------------------------- 1 | // 2 | // Basketball.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Game.h" 10 | 11 | @interface Basketball : Game 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/模版方法模式/Basketball.m: -------------------------------------------------------------------------------- 1 | // 2 | // Basketball.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Basketball.h" 10 | 11 | @implementation Basketball 12 | 13 | // 初始化游戏 14 | - (void)initGame { 15 | NSLog(@"init Baskeball"); 16 | } 17 | 18 | // 暂停操作 19 | - (void)pause { 20 | NSLog(@"pause Baskeball"); 21 | 22 | } 23 | 24 | // 游戏存储 25 | - (void)gameSave { 26 | NSLog(@"gameSave Baskeball"); 27 | 28 | } 29 | 30 | // 退出游戏 31 | - (void)exitGame { 32 | NSLog(@"exitGame Baskeball"); 33 | 34 | } 35 | 36 | // 开始游戏 37 | - (void)startPlay { 38 | NSLog(@"startPlay Baskeball"); 39 | 40 | } 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/模版方法模式/Football.h: -------------------------------------------------------------------------------- 1 | // 2 | // Football.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Game.h" 10 | 11 | @interface Football : Game 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/模版方法模式/Football.m: -------------------------------------------------------------------------------- 1 | // 2 | // Football.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Football.h" 10 | 11 | @implementation Football 12 | 13 | // 初始化游戏 14 | - (void)initGame { 15 | NSLog(@"init football"); 16 | } 17 | 18 | // 暂停操作 19 | - (void)pause { 20 | NSLog(@"pause football"); 21 | 22 | } 23 | 24 | // 游戏存储 25 | - (void)gameSave { 26 | NSLog(@"gameSave football"); 27 | 28 | } 29 | 30 | // 退出游戏 31 | - (void)exitGame { 32 | NSLog(@"exitGame football"); 33 | 34 | } 35 | 36 | // 开始游戏 37 | - (void)startPlay { 38 | NSLog(@"startPlay football"); 39 | 40 | } 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/模版方法模式/Game.h: -------------------------------------------------------------------------------- 1 | // 2 | // Game.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface Game : NSObject 12 | 13 | // 初始化游戏 14 | - (void)initGame; 15 | 16 | // 暂停操作 17 | - (void)pause; 18 | 19 | // 游戏存储 20 | - (void)gameSave; 21 | 22 | // 退出游戏 23 | - (void)exitGame; 24 | 25 | // 开始游戏 26 | - (void)startPlay; 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/模版方法模式/Game.m: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // Game.m 4 | // DesignModeDemo 5 | // 6 | // Created by koala on 2018/4/20. 7 | // Copyright © 2018年 koala. All rights reserved. 8 | // 9 | 10 | #import "Game.h" 11 | 12 | @implementation Game 13 | 14 | // 初始化游戏 15 | - (void)initGame { 16 | } 17 | 18 | // 暂停操作 19 | - (void)pause { 20 | 21 | } 22 | 23 | // 游戏存储 24 | - (void)gameSave { 25 | 26 | } 27 | 28 | // 退出游戏 29 | - (void)exitGame { 30 | 31 | } 32 | 33 | // 开始游戏 34 | - (void)startPlay { 35 | 36 | } 37 | 38 | @end 39 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/模版方法模式/TemplatePatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // TemplatePatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TemplatePatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/模版方法模式/TemplatePatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // TemplatePatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "TemplatePatternViewController.h" 10 | #import "Basketball.h" 11 | #import "Football.h" 12 | 13 | @interface TemplatePatternViewController () 14 | 15 | @end 16 | 17 | @implementation TemplatePatternViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | // Do any additional setup after loading the view. 22 | 23 | // 足球游戏 24 | Game *footballGame = [Football new]; 25 | [footballGame initGame]; 26 | [footballGame startPlay]; 27 | [footballGame gameSave]; 28 | [footballGame pause]; 29 | [footballGame exitGame]; 30 | 31 | NSLog(@"#####################\n"); 32 | 33 | 34 | // 篮球游戏的创建 35 | Game *baskeballGame = [Basketball new]; 36 | [baskeballGame initGame]; 37 | [baskeballGame startPlay]; 38 | [baskeballGame gameSave]; 39 | [baskeballGame pause]; 40 | [baskeballGame exitGame]; 41 | 42 | 43 | } 44 | 45 | 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/状态模式/BuyState.h: -------------------------------------------------------------------------------- 1 | // 2 | // BuyState.h 3 | // 行为型设计模式-状态模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | #import 9 | #import "CellingState.h" 10 | 11 | @interface BuyState : NSObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/状态模式/BuyState.m: -------------------------------------------------------------------------------- 1 | // 2 | // BuyState.m 3 | // 行为型设计模式-状态模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "BuyState.h" 10 | 11 | @implementation BuyState 12 | -(void)choose{ 13 | NSLog(@"不能选择"); 14 | } 15 | -(void)buy{ 16 | NSLog(@"投币"); 17 | } 18 | -(void)shipment{ 19 | NSLog(@"不能出货"); 20 | } 21 | @end 22 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/状态模式/Celling.h: -------------------------------------------------------------------------------- 1 | // 2 | // Celling.h 3 | // 行为型设计模式-状态模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "CellingState.h" 11 | @interface Celling : NSObject 12 | @property (nonatomic,strong) id state; 13 | 14 | -(void)choose; 15 | -(void)buy; 16 | -(void)shipment; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/状态模式/Celling.m: -------------------------------------------------------------------------------- 1 | // 2 | // Celling.m 3 | // 行为型设计模式-状态模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "Celling.h" 10 | #import "ShipmentState.h" 11 | #import "BuyState.h" 12 | #import "ChoosingState.h" 13 | @implementation Celling 14 | - (instancetype)init 15 | { 16 | self = [super init]; 17 | if (self) { 18 | self.state = [ShipmentState new]; 19 | } 20 | return self; 21 | } 22 | -(void)choose{ 23 | [self.state choose]; 24 | } 25 | -(void)buy{ 26 | [self.state buy]; 27 | } 28 | -(void)shipment{ 29 | [self.state shipment]; 30 | } 31 | @end 32 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/状态模式/CellingState.h: -------------------------------------------------------------------------------- 1 | // 2 | // CellingState.h 3 | // 行为型设计模式-状态模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @protocol CellingState 12 | -(void)choose; 13 | -(void)buy; 14 | -(void)shipment; 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/状态模式/ChoosingState.h: -------------------------------------------------------------------------------- 1 | // 2 | // ChoosingState.h 3 | // 行为型设计模式-状态模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | #import 9 | #import "CellingState.h" 10 | @interface ChoosingState :NSObject 11 | 12 | @end 13 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/状态模式/ChoosingState.m: -------------------------------------------------------------------------------- 1 | // 2 | // ChoosingState.m 3 | // 行为型设计模式-状态模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "ChoosingState.h" 10 | 11 | @implementation ChoosingState 12 | -(void)choose{ 13 | NSLog(@"选择物品"); 14 | } 15 | -(void)buy{ 16 | NSLog(@"不能买"); 17 | } 18 | -(void)shipment{ 19 | NSLog(@"不能出货"); 20 | } 21 | @end 22 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/状态模式/ShipmentState.h: -------------------------------------------------------------------------------- 1 | // 2 | // ShipmentState.h 3 | // 行为型设计模式-状态模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "CellingState.h" 10 | #import 11 | 12 | @interface ShipmentState : NSObject 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/状态模式/ShipmentState.m: -------------------------------------------------------------------------------- 1 | // 2 | // ShipmentState.m 3 | // 行为型设计模式-状态模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "ShipmentState.h" 10 | 11 | @implementation ShipmentState 12 | -(void)choose{ 13 | NSLog(@"不能选择物品"); 14 | } 15 | -(void)buy{ 16 | NSLog(@"不能买"); 17 | } 18 | -(void)shipment{ 19 | NSLog(@"出货"); 20 | } 21 | @end 22 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/状态模式/StatePatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // StatePatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface StatePatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/状态模式/StatePatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // StatePatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "StatePatternViewController.h" 10 | #import "Celling.h" 11 | #import "BuyState.h" 12 | #import "ShipmentState.h" 13 | #import "ChoosingState.h" 14 | 15 | @interface StatePatternViewController () 16 | 17 | @end 18 | 19 | @implementation StatePatternViewController 20 | 21 | - (void)viewDidLoad { 22 | [super viewDidLoad]; 23 | // Do any additional setup after loading the view. 24 | 25 | Celling * celling = [Celling new]; 26 | [celling choose]; 27 | celling.state = [ChoosingState new]; 28 | [celling choose]; 29 | celling.state = [BuyState new]; 30 | [celling buy]; 31 | celling.state = [ShipmentState new]; 32 | [celling shipment]; 33 | } 34 | 35 | - (void)didReceiveMemoryWarning { 36 | [super didReceiveMemoryWarning]; 37 | // Dispose of any resources that can be recreated. 38 | } 39 | 40 | /* 41 | #pragma mark - Navigation 42 | 43 | // In a storyboard-based application, you will often want to do a little preparation before navigation 44 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 45 | // Get the new view controller using [segue destinationViewController]. 46 | // Pass the selected object to the new view controller. 47 | } 48 | */ 49 | 50 | @end 51 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/策略模式/Context.h: -------------------------------------------------------------------------------- 1 | // 2 | // Context.h 3 | // 行为型设计模式-策略模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Strategy.h" 11 | @interface Context : NSObject 12 | @property (nonatomic,strong) id strategy; 13 | -(void)calulate; 14 | @end 15 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/策略模式/Context.m: -------------------------------------------------------------------------------- 1 | // 2 | // Context.m 3 | // 行为型设计模式-策略模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "Context.h" 10 | 11 | @implementation Context 12 | -(void)calulate{ 13 | NSLog(@" 第一个数10 第二个数5 计算结果 %d", [self.strategy doOperation:10 and:5]); 14 | 15 | } 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/策略模式/OperationAdd.h: -------------------------------------------------------------------------------- 1 | // 2 | // OperationAdd.h 3 | // 行为型设计模式-策略模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Strategy.h" 11 | @interface OperationAdd : NSObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/策略模式/OperationAdd.m: -------------------------------------------------------------------------------- 1 | // 2 | // OperationAdd.m 3 | // 行为型设计模式-策略模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "OperationAdd.h" 10 | 11 | @implementation OperationAdd 12 | -(int)doOperation:(int)num1 and:(int)num2{ 13 | NSLog(@"当前算法加法"); 14 | return num2 + num1; 15 | } 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/策略模式/OperationMultiply.h: -------------------------------------------------------------------------------- 1 | // 2 | // OperationMultiply.h 3 | // 行为型设计模式-策略模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Strategy.h" 11 | @interface OperationMultiply : NSObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/策略模式/OperationMultiply.m: -------------------------------------------------------------------------------- 1 | // 2 | // OperationMultiply.m 3 | // 行为型设计模式-策略模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "OperationMultiply.h" 10 | 11 | @implementation OperationMultiply 12 | -(int)doOperation:(int)num1 and:(int)num2{ 13 | NSLog(@"当前算法乘法"); 14 | return num1 * num2; 15 | } 16 | @end 17 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/策略模式/OperationSubStract.h: -------------------------------------------------------------------------------- 1 | // 2 | // OperationSubStract.h 3 | // 行为型设计模式-策略模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Strategy.h" 11 | @interface OperationSubStract : NSObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/策略模式/OperationSubStract.m: -------------------------------------------------------------------------------- 1 | // 2 | // OperationSubStract.m 3 | // 行为型设计模式-策略模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "OperationSubStract.h" 10 | 11 | @implementation OperationSubStract 12 | -(int)doOperation:(int)num1 and:(int)num2 13 | { 14 | NSLog(@"当前算法减法"); 15 | return num1 - num2; 16 | } 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/策略模式/Strategy.h: -------------------------------------------------------------------------------- 1 | // 2 | // Strategy.h 3 | // 行为型设计模式-策略模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @protocol Strategy 12 | -(int)doOperation:(int)num1 and:(int)num2; 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/策略模式/StrategyPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // StrategyPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface StrategyPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/策略模式/StrategyPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // StrategyPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "StrategyPatternViewController.h" 10 | #import "Context.h" 11 | #import "OperationAdd.h" 12 | #import "OperationMultiply.h" 13 | #import "OperationSubStract.h" 14 | 15 | @interface StrategyPatternViewController () 16 | 17 | @end 18 | 19 | @implementation StrategyPatternViewController 20 | 21 | - (void)viewDidLoad { 22 | [super viewDidLoad]; 23 | // Do any additional setup after loading the view. 24 | 25 | Context * context = [[Context alloc]init]; 26 | context.strategy = [OperationAdd new]; 27 | [context calulate]; 28 | context.strategy = [OperationMultiply new]; 29 | [context calulate]; 30 | context.strategy = [OperationSubStract new]; 31 | [context calulate]; 32 | } 33 | 34 | 35 | @end 36 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/观察者模式/ObserverPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ObserverPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ObserverPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/观察者模式/ObserverPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ObserverPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "ObserverPatternViewController.h" 10 | #import "SubscibeCenter.h" 11 | 12 | @interface ObserverPatternViewController () 13 | 14 | @end 15 | 16 | @implementation ObserverPatternViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | // Do any additional setup after loading the view. 21 | 22 | 23 | [SubscibeCenter creatNumber:@"订阅号-美食"]; 24 | 25 | [SubscibeCenter addUser:self withNumber:@"订阅号-美食"]; 26 | 27 | //收不到这个消息 28 | [SubscibeCenter sendMessage:@"有新书啦" withNumber:@"订阅号-书记"]; 29 | 30 | //可以收到这条消息 31 | [SubscibeCenter sendMessage:@"簋街牛蛙贼好吃" withNumber:@"订阅号-美食"]; 32 | 33 | } 34 | 35 | #pragma mark - SubscibeProtocol 36 | 37 | - (void)sendMessage:(NSString *)message withSubscibeNum:(NSString *)subscibeNum { 38 | NSLog(@"message is : %@ , number is : %@",message,subscibeNum); 39 | } 40 | 41 | @end 42 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/观察者模式/SubscibeCenter.h: -------------------------------------------------------------------------------- 1 | // 2 | // SubscibeCenter.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "SubscibeProtocol.h" 11 | 12 | @interface SubscibeCenter : NSObject 13 | 14 | + (void)creatNumber:(NSString *)number; 15 | 16 | + (void)removeNumber:(NSString *)number; 17 | 18 | + (void)addUser:(id )user withNumber:(NSString *)number; 19 | 20 | + (void)removeUser:(id )user withNumber:(NSString *)number; 21 | 22 | + (void)sendMessage:(NSString *)message withNumber:(NSString *)number; 23 | 24 | @end 25 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/观察者模式/SubscibeCenter.m: -------------------------------------------------------------------------------- 1 | // 2 | // SubscibeCenter.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "SubscibeCenter.h" 10 | 11 | static NSMutableDictionary *center = nil; 12 | 13 | @implementation SubscibeCenter 14 | 15 | + (void)initialize { 16 | if (self == [SubscibeCenter class]) { 17 | center = [NSMutableDictionary dictionary]; 18 | } 19 | } 20 | 21 | + (void)creatNumber:(NSString *)number { 22 | 23 | NSHashTable *hashTable = [self exitNumber:number]; 24 | if (hashTable == nil) { 25 | hashTable = [NSHashTable weakObjectsHashTable]; 26 | [center setObject:hashTable forKey:number]; 27 | } 28 | 29 | } 30 | 31 | + (void)removeNumber:(NSString *)number { 32 | NSHashTable *hashTable = [self exitNumber:number]; 33 | if (hashTable) { 34 | [center removeObjectForKey:number]; 35 | } 36 | } 37 | 38 | + (void)addUser:(id )user withNumber:(NSString *)number { 39 | NSHashTable *hashTable = [self exitNumber:number]; 40 | [hashTable addObject:user]; 41 | } 42 | 43 | + (void)removeUser:(id )user withNumber:(NSString *)number { 44 | NSHashTable *hashTable = [self exitNumber:number]; 45 | [hashTable removeObject:user]; 46 | } 47 | 48 | + (void)sendMessage:(NSString *)message withNumber:(NSString *)number { 49 | NSHashTable *hashTable = [self exitNumber:number]; 50 | if (hashTable) { 51 | NSEnumerator *enumerato = [hashTable objectEnumerator]; 52 | id object = nil; 53 | 54 | while (object = [enumerato nextObject]) { 55 | 56 | if ([object respondsToSelector:@selector(sendMessage:withSubscibeNum:)]) { 57 | [object sendMessage:message withSubscibeNum:number]; 58 | } 59 | 60 | } 61 | 62 | } 63 | } 64 | 65 | + (NSHashTable *)exitNumber:(NSString *)number { 66 | 67 | return [center objectForKey:number]; 68 | } 69 | 70 | 71 | @end 72 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/观察者模式/SubscibeProtocol.h: -------------------------------------------------------------------------------- 1 | // 2 | // SubscibeProtocol.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/16. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @protocol SubscibeProtocol 12 | 13 | @required 14 | 15 | - (void)sendMessage:(NSString *)message withSubscibeNum:(NSString *)subscibeNum; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/InterpreterPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // InterpreterPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface InterpreterPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/InterpreterPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // InterpreterPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "InterpreterPatternViewController.h" 10 | #import "Calculator.h" 11 | 12 | @interface InterpreterPatternViewController () 13 | 14 | @end 15 | 16 | @implementation InterpreterPatternViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | // Do any additional setup after loading the view. 21 | 22 | Calculator * calculator = [Calculator new]; 23 | [calculator calculator:@"125+175+100-100"]; 24 | int result= [calculator calculator]; 25 | NSLog(@"result %d",result); 26 | } 27 | 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/四则运算/AdditionExpression.h: -------------------------------------------------------------------------------- 1 | // 2 | // AdditionExpression.h 3 | // 行为型设计模式-解释器模式 4 | // 5 | // Created by 温杰 on 2018/4/10. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "OperatorExpression.h" 10 | 11 | @interface AdditionExpression : OperatorExpression 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/四则运算/AdditionExpression.m: -------------------------------------------------------------------------------- 1 | // 2 | // AdditionExpression.m 3 | // 行为型设计模式-解释器模式 4 | // 5 | // Created by 温杰 on 2018/4/10. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "AdditionExpression.h" 10 | 11 | @implementation AdditionExpression 12 | -(int)interptet{ 13 | return [_mArithmeticExpression1 interptet]+[_mArithmeticExpression2 interptet]; 14 | } 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/四则运算/ArithmeticExpression.h: -------------------------------------------------------------------------------- 1 | // 2 | // ArithmeticExpression.h 3 | // 行为型设计模式-解释器模式 4 | // 5 | // Created by 温杰 on 2018/4/10. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @protocol ArithmeticExpression 12 | -(int)interptet; 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/四则运算/Calculator.h: -------------------------------------------------------------------------------- 1 | // 2 | // Calculator.h 3 | // 行为型设计模式-解释器模式 4 | // 5 | // Created by 温杰 on 2018/4/10. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "ArithmeticExpression.h" 11 | #import "NumExpression.h" 12 | #import "AdditionExpression.h" 13 | #import "ReduceExpression.h" 14 | @interface Calculator : NSObject 15 | -(void)calculator:(NSString *)expression; 16 | -(int)calculator; 17 | @end 18 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/四则运算/Calculator.m: -------------------------------------------------------------------------------- 1 | // 2 | // Calculator.m 3 | // 行为型设计模式-解释器模式 4 | // 5 | // Created by 温杰 on 2018/4/10. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "Calculator.h" 10 | @interface Calculator() 11 | @property (nonatomic,strong) NSMutableArray *mArithmeticExpressionStack; 12 | @end 13 | 14 | @implementation Calculator 15 | 16 | - (instancetype)init 17 | { 18 | self = [super init]; 19 | if (self) { 20 | self.mArithmeticExpressionStack = [NSMutableArray new]; 21 | } 22 | return self; 23 | } 24 | 25 | -(void)calculator:(NSString *)expression{ 26 | id one; 27 | id two; 28 | 29 | while (1) { 30 | if ([expression hasPrefix:@"+"]) { 31 | one = [self.mArithmeticExpressionStack lastObject]; 32 | [self.mArithmeticExpressionStack removeLastObject]; 33 | expression = [expression substringFromIndex:1]; 34 | int b =[expression intValue]; 35 | expression = [self getExpression:expression]; 36 | two = [[NumExpression alloc]initWithNum:b]; 37 | AdditionExpression * add= [AdditionExpression new]; 38 | [ add OperatorExpression:one :two]; 39 | [self.mArithmeticExpressionStack addObject:add]; 40 | if (expression.length==0) { 41 | break; 42 | } 43 | }else if ([expression hasPrefix:@"-"]){ 44 | one = [self.mArithmeticExpressionStack lastObject]; 45 | [self.mArithmeticExpressionStack removeLastObject]; 46 | expression = [expression substringFromIndex:1]; 47 | int b =[expression intValue]; 48 | expression = [self getExpression:expression]; 49 | two = [[NumExpression alloc]initWithNum:b]; 50 | ReduceExpression * add= [ReduceExpression new]; 51 | [ add OperatorExpression:one :two]; 52 | [self.mArithmeticExpressionStack addObject:add]; 53 | if (expression.length==0) { 54 | break; 55 | } 56 | } 57 | else{ 58 | int a=[expression intValue]; 59 | NumExpression * num = [[NumExpression alloc]initWithNum:a]; 60 | [self.mArithmeticExpressionStack addObject:num]; 61 | expression = [self getExpression:expression]; 62 | if (expression.length==0) { 63 | break; 64 | } 65 | } 66 | } 67 | } 68 | 69 | 70 | -(NSString *)getExpression:(NSString *)expression{ 71 | int a=[expression intValue]; 72 | NSString * str =[NSString stringWithFormat:@"%d",a]; 73 | return [expression substringFromIndex:str.length]; 74 | } 75 | 76 | -(int)calculator{ 77 | id object= [self.mArithmeticExpressionStack lastObject]; 78 | [self.mArithmeticExpressionStack removeLastObject]; 79 | return [object interptet]; 80 | } 81 | 82 | 83 | 84 | @end 85 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/四则运算/NumExpression.h: -------------------------------------------------------------------------------- 1 | // 2 | // NumExpression.h 3 | // 行为型设计模式-解释器模式 4 | // 5 | // Created by 温杰 on 2018/4/10. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "ArithmeticExpression.h" 11 | @interface NumExpression : NSObject 12 | - (instancetype)initWithNum:(int)num; 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/四则运算/NumExpression.m: -------------------------------------------------------------------------------- 1 | // 2 | // NumExpression.m 3 | // 行为型设计模式-解释器模式 4 | // 5 | // Created by 温杰 on 2018/4/10. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "NumExpression.h" 10 | @interface NumExpression() 11 | @property (nonatomic,assign) int num; 12 | @end 13 | @implementation NumExpression 14 | - (instancetype)initWithNum:(int)num 15 | { 16 | self = [super init]; 17 | if (self) { 18 | self.num = num; 19 | } 20 | return self; 21 | } 22 | -(int)interptet{ 23 | return self.num; 24 | } 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/四则运算/OperatorExpression.h: -------------------------------------------------------------------------------- 1 | // 2 | // OperatorExpression.h 3 | // 行为型设计模式-解释器模式 4 | // 5 | // Created by 温杰 on 2018/4/10. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "ArithmeticExpression.h" 11 | @interface OperatorExpression : NSObject 12 | { 13 | @protected 14 | id _mArithmeticExpression1; 15 | id _mArithmeticExpression2; 16 | } 17 | 18 | -(void)OperatorExpression:(id)mArithmeticExpression1 :(id)mArithmeticExpression2; 19 | @end 20 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/四则运算/OperatorExpression.m: -------------------------------------------------------------------------------- 1 | // 2 | // OperatorExpression.m 3 | // 行为型设计模式-解释器模式 4 | // 5 | // Created by 温杰 on 2018/4/10. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "OperatorExpression.h" 10 | 11 | @interface OperatorExpression() 12 | 13 | @end 14 | 15 | @implementation OperatorExpression 16 | -(void)OperatorExpression:(id)mArithmeticExpression1 :(id)mArithmeticExpression2{ 17 | _mArithmeticExpression1 = mArithmeticExpression1; 18 | _mArithmeticExpression2 = mArithmeticExpression2; 19 | } 20 | 21 | 22 | 23 | @end 24 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/四则运算/ReduceExpression.h: -------------------------------------------------------------------------------- 1 | // 2 | // ReduceExpression.h 3 | // 行为型设计模式-解释器模式 4 | // 5 | // Created by 温杰 on 2018/4/10. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "OperatorExpression.h" 10 | 11 | @interface ReduceExpression : OperatorExpression 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/解释器模式/四则运算/ReduceExpression.m: -------------------------------------------------------------------------------- 1 | // 2 | // ReduceExpression.m 3 | // 行为型设计模式-解释器模式 4 | // 5 | // Created by 温杰 on 2018/4/10. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "ReduceExpression.h" 10 | 11 | @implementation ReduceExpression 12 | -(int)interptet{ 13 | return [_mArithmeticExpression1 interptet]-[_mArithmeticExpression2 interptet]; 14 | } 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/AccountBook.h: -------------------------------------------------------------------------------- 1 | // 2 | // AccountBook.h 3 | // 行为型设计模式-访问者模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Bill.h" 11 | #import "AccountBookViewer.h" 12 | @interface AccountBook : NSObject 13 | -(void)addBill:(id)bill; 14 | -(void)show:(id)view; 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/AccountBook.m: -------------------------------------------------------------------------------- 1 | // 2 | // AccountBook.m 3 | // 行为型设计模式-访问者模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "AccountBook.h" 10 | @interface AccountBook() 11 | @property (nonatomic,strong) NSMutableArray *billList; 12 | @end 13 | @implementation AccountBook 14 | 15 | - (instancetype)init 16 | { 17 | self = [super init]; 18 | if (self) { 19 | self.billList = [NSMutableArray new]; 20 | } 21 | return self; 22 | } 23 | -(void)addBill:(id)bill{ 24 | [self.billList addObject:bill]; 25 | } 26 | -(void)show:(id)view{ 27 | for (idbill in self.billList) { 28 | [bill accept:view]; 29 | } 30 | } 31 | 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/AccountBookViewer.h: -------------------------------------------------------------------------------- 1 | // 2 | // AccountBookViewer.h 3 | // 行为型设计模式-访问者模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @class IncomeBill,ConsumeBill; 12 | @protocol AccountBookViewer 13 | -(void)lookIncomBill:(IncomeBill*)incomeBill; 14 | -(void)lookConsumeBill:(ConsumeBill *)consumeBill; 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/Bill.h: -------------------------------------------------------------------------------- 1 | // 2 | // Bill.h 3 | // 行为型设计模式-访问者模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AccountBookViewer.h" 11 | @protocol Bill 12 | 13 | -(void)accept:(id)viewer; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/Boss.h: -------------------------------------------------------------------------------- 1 | // 2 | // Boss.h 3 | // 行为型设计模式-访问者模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AccountBookViewer.h" 11 | @interface Boss : NSObject 12 | @property (nonatomic,assign) double totalIncome; 13 | @property (nonatomic,assign) double totalConsume; 14 | @end 15 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/Boss.m: -------------------------------------------------------------------------------- 1 | // 2 | // Boss.m 3 | // 行为型设计模式-访问者模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "Boss.h" 10 | #import "IncomeBill.h" 11 | #import "ConsumeBill.h" 12 | 13 | @implementation Boss 14 | -(void)lookIncomBill:(IncomeBill *)incomeBill{ 15 | self.totalIncome += incomeBill.amount; 16 | } 17 | 18 | -(void)lookConsumeBill:(ConsumeBill *)consumeBill{ 19 | self.totalConsume = consumeBill.amount; 20 | 21 | } 22 | 23 | @end 24 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/CAP.h: -------------------------------------------------------------------------------- 1 | // 2 | // CAP.h 3 | // 行为型设计模式-访问者模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AccountBookViewer.h" 11 | @interface CAP : NSObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/CAP.m: -------------------------------------------------------------------------------- 1 | // 2 | // CAP.m 3 | // 行为型设计模式-访问者模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "CAP.h" 10 | #import "IncomeBill.h" 11 | #import "ConsumeBill.h" 12 | @implementation CAP 13 | -(void)lookIncomBill:(IncomeBill *)incomeBill{ 14 | NSLog(@"是否交税了"); 15 | } 16 | 17 | -(void)lookConsumeBill:(ConsumeBill *)consumeBill{ 18 | if ([consumeBill.item isEqualToString:@"工资"]) { 19 | NSLog(@"是否交个人所得税"); 20 | } 21 | } 22 | @end 23 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/ConsumeBill.h: -------------------------------------------------------------------------------- 1 | // 2 | // ConsumeBill.h 3 | // 行为型设计模式-访问者模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Bill.h" 11 | @interface ConsumeBill : NSObject 12 | @property (nonatomic,assign) double amount; 13 | @property (nonatomic,strong) NSString *item; 14 | - (instancetype)initWithItem:(NSString *)item Amount:(double)amount; 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/ConsumeBill.m: -------------------------------------------------------------------------------- 1 | // 2 | // ConsumeBill.m 3 | // 行为型设计模式-访问者模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "ConsumeBill.h" 10 | 11 | 12 | @implementation ConsumeBill 13 | 14 | - (instancetype)initWithItem:(NSString *)item Amount:(double)amount 15 | { 16 | self = [super init]; 17 | if (self) { 18 | self.item = item; 19 | self.amount =amount; 20 | } 21 | return self; 22 | } 23 | 24 | -(void)accept:(id)viewer{ 25 | [viewer lookConsumeBill:self]; 26 | } 27 | @end 28 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/IncomeBill.h: -------------------------------------------------------------------------------- 1 | // 2 | // IncomeBill.h 3 | // 行为型设计模式-访问者模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Bill.h" 11 | @interface IncomeBill : NSObject 12 | @property (nonatomic,assign) double amount; 13 | @property (nonatomic,strong) NSString *item; 14 | - (instancetype)initWithItem:(NSString *)item Amount:(double)amount; 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/IncomeBill.m: -------------------------------------------------------------------------------- 1 | // 2 | // IncomeBill.m 3 | // 行为型设计模式-访问者模式 4 | // 5 | // Created by 温杰 on 2018/4/11. 6 | // Copyright © 2018年 温杰. All rights reserved. 7 | // 8 | 9 | #import "IncomeBill.h" 10 | 11 | @implementation IncomeBill 12 | - (instancetype)initWithItem:(NSString *)item Amount:(double)amount 13 | { 14 | self = [super init]; 15 | if (self) { 16 | self.item = item; 17 | self.amount =amount; 18 | } 19 | return self; 20 | } 21 | 22 | -(void)accept:(id)viewer{ 23 | [viewer lookIncomBill:self]; 24 | } 25 | @end 26 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/VisitorPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // VisitorPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface VisitorPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/访问者模式/VisitorPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // VisitorPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/24. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "VisitorPatternViewController.h" 10 | #import "AccountBook.h" 11 | #import "IncomeBill.h" 12 | #import "ConsumeBill.h" 13 | #import "Boss.h" 14 | #import "CAP.h" 15 | 16 | @interface VisitorPatternViewController () 17 | 18 | @end 19 | 20 | @implementation VisitorPatternViewController 21 | 22 | - (void)viewDidLoad { 23 | [super viewDidLoad]; 24 | // Do any additional setup after loading the view. 25 | 26 | AccountBook * accountBook= [AccountBook new]; 27 | IncomeBill * bill = [IncomeBill new]; 28 | bill.item=@"卖广告"; 29 | bill.amount = 10000; 30 | [accountBook addBill:bill]; 31 | bill = [IncomeBill new]; 32 | bill.item=@"卖商品"; 33 | bill.amount = 20000; 34 | [accountBook addBill:bill]; 35 | 36 | ConsumeBill *consume = [ConsumeBill new]; 37 | consume.item = @"工资"; 38 | consume.amount = 1000; 39 | [accountBook addBill:consume]; 40 | 41 | consume = [ConsumeBill new]; 42 | consume.item = @"材料费"; 43 | consume.amount = 2000; 44 | [accountBook addBill:consume]; 45 | 46 | 47 | Boss * boss = [Boss new]; 48 | [accountBook show:boss]; 49 | CAP * cap = [CAP new]; 50 | [accountBook show:cap]; 51 | 52 | NSLog(@"收入:%lf 支出: %lf",boss.totalIncome,boss.totalConsume); 53 | } 54 | 55 | 56 | 57 | @end 58 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/责任链模式/ChainOfResponsibilityPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ChainOfResponsibilityPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ChainOfResponsibilityPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/责任链模式/ChainOfResponsibilityPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ChainOfResponsibilityPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "ChainOfResponsibilityPatternViewController.h" 10 | #import "Order.h" 11 | #import "GroupLeaderHandler.h" 12 | #import "ManagerHandler.h" 13 | #import "ChairmanHandler.h" 14 | 15 | @interface ChainOfResponsibilityPatternViewController () 16 | 17 | @end 18 | 19 | @implementation ChainOfResponsibilityPatternViewController 20 | 21 | - (void)viewDidLoad { 22 | [super viewDidLoad]; 23 | // Do any additional setup after loading the view. 24 | 25 | Handler *handler1 = [[GroupLeaderHandler alloc] init]; 26 | Handler *handler2 = [[ManagerHandler alloc] init]; 27 | Handler *handler3 = [[ChairmanHandler alloc] init]; 28 | 29 | handler1.successor = handler2; 30 | handler2.successor = handler3; 31 | 32 | Order *order = [[Order alloc] init]; 33 | order.orderMoney = 500; 34 | [handler1 handlerOrder:order]; 35 | NSLog(@"###############################\n"); 36 | 37 | order.orderMoney = 3000; 38 | [handler1 handlerOrder:order]; 39 | NSLog(@"###############################\n"); 40 | 41 | 42 | order.orderMoney = 20000; 43 | [handler1 handlerOrder:order]; 44 | NSLog(@"###############################\n"); 45 | 46 | order.orderMoney = 500000; 47 | [handler1 handlerOrder:order]; 48 | 49 | } 50 | 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/责任链模式/ChairmanHandler.h: -------------------------------------------------------------------------------- 1 | // 2 | // ChairmanHandler.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Handler.h" 10 | 11 | @interface ChairmanHandler : Handler 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/责任链模式/ChairmanHandler.m: -------------------------------------------------------------------------------- 1 | // 2 | // ChairmanHandler.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "ChairmanHandler.h" 10 | 11 | @implementation ChairmanHandler 12 | 13 | - (void)handlerOrder:(Order *)order { 14 | 15 | if (order.orderMoney < 100000.0f) { 16 | 17 | NSLog(@"订单小于10万,董事长审批订单通过"); 18 | 19 | }else{ 20 | NSLog(@"订单大于10万,需要开会讨论,等待下"); 21 | } 22 | } 23 | 24 | @end 25 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/责任链模式/GroupLeaderHandler.h: -------------------------------------------------------------------------------- 1 | // 2 | // GroupLeaderHandler.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Handler.h" 10 | 11 | @interface GroupLeaderHandler : Handler 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/责任链模式/GroupLeaderHandler.m: -------------------------------------------------------------------------------- 1 | // 2 | // GroupLeaderHandler.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "GroupLeaderHandler.h" 10 | 11 | @implementation GroupLeaderHandler 12 | 13 | - (void)handlerOrder:(Order *)order { 14 | 15 | if (order.orderMoney < 2000.0f) { 16 | 17 | NSLog(@"订单小于2000,组长审批订单直接通过"); 18 | }else{ 19 | 20 | NSLog(@"订单大于2000,需要经理审批"); 21 | [self.successor handlerOrder:order]; 22 | 23 | } 24 | } 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/责任链模式/Handler.h: -------------------------------------------------------------------------------- 1 | // 2 | // Handler.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Order.h" 11 | 12 | @interface Handler : NSObject 13 | 14 | @property (nonatomic, strong) Handler *successor; 15 | 16 | - (void)handlerOrder:(Order *)order; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/责任链模式/Handler.m: -------------------------------------------------------------------------------- 1 | // 2 | // Handler.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Handler.h" 10 | 11 | @implementation Handler 12 | 13 | - (void)handlerOrder:(Order *)order { 14 | 15 | [self.successor handlerOrder:order]; 16 | } 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/责任链模式/ManagerHandler.h: -------------------------------------------------------------------------------- 1 | // 2 | // ManagerHandler.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Handler.h" 10 | 11 | @interface ManagerHandler : Handler 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/责任链模式/ManagerHandler.m: -------------------------------------------------------------------------------- 1 | // 2 | // ManagerHandler.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "ManagerHandler.h" 10 | 11 | @implementation ManagerHandler 12 | 13 | - (void)handlerOrder:(Order *)order { 14 | 15 | if (order.orderMoney < 10000.0f) { 16 | 17 | NSLog(@"订单小于1万,经理审批订单直接通过"); 18 | }else{ 19 | 20 | NSLog(@"订单大于1万,需要董事长审批"); 21 | [self.successor handlerOrder:order]; 22 | } 23 | } 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/责任链模式/Order.h: -------------------------------------------------------------------------------- 1 | // 2 | // Order.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface Order : NSObject 12 | 13 | @property (nonatomic, assign) float orderMoney; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/责任链模式/Order.m: -------------------------------------------------------------------------------- 1 | // 2 | // Order.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Order.h" 10 | 11 | @implementation Order 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/迭代器模式/IteratorPatternViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // IteratorPatternViewController.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface IteratorPatternViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/迭代器模式/IteratorPatternViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // IteratorPatternViewController.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "IteratorPatternViewController.h" 10 | #import "LinkedList.h" 11 | #import "LinkIterator.h" 12 | 13 | @interface IteratorPatternViewController () 14 | 15 | @end 16 | 17 | @implementation IteratorPatternViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | // Do any additional setup after loading the view. 22 | 23 | LinkedList *linkList = [[LinkedList alloc] init]; 24 | [linkList addNodeData:@"A"]; 25 | [linkList addNodeData:@"B"]; 26 | [linkList addNodeData:@"C"]; 27 | 28 | // 2.迭代器的创建 29 | LinkIterator *linkIterator = [LinkIterator linkedObjectIterator:linkList]; 30 | 31 | // 3.访问每一个元素 32 | Node *node = nil; 33 | while (node = [linkIterator nextObject]) { 34 | NSLog(@"nodeData is --- %@",node.nodeData); 35 | } 36 | 37 | 38 | } 39 | 40 | 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/迭代器模式/LinkIterator.h: -------------------------------------------------------------------------------- 1 | // 2 | // LinkIterator.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "LinkListProtocol.h" 11 | #import "LinkedList.h" 12 | 13 | @interface LinkIterator : NSObject 14 | 15 | /** 16 | 迭代器的实现方法 17 | */ 18 | + (instancetype)linkedObjectIterator:(LinkedList *)linkedList; 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/迭代器模式/LinkIterator.m: -------------------------------------------------------------------------------- 1 | // 2 | // LinkIterator.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "LinkIterator.h" 10 | 11 | @interface LinkIterator () 12 | 13 | @property (nonatomic, strong) LinkedList *linkList; 14 | @property (nonatomic, strong) Node *currentNode; 15 | 16 | @end 17 | 18 | @implementation LinkIterator 19 | 20 | + (instancetype)linkedObjectIterator:(LinkedList *)linkedList { 21 | 22 | LinkIterator *linkIterator = [[LinkIterator alloc] init]; 23 | linkIterator.linkList = linkedList; 24 | linkIterator.currentNode = linkedList.headNode; 25 | return linkIterator; 26 | } 27 | 28 | 29 | #pragma mark - LinkListProtocol 30 | 31 | - (id)nextObject { 32 | self.currentNode = self.currentNode.nextNode; 33 | return self.currentNode; 34 | } 35 | 36 | - (void)resetIterator { 37 | self.currentNode = self.linkList.headNode; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/迭代器模式/LinkListProtocol.h: -------------------------------------------------------------------------------- 1 | // 2 | // LinkListProtocol.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @protocol LinkListProtocol 12 | 13 | /** 14 | 下一个对象 15 | */ 16 | - (id)nextObject; 17 | 18 | 19 | /** 20 | 重置迭代器 21 | */ 22 | - (void)resetIterator; 23 | 24 | @end 25 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/迭代器模式/LinkedList.h: -------------------------------------------------------------------------------- 1 | // 2 | // LinkedList.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Node.h" 11 | 12 | @interface LinkedList : NSObject 13 | 14 | /** 15 | 头结点 16 | */ 17 | @property (nonatomic, strong) Node *headNode; 18 | 19 | /** 20 | 链表中的节点数 21 | */ 22 | @property (nonatomic, assign) NSInteger numberOfNode; 23 | 24 | 25 | 26 | /** 27 | 添加节点数据 28 | */ 29 | - (void)addNodeData:(id)nodeData; 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/迭代器模式/LinkedList.m: -------------------------------------------------------------------------------- 1 | // 2 | // LinkedList.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "LinkedList.h" 10 | 11 | @implementation LinkedList 12 | 13 | - (instancetype)init { 14 | self = [super init]; 15 | if (self) { 16 | //系统指向第一个节点,里面是空的,第二个节点才有数据。 17 | self.headNode = [Node new]; 18 | } 19 | return self; 20 | } 21 | 22 | 23 | - (void)addNodeData:(id)nodeData { 24 | 25 | if (self.headNode == nil) { 26 | self.headNode = [Node nodeData:nodeData]; 27 | }else{ 28 | [self addNodeData:nodeData node:self.headNode]; 29 | } 30 | 31 | self.numberOfNode++; 32 | 33 | } 34 | 35 | - (void)addNodeData:(id)nodeData node:(Node *)node { 36 | 37 | if (node.nextNode == nil) { 38 | node.nextNode = [Node nodeData:nodeData]; 39 | }else{ 40 | [self addNodeData:nodeData node:node.nextNode]; 41 | } 42 | 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/迭代器模式/Node.h: -------------------------------------------------------------------------------- 1 | // 2 | // Node.h 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface Node : NSObject 12 | 13 | /** 14 | 指向下一个节点 15 | */ 16 | @property (nonatomic, strong) Node *nextNode; 17 | 18 | /** 19 | 节点里面的data 20 | */ 21 | @property (nonatomic, strong) id nodeData; 22 | 23 | 24 | + (instancetype)nodeData:(id)nodeData; 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /DesignModeDemo/DesignModeDemo/行为型模式/迭代器模式/Node.m: -------------------------------------------------------------------------------- 1 | // 2 | // Node.m 3 | // DesignModeDemo 4 | // 5 | // Created by koala on 2018/4/20. 6 | // Copyright © 2018年 koala. All rights reserved. 7 | // 8 | 9 | #import "Node.h" 10 | 11 | @implementation Node 12 | 13 | + (instancetype)nodeData:(id)nodeData { 14 | 15 | Node *node = [[self alloc] init]; 16 | node.nodeData = nodeData; 17 | return node; 18 | 19 | } 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /DesignModeDemo/demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WiKi123/DesignPattern/b6300815a52f99c30f1f050b0b3433d3eadeba32/DesignModeDemo/demo.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 WiKi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://github.com/WiKi123/DesignPattern/blob/master/DesignModeDemo/demo.png) 2 | 3 | #设计模式分为三大类: 4 | 5 | 创建型模式,共五种: 6 | 工厂方法模式、抽象工厂模式、单例模式、构建者模式、原型模式。 7 | 8 | 结构型模式,共七种: 9 | 适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。 10 | 11 | 行为型模式,共十一种: 12 | 策略模式、模板方法模式、观察者模式、迭代器模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。 13 | 14 | 其实还有两类:并发型模式和线程池模式。 15 | 16 | 17 | [关于各种设计模式的详细介绍](https://github.com/WiKi123/DesignPattern/blob/master/DesignPatternIntroduce.md) 18 | 19 | --------------------------------------------------------------------------------------------------------------- 20 | 21 | #设计模式的六大原则: 22 | 23 | 总原则-开闭原则 24 | 25 | 对扩展开放,对修改封闭。 26 | 在程序需要进行拓展的时候,不能去修改原有的代码,而是要扩展原有代码,实现一个热插拔的效果。所以一句话概括就是:为了使程序的扩展性好,易于维护和升级。 27 | 想要达到这样的效果,我们需要使用接口和抽象类等。 28 | 29 | 1、单一职责原则 30 | 不要存在多于一个导致类变更的原因,也就是说每个类应该实现单一的职责,否则就应该把类拆分。 31 | 32 | 2、里氏替换原则(Liskov Substitution Principle) 33 | 任何基类可以出现的地方,子类一定可以出现。里氏替换原则是继承复用的基石,只有当衍生类可以替换基类,软件单位的功能不受到影响时,基类才能真正被复用,而衍生类也能够在基类的基础上增加新的行为。 34 | 里氏代换原则是对“开-闭”原则的补充。实现“开闭”原则的关键步骤就是抽象化。而基类与子类的继承关系就是抽象化的具体实现,所以里氏代换原则是对实现抽象化的具体步骤的规范。里氏替换原则中,子类对父类的方法尽量不要重写和重载。因为父类代表了定义好的结构,通过这个规范的接口与外界交互,子类不应该随便破坏它。 35 | 36 | 3、依赖倒转原则(Dependence Inversion Principle) 37 | 面向接口编程,依赖于抽象而不依赖于具体。写代码时用到具体类时,不与具体类交互,而与具体类的上层接口交互。 38 | 39 | 4、接口隔离原则(Interface Segregation Principle) 40 | 每个接口中不存在子类用不到却必须实现的方法,如果不然,就要将接口拆分。使用多个隔离的接口,比使用单个接口(多个接口方法集合到一个的接口)要好。 41 | 42 | 5、迪米特法则(最少知道原则)(Demeter Principle) 43 | 一个类对自己依赖的类知道的越少越好。无论被依赖的类多么复杂,都应该将逻辑封装在方法的内部,通过public方法提供给外部。这样当被依赖的类变化时,才能最小的影响该类。 44 | 最少知道原则的另一个表达方式是:只与直接的朋友通信。类之间只要有耦合关系,就叫朋友关系。耦合分为依赖、关联、聚合、组合等。我们称出现为成员变量、方法参数、方法返回值中的类为直接朋友。局部变量、临时变量则不是直接的朋友。我们要求陌生的类不要作为局部变量出现在类中。 45 | 46 | 6、合成复用原则(Composite Reuse Principle) 47 | 尽量首先使用合成/聚合的方式,而不是使用继承。 48 | 49 | 50 | Thanks 温杰. 51 | Thanks http://www.runoob.com/sitemap. 52 | 53 | --------------------------------------------------------------------------------