├── 9781430233305.jpg
├── LICENSE.txt
├── README.md
├── code
├── 978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code
│ ├── AbstractFactory-Info.plist
│ ├── AbstractFactory.xcodeproj
│ │ ├── Carlo.mode1v3
│ │ ├── Carlo.pbxuser
│ │ └── project.pbxproj
│ ├── AbstractFactoryViewController.xib
│ ├── AbstractFactory_Prefix.pch
│ ├── Classes
│ │ ├── AbstractFactoryAppDelegate.h
│ │ ├── AbstractFactoryAppDelegate.m
│ │ ├── AbstractFactoryViewController.h
│ │ ├── AbstractFactoryViewController.m
│ │ ├── AcmeBrandingFactory.h
│ │ ├── AcmeBrandingFactory.m
│ │ ├── AcmeMainButton.h
│ │ ├── AcmeMainButton.m
│ │ ├── AcmeToolbar.h
│ │ ├── AcmeToolbar.m
│ │ ├── AcmeView.h
│ │ ├── AcmeView.m
│ │ ├── BrandingFactory.h
│ │ ├── BrandingFactory.m
│ │ ├── SierraBrandingFactory.h
│ │ ├── SierraBrandingFactory.m
│ │ ├── SierraMainButton.h
│ │ ├── SierraMainButton.m
│ │ ├── SierraToolbar.h
│ │ ├── SierraToolbar.m
│ │ ├── SierraView.h
│ │ └── SierraView.m
│ ├── MainWindow.xib
│ └── main.m
├── 978-1-4302-3330-5_Chung_Ch06_Builder_code
│ ├── Builder-Info.plist
│ ├── Builder.xcodeproj
│ │ ├── Carlo.mode1v3
│ │ ├── Carlo.pbxuser
│ │ ├── carlochung.mode1v3
│ │ ├── carlochung.pbxuser
│ │ └── project.pbxproj
│ ├── BuilderViewController.xib
│ ├── Builder_Prefix.pch
│ ├── Character.h
│ ├── Character.m
│ ├── CharacterBuilder.h
│ ├── CharacterBuilder.m
│ ├── ChasingGame.h
│ ├── ChasingGame.m
│ ├── Classes
│ │ ├── BuilderAppDelegate.h
│ │ ├── BuilderAppDelegate.m
│ │ ├── BuilderViewController.h
│ │ └── BuilderViewController.m
│ ├── MainWindow.xib
│ ├── StandardCharacterBuilder.h
│ ├── StandardCharacterBuilder.m
│ └── main.m
├── 978-1-4302-3330-5_Chung_Ch07_Singleton_code
│ ├── Classes
│ │ ├── Go4Singleton.cpp
│ │ ├── Go4Singleton.h
│ │ ├── MySingleton.h
│ │ ├── MySingleton.m
│ │ ├── Singleton.h
│ │ ├── Singleton.m
│ │ ├── SingletonAppDelegate.h
│ │ └── SingletonAppDelegate.m
│ ├── MainWindow.xib
│ ├── Singleton-Info.plist
│ ├── Singleton.xcodeproj
│ │ ├── Carlo.mode1v3
│ │ ├── Carlo.pbxuser
│ │ ├── carlochung.mode1v3
│ │ ├── carlochung.pbxuser
│ │ └── project.pbxproj
│ ├── Singleton_Prefix.pch
│ └── main.m
├── 978-1-4302-3330-5_Chung_Ch09_Bridge_code
│ ├── Bridge-Info.plist
│ ├── Bridge.xcodeproj
│ │ ├── Carlo.mode1v3
│ │ ├── Carlo.pbxuser
│ │ ├── carlochung.mode1v3
│ │ ├── carlochung.pbxuser
│ │ └── project.pbxproj
│ ├── BridgeViewController.xib
│ ├── Bridge_Prefix.pch
│ ├── Classes
│ │ ├── BridgeAppDelegate.h
│ │ ├── BridgeAppDelegate.m
│ │ ├── BridgeViewController.h
│ │ └── BridgeViewController.m
│ ├── ConsoleCommands.h
│ ├── ConsoleController.h
│ ├── ConsoleController.m
│ ├── ConsoleEmulator.h
│ ├── ConsoleEmulator.m
│ ├── GameBoyEmulator.h
│ ├── GameBoyEmulator.m
│ ├── GameGearEmulator.h
│ ├── GameGearEmulator.m
│ ├── MainWindow.xib
│ ├── TouchConsoleController.h
│ ├── TouchConsoleController.m
│ └── main.m
├── 978-1-4302-3330-5_Chung_Ch10_Facade_code
│ ├── CabDriver.h
│ ├── CabDriver.m
│ ├── Car.h
│ ├── Car.m
│ ├── Classes
│ │ ├── FacadeAppDelegate.h
│ │ ├── FacadeAppDelegate.m
│ │ ├── FacadeViewController.h
│ │ └── FacadeViewController.m
│ ├── Facade-Info.plist
│ ├── Facade.xcodeproj
│ │ ├── Carlo.mode1v3
│ │ ├── Carlo.pbxuser
│ │ ├── carlochung.mode1v3
│ │ ├── carlochung.pbxuser
│ │ └── project.pbxproj
│ ├── FacadeViewController.xib
│ ├── Facade_Prefix.pch
│ ├── MainWindow.xib
│ ├── Taximeter.h
│ ├── Taximeter.m
│ └── main.m
├── 978-1-4302-3330-5_Chung_Ch16_Decorator_code
│ ├── Classes
│ │ ├── DecoratorAppDelegate.h
│ │ ├── DecoratorAppDelegate.m
│ │ ├── DecoratorView.h
│ │ ├── DecoratorView.m
│ │ ├── DecoratorViewController.h
│ │ └── DecoratorViewController.m
│ ├── Decorator-Info.plist
│ ├── Decorator.xcodeproj
│ │ ├── Carlo.mode1v3
│ │ ├── Carlo.pbxuser
│ │ ├── carlochung.mode1v3
│ │ ├── carlochung.pbxuser
│ │ └── project.pbxproj
│ ├── DecoratorViewController.xib
│ ├── Decorator_Prefix.pch
│ ├── ImageComponent.h
│ ├── ImageFilter.h
│ ├── ImageFilter.m
│ ├── ImageShadowFilter.h
│ ├── ImageShadowFilter.m
│ ├── ImageTransformFilter.h
│ ├── ImageTransformFilter.m
│ ├── Images
│ │ └── Image.png
│ ├── MainWindow.xib
│ ├── UIImage+BaseFilter.h
│ ├── UIImage+BaseFilter.m
│ ├── UIImage+ImageComponent.h
│ ├── UIImage+ImageComponent.m
│ ├── UIImage+Shadow.h
│ ├── UIImage+Shadow.m
│ ├── UIImage+Transform.h
│ ├── UIImage+Transform.m
│ └── main.m
├── 978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code
│ ├── Attack.h
│ ├── Attack.m
│ ├── AttackHandler.h
│ ├── AttackHandler.m
│ ├── Avatar.h
│ ├── Avatar.m
│ ├── ChainOfResponsibility-Info.plist
│ ├── ChainOfResponsibility.xcodeproj
│ │ ├── Carlo.mode1v3
│ │ ├── Carlo.pbxuser
│ │ ├── carlochung.mode1v3
│ │ ├── carlochung.pbxuser
│ │ └── project.pbxproj
│ ├── ChainOfResponsibilityViewController.xib
│ ├── ChainOfResponsibility_Prefix.pch
│ ├── Classes
│ │ ├── ChainOfResponsibilityAppDelegate.h
│ │ ├── ChainOfResponsibilityAppDelegate.m
│ │ ├── ChainOfResponsibilityViewController.h
│ │ └── ChainOfResponsibilityViewController.m
│ ├── CrystalShield.h
│ ├── CrystalShield.m
│ ├── LightningAttack.h
│ ├── LightningAttack.m
│ ├── MagicFireAttack.h
│ ├── MagicFireAttack.m
│ ├── MainWindow.xib
│ ├── MetalArmor.h
│ ├── MetalArmor.m
│ ├── SwordAttack.h
│ ├── SwordAttack.m
│ └── main.m
├── 978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code
│ ├── Classes
│ │ ├── AnySandwich.h
│ │ ├── AnySandwich.m
│ │ ├── Hamburger.h
│ │ ├── Hamburger.m
│ │ ├── Hotdog.h
│ │ ├── Hotdog.m
│ │ ├── ReubenSandwich.h
│ │ ├── ReubenSandwich.m
│ │ ├── TemplateMethodAppDelegate.h
│ │ ├── TemplateMethodAppDelegate.m
│ │ ├── TemplateMethodViewController.h
│ │ └── TemplateMethodViewController.m
│ ├── MainWindow.xib
│ ├── TemplateMethod-Info.plist
│ ├── TemplateMethod.xcodeproj
│ │ ├── Carlo.mode1v3
│ │ ├── Carlo.pbxuser
│ │ ├── carlochung.mode1v3
│ │ ├── carlochung.pbxuser
│ │ └── project.pbxproj
│ ├── TemplateMethodViewController.xib
│ ├── TemplateMethod_Prefix.pch
│ └── main.m
├── 978-1-4302-3330-5_Chung_Ch19_Strategy_code
│ ├── Classes
│ │ ├── AlphaInputValidator.h
│ │ ├── AlphaInputValidator.m
│ │ ├── CustomTextField.h
│ │ ├── CustomTextField.m
│ │ ├── InputValidator.h
│ │ ├── InputValidator.m
│ │ ├── NumericInputValidator.h
│ │ ├── NumericInputValidator.m
│ │ ├── StrategyAppDelegate.h
│ │ ├── StrategyAppDelegate.m
│ │ ├── StrategyViewController.h
│ │ └── StrategyViewController.m
│ ├── MainWindow.xib
│ ├── Strategy-Info.plist
│ ├── Strategy.xcodeproj
│ │ ├── Carlo.mode1v3
│ │ ├── Carlo.pbxuser
│ │ ├── carlochung.mode1v3
│ │ ├── carlochung.pbxuser
│ │ └── project.pbxproj
│ ├── StrategyViewController.xib
│ ├── Strategy_Prefix.pch
│ └── main.m
├── 978-1-4302-3330-5_Chung_Ch21_Flyweight_code
│ ├── Classes
│ │ ├── ExtrinsicFlowerState.h
│ │ ├── FlyweightAppDelegate.h
│ │ ├── FlyweightAppDelegate.m
│ │ ├── FlyweightView.h
│ │ ├── FlyweightView.m
│ │ ├── FlyweightViewController.h
│ │ └── FlyweightViewController.m
│ ├── FlowerFactory.h
│ ├── FlowerFactory.m
│ ├── FlowerView.h
│ ├── FlowerView.m
│ ├── Flyweight-Info.plist
│ ├── Flyweight.xcodeproj
│ │ ├── Carlo.mode1v3
│ │ ├── Carlo.pbxuser
│ │ ├── carlochung.mode1v3
│ │ ├── carlochung.pbxuser
│ │ └── project.pbxproj
│ ├── FlyweightViewController.xib
│ ├── Flyweight_Prefix.pch
│ ├── MainWindow.xib
│ ├── images
│ │ ├── anemone.png
│ │ ├── cosmos.png
│ │ ├── gerberas.png
│ │ ├── hollyhock.png
│ │ ├── jasmine.png
│ │ └── zinnia.png
│ └── main.m
└── 978-1-4302-3330-5_Chung_TouchPainter_code
│ └── TouchPainter
│ ├── BridgeView.xib
│ ├── BridgeViewIpad.xib
│ ├── Classes
│ ├── CanvasView.h
│ ├── CanvasView.m
│ ├── CanvasViewController.h
│ ├── CanvasViewController.m
│ ├── CanvasViewController.xib
│ ├── CanvasViewGenerator.h
│ ├── CanvasViewGenerator.m
│ ├── ClothCanvasView.h
│ ├── ClothCanvasView.m
│ ├── ClothCanvasViewGenerator.h
│ ├── ClothCanvasViewGenerator.m
│ ├── Command.h
│ ├── Command.m
│ ├── CommandBarButton.h
│ ├── CommandBarButton.m
│ ├── CommandSlider.h
│ ├── CommandSlider.m
│ ├── CoordinatingController.h
│ ├── CoordinatingController.m
│ ├── DeleteScribbleCommand.h
│ ├── DeleteScribbleCommand.m
│ ├── Dot.h
│ ├── Dot.m
│ ├── Mark.h
│ ├── MarkEnumerator+Internal.h
│ ├── MarkEnumerator.h
│ ├── MarkEnumerator.m
│ ├── MarkRenderer.h
│ ├── MarkRenderer.m
│ ├── MarkVisitor.h
│ ├── NSMutableArray+Stack.h
│ ├── NSMutableArray+Stack.m
│ ├── OpenScribbleCommand.h
│ ├── OpenScribbleCommand.m
│ ├── PaletteViewController.h
│ ├── PaletteViewController.m
│ ├── PaletteViewController.xib
│ ├── PaperCanvasView.h
│ ├── PaperCanvasView.m
│ ├── PaperCanvasViewGenerator.h
│ ├── PaperCanvasViewGenerator.m
│ ├── PrototypeController.h
│ ├── PrototypeController.m
│ ├── SaveScribbleCommand.h
│ ├── SaveScribbleCommand.m
│ ├── Scribble.h
│ ├── Scribble.m
│ ├── ScribbleManager.h
│ ├── ScribbleManager.m
│ ├── ScribbleMemento+Friend.h
│ ├── ScribbleMemento.h
│ ├── ScribbleMemento.m
│ ├── ScribbleSource.h
│ ├── ScribbleThumbnailCell.h
│ ├── ScribbleThumbnailCell.m
│ ├── ScribbleThumbnailView.h
│ ├── ScribbleThumbnailView.m
│ ├── ScribbleThumbnailViewImageProxy.h
│ ├── ScribbleThumbnailViewImageProxy.m
│ ├── SetStrokeColorCommand.h
│ ├── SetStrokeColorCommand.m
│ ├── SetStrokeSizeCommand.h
│ ├── SetStrokeSizeCommand.m
│ ├── Stroke.h
│ ├── Stroke.m
│ ├── ThumbnailViewController.h
│ ├── ThumbnailViewController.m
│ ├── ThumbnailViewController.xib
│ ├── TouchPainterAppDelegate.h
│ ├── TouchPainterAppDelegate.m
│ ├── UIView+UIImage.h
│ ├── UIView+UIImage.m
│ ├── Vertex.h
│ └── Vertex.m
│ ├── MainWindow.xib
│ ├── Resources
│ └── Images
│ │ ├── background_texture.png
│ │ ├── large_dot.png
│ │ ├── open.png
│ │ ├── palette.png
│ │ ├── placeholder.png
│ │ ├── redo.png
│ │ ├── save.png
│ │ ├── small_dot.png
│ │ └── undo.png
│ ├── TouchPainter-Info.plist
│ ├── TouchPainter.xcodeproj
│ ├── Carlo.mode1v3
│ ├── Carlo.pbxuser
│ ├── carlochung.mode1v3
│ ├── carlochung.pbxuser
│ └── project.pbxproj
│ ├── TouchPainterViewController.xib
│ ├── TouchPainter_Prefix.pch
│ └── main.m
└── contributing.md
/9781430233305.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/9781430233305.jpg
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/LICENSE.txt
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Apress Source Code
2 |
3 | This repository accompanies [*Pro Objective-C Design Patterns for iOS*](http://www.apress.com/9781430233305) by Carlo Chung (Apress, 2011).
4 |
5 | 
6 |
7 | Download the files as a zip using the green button, or clone the repository to your machine using Git.
8 |
9 | ## Releases
10 |
11 | Release v1.0 corresponds to the code in the published book, without corrections or updates.
12 |
13 | ## Contributions
14 |
15 | See the file Contributing.md for more information on how you can contribute to this repository.
16 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/AbstractFactory-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleDisplayName
8 | ${PRODUCT_NAME}
9 | CFBundleExecutable
10 | ${EXECUTABLE_NAME}
11 | CFBundleIconFile
12 |
13 | CFBundleIdentifier
14 | com.yourcompany.${PRODUCT_NAME:rfc1034identifier}
15 | CFBundleInfoDictionaryVersion
16 | 6.0
17 | CFBundleName
18 | ${PRODUCT_NAME}
19 | CFBundlePackageType
20 | APPL
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | 1.0
25 | LSRequiresIPhoneOS
26 |
27 | NSMainNibFile
28 | MainWindow
29 |
30 |
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/AbstractFactory_Prefix.pch:
--------------------------------------------------------------------------------
1 | //
2 | // Prefix header for all source files of the 'AbstractFactory' target in the 'AbstractFactory' project
3 | //
4 |
5 | #ifdef __OBJC__
6 | #import
7 | #import
8 | #endif
9 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/AbstractFactoryAppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AbstractFactoryAppDelegate.h
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class AbstractFactoryViewController;
12 |
13 | @interface AbstractFactoryAppDelegate : NSObject {
14 | UIWindow *window;
15 | AbstractFactoryViewController *viewController;
16 | }
17 |
18 | @property (nonatomic, retain) IBOutlet UIWindow *window;
19 | @property (nonatomic, retain) IBOutlet AbstractFactoryViewController *viewController;
20 |
21 | @end
22 |
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/AbstractFactoryViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // AbstractFactoryViewController.h
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AbstractFactoryViewController : UIViewController {
12 |
13 | }
14 |
15 | @end
16 |
17 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/AcmeBrandingFactory.h:
--------------------------------------------------------------------------------
1 | //
2 | // AcmeBrandingFactory.h
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "BrandingFactory.h"
11 |
12 |
13 | @interface AcmeBrandingFactory : BrandingFactory
14 | {
15 |
16 | }
17 |
18 | - (UIView *) brandedView;
19 | - (UIButton *) brandedMainButton;
20 | - (UIToolbar *) brandedToolbar;
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/AcmeBrandingFactory.m:
--------------------------------------------------------------------------------
1 | //
2 | // AcmeBrandingFactory.m
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "AcmeBrandingFactory.h"
10 | #import "AcmeView.h"
11 | #import "AcmeMainButton.h"
12 | #import "AcmeToolbar.h"
13 |
14 |
15 | @implementation AcmeBrandingFactory
16 |
17 | - (UIView *) brandedView
18 | {
19 | // returns a custom view for Acme
20 | return [[[AcmeView alloc] init] autorelease];
21 | }
22 |
23 | - (UIButton *) brandedMainButton
24 | {
25 | // returns a custom main button for Acme
26 | return [[[AcmeMainButton alloc] init] autorelease];
27 | }
28 |
29 | - (UIToolbar *) brandedToolbar
30 | {
31 | // returns a custom toolbar for Acme
32 | return [[[AcmeToolbar alloc] init] autorelease];
33 | }
34 |
35 | @end
36 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/AcmeMainButton.h:
--------------------------------------------------------------------------------
1 | //
2 | // AcmeMainButton.h
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AcmeMainButton : UIButton
12 | {
13 |
14 | }
15 |
16 | @end
17 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/AcmeMainButton.m:
--------------------------------------------------------------------------------
1 | //
2 | // AcmeMainButton.m
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "AcmeMainButton.h"
10 |
11 |
12 | @implementation AcmeMainButton
13 |
14 | - (id) init
15 | {
16 | if (self = [super init])
17 | {
18 | [self setTitle:@"Acme" forState:UIControlStateNormal];
19 | }
20 |
21 | return self;
22 | }
23 |
24 | - (void) awakeFromNib
25 | {
26 | [self init];
27 |
28 | return;
29 | }
30 |
31 | @end
32 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/AcmeToolbar.h:
--------------------------------------------------------------------------------
1 | //
2 | // AcmeToolbar.h
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface AcmeToolbar : UIToolbar
13 | {
14 |
15 | }
16 |
17 | @end
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/AcmeToolbar.m:
--------------------------------------------------------------------------------
1 | //
2 | // AcmeToolbar.m
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "AcmeToolbar.h"
10 |
11 |
12 | @implementation AcmeToolbar
13 |
14 | @end
15 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/AcmeView.h:
--------------------------------------------------------------------------------
1 | //
2 | // AcmeView.h
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface AcmeView : UIView
13 | {
14 |
15 | }
16 |
17 | @end
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/AcmeView.m:
--------------------------------------------------------------------------------
1 | //
2 | // AcmeView.m
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "AcmeView.h"
10 |
11 |
12 | @implementation AcmeView
13 |
14 |
15 | - (id)initWithFrame:(CGRect)frame {
16 | if ((self = [super initWithFrame:frame])) {
17 | // Initialization code
18 | }
19 | return self;
20 | }
21 |
22 | /*
23 | // Only override drawRect: if you perform custom drawing.
24 | // An empty implementation adversely affects performance during animation.
25 | - (void)drawRect:(CGRect)rect {
26 | // Drawing code
27 | }
28 | */
29 |
30 | - (void)dealloc {
31 | [super dealloc];
32 | }
33 |
34 |
35 | @end
36 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/BrandingFactory.h:
--------------------------------------------------------------------------------
1 | //
2 | // BrandingFactory.h
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface BrandingFactory : NSObject
12 | {
13 |
14 | }
15 |
16 | + (BrandingFactory *) factory;
17 |
18 | - (UIView *) brandedView;
19 | - (UIButton *) brandedMainButton;
20 | - (UIToolbar *) brandedToolbar;
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/BrandingFactory.m:
--------------------------------------------------------------------------------
1 | //
2 | // BrandingFactory.m
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "BrandingFactory.h"
10 | #import "AcmeBrandingFactory.h"
11 | #import "SierraBrandingFactory.h"
12 |
13 |
14 | @implementation BrandingFactory
15 |
16 | + (BrandingFactory *) factory
17 | {
18 | #if defined (USE_ACME)
19 | return [[[AcmeBrandingFactory alloc] init] autorelease];
20 | #elif defined (USE_SIERRA)
21 | return [[[SierraBrandingFactory alloc] init] autorelease];
22 | #else
23 | return nil;
24 | #endif
25 | }
26 |
27 | - (UIView *) brandedView
28 | {
29 | return nil;
30 | }
31 |
32 | - (UIButton *) brandedMainButton
33 | {
34 | return nil;
35 | }
36 |
37 | - (UIToolbar *) brandedToolbar
38 | {
39 | return nil;
40 | }
41 |
42 | @end
43 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/SierraBrandingFactory.h:
--------------------------------------------------------------------------------
1 | //
2 | // SierraBrandingFactory.h
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/16/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "BrandingFactory.h"
11 |
12 |
13 | @interface SierraBrandingFactory : BrandingFactory
14 | {
15 |
16 | }
17 |
18 | - (UIView*) brandedView;
19 | - (UIButton*) brandedMainButton;
20 | - (UIToolbar*) brandedToolbar;
21 |
22 | @end
23 |
24 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/SierraBrandingFactory.m:
--------------------------------------------------------------------------------
1 | //
2 | // SierraBrandingFactory.m
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/16/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "SierraBrandingFactory.h"
10 | #import "SierraView.h"
11 | #import "SierraMainButton.h"
12 | #import "SierraToolbar.h"
13 |
14 | @implementation SierraBrandingFactory
15 |
16 | - (UIView*) brandedView
17 | {
18 | // returns a custom view for Sierra
19 | return [[[SierraView alloc] init] autorelease];
20 | }
21 |
22 | - (UIButton*) brandedMainButton
23 | {
24 | // returns a custom main button for Sierra
25 | return [[[SierraMainButton alloc] init] autorelease];
26 | }
27 |
28 | - (UIToolbar*) brandedToolbar
29 | {
30 | // returns a custom toolbar for Sierra
31 | return [[[SierraToolbar alloc] init] autorelease];
32 | }
33 |
34 | @end
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/SierraMainButton.h:
--------------------------------------------------------------------------------
1 | //
2 | // SierraMainButton.h
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface SierraMainButton : UIButton
12 | {
13 |
14 | }
15 |
16 | @end
17 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/SierraMainButton.m:
--------------------------------------------------------------------------------
1 | //
2 | // SierraMainButton.m
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "SierraMainButton.h"
10 |
11 |
12 | @implementation SierraMainButton
13 |
14 |
15 | - (id) init
16 | {
17 | if (self = [super init])
18 | {
19 | [self setTitle:@"Sierra" forState:UIControlStateNormal];
20 | }
21 |
22 | return self;
23 | }
24 |
25 | - (void) awakeFromNib
26 | {
27 | [self init];
28 |
29 | return;
30 | }
31 |
32 | @end
33 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/SierraToolbar.h:
--------------------------------------------------------------------------------
1 | //
2 | // SierraToolbar.h
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface SierraToolbar : UIToolbar
13 | {
14 |
15 | }
16 |
17 | @end
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/SierraToolbar.m:
--------------------------------------------------------------------------------
1 | //
2 | // SierraToolbar.m
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "SierraToolbar.h"
10 |
11 |
12 | @implementation SierraToolbar
13 |
14 | @end
15 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/SierraView.h:
--------------------------------------------------------------------------------
1 | //
2 | // SierraView.h
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface SierraView : UIView
13 | {
14 |
15 | }
16 |
17 | @end
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/Classes/SierraView.m:
--------------------------------------------------------------------------------
1 | //
2 | // SierraView.m
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "SierraView.h"
10 |
11 |
12 | @implementation SierraView
13 |
14 |
15 | - (id)initWithFrame:(CGRect)frame {
16 | if ((self = [super initWithFrame:frame])) {
17 | // Initialization code
18 | }
19 | return self;
20 | }
21 |
22 | /*
23 | // Only override drawRect: if you perform custom drawing.
24 | // An empty implementation adversely affects performance during animation.
25 | - (void)drawRect:(CGRect)rect {
26 | // Drawing code
27 | }
28 | */
29 |
30 | - (void)dealloc {
31 | [super dealloc];
32 | }
33 |
34 |
35 | @end
36 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch05_AbstractFactory_code/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // AbstractFactory
4 | //
5 | // Created by Carlo Chung on 11/1/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | int main(int argc, char *argv[]) {
12 |
13 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
14 | int retVal = UIApplicationMain(argc, argv, nil, nil);
15 | [pool release];
16 | return retVal;
17 | }
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/Builder-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleDisplayName
8 | ${PRODUCT_NAME}
9 | CFBundleExecutable
10 | ${EXECUTABLE_NAME}
11 | CFBundleIconFile
12 |
13 | CFBundleIdentifier
14 | com.yourcompany.${PRODUCT_NAME:rfc1034identifier}
15 | CFBundleInfoDictionaryVersion
16 | 6.0
17 | CFBundleName
18 | ${PRODUCT_NAME}
19 | CFBundlePackageType
20 | APPL
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | 1.0
25 | LSRequiresIPhoneOS
26 |
27 | NSMainNibFile
28 | MainWindow
29 |
30 |
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/Builder_Prefix.pch:
--------------------------------------------------------------------------------
1 | //
2 | // Prefix header for all source files of the 'Builder' target in the 'Builder' project
3 | //
4 |
5 | #ifdef __OBJC__
6 | #import
7 | #import
8 | #endif
9 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/Character.h:
--------------------------------------------------------------------------------
1 | //
2 | // Character.h
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface Character : NSObject
13 | {
14 | @private
15 | float protection_;
16 | float power_;
17 | float strength_;
18 | float stamina_;
19 | float intelligence_;
20 | float agility_;
21 | float aggressiveness_;
22 | }
23 |
24 | @property (nonatomic, assign) float protection;
25 | @property (nonatomic, assign) float power;
26 | @property (nonatomic, assign) float strength;
27 | @property (nonatomic, assign) float stamina;
28 | @property (nonatomic, assign) float intelligence;
29 | @property (nonatomic, assign) float agility;
30 | @property (nonatomic, assign) float aggressiveness;
31 |
32 | @end
33 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/Character.m:
--------------------------------------------------------------------------------
1 | //
2 | // Character.m
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "Character.h"
10 |
11 |
12 | @implementation Character
13 |
14 | @synthesize protection=protection_;
15 | @synthesize power=power_;
16 | @synthesize strength=strength_;
17 | @synthesize stamina=stamina_;
18 | @synthesize intelligence=intelligence_;
19 | @synthesize agility=agility_;
20 | @synthesize aggressiveness=aggressiveness_;
21 |
22 | - (id) init
23 | {
24 | if (self = [super init])
25 | {
26 | protection_ = 1.0;
27 | power_ = 1.0;
28 | strength_ = 1.0;
29 | stamina_ = 1.0;
30 | intelligence_ = 1.0;
31 | agility_ = 1.0;
32 | aggressiveness_ = 1.0;
33 | }
34 |
35 | return self;
36 | }
37 |
38 | @end
39 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/CharacterBuilder.h:
--------------------------------------------------------------------------------
1 | //
2 | // CharacterBuilder.h
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Character.h"
11 |
12 | @interface CharacterBuilder : NSObject
13 | {
14 | @protected
15 | Character *character_;
16 | }
17 |
18 | @property (nonatomic, readonly) Character *character;
19 |
20 | - (CharacterBuilder *) buildNewCharacter;
21 | - (CharacterBuilder *) buildStrength:(float) value;
22 | - (CharacterBuilder *) buildStamina:(float) value;
23 | - (CharacterBuilder *) buildIntelligence:(float) value;
24 | - (CharacterBuilder *) buildAgility:(float) value;
25 | - (CharacterBuilder *) buildAggressiveness:(float) value;
26 |
27 | @end
28 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/CharacterBuilder.m:
--------------------------------------------------------------------------------
1 | //
2 | // CharacterBuilder.m
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "CharacterBuilder.h"
10 |
11 |
12 | @implementation CharacterBuilder
13 |
14 | @synthesize character=character_;
15 |
16 |
17 | - (CharacterBuilder *) buildNewCharacter
18 | {
19 | // autorelease the previous character
20 | // before creating a new one
21 | [character_ autorelease];
22 | character_ = [[Character alloc] init];
23 |
24 | return self;
25 | }
26 |
27 | - (CharacterBuilder *) buildStrength:(float) value
28 | {
29 | character_.strength = value;
30 | return self;
31 | }
32 |
33 | - (CharacterBuilder *) buildStamina:(float) value
34 | {
35 | character_.stamina = value;
36 | return self;
37 | }
38 |
39 | - (CharacterBuilder *) buildIntelligence:(float) value
40 | {
41 | character_.intelligence = value;
42 | return self;
43 | }
44 |
45 | - (CharacterBuilder *) buildAgility:(float) value
46 | {
47 | character_.agility = value;
48 | return self;
49 | }
50 |
51 | - (CharacterBuilder *) buildAggressiveness:(float) value
52 | {
53 | character_.aggressiveness = value;
54 | return self;
55 | }
56 |
57 | - (void) dealloc
58 | {
59 | [character_ autorelease];
60 | [super dealloc];
61 | }
62 |
63 | @end
64 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/ChasingGame.h:
--------------------------------------------------------------------------------
1 | //
2 | // ChasingGame.h
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "StandardCharacterBuilder.h"
11 |
12 | @interface ChasingGame : NSObject
13 | {
14 |
15 | }
16 |
17 | - (Character *) createPlayer:(CharacterBuilder *) builder;
18 | - (Character *) createEnemy:(CharacterBuilder *) builder;
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/ChasingGame.m:
--------------------------------------------------------------------------------
1 | //
2 | // ChasingGame.m
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "ChasingGame.h"
10 |
11 |
12 | @implementation ChasingGame
13 |
14 | - (Character *) createPlayer:(CharacterBuilder *) builder
15 | {
16 | [builder buildNewCharacter];
17 | [builder buildStrength:50.0];
18 | [builder buildStamina:25.0];
19 | [builder buildIntelligence:75.0];
20 | [builder buildAgility:65.0];
21 | [builder buildAggressiveness:35.0];
22 |
23 | // an alternative way to build a character
24 | [[[[[[builder buildNewCharacter]
25 | buildStrength:50.0]
26 | buildStamina:25.0]
27 | buildIntelligence:75.0]
28 | buildAgility:65.0]
29 | buildAggressiveness:35.0];
30 |
31 | return [builder character];
32 | }
33 |
34 | - (Character *) createEnemy:(CharacterBuilder *) builder
35 | {
36 | [builder buildNewCharacter];
37 | [builder buildStrength:80.0];
38 | [builder buildStamina:65.0];
39 | [builder buildIntelligence:35.0];
40 | [builder buildAgility:25.0];
41 | [builder buildAggressiveness:95.0];
42 |
43 | return [builder character];
44 | }
45 |
46 | @end
47 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/Classes/BuilderAppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // BuilderAppDelegate.h
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class BuilderViewController;
12 |
13 | @interface BuilderAppDelegate : NSObject {
14 | UIWindow *window;
15 | BuilderViewController *viewController;
16 | }
17 |
18 | @property (nonatomic, retain) IBOutlet UIWindow *window;
19 | @property (nonatomic, retain) IBOutlet BuilderViewController *viewController;
20 |
21 | @end
22 |
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/Classes/BuilderAppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // BuilderAppDelegate.m
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "BuilderAppDelegate.h"
10 | #import "BuilderViewController.h"
11 |
12 | @implementation BuilderAppDelegate
13 |
14 | @synthesize window;
15 | @synthesize viewController;
16 |
17 |
18 | #pragma mark -
19 | #pragma mark Application lifecycle
20 |
21 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
22 |
23 | // Override point for customization after application launch.
24 |
25 | // Add the view controller's view to the window and display.
26 | [window addSubview:viewController.view];
27 | [window makeKeyAndVisible];
28 |
29 | return YES;
30 | }
31 |
32 |
33 | - (void)applicationWillResignActive:(UIApplication *)application {
34 | /*
35 | 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.
36 | Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
37 | */
38 | }
39 |
40 |
41 | - (void)applicationDidEnterBackground:(UIApplication *)application {
42 | /*
43 | 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.
44 | If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
45 | */
46 | }
47 |
48 |
49 | - (void)applicationWillEnterForeground:(UIApplication *)application {
50 | /*
51 | Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
52 | */
53 | }
54 |
55 |
56 | - (void)applicationDidBecomeActive:(UIApplication *)application {
57 | /*
58 | 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.
59 | */
60 | }
61 |
62 |
63 | - (void)applicationWillTerminate:(UIApplication *)application {
64 | /*
65 | Called when the application is about to terminate.
66 | See also applicationDidEnterBackground:.
67 | */
68 | }
69 |
70 |
71 | #pragma mark -
72 | #pragma mark Memory management
73 |
74 | - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
75 | /*
76 | Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
77 | */
78 | }
79 |
80 |
81 | - (void)dealloc {
82 | [viewController release];
83 | [window release];
84 | [super dealloc];
85 | }
86 |
87 |
88 | @end
89 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/Classes/BuilderViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // BuilderViewController.h
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface BuilderViewController : UIViewController {
12 |
13 | }
14 |
15 | @end
16 |
17 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/Classes/BuilderViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // BuilderViewController.m
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "BuilderViewController.h"
10 | #import "ChasingGame.h"
11 | #import "StandardCharacterBuilder.h"
12 |
13 |
14 | @implementation BuilderViewController
15 |
16 |
17 |
18 | /*
19 | // The designated initializer. Override to perform setup that is required before the view is loaded.
20 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
21 | if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
22 | // Custom initialization
23 | }
24 | return self;
25 | }
26 | */
27 |
28 | /*
29 | // Implement loadView to create a view hierarchy programmatically, without using a nib.
30 | - (void)loadView {
31 | }
32 | */
33 |
34 |
35 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
36 | - (void)viewDidLoad
37 | {
38 | [super viewDidLoad];
39 |
40 | CharacterBuilder *characterBuilder = [[[StandardCharacterBuilder alloc] init] autorelease];
41 | ChasingGame *game = [[[ChasingGame alloc] init] autorelease];
42 |
43 | Character *player = [game createPlayer:characterBuilder];
44 | Character *enemy = [game createEnemy:characterBuilder];
45 |
46 | // do something else with the
47 | // player and enemey
48 | }
49 |
50 |
51 |
52 | /*
53 | // Override to allow orientations other than the default portrait orientation.
54 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
55 | // Return YES for supported orientations
56 | return (interfaceOrientation == UIInterfaceOrientationPortrait);
57 | }
58 | */
59 |
60 | - (void)didReceiveMemoryWarning {
61 | // Releases the view if it doesn't have a superview.
62 | [super didReceiveMemoryWarning];
63 |
64 | // Release any cached data, images, etc that aren't in use.
65 | }
66 |
67 | - (void)viewDidUnload {
68 | // Release any retained subviews of the main view.
69 | // e.g. self.myOutlet = nil;
70 | }
71 |
72 |
73 | - (void)dealloc {
74 | [super dealloc];
75 | }
76 |
77 | @end
78 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/StandardCharacterBuilder.h:
--------------------------------------------------------------------------------
1 | //
2 | // StandardCharacterBuilder.h
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "CharacterBuilder.h"
11 |
12 | @interface StandardCharacterBuilder : CharacterBuilder
13 | {
14 |
15 | }
16 |
17 | // overriden methods from the abstract CharacterBuilder
18 | - (CharacterBuilder *) buildStrength:(float) value;
19 | - (CharacterBuilder *) buildStamina:(float) value;
20 | - (CharacterBuilder *) buildIntelligence:(float) value;
21 | - (CharacterBuilder *) buildAgility:(float) value;
22 | - (CharacterBuilder *) buildAggressiveness:(float) value;
23 |
24 | @end
25 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/StandardCharacterBuilder.m:
--------------------------------------------------------------------------------
1 | //
2 | // StandardCharacterBuilder.m
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "StandardCharacterBuilder.h"
10 |
11 |
12 | @implementation StandardCharacterBuilder
13 |
14 | - (CharacterBuilder *) buildStrength:(float) value
15 | {
16 | // update the protection value of the character
17 | character_.protection *= value;
18 |
19 | // update the power value of the character
20 | character_.power *= value;
21 |
22 | // finally set the strength value and return this builder
23 | return [super buildStrength:value];
24 | }
25 |
26 | - (CharacterBuilder *) buildStamina:(float) value
27 | {
28 | // update the protection value of the character
29 | character_.protection *= value;
30 |
31 | // update the power value of the character
32 | character_.power *= value;
33 |
34 | // finally set the strength value and return this builder
35 | return [super buildStamina:value];
36 | }
37 |
38 | - (CharacterBuilder *) buildIntelligence:(float) value
39 | {
40 | // update the protection value of the character
41 | character_.protection *= value;
42 |
43 | // update the power value of the character
44 | character_.power /= value;
45 |
46 | // finally set the strength value and return this builder
47 | return [super buildIntelligence:value];
48 | }
49 |
50 | - (CharacterBuilder *) buildAgility:(float) value
51 | {
52 | // update the protection value of the character
53 | character_.protection *= value;
54 |
55 | // update the power value of the character
56 | character_.power /= value;
57 |
58 | // finally set the strength value and return this builder
59 | return [super buildAgility:value];
60 | }
61 |
62 | - (CharacterBuilder *) buildAggressiveness:(float) value
63 | {
64 | // update the protection value of the character
65 | character_.protection /= value;
66 |
67 | // update the power value of the character
68 | character_.power *= value;
69 |
70 | // finally set the strength value and return this builder
71 | return [super buildAggressiveness:value];
72 | }
73 |
74 |
75 | @end
76 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch06_Builder_code/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // Builder
4 | //
5 | // Created by Carlo Chung on 11/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | int main(int argc, char *argv[]) {
12 |
13 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
14 | int retVal = UIApplicationMain(argc, argv, nil, nil);
15 | [pool release];
16 | return retVal;
17 | }
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch07_Singleton_code/Classes/Go4Singleton.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Go4Singleton.cpp
3 | * Singleton
4 | *
5 | * Created by Carlo Chung on 6/10/10.
6 | * Copyright 2010 Carlo Chung. All rights reserved.
7 | *
8 | */
9 |
10 | #include "Go4Singleton.h"
11 |
12 | Singleton *Singleton::_instance = 0;
13 |
14 | Singleton *Singleton::Instance()
15 | {
16 | if (_instance == 0)
17 | {
18 | _instance = new Singleton;
19 | }
20 |
21 | return _instance;
22 | }
23 |
24 |
25 | Singleton::Singleton()
26 | {
27 |
28 | }
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch07_Singleton_code/Classes/Go4Singleton.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Go4Singleton.h
3 | * Singleton
4 | *
5 | * Created by Carlo Chung on 6/10/10.
6 | * Copyright 2010 Carlo Chung. All rights reserved.
7 | *
8 | */
9 |
10 | class Singleton
11 | {
12 |
13 | public:
14 | static Singleton *Instance();
15 |
16 | protected:
17 | Singleton();
18 |
19 | private:
20 | static Singleton *_instance;
21 |
22 | };
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch07_Singleton_code/Classes/MySingleton.h:
--------------------------------------------------------------------------------
1 | //
2 | // MySingleton.h
3 | // Singleton
4 | //
5 | // Created by Carlo Chung on 2/17/11.
6 | // Copyright 2011 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Singleton.h"
11 |
12 |
13 |
14 | @interface MySingleton : Singleton
15 | {
16 |
17 | }
18 |
19 | @end
20 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch07_Singleton_code/Classes/MySingleton.m:
--------------------------------------------------------------------------------
1 | //
2 | // MySingleton.m
3 | // Singleton
4 | //
5 | // Created by Carlo Chung on 2/17/11.
6 | // Copyright 2011 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "MySingleton.h"
10 |
11 |
12 |
13 | @implementation MySingleton
14 |
15 | - (id) init
16 | {
17 |
18 | return self;
19 | }
20 |
21 | - (void) operation
22 | {
23 | // do something
24 | NSLog(@"MySingleton");
25 | }
26 |
27 | @end
28 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch07_Singleton_code/Classes/Singleton.h:
--------------------------------------------------------------------------------
1 | //
2 | // Singleton.h
3 | // Singleton
4 | //
5 | // Created by Carlo Chung on 6/10/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface Singleton : NSObject
13 | {
14 |
15 | }
16 |
17 | + (Singleton *) sharedInstance;
18 |
19 | - (void) operation;
20 |
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch07_Singleton_code/Classes/Singleton.m:
--------------------------------------------------------------------------------
1 | //
2 | // Singleton.m
3 | // Singleton
4 | //
5 | // Created by Carlo Chung on 6/10/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "Singleton.h"
10 |
11 |
12 | @implementation Singleton
13 |
14 |
15 | static Singleton *sharedSingleton_ = nil;
16 |
17 | - (void) operation
18 | {
19 | // do something
20 | NSLog(@"Singleton");
21 | }
22 |
23 | + (Singleton *) sharedInstance
24 | {
25 | if (sharedSingleton_ == nil)
26 | {
27 | sharedSingleton_ = [NSAllocateObject([self class], 0, NULL) init];
28 | }
29 |
30 | return sharedSingleton_;
31 | }
32 |
33 |
34 | + (id) allocWithZone:(NSZone *)zone
35 | {
36 | return [[self sharedInstance] retain];
37 | }
38 |
39 |
40 | - (id) copyWithZone:(NSZone*)zone
41 | {
42 | return self;
43 | }
44 |
45 | - (id) retain
46 | {
47 | return self;
48 | }
49 |
50 | - (NSUInteger) retainCount
51 | {
52 | return NSUIntegerMax; // denotes an object that cannot be released
53 | }
54 |
55 | - (void) release
56 | {
57 | // do nothing
58 | }
59 |
60 | - (id) autorelease
61 | {
62 | return self;
63 | }
64 |
65 | @end
66 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch07_Singleton_code/Classes/SingletonAppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // SingletonAppDelegate.h
3 | // Singleton
4 | //
5 | // Created by Carlo Chung on 6/10/10.
6 | // Copyright Carlo Chung 2010. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface SingletonAppDelegate : NSObject {
12 | UIWindow *window;
13 | }
14 |
15 | @property (nonatomic, retain) IBOutlet UIWindow *window;
16 |
17 | @end
18 |
19 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch07_Singleton_code/Classes/SingletonAppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // SingletonAppDelegate.m
3 | // Singleton
4 | //
5 | // Created by Carlo Chung on 6/10/10.
6 | // Copyright Carlo Chung 2010. All rights reserved.
7 | //
8 |
9 | #import "SingletonAppDelegate.h"
10 | #import "MySingleton.h"
11 |
12 | @implementation SingletonAppDelegate
13 |
14 | @synthesize window;
15 |
16 |
17 | #pragma mark -
18 | #pragma mark Application lifecycle
19 |
20 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
21 |
22 | // Override point for customization after application launch.
23 |
24 | Singleton *s = [MySingleton sharedInstance];
25 |
26 | [s operation];
27 |
28 | [window makeKeyAndVisible];
29 |
30 | return YES;
31 | }
32 |
33 |
34 | - (void)applicationWillResignActive:(UIApplication *)application {
35 | /*
36 | 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.
37 | Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
38 | */
39 | }
40 |
41 |
42 | - (void)applicationDidEnterBackground:(UIApplication *)application {
43 | /*
44 | 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.
45 | If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
46 | */
47 | }
48 |
49 |
50 | - (void)applicationWillEnterForeground:(UIApplication *)application {
51 | /*
52 | Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
53 | */
54 | }
55 |
56 |
57 | - (void)applicationDidBecomeActive:(UIApplication *)application {
58 | /*
59 | 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.
60 | */
61 | }
62 |
63 |
64 | - (void)applicationWillTerminate:(UIApplication *)application {
65 | /*
66 | Called when the application is about to terminate.
67 | See also applicationDidEnterBackground:.
68 | */
69 | }
70 |
71 |
72 | #pragma mark -
73 | #pragma mark Memory management
74 |
75 | - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
76 | /*
77 | Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
78 | */
79 | }
80 |
81 |
82 | - (void)dealloc {
83 | [window release];
84 | [super dealloc];
85 | }
86 |
87 |
88 | @end
89 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch07_Singleton_code/Singleton-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleDisplayName
8 | ${PRODUCT_NAME}
9 | CFBundleExecutable
10 | ${EXECUTABLE_NAME}
11 | CFBundleIconFile
12 |
13 | CFBundleIdentifier
14 | com.yourcompany.${PRODUCT_NAME:rfc1034identifier}
15 | CFBundleInfoDictionaryVersion
16 | 6.0
17 | CFBundleName
18 | ${PRODUCT_NAME}
19 | CFBundlePackageType
20 | APPL
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | 1.0
25 | LSRequiresIPhoneOS
26 |
27 | NSMainNibFile
28 | MainWindow
29 |
30 |
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch07_Singleton_code/Singleton_Prefix.pch:
--------------------------------------------------------------------------------
1 | //
2 | // Prefix header for all source files of the 'Singleton' target in the 'Singleton' project
3 | //
4 |
5 | #ifdef __OBJC__
6 | #import
7 | #import
8 | #endif
9 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch07_Singleton_code/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // Singleton
4 | //
5 | // Created by Carlo Chung on 6/10/10.
6 | // Copyright Carlo Chung 2010. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | int main(int argc, char *argv[]) {
12 |
13 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
14 | int retVal = UIApplicationMain(argc, argv, nil, nil);
15 | [pool release];
16 | return retVal;
17 | }
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/Bridge-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleDisplayName
8 | ${PRODUCT_NAME}
9 | CFBundleExecutable
10 | ${EXECUTABLE_NAME}
11 | CFBundleIconFile
12 |
13 | CFBundleIdentifier
14 | com.yourcompany.${PRODUCT_NAME:rfc1034identifier}
15 | CFBundleInfoDictionaryVersion
16 | 6.0
17 | CFBundleName
18 | ${PRODUCT_NAME}
19 | CFBundlePackageType
20 | APPL
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | 1.0
25 | LSRequiresIPhoneOS
26 |
27 | NSMainNibFile
28 | MainWindow
29 |
30 |
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/Bridge_Prefix.pch:
--------------------------------------------------------------------------------
1 | //
2 | // Prefix header for all source files of the 'Bridge' target in the 'Bridge' project
3 | //
4 |
5 | #ifdef __OBJC__
6 | #import
7 | #import
8 | #endif
9 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/Classes/BridgeAppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // BridgeAppDelegate.h
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class BridgeViewController;
12 |
13 | @interface BridgeAppDelegate : NSObject {
14 | UIWindow *window;
15 | BridgeViewController *viewController;
16 | }
17 |
18 | @property (nonatomic, retain) IBOutlet UIWindow *window;
19 | @property (nonatomic, retain) IBOutlet BridgeViewController *viewController;
20 |
21 | @end
22 |
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/Classes/BridgeViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // BridgeViewController.h
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface BridgeViewController : UIViewController {
12 |
13 | }
14 |
15 | @end
16 |
17 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/Classes/BridgeViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // BridgeViewController.m
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "BridgeViewController.h"
10 |
11 | @implementation BridgeViewController
12 |
13 |
14 |
15 | /*
16 | // The designated initializer. Override to perform setup that is required before the view is loaded.
17 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
18 | if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
19 | // Custom initialization
20 | }
21 | return self;
22 | }
23 | */
24 |
25 | /*
26 | // Implement loadView to create a view hierarchy programmatically, without using a nib.
27 | - (void)loadView {
28 | }
29 | */
30 |
31 |
32 | /*
33 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
34 | - (void)viewDidLoad {
35 | [super viewDidLoad];
36 | }
37 | */
38 |
39 |
40 | /*
41 | // Override to allow orientations other than the default portrait orientation.
42 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
43 | // Return YES for supported orientations
44 | return (interfaceOrientation == UIInterfaceOrientationPortrait);
45 | }
46 | */
47 |
48 | - (void)didReceiveMemoryWarning {
49 | // Releases the view if it doesn't have a superview.
50 | [super didReceiveMemoryWarning];
51 |
52 | // Release any cached data, images, etc that aren't in use.
53 | }
54 |
55 | - (void)viewDidUnload {
56 | // Release any retained subviews of the main view.
57 | // e.g. self.myOutlet = nil;
58 | }
59 |
60 |
61 | - (void)dealloc {
62 | [super dealloc];
63 | }
64 |
65 | @end
66 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/ConsoleCommands.h:
--------------------------------------------------------------------------------
1 | /*
2 | * ConsoleCommands.h
3 | * Bridge
4 | *
5 | * Created by Carlo Chung on 11/26/10.
6 | * Copyright 2010 Carlo Chung. All rights reserved.
7 | *
8 | */
9 |
10 | typedef enum
11 | {
12 | kConsoleCommandUp,
13 | kConsoleCommandDown,
14 | kConsoleCommandLeft,
15 | kConsoleCommandRight,
16 | kConsoleCommandSelect,
17 | kConsoleCommandStart,
18 | kConsoleCommandAction1,
19 | kConsoleCommandAction2
20 | } ConsoleCommand;
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/ConsoleController.h:
--------------------------------------------------------------------------------
1 | //
2 | // VirtualController.h
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "ConsoleEmulator.h"
11 | #import "ConsoleCommands.h"
12 |
13 | @interface ConsoleController : NSObject
14 | {
15 | @private
16 | ConsoleEmulator *emulator_;
17 | }
18 |
19 | @property (nonatomic, retain) ConsoleEmulator *emulator;
20 |
21 | - (void) setCommand:(ConsoleCommand) command;
22 |
23 | // other behaviors and properties
24 |
25 | @end
26 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/ConsoleController.m:
--------------------------------------------------------------------------------
1 | //
2 | // VirtualController.m
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "ConsoleController.h"
10 |
11 |
12 | @implementation ConsoleController
13 |
14 | @synthesize emulator=emulator_;
15 |
16 | - (void) setCommand:(ConsoleCommand) command
17 | {
18 | [emulator_ loadInstructionsForCommand:command];
19 | [emulator_ executeInstructions];
20 | }
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/ConsoleEmulator.h:
--------------------------------------------------------------------------------
1 | //
2 | // ConsoleEmulator.h
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "ConsoleCommands.h"
11 |
12 | @interface ConsoleEmulator : NSObject
13 | {
14 |
15 | }
16 |
17 | - (void) loadInstructionsForCommand:(ConsoleCommand) command;
18 | - (void) executeInstructions;
19 |
20 | // other behaviors and properties.
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/ConsoleEmulator.m:
--------------------------------------------------------------------------------
1 | //
2 | // ConsoleEmulator.m
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "ConsoleEmulator.h"
10 |
11 |
12 | @implementation ConsoleEmulator
13 |
14 | - (void) loadInstructionsForCommand:(ConsoleCommand) command
15 | {
16 | // should throw an exception
17 | }
18 |
19 | - (void) executeInstructions
20 | {
21 | // subclasses can use this default
22 | // implementation to
23 | // execute all loaded instructions
24 | // in the command data structure.
25 | }
26 |
27 | @end
28 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/GameBoyEmulator.h:
--------------------------------------------------------------------------------
1 | //
2 | // GameBoyEmulator.h
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "ConsoleEmulator.h"
11 |
12 | @interface GameBoyEmulator : ConsoleEmulator
13 | {
14 |
15 | }
16 |
17 | // overridden behaviors from the abstract class
18 | - (void) loadInstructionsForCommand:(ConsoleCommand) command;
19 | - (void) executeInstructions;
20 |
21 | // other behaviors and properties.
22 |
23 | @end
24 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/GameBoyEmulator.m:
--------------------------------------------------------------------------------
1 | //
2 | // GameBoyEmulator.m
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "GameBoyEmulator.h"
10 |
11 |
12 | @implementation GameBoyEmulator
13 |
14 | - (void) loadInstructionsForCommand:(ConsoleCommand) command
15 | {
16 | // load specific instructions for
17 | // the Game Boy
18 | }
19 |
20 | - (void) executeInstructions
21 | {
22 | // execute loaded instructions
23 | }
24 |
25 | @end
26 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/GameGearEmulator.h:
--------------------------------------------------------------------------------
1 | //
2 | // GameGearEmulator.h
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "ConsoleEmulator.h"
11 |
12 | @interface GameGearEmulator : ConsoleEmulator
13 | {
14 |
15 | }
16 |
17 | // overridden behaviors from the abstract class
18 | - (void) loadInstructionsForCommand:(ConsoleCommand) command;
19 | - (void) executeInstructions;
20 |
21 | // other behaviors and properties.
22 |
23 | @end
24 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/GameGearEmulator.m:
--------------------------------------------------------------------------------
1 | //
2 | // GameGearEmulator.m
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "GameGearEmulator.h"
10 |
11 |
12 | @implementation GameGearEmulator
13 |
14 | - (void) loadInstructionsForCommand:(ConsoleCommand) command
15 | {
16 | // load specific instructions for
17 | // the Game Gear
18 | }
19 |
20 | - (void) executeInstructions
21 | {
22 | // execute loaded instructions
23 | }
24 |
25 | @end
26 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/TouchConsoleController.h:
--------------------------------------------------------------------------------
1 | //
2 | // TouchConsoleController.h
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "ConsoleController.h"
11 |
12 | @interface TouchConsoleController : ConsoleController
13 | {
14 |
15 | }
16 |
17 | - (void) up;
18 | - (void) down;
19 | - (void) left;
20 | - (void) right;
21 | - (void) select;
22 | - (void) start;
23 | - (void) action1;
24 | - (void) action2;
25 |
26 | @end
27 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/TouchConsoleController.m:
--------------------------------------------------------------------------------
1 | //
2 | // TouchConsoleController.m
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "TouchConsoleController.h"
10 | #import "ConsoleEmulator.h"
11 |
12 |
13 | @implementation TouchConsoleController
14 |
15 | - (void) up
16 | {
17 | [super setCommand:kConsoleCommandUp];
18 | }
19 |
20 | - (void) down
21 | {
22 | [super setCommand:kConsoleCommandDown];
23 | }
24 |
25 | - (void) left
26 | {
27 | [super setCommand:kConsoleCommandLeft];
28 | }
29 |
30 | - (void) right
31 | {
32 | [super setCommand:kConsoleCommandRight];
33 | }
34 |
35 | - (void) select
36 | {
37 | [super setCommand:kConsoleCommandSelect];
38 | }
39 |
40 | - (void) start
41 | {
42 | [super setCommand:kConsoleCommandStart];
43 | }
44 |
45 | - (void) action1
46 | {
47 | [super setCommand:kConsoleCommandAction1];
48 | }
49 |
50 | - (void) action2
51 | {
52 | [super setCommand:kConsoleCommandAction2];
53 | }
54 |
55 | @end
56 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch09_Bridge_code/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // Bridge
4 | //
5 | // Created by Carlo Chung on 11/26/10.
6 | // Copyright 2010 __MyCompanyName__. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | int main(int argc, char *argv[]) {
12 |
13 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
14 | int retVal = UIApplicationMain(argc, argv, nil, nil);
15 | [pool release];
16 | return retVal;
17 | }
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch10_Facade_code/CabDriver.h:
--------------------------------------------------------------------------------
1 | //
2 | // CabDriver.h
3 | // Facade
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Car.h"
11 | #import "Taximeter.h"
12 |
13 | @interface CabDriver : NSObject
14 | {
15 |
16 | }
17 |
18 | - (void) driveToLocation:(CGPoint) x;
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch10_Facade_code/CabDriver.m:
--------------------------------------------------------------------------------
1 | //
2 | // CabDriver.m
3 | // Facade
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "CabDriver.h"
10 |
11 |
12 | @implementation CabDriver
13 |
14 | - (void) driveToLocation:(CGPoint) x
15 | {
16 | // ...
17 |
18 | // set off the taximeter
19 | Taximeter *meter = [[Taximeter alloc] init];
20 | [meter start];
21 |
22 | // operate the vehicle
23 | // until location x is reached
24 | Car *car = [[Car alloc] init];
25 | [car releaseBrakes];
26 | [car changeGears];
27 | [car pressAccelerator];
28 |
29 | // ...
30 |
31 | // when it's reached location x
32 | // then stop the car and taximeter
33 | [car releaseAccelerator];
34 | [car pressBrakes];
35 | [meter stop];
36 |
37 | // ...
38 | }
39 |
40 | @end
41 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch10_Facade_code/Car.h:
--------------------------------------------------------------------------------
1 | //
2 | // Car.h
3 | // Facade
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface Car : NSObject
13 | {
14 |
15 | }
16 |
17 | // ...
18 |
19 | - (void) releaseBrakes;
20 | - (void) changeGears;
21 | - (void) pressAccelerator;
22 | - (void) pressBrakes;
23 | - (void) releaseAccelerator;
24 |
25 | // ...
26 |
27 | @end
28 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch10_Facade_code/Car.m:
--------------------------------------------------------------------------------
1 | //
2 | // Car.m
3 | // Facade
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "Car.h"
10 |
11 |
12 | @implementation Car
13 |
14 | - (void) releaseBrakes
15 | {
16 |
17 | }
18 |
19 | - (void) changeGears
20 | {
21 |
22 | }
23 |
24 | - (void) pressAccelerator
25 | {
26 |
27 | }
28 |
29 | - (void) pressBrakes
30 | {
31 |
32 | }
33 |
34 | - (void) releaseAccelerator
35 | {
36 |
37 | }
38 |
39 | @end
40 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch10_Facade_code/Classes/FacadeAppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // FacadeAppDelegate.h
3 | // Facade
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class FacadeViewController;
12 |
13 | @interface FacadeAppDelegate : NSObject {
14 | UIWindow *window;
15 | FacadeViewController *viewController;
16 | }
17 |
18 | @property (nonatomic, retain) IBOutlet UIWindow *window;
19 | @property (nonatomic, retain) IBOutlet FacadeViewController *viewController;
20 |
21 | @end
22 |
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch10_Facade_code/Classes/FacadeViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // FacadeViewController.h
3 | // Facade
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface FacadeViewController : UIViewController {
12 |
13 | }
14 |
15 | @end
16 |
17 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch10_Facade_code/Classes/FacadeViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // FacadeViewController.m
3 | // Facade
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "FacadeViewController.h"
10 |
11 | @implementation FacadeViewController
12 |
13 |
14 |
15 | /*
16 | // The designated initializer. Override to perform setup that is required before the view is loaded.
17 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
18 | if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
19 | // Custom initialization
20 | }
21 | return self;
22 | }
23 | */
24 |
25 | /*
26 | // Implement loadView to create a view hierarchy programmatically, without using a nib.
27 | - (void)loadView {
28 | }
29 | */
30 |
31 |
32 | /*
33 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
34 | - (void)viewDidLoad {
35 | [super viewDidLoad];
36 | }
37 | */
38 |
39 |
40 | /*
41 | // Override to allow orientations other than the default portrait orientation.
42 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
43 | // Return YES for supported orientations
44 | return (interfaceOrientation == UIInterfaceOrientationPortrait);
45 | }
46 | */
47 |
48 | - (void)didReceiveMemoryWarning {
49 | // Releases the view if it doesn't have a superview.
50 | [super didReceiveMemoryWarning];
51 |
52 | // Release any cached data, images, etc that aren't in use.
53 | }
54 |
55 | - (void)viewDidUnload {
56 | // Release any retained subviews of the main view.
57 | // e.g. self.myOutlet = nil;
58 | }
59 |
60 |
61 | - (void)dealloc {
62 | [super dealloc];
63 | }
64 |
65 | @end
66 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch10_Facade_code/Facade-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleDisplayName
8 | ${PRODUCT_NAME}
9 | CFBundleExecutable
10 | ${EXECUTABLE_NAME}
11 | CFBundleIconFile
12 |
13 | CFBundleIdentifier
14 | com.yourcompany.${PRODUCT_NAME:rfc1034identifier}
15 | CFBundleInfoDictionaryVersion
16 | 6.0
17 | CFBundleName
18 | ${PRODUCT_NAME}
19 | CFBundlePackageType
20 | APPL
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | 1.0
25 | LSRequiresIPhoneOS
26 |
27 | NSMainNibFile
28 | MainWindow
29 |
30 |
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch10_Facade_code/Facade_Prefix.pch:
--------------------------------------------------------------------------------
1 | //
2 | // Prefix header for all source files of the 'Facade' target in the 'Facade' project
3 | //
4 |
5 | #ifdef __OBJC__
6 | #import
7 | #import
8 | #endif
9 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch10_Facade_code/Taximeter.h:
--------------------------------------------------------------------------------
1 | //
2 | // Taximeter.h
3 | // Facade
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface Taximeter : NSObject
13 | {
14 |
15 | }
16 |
17 | - (void) start;
18 | - (void) stop;
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch10_Facade_code/Taximeter.m:
--------------------------------------------------------------------------------
1 | //
2 | // Taximeter.m
3 | // Facade
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "Taximeter.h"
10 |
11 |
12 | @implementation Taximeter
13 |
14 | - (void) start
15 | {
16 |
17 | }
18 |
19 | - (void) stop
20 | {
21 |
22 | }
23 |
24 | @end
25 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch10_Facade_code/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // Facade
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | int main(int argc, char *argv[]) {
12 |
13 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
14 | int retVal = UIApplicationMain(argc, argv, nil, nil);
15 | [pool release];
16 | return retVal;
17 | }
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/Classes/DecoratorAppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // DecoratorAppDelegate.h
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class DecoratorViewController;
12 |
13 | @interface DecoratorAppDelegate : NSObject {
14 | UIWindow *window;
15 | DecoratorViewController *viewController;
16 | }
17 |
18 | @property (nonatomic, retain) IBOutlet UIWindow *window;
19 | @property (nonatomic, retain) IBOutlet DecoratorViewController *viewController;
20 |
21 | @end
22 |
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/Classes/DecoratorView.h:
--------------------------------------------------------------------------------
1 | //
2 | // DecoratorView.h
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 1/25/11.
6 | // Copyright 2011 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface DecoratorView : UIView
13 | {
14 | @private
15 | UIImage *image_;
16 | }
17 |
18 | @property (nonatomic, retain) UIImage *image;
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/Classes/DecoratorView.m:
--------------------------------------------------------------------------------
1 | //
2 | // DecoratorView.m
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 1/25/11.
6 | // Copyright 2011 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "DecoratorView.h"
10 |
11 |
12 | @implementation DecoratorView
13 |
14 | @synthesize image=image_;
15 |
16 | - (id)initWithFrame:(CGRect)frame {
17 |
18 | self = [super initWithFrame:frame];
19 | if (self) {
20 | // Initialization code.
21 | [self setBackgroundColor:[UIColor clearColor]];
22 | }
23 | return self;
24 | }
25 |
26 | // Only override drawRect: if you perform custom drawing.
27 | // An empty implementation adversely affects performance during animation.
28 | - (void)drawRect:(CGRect)rect
29 | {
30 | // Drawing code.
31 | [image_ drawInRect:rect];
32 | }
33 |
34 |
35 | - (void)dealloc {
36 | [super dealloc];
37 | }
38 |
39 |
40 | @end
41 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/Classes/DecoratorViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // DecoratorViewController.h
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface DecoratorViewController : UIViewController {
12 |
13 | }
14 |
15 | @end
16 |
17 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/Classes/DecoratorViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // DecoratorViewController.m
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "DecoratorViewController.h"
10 | #import "UIImage+Transform.h"
11 | #import "UIImage+Shadow.h"
12 | #import "ImageTransformFilter.h"
13 | #import "ImageShadowFilter.h"
14 | #import "DecoratorView.h"
15 |
16 | @implementation DecoratorViewController
17 |
18 |
19 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
20 | - (void)viewDidLoad
21 | {
22 | [super viewDidLoad];
23 |
24 | // load the original image
25 | UIImage *image = [UIImage imageNamed:@"Image.png"];
26 |
27 |
28 | // create a transformation
29 | CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(-M_PI / 4.0);
30 | CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(-image.size.width / 2.0,
31 | image.size.height / 8.0);
32 | CGAffineTransform finalTransform = CGAffineTransformConcat(rotateTransform, translateTransform);
33 |
34 | // a true subclass approach
35 | id transformedImage = [[[ImageTransformFilter alloc] initWithImageComponent:image
36 | transform:finalTransform]
37 | autorelease];
38 | id finalImage = [[[ImageShadowFilter alloc] initWithImageComponent:transformedImage]
39 | autorelease];
40 |
41 | /*
42 | // a category approach
43 | // add transformation
44 | UIImage *transformedImage = [image imageWithTransform:finalTransform];
45 |
46 | // add shadow
47 | id finalImage = [transformedImage imageWithDropShadow];
48 |
49 | // category approach in one line
50 | //id finalImage = [[image imageWithTransform:finalTransform] imageWithDropShadow];
51 | */
52 |
53 | // create a new image view
54 | // with a filtered image
55 | DecoratorView *decoratorView = [[[DecoratorView alloc] initWithFrame:[self.view bounds]]
56 | autorelease];
57 | [decoratorView setImage:finalImage];
58 | [self.view addSubview:decoratorView];
59 | }
60 |
61 |
62 | - (void)didReceiveMemoryWarning
63 | {
64 | // Releases the view if it doesn't have a superview.
65 | [super didReceiveMemoryWarning];
66 |
67 | // Release any cached data, images, etc that aren't in use.
68 | }
69 |
70 | - (void)viewDidUnload
71 | {
72 | // Release any retained subviews of the main view.
73 | // e.g. self.myOutlet = nil;
74 | }
75 |
76 |
77 | - (void)dealloc
78 | {
79 | [super dealloc];
80 | }
81 |
82 | @end
83 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/Decorator-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleDisplayName
8 | ${PRODUCT_NAME}
9 | CFBundleExecutable
10 | ${EXECUTABLE_NAME}
11 | CFBundleIconFile
12 |
13 | CFBundleIdentifier
14 | com.yourcompany.${PRODUCT_NAME:rfc1034identifier}
15 | CFBundleInfoDictionaryVersion
16 | 6.0
17 | CFBundleName
18 | ${PRODUCT_NAME}
19 | CFBundlePackageType
20 | APPL
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | 1.0
25 | LSRequiresIPhoneOS
26 |
27 | NSMainNibFile
28 | MainWindow
29 | UIStatusBarHidden
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/Decorator_Prefix.pch:
--------------------------------------------------------------------------------
1 | //
2 | // Prefix header for all source files of the 'Decorator' target in the 'Decorator' project
3 | //
4 |
5 | #ifdef __OBJC__
6 | #import
7 | #import
8 | #endif
9 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/ImageComponent.h:
--------------------------------------------------------------------------------
1 | //
2 | // ImageComponent.h
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/30/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @protocol ImageComponent
13 |
14 | // We will intercept these
15 | // UIImage methods and add
16 | // additional behavior
17 | @optional
18 | - (void) drawAsPatternInRect:(CGRect)rect;
19 | - (void) drawAtPoint:(CGPoint)point;
20 | - (void) drawAtPoint:(CGPoint)point blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
21 | - (void) drawInRect:(CGRect)rect;
22 | - (void) drawInRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
23 |
24 | @end
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/ImageFilter.h:
--------------------------------------------------------------------------------
1 | //
2 | // ImageFilter.h
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/30/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "ImageComponent.h"
11 | #import "UIImage+ImageComponent.h"
12 |
13 | @interface ImageFilter : NSObject
14 | {
15 | @private
16 | id component_;
17 | }
18 |
19 | @property (nonatomic, retain) id component;
20 |
21 | - (void) apply;
22 | - (id) initWithImageComponent:(id ) component;
23 | - (id) forwardingTargetForSelector:(SEL)aSelector;
24 |
25 | /*
26 | // overridden methods in UIImage APIs
27 | - (void) drawAsPatternInRect:(CGRect)rect;
28 | - (void) drawAtPoint:(CGPoint)point;
29 | - (void) drawAtPoint:(CGPoint)point blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
30 | - (void) drawInRect:(CGRect)rect;
31 | - (void) drawInRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
32 | */
33 |
34 | @end
35 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/ImageFilter.m:
--------------------------------------------------------------------------------
1 | //
2 | // ImageFilter.m
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/30/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "ImageFilter.h"
10 |
11 |
12 | @implementation ImageFilter
13 |
14 | @synthesize component=component_;
15 |
16 |
17 | - (id) initWithImageComponent:(id ) component
18 | {
19 | if (self = [super init])
20 | {
21 | // save an ImageComponent
22 | [self setComponent:component];
23 | }
24 |
25 | return self;
26 | }
27 |
28 | - (void) apply
29 | {
30 | // should be overridden by subclasses
31 | // to apply real filters
32 | }
33 |
34 | - (id) forwardingTargetForSelector:(SEL)aSelector
35 | {
36 | NSString *selectorName = NSStringFromSelector(aSelector);
37 | if ([selectorName hasPrefix:@"draw"])
38 | {
39 | [self apply];
40 | }
41 |
42 | return component_;
43 | }
44 |
45 | /*
46 | - (void) drawAsPatternInRect:(CGRect)rect
47 | {
48 | [self apply];
49 | [component_ drawAsPatternInRect:rect];
50 | }
51 |
52 | - (void) drawAtPoint:(CGPoint)point
53 | {
54 | [self apply];
55 | [component_ drawAtPoint:point];
56 | }
57 |
58 | - (void) drawAtPoint:(CGPoint)point blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha
59 | {
60 | [self apply];
61 | [component_ drawAtPoint:point
62 | blendMode:blendMode
63 | alpha:alpha];
64 | }
65 |
66 | - (void) drawInRect:(CGRect)rect
67 | {
68 | [self apply];
69 | [component_ drawInRect:rect];
70 | }
71 |
72 | - (void) drawInRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha
73 | {
74 | [self apply];
75 | [component_ drawInRect:rect
76 | blendMode:blendMode
77 | alpha:alpha];
78 | }
79 | */
80 |
81 |
82 |
83 | @end
84 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/ImageShadowFilter.h:
--------------------------------------------------------------------------------
1 | //
2 | // ImageShadowFilter.h
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/30/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "ImageFilter.h"
11 |
12 | @interface ImageShadowFilter : ImageFilter
13 | {
14 |
15 | }
16 |
17 | - (void) apply;
18 |
19 | @end
20 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/ImageShadowFilter.m:
--------------------------------------------------------------------------------
1 | //
2 | // ImageShadowFilter.m
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/30/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "ImageShadowFilter.h"
10 |
11 |
12 | @implementation ImageShadowFilter
13 |
14 | - (void) apply
15 | {
16 | CGContextRef context = UIGraphicsGetCurrentContext();
17 |
18 | // set up shadow
19 | CGSize offset = CGSizeMake (-25, 15);
20 | CGContextSetShadow(context, offset, 20.0);
21 | }
22 |
23 | @end
24 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/ImageTransformFilter.h:
--------------------------------------------------------------------------------
1 | //
2 | // ImageTransformFilter.h
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/30/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "ImageFilter.h"
11 |
12 | @interface ImageTransformFilter : ImageFilter
13 | {
14 | @private
15 | CGAffineTransform transform_;
16 | }
17 |
18 | @property (nonatomic, assign) CGAffineTransform transform;
19 |
20 | - (id) initWithImageComponent:(id )component
21 | transform:(CGAffineTransform)transform;
22 | - (void) apply;
23 |
24 | @end
25 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/ImageTransformFilter.m:
--------------------------------------------------------------------------------
1 | //
2 | // ImageTransformFilter.m
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/30/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "ImageTransformFilter.h"
10 |
11 | @implementation ImageTransformFilter
12 |
13 | @synthesize transform=transform_;
14 |
15 |
16 | - (id) initWithImageComponent:(id )component
17 | transform:(CGAffineTransform)transform
18 | {
19 | if (self = [super initWithImageComponent:component])
20 | {
21 | [self setTransform:transform];
22 | }
23 |
24 | return self;
25 | }
26 |
27 | - (void) apply
28 | {
29 | CGContextRef context = UIGraphicsGetCurrentContext();
30 |
31 | // setup transformation
32 | CGContextConcatCTM(context, transform_);
33 | }
34 |
35 | @end
36 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/Images/Image.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/Images/Image.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/UIImage+BaseFilter.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+BaseFilter.h
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface UIImage (BaseFilter)
13 |
14 | - (CGContextRef) beginContext;
15 | - (UIImage *) getImageFromCurrentImageContext;
16 | - (void) endContext;
17 |
18 | @end
19 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/UIImage+BaseFilter.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+BaseFilter.m
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "UIImage+BaseFilter.h"
10 |
11 |
12 | @implementation UIImage (BaseFilter)
13 |
14 | - (CGContextRef) beginContext
15 | {
16 | // Create a graphics context with the target size
17 | // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions
18 | // to take the scale into consideration
19 | // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
20 | CGSize size = [self size];
21 | if (NULL != UIGraphicsBeginImageContextWithOptions)
22 | UIGraphicsBeginImageContextWithOptions(size, NO, 0);
23 | else
24 | UIGraphicsBeginImageContext(size);
25 |
26 | CGContextRef context = UIGraphicsGetCurrentContext();
27 |
28 | return context;
29 | }
30 |
31 | - (UIImage *) getImageFromCurrentImageContext
32 | {
33 | [self drawAtPoint:CGPointZero];
34 |
35 | // Retrieve the UIImage from the current context
36 | UIImage *imageOut = UIGraphicsGetImageFromCurrentImageContext();
37 |
38 | return imageOut;
39 | }
40 |
41 | - (void) endContext
42 | {
43 | UIGraphicsEndImageContext();
44 | }
45 |
46 | @end
47 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/UIImage+ImageComponent.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+ImageComponent.h
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/30/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "ImageComponent.h"
11 |
12 | @interface UIImage (ImageComponent)
13 |
14 | @end
15 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/UIImage+ImageComponent.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+ImageComponent.m
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/30/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "UIImage+ImageComponent.h"
10 |
11 | /*
12 | @implementation UIImage (ImageComponent)
13 |
14 |
15 | @end
16 | */
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/UIImage+Shadow.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+Shadow.h
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface UIImage (Shadow)
13 |
14 | - (UIImage *) imageWithDropShadow;
15 |
16 | @end
17 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/UIImage+Shadow.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+Shadow.m
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "UIImage+Shadow.h"
10 | #import "UIImage+BaseFilter.h"
11 |
12 | @implementation UIImage (Shadow)
13 |
14 |
15 | - (UIImage *) imageWithDropShadow
16 | {
17 | CGContextRef context = [self beginContext];
18 |
19 | // set up shadow
20 | CGSize offset = CGSizeMake (-25, 15);
21 | CGContextSetShadow(context, offset, 20.0);
22 |
23 | // Draw the original image to the context
24 | UIImage * imageOut = [self getImageFromCurrentImageContext];
25 |
26 | [self endContext];
27 |
28 | return imageOut;
29 | }
30 |
31 | @end
32 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/UIImage+Transform.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+Transform.h
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface UIImage (Transform)
13 |
14 | - (UIImage *) imageWithTransform:(CGAffineTransform)transform;
15 |
16 | @end
17 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/UIImage+Transform.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+Transform.m
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "UIImage+Transform.h"
10 | #import "UIImage+BaseFilter.h"
11 |
12 | @implementation UIImage (Transform)
13 |
14 | - (UIImage *) imageWithTransform:(CGAffineTransform)transform
15 | {
16 | CGContextRef context = [self beginContext];
17 |
18 | // setup transformation
19 | CGContextConcatCTM(context, transform);
20 |
21 | // Draw the original image to the context
22 | UIImage *imageOut = [self getImageFromCurrentImageContext];
23 |
24 | [self endContext];
25 |
26 | return imageOut;
27 | }
28 |
29 | @end
30 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch16_Decorator_code/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // Decorator
4 | //
5 | // Created by Carlo Chung on 11/15/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | int main(int argc, char *argv[]) {
12 |
13 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
14 | int retVal = UIApplicationMain(argc, argv, nil, nil);
15 | [pool release];
16 | return retVal;
17 | }
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/Attack.h:
--------------------------------------------------------------------------------
1 | //
2 | // Attack.h
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface Attack : NSObject
13 | {
14 |
15 | }
16 |
17 | @end
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/Attack.m:
--------------------------------------------------------------------------------
1 | //
2 | // Attack.m
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "Attack.h"
10 |
11 |
12 | @implementation Attack
13 |
14 | @end
15 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/AttackHandler.h:
--------------------------------------------------------------------------------
1 | //
2 | // Entity.h
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Attack.h"
11 |
12 | @interface AttackHandler : NSObject
13 | {
14 | @private
15 | AttackHandler *nextAttackHandler_;
16 | }
17 |
18 | @property (nonatomic, retain) AttackHandler *nextAttackHandler;
19 |
20 | - (void) handleAttack:(Attack *)attack;
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/AttackHandler.m:
--------------------------------------------------------------------------------
1 | //
2 | // Entity.m
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "AttackHandler.h"
10 |
11 |
12 | @implementation AttackHandler
13 |
14 | @synthesize nextAttackHandler=nextAttackHandler_;
15 |
16 |
17 | - (void) handleAttack:(Attack *)attack
18 | {
19 | [nextAttackHandler_ handleAttack:attack];
20 | }
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/Avatar.h:
--------------------------------------------------------------------------------
1 | //
2 | // Avatar.h
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AttackHandler.h"
11 |
12 | @interface Avatar : AttackHandler
13 | {
14 |
15 | }
16 |
17 | // overridden method
18 | - (void) handleAttack:(Attack *)attack;
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/Avatar.m:
--------------------------------------------------------------------------------
1 | //
2 | // Avatar.m
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "Avatar.h"
10 |
11 |
12 | @implementation Avatar
13 |
14 | - (void) handleAttack:(Attack *)attack
15 | {
16 | // when an attack reaches this point,
17 | // I'm hit.
18 | // actual points taken off depends on
19 | // the type of attack.
20 | NSLog(@"Oh! I'm hit with a %@!", [attack class]);
21 | }
22 |
23 |
24 | @end
25 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/ChainOfResponsibility-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleDisplayName
8 | ${PRODUCT_NAME}
9 | CFBundleExecutable
10 | ${EXECUTABLE_NAME}
11 | CFBundleIconFile
12 |
13 | CFBundleIdentifier
14 | com.yourcompany.${PRODUCT_NAME:rfc1034identifier}
15 | CFBundleInfoDictionaryVersion
16 | 6.0
17 | CFBundleName
18 | ${PRODUCT_NAME}
19 | CFBundlePackageType
20 | APPL
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | 1.0
25 | LSRequiresIPhoneOS
26 |
27 | NSMainNibFile
28 | MainWindow
29 |
30 |
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/ChainOfResponsibility_Prefix.pch:
--------------------------------------------------------------------------------
1 | //
2 | // Prefix header for all source files of the 'ChainOfResponsibility' target in the 'ChainOfResponsibility' project
3 | //
4 |
5 | #ifdef __OBJC__
6 | #import
7 | #import
8 | #endif
9 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/Classes/ChainOfResponsibilityAppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // ChainOfResponsibilityAppDelegate.h
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class ChainOfResponsibilityViewController;
12 |
13 | @interface ChainOfResponsibilityAppDelegate : NSObject {
14 | UIWindow *window;
15 | ChainOfResponsibilityViewController *viewController;
16 | }
17 |
18 | @property (nonatomic, retain) IBOutlet UIWindow *window;
19 | @property (nonatomic, retain) IBOutlet ChainOfResponsibilityViewController *viewController;
20 |
21 | @end
22 |
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/Classes/ChainOfResponsibilityViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ChainOfResponsibilityViewController.h
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface ChainOfResponsibilityViewController : UIViewController {
12 |
13 | }
14 |
15 | @end
16 |
17 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/CrystalShield.h:
--------------------------------------------------------------------------------
1 | //
2 | // CrystalShield.h
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AttackHandler.h"
11 |
12 | @interface CrystalShield : AttackHandler
13 | {
14 |
15 | }
16 |
17 | // overridden method
18 | - (void) handleAttack:(Attack *)attack;
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/CrystalShield.m:
--------------------------------------------------------------------------------
1 | //
2 | // CrystalShield.m
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "CrystalShield.h"
10 | #import "MagicFireAttack.h"
11 |
12 | @implementation CrystalShield
13 |
14 | - (void) handleAttack:(Attack *)attack
15 | {
16 | if ([attack isKindOfClass:[MagicFireAttack class]])
17 | {
18 | // no damage beyond this shield
19 | NSLog(@"%@", @"No damage from a magic fire attack!");
20 | }
21 | else
22 | {
23 | NSLog(@"I don't know this attack: %@", [attack class]);
24 | [super handleAttack:attack];
25 | }
26 | }
27 |
28 | @end
29 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/LightningAttack.h:
--------------------------------------------------------------------------------
1 | //
2 | // LightningAttack.h
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Attack.h"
11 |
12 | @interface LightningAttack : Attack
13 | {
14 |
15 | }
16 |
17 | @end
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/LightningAttack.m:
--------------------------------------------------------------------------------
1 | //
2 | // LightningAttack.m
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "LightningAttack.h"
10 |
11 |
12 | @implementation LightningAttack
13 |
14 | @end
15 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/MagicFireAttack.h:
--------------------------------------------------------------------------------
1 | //
2 | // MagicFireAttack.h
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Attack.h"
11 |
12 | @interface MagicFireAttack : Attack
13 | {
14 |
15 | }
16 |
17 | @end
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/MagicFireAttack.m:
--------------------------------------------------------------------------------
1 | //
2 | // MagicFireAttack.m
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "MagicFireAttack.h"
10 |
11 |
12 | @implementation MagicFireAttack
13 |
14 | @end
15 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/MetalArmor.h:
--------------------------------------------------------------------------------
1 | //
2 | // MetalArmor.h
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AttackHandler.h"
11 |
12 | @interface MetalArmor : AttackHandler
13 | {
14 |
15 | }
16 |
17 | // overridden method
18 | - (void) handleAttack:(Attack *)attack;
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/MetalArmor.m:
--------------------------------------------------------------------------------
1 | //
2 | // MetalArmor.m
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "MetalArmor.h"
10 | #import "SwordAttack.h"
11 |
12 | @implementation MetalArmor
13 |
14 | - (void) handleAttack:(Attack *)attack
15 | {
16 | if ([attack isKindOfClass:[SwordAttack class]])
17 | {
18 | // no damage beyond this armor
19 | NSLog(@"%@", @"No damage from a sword attack!");
20 | }
21 | else
22 | {
23 | NSLog(@"I don't know this attack: %@", [attack class]);
24 | [super handleAttack:attack];
25 | }
26 | }
27 |
28 | @end
29 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/SwordAttack.h:
--------------------------------------------------------------------------------
1 | //
2 | // SwordAttack.h
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Attack.h"
11 |
12 | @interface SwordAttack : Attack
13 | {
14 |
15 | }
16 |
17 | @end
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/SwordAttack.m:
--------------------------------------------------------------------------------
1 | //
2 | // SwordAttack.m
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "SwordAttack.h"
10 |
11 |
12 | @implementation SwordAttack
13 |
14 | @end
15 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch17_ChainOfResponsibility_code/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // ChainOfResponsibility
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | int main(int argc, char *argv[]) {
12 |
13 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
14 | int retVal = UIApplicationMain(argc, argv, nil, nil);
15 | [pool release];
16 | return retVal;
17 | }
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/Classes/AnySandwich.h:
--------------------------------------------------------------------------------
1 | //
2 | // AbstractSandwich.h
3 | // TemplateMethod
4 | //
5 | // Created by Carlo Chung on 7/31/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface AnySandwich : NSObject
13 | {
14 |
15 | }
16 |
17 | - (void) make;
18 |
19 | // Steps to make a sandwich
20 | - (void) prepareBread;
21 | - (void) putBreadOnPlate;
22 | - (void) addMeat;
23 | - (void) addCondiments;
24 | - (void) extraStep;
25 | - (void) serve;
26 |
27 | @end
28 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/Classes/AnySandwich.m:
--------------------------------------------------------------------------------
1 | //
2 | // AbstractSandwich.m
3 | // TemplateMethod
4 | //
5 | // Created by Carlo Chung on 7/31/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "AnySandwich.h"
10 |
11 |
12 | @implementation AnySandwich
13 |
14 | - (void) make
15 | {
16 | [self prepareBread];
17 | [self putBreadOnPlate];
18 | [self addMeat];
19 | [self addCondiments];
20 | [self extraStep];
21 | [self serve];
22 | }
23 |
24 | - (void) putBreadOnPlate
25 | {
26 | // We need first to put bread on a plate for any sandwich.
27 | }
28 |
29 | - (void) serve
30 | {
31 | // Any sandwich will be served eventually.
32 | }
33 |
34 | #pragma mark -
35 | #pragma Details will be handled by subclasses
36 |
37 | - (void) prepareBread
38 | {
39 |
40 | [NSException raise:NSInternalInconsistencyException
41 | format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)];
42 | }
43 |
44 | - (void) addMeat
45 | {
46 | [NSException raise:NSInternalInconsistencyException
47 | format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)];
48 | }
49 |
50 | - (void) addCondiments
51 | {
52 | [NSException raise:NSInternalInconsistencyException
53 | format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)];
54 | }
55 |
56 | - (void) extraStep{}
57 |
58 | @end
59 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/Classes/Hamburger.h:
--------------------------------------------------------------------------------
1 | //
2 | // Hamburger.h
3 | // TemplateMethod
4 | //
5 | // Created by Carlo Chung on 7/31/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AnySandwich.h"
11 |
12 | @interface Hamburger : AnySandwich
13 | {
14 |
15 | }
16 |
17 | - (void) prepareBread;
18 | - (void) addMeat;
19 | - (void) addCondiments;
20 | //- (void) extraStep;
21 |
22 | // Hamburger specific methods
23 | - (void) getBurgerBun;
24 | - (void) addKetchup;
25 | - (void) addMustard;
26 | - (void) addBeefPatty;
27 | - (void) addCheese;
28 | - (void) addPickles;
29 |
30 | @end
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/Classes/Hamburger.m:
--------------------------------------------------------------------------------
1 | //
2 | // Hamburger.m
3 | // TemplateMethod
4 | //
5 | // Created by Carlo Chung on 7/31/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "Hamburger.h"
10 |
11 |
12 | @implementation Hamburger
13 |
14 | - (void) prepareBread;
15 | {
16 | [self getBurgerBun];
17 | }
18 |
19 | - (void) addMeat
20 | {
21 | [self addBeefPatty];
22 | }
23 |
24 | - (void) addCondiments
25 | {
26 | [self addKetchup];
27 | [self addMustard];
28 | [self addCheese];
29 | [self addPickles];
30 | }
31 |
32 | #pragma mark -
33 | #pragma mark Hamburger Specific Methods
34 |
35 | - (void) getBurgerBun
36 | {
37 | // A hamburger needs a bun.
38 | }
39 |
40 | - (void) addKetchup
41 | {
42 | // Before adding anything to a bun, we need to put ketchup.
43 | }
44 |
45 | - (void) addMustard
46 | {
47 | // Then add some mustard.
48 | }
49 |
50 | - (void) addBeefPatty
51 | {
52 | // A piece of beef patty is the main character in a burger.
53 | }
54 |
55 | - (void) addCheese
56 | {
57 | // Let's just assume every burger has cheese.
58 | }
59 |
60 | - (void) addPickles
61 | {
62 | // Then finally add some pickles to it.
63 | }
64 |
65 | @end
66 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/Classes/Hotdog.h:
--------------------------------------------------------------------------------
1 | //
2 | // Hotdog.h
3 | // TemplateMethod
4 | //
5 | // Created by Carlo Chung on 7/31/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AnySandwich.h"
11 |
12 | @interface Hotdog : AnySandwich
13 | {
14 |
15 | }
16 |
17 | - (void) prepareBread;
18 | - (void) addMeat;
19 | - (void) addCondiments;
20 | //- (void) extraStep;
21 |
22 | // Hotdog specific methods
23 | - (void) getHotdogBun;
24 | - (void) addWiener;
25 | - (void) addKetchup;
26 | - (void) addMustard;
27 | - (void) addOnion;
28 |
29 | @end
30 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/Classes/Hotdog.m:
--------------------------------------------------------------------------------
1 | //
2 | // Hotdog.m
3 | // TemplateMethod
4 | //
5 | // Created by Carlo Chung on 7/31/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "Hotdog.h"
10 |
11 |
12 | @implementation Hotdog
13 |
14 | - (void) prepareBread
15 | {
16 | [self getHotdogBun];
17 | }
18 |
19 | - (void) addMeat
20 | {
21 | [self addWiener];
22 | }
23 |
24 | - (void) addCondiments
25 | {
26 | [self addKetchup];
27 | [self addMustard];
28 | [self addOnion];
29 | }
30 |
31 | #pragma mark -
32 | #pragma mark Hotdog Specific Methods
33 |
34 | - (void) getHotdogBun
35 | {
36 | // First of all, we need a hotdog bun.
37 | }
38 |
39 | - (void) addWiener
40 | {
41 | // A nice piece of wiener is the main character here.
42 | }
43 |
44 | - (void) addKetchup
45 | {
46 | // Every hotdog needs ketchup.
47 | }
48 |
49 | - (void) addMustard
50 | {
51 | // I think mustard is also needed.
52 | }
53 |
54 | - (void) addOnion
55 | {
56 | // I think adding onion is a good idea.
57 | }
58 |
59 | @end
60 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/Classes/ReubenSandwich.h:
--------------------------------------------------------------------------------
1 | //
2 | // ReubenSandwich.h
3 | // TemplateMethod
4 | //
5 | // Created by Carlo Chung on 7/31/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AnySandwich.h"
11 |
12 | @interface ReubenSandwich : AnySandwich
13 | {
14 |
15 | }
16 |
17 | - (void) prepareBread;
18 | - (void) addMeat;
19 | - (void) addCondiments;
20 | - (void) extraStep;
21 |
22 | // ReubenSandwich's specific operations
23 | - (void) cutRyeBread;
24 | - (void) addCornBeef;
25 | - (void) addSauerkraut;
26 | - (void) addThousandIslandDressing;
27 | - (void) addSwissCheese;
28 | - (void) grillIt;
29 |
30 | @end
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/Classes/ReubenSandwich.m:
--------------------------------------------------------------------------------
1 | //
2 | // ReubenSandwich.m
3 | // TemplateMethod
4 | //
5 | // Created by Carlo Chung on 7/31/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "ReubenSandwich.h"
10 |
11 |
12 | @implementation ReubenSandwich
13 |
14 | - (void) prepareBread
15 | {
16 | [self cutRyeBread];
17 | }
18 |
19 | - (void) addMeat
20 | {
21 | [self addCornBeef];
22 | }
23 |
24 | - (void) addCondiments
25 | {
26 | [self addSauerkraut];
27 | [self addThousandIslandDressing];
28 | [self addSwissCheese];
29 | }
30 |
31 | - (void) extraStep
32 | {
33 | [self grillIt];
34 | }
35 |
36 | #pragma mark -
37 | #pragma mark ReubenSandwich Specific Methods
38 |
39 | - (void) cutRyeBread
40 | {
41 | // A Reuben sandwich requires two slices of rye bread
42 | }
43 |
44 | - (void) addCornBeef
45 | {
46 | // ... add tons of corn beef
47 | }
48 |
49 | - (void) addSauerkraut
50 | {
51 | // ... and sauerkraut.
52 | }
53 |
54 | - (void) addThousandIslandDressing
55 | {
56 | // ... don't forget to put Thousand Island dressing
57 | }
58 |
59 | - (void) addSwissCheese
60 | {
61 | // ... as well as some good Swiss cheese.
62 | }
63 |
64 | - (void) grillIt
65 | {
66 | // finally it needs to be toasted.
67 | }
68 |
69 | @end
70 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/Classes/TemplateMethodAppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // TemplateMethodAppDelegate.h
3 | // TemplateMethod
4 | //
5 | // Created by Carlo Chung on 7/31/10.
6 | // Copyright Carlo Chung 2010. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class TemplateMethodViewController;
12 |
13 | @interface TemplateMethodAppDelegate : NSObject
14 | {
15 | UIWindow *window;
16 | TemplateMethodViewController *viewController;
17 | }
18 |
19 | @property (nonatomic, retain) IBOutlet UIWindow *window;
20 | @property (nonatomic, retain) IBOutlet TemplateMethodViewController *viewController;
21 |
22 | @end
23 |
24 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/Classes/TemplateMethodViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // TemplateMethodViewController.h
3 | // TemplateMethod
4 | //
5 | // Created by Carlo Chung on 7/31/10.
6 | // Copyright Carlo Chung 2010. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface TemplateMethodViewController : UIViewController
12 | {
13 |
14 | }
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/Classes/TemplateMethodViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // TemplateMethodViewController.m
3 | // TemplateMethod
4 | //
5 | // Created by Carlo Chung on 7/31/10.
6 | // Copyright Carlo Chung 2010. All rights reserved.
7 | //
8 |
9 | #import "TemplateMethodViewController.h"
10 | #import "AnySandwich.h"
11 |
12 | @implementation TemplateMethodViewController
13 |
14 |
15 |
16 | /*
17 | // The designated initializer. Override to perform setup that is required before the view is loaded.
18 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
19 | if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
20 | // Custom initialization
21 | }
22 | return self;
23 | }
24 | */
25 |
26 | /*
27 | // Implement loadView to create a view hierarchy programmatically, without using a nib.
28 | - (void)loadView {
29 | }
30 | */
31 |
32 |
33 |
34 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
35 | - (void)viewDidLoad
36 | {
37 | [super viewDidLoad];
38 |
39 | // this method call should throw an exception
40 | AnySandwich * sandwich = [[AnySandwich alloc] init];
41 | [sandwich make];
42 | [sandwich release];
43 | }
44 |
45 |
46 |
47 | /*
48 | // Override to allow orientations other than the default portrait orientation.
49 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
50 | // Return YES for supported orientations
51 | return (interfaceOrientation == UIInterfaceOrientationPortrait);
52 | }
53 | */
54 |
55 | - (void)didReceiveMemoryWarning {
56 | // Releases the view if it doesn't have a superview.
57 | [super didReceiveMemoryWarning];
58 |
59 | // Release any cached data, images, etc that aren't in use.
60 | }
61 |
62 | - (void)viewDidUnload {
63 | // Release any retained subviews of the main view.
64 | // e.g. self.myOutlet = nil;
65 | }
66 |
67 |
68 | - (void)dealloc {
69 | [super dealloc];
70 | }
71 |
72 | @end
73 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/TemplateMethod-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleDisplayName
8 | ${PRODUCT_NAME}
9 | CFBundleExecutable
10 | ${EXECUTABLE_NAME}
11 | CFBundleIconFile
12 |
13 | CFBundleIdentifier
14 | com.yourcompany.${PRODUCT_NAME:rfc1034identifier}
15 | CFBundleInfoDictionaryVersion
16 | 6.0
17 | CFBundleName
18 | ${PRODUCT_NAME}
19 | CFBundlePackageType
20 | APPL
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | 1.0
25 | LSRequiresIPhoneOS
26 |
27 | NSMainNibFile
28 | MainWindow
29 |
30 |
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/TemplateMethod_Prefix.pch:
--------------------------------------------------------------------------------
1 | //
2 | // Prefix header for all source files of the 'TemplateMethod' target in the 'TemplateMethod' project
3 | //
4 |
5 | #ifdef __OBJC__
6 | #import
7 | #import
8 | #endif
9 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch18_TemplateMethod_code/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // TemplateMethod
4 | //
5 | // Created by Carlo Chung on 7/31/10.
6 | // Copyright Carlo Chung 2010. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | int main(int argc, char *argv[]) {
12 |
13 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
14 | int retVal = UIApplicationMain(argc, argv, nil, nil);
15 | [pool release];
16 | return retVal;
17 | }
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Classes/AlphaInputValidator.h:
--------------------------------------------------------------------------------
1 | //
2 | // AlphaInputValidator.h
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 8/2/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "InputValidator.h"
11 |
12 |
13 | @interface AlphaInputValidator : InputValidator
14 | {
15 |
16 | }
17 |
18 | // A validation method that makes sure the input only
19 | // contains letters only i.e. a-z A-Z
20 | - (BOOL) validateInput:(UITextField *)input error:(NSError **) error;
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Classes/AlphaInputValidator.m:
--------------------------------------------------------------------------------
1 | //
2 | // AlphaInputValidator.m
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 8/2/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "AlphaInputValidator.h"
10 |
11 | @implementation AlphaInputValidator
12 |
13 |
14 |
15 | - (BOOL) validateInput:(UITextField *)input error:(NSError **) error
16 | {
17 | NSRegularExpression *regex = [NSRegularExpression
18 | regularExpressionWithPattern:@"^[a-zA-Z]*$"
19 | options:NSRegularExpressionAnchorsMatchLines
20 | error:nil];
21 |
22 | NSUInteger numberOfMatches = [regex
23 | numberOfMatchesInString:[input text]
24 | options:NSMatchingAnchored
25 | range:NSMakeRange(0, [[input text] length])];
26 |
27 | // If there is not a single match
28 | // then return an error and NO
29 | if (numberOfMatches == 0)
30 | {
31 | if (error != nil)
32 | {
33 | NSString *description = NSLocalizedString(@"Input Validation Failed", @"");
34 | NSString *reason = NSLocalizedString(@"The input can only contain letters", @"");
35 |
36 | NSArray *objArray = [NSArray arrayWithObjects:description, reason, nil];
37 | NSArray *keyArray = [NSArray arrayWithObjects:NSLocalizedDescriptionKey,
38 | NSLocalizedFailureReasonErrorKey, nil];
39 |
40 | NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:objArray
41 | forKeys:keyArray];
42 | *error = [NSError errorWithDomain:InputValidationErrorDomain
43 | code:1002
44 | userInfo:userInfo];
45 | }
46 |
47 | return NO;
48 | }
49 |
50 | return YES;
51 | }
52 |
53 | @end
54 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Classes/CustomTextField.h:
--------------------------------------------------------------------------------
1 | //
2 | // CustomTextField.h
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 8/2/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "InputValidator.h"
11 |
12 | @interface CustomTextField : UITextField
13 | {
14 | @private
15 | InputValidator *inputValidator_;
16 | }
17 |
18 | @property (nonatomic, retain) IBOutlet InputValidator *inputValidator;
19 |
20 | - (BOOL) validate;
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Classes/CustomTextField.m:
--------------------------------------------------------------------------------
1 | //
2 | // CustomTextField.m
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 8/2/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "CustomTextField.h"
10 |
11 |
12 | @implementation CustomTextField
13 |
14 | @synthesize inputValidator=inputValidator_;
15 |
16 | - (BOOL) validate
17 | {
18 | NSError *error = nil;
19 | BOOL validationResult = [inputValidator_ validateInput:self error:&error];
20 |
21 | if (!validationResult)
22 | {
23 | UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[error localizedDescription]
24 | message:[error localizedFailureReason]
25 | delegate:nil
26 | cancelButtonTitle:NSLocalizedString(@"OK", @"")
27 | otherButtonTitles:nil];
28 | [alertView show];
29 | [alertView release];
30 | }
31 |
32 | return validationResult;
33 | }
34 |
35 | - (void) dealloc
36 | {
37 | [inputValidator_ release];
38 | [super dealloc];
39 | }
40 |
41 | @end
42 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Classes/InputValidator.h:
--------------------------------------------------------------------------------
1 | //
2 | // InputValidator.h
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 2/15/11.
6 | // Copyright 2011 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | static NSString * const InputValidationErrorDomain = @"InputValidationErrorDomain";
12 |
13 | @interface InputValidator : NSObject
14 | {
15 |
16 | }
17 |
18 | // A stub for any actual validation strategy
19 | - (BOOL) validateInput:(UITextField *)input error:(NSError **) error;
20 |
21 | @end
22 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Classes/InputValidator.m:
--------------------------------------------------------------------------------
1 | //
2 | // InputValidator.m
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 2/15/11.
6 | // Copyright 2011 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "InputValidator.h"
10 |
11 |
12 | @implementation InputValidator
13 |
14 | // A stub for any actual validation strategy
15 | - (BOOL) validateInput:(UITextField *)input error:(NSError **) error
16 | {
17 | if (error)
18 | {
19 | *error = nil;
20 | }
21 |
22 | return NO;
23 | }
24 |
25 | @end
26 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Classes/NumericInputValidator.h:
--------------------------------------------------------------------------------
1 | //
2 | // NumberInputValidator.h
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 8/2/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "InputValidator.h"
11 |
12 | @interface NumericInputValidator : InputValidator
13 | {
14 |
15 | }
16 |
17 | // A validation method that makes sure the input only contains
18 | // numbers i.e. 0-9
19 | - (BOOL) validateInput:(UITextField *)input error:(NSError **) error;
20 |
21 | @end
22 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Classes/NumericInputValidator.m:
--------------------------------------------------------------------------------
1 | //
2 | // NumberInputValidator.m
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 8/2/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "NumericInputValidator.h"
10 |
11 |
12 | @implementation NumericInputValidator
13 |
14 | - (BOOL) validateInput:(UITextField *)input error:(NSError**) error
15 | {
16 | NSError *regError = nil;
17 | NSRegularExpression *regex = [NSRegularExpression
18 | regularExpressionWithPattern:@"^[0-9]*$"
19 | options:NSRegularExpressionAnchorsMatchLines
20 | error:®Error];
21 |
22 | NSUInteger numberOfMatches = [regex
23 | numberOfMatchesInString:[input text]
24 | options:NSMatchingAnchored
25 | range:NSMakeRange(0, [[input text] length])];
26 |
27 | // if there is not a single match
28 | // then return an error and NO
29 | if (numberOfMatches == 0)
30 | {
31 | if (error != nil)
32 | {
33 | NSString *description = NSLocalizedString(@"Input Validation Failed", @"");
34 | NSString *reason = NSLocalizedString(@"The input can only contain numerical values", @"");
35 |
36 | NSArray *objArray = [NSArray arrayWithObjects:description, reason, nil];
37 | NSArray *keyArray = [NSArray arrayWithObjects:NSLocalizedDescriptionKey,
38 | NSLocalizedFailureReasonErrorKey, nil];
39 |
40 | NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:objArray
41 | forKeys:keyArray];
42 | *error = [NSError errorWithDomain:InputValidationErrorDomain
43 | code:1001
44 | userInfo:userInfo];
45 | }
46 |
47 | return NO;
48 | }
49 |
50 | return YES;
51 | }
52 |
53 | @end
54 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Classes/StrategyAppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // StrategyAppDelegate.h
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 8/2/10.
6 | // Copyright Carlo Chung 2010. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class StrategyViewController;
12 |
13 | @interface StrategyAppDelegate : NSObject
14 | {
15 | UIWindow *window;
16 | StrategyViewController *viewController;
17 | }
18 |
19 | @property (nonatomic, retain) IBOutlet UIWindow *window;
20 | @property (nonatomic, retain) IBOutlet StrategyViewController *viewController;
21 |
22 | @end
23 |
24 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Classes/StrategyAppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // StrategyAppDelegate.m
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 8/2/10.
6 | // Copyright Carlo Chung 2010. All rights reserved.
7 | //
8 |
9 | #import "StrategyAppDelegate.h"
10 | #import "StrategyViewController.h"
11 |
12 | @implementation StrategyAppDelegate
13 |
14 | @synthesize window;
15 | @synthesize viewController;
16 |
17 |
18 | #pragma mark -
19 | #pragma mark Application lifecycle
20 |
21 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
22 |
23 | // Override point for customization after application launch.
24 |
25 | // Add the view controller's view to the window and display.
26 | [window addSubview:viewController.view];
27 | [window makeKeyAndVisible];
28 |
29 | return YES;
30 | }
31 |
32 |
33 | - (void)applicationWillResignActive:(UIApplication *)application {
34 | /*
35 | 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.
36 | Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
37 | */
38 | }
39 |
40 |
41 | - (void)applicationDidEnterBackground:(UIApplication *)application {
42 | /*
43 | 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.
44 | If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
45 | */
46 | }
47 |
48 |
49 | - (void)applicationWillEnterForeground:(UIApplication *)application {
50 | /*
51 | Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
52 | */
53 | }
54 |
55 |
56 | - (void)applicationDidBecomeActive:(UIApplication *)application {
57 | /*
58 | 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.
59 | */
60 | }
61 |
62 |
63 | - (void)applicationWillTerminate:(UIApplication *)application {
64 | /*
65 | Called when the application is about to terminate.
66 | See also applicationDidEnterBackground:.
67 | */
68 | }
69 |
70 |
71 | #pragma mark -
72 | #pragma mark Memory management
73 |
74 | - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
75 | /*
76 | Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
77 | */
78 | }
79 |
80 |
81 | - (void)dealloc {
82 | [viewController release];
83 | [window release];
84 | [super dealloc];
85 | }
86 |
87 |
88 | @end
89 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Classes/StrategyViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // StrategyViewController.h
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 8/2/10.
6 | // Copyright Carlo Chung 2010. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "NumericInputValidator.h"
11 | #import "AlphaInputValidator.h"
12 | #import "CustomTextField.h"
13 |
14 | @interface StrategyViewController : UIViewController
15 | {
16 | @private
17 | CustomTextField *numericTextField_;
18 | CustomTextField *alphaTextField_;
19 | }
20 |
21 | @property (nonatomic, retain) IBOutlet CustomTextField *numericTextField;
22 | @property (nonatomic, retain) IBOutlet CustomTextField *alphaTextField;
23 |
24 | @end
25 |
26 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Classes/StrategyViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // StrategyViewController.m
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 8/2/10.
6 | // Copyright Carlo Chung 2010. All rights reserved.
7 | //
8 |
9 | #import "StrategyViewController.h"
10 |
11 |
12 | @implementation StrategyViewController
13 |
14 | @synthesize numericTextField, alphaTextField;
15 |
16 |
17 | /*
18 | // The designated initializer. Override to perform setup that is required before the view is loaded.
19 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
20 | if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
21 | // Custom initialization
22 | }
23 | return self;
24 | }
25 | */
26 |
27 | /*
28 | // Implement loadView to create a view hierarchy programmatically, without using a nib.
29 | - (void)loadView {
30 | }
31 | */
32 |
33 | /*
34 |
35 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
36 | - (void)viewDidLoad
37 | {
38 | [super viewDidLoad];
39 |
40 | id numValidator = [[NumericInputValidator alloc] init];
41 | id alphaValidator = [[AlphaInputValidator alloc] init];
42 |
43 | [numericTextField setInputValidator:numValidator];
44 | [alphaTextField setInputValidator:alphaValidator];
45 |
46 | [numValidator release];
47 | [alphaValidator release];
48 | }
49 | */
50 |
51 | /*
52 | // Override to allow orientations other than the default portrait orientation.
53 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
54 | // Return YES for supported orientations
55 | return (interfaceOrientation == UIInterfaceOrientationPortrait);
56 | }
57 | */
58 |
59 | - (void)didReceiveMemoryWarning {
60 | // Releases the view if it doesn't have a superview.
61 | [super didReceiveMemoryWarning];
62 |
63 | // Release any cached data, images, etc that aren't in use.
64 | }
65 |
66 | - (void)viewDidUnload {
67 | // Release any retained subviews of the main view.
68 | // e.g. self.myOutlet = nil;
69 | }
70 |
71 |
72 | - (void)dealloc
73 | {
74 | [numericTextField_ release];
75 | [alphaTextField_ release];
76 | [super dealloc];
77 |
78 | }
79 |
80 | #pragma mark -
81 | #pragma mark UITextFieldDelegate methods
82 |
83 | - (void)textFieldDidEndEditing:(UITextField *)textField
84 | {
85 | //if (textField == numericTextField)
86 | //{
87 | // validate [textField text] and make sure
88 | // the value is numeric
89 | //}
90 | //else if (textField == alphaTextField)
91 | //{
92 | // validate [textField text] and make sure
93 | // the value contains only letters
94 | //}
95 |
96 | if ([textField isKindOfClass:[CustomTextField class]])
97 | {
98 | [(CustomTextField*)textField validate];
99 | }
100 | }
101 |
102 | @end
103 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Strategy-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleDisplayName
8 | ${PRODUCT_NAME}
9 | CFBundleExecutable
10 | ${EXECUTABLE_NAME}
11 | CFBundleIconFile
12 |
13 | CFBundleIdentifier
14 | com.yourcompany.${PRODUCT_NAME:rfc1034identifier}
15 | CFBundleInfoDictionaryVersion
16 | 6.0
17 | CFBundleName
18 | ${PRODUCT_NAME}
19 | CFBundlePackageType
20 | APPL
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | 1.0
25 | LSRequiresIPhoneOS
26 |
27 | NSMainNibFile
28 | MainWindow
29 |
30 |
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/Strategy_Prefix.pch:
--------------------------------------------------------------------------------
1 | //
2 | // Prefix header for all source files of the 'Strategy' target in the 'Strategy' project
3 | //
4 |
5 | #ifdef __OBJC__
6 | #import
7 | #import
8 | #endif
9 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch19_Strategy_code/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // Strategy
4 | //
5 | // Created by Carlo Chung on 8/2/10.
6 | // Copyright Carlo Chung 2010. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | int main(int argc, char *argv[]) {
12 |
13 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
14 | int retVal = UIApplicationMain(argc, argv, nil, nil);
15 | [pool release];
16 | return retVal;
17 | }
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/Classes/ExtrinsicFlowerState.h:
--------------------------------------------------------------------------------
1 | /*
2 | * ExtrinsicFlowerState.h
3 | * Flyweight
4 | *
5 | * Created by Carlo Chung on 3/2/11.
6 | * Copyright 2011 Carlo Chung. All rights reserved.
7 | *
8 | */
9 |
10 | typedef struct
11 | {
12 | UIView *flowerView;
13 | CGRect area;
14 | } ExtrinsicFlowerState;
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/Classes/FlyweightAppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // FlyweightAppDelegate.h
3 | // Flyweight
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class FlyweightViewController;
12 |
13 | @interface FlyweightAppDelegate : NSObject
14 | {
15 | UIWindow *window;
16 | FlyweightViewController *viewController;
17 | }
18 |
19 | @property (nonatomic, retain) IBOutlet UIWindow *window;
20 | @property (nonatomic, retain) IBOutlet FlyweightViewController *viewController;
21 |
22 | @end
23 |
24 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/Classes/FlyweightAppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // FlyweightAppDelegate.m
3 | // Flyweight
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "FlyweightAppDelegate.h"
10 | #import "FlyweightViewController.h"
11 |
12 | @implementation FlyweightAppDelegate
13 |
14 | @synthesize window;
15 | @synthesize viewController;
16 |
17 |
18 | #pragma mark -
19 | #pragma mark Application lifecycle
20 |
21 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
22 |
23 | // Override point for customization after application launch.
24 |
25 | // Add the view controller's view to the window and display.
26 | [window addSubview:viewController.view];
27 | [window makeKeyAndVisible];
28 |
29 | return YES;
30 | }
31 |
32 |
33 | - (void)applicationWillResignActive:(UIApplication *)application {
34 | /*
35 | 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.
36 | Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
37 | */
38 | }
39 |
40 |
41 | - (void)applicationDidEnterBackground:(UIApplication *)application {
42 | /*
43 | 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.
44 | If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
45 | */
46 | }
47 |
48 |
49 | - (void)applicationWillEnterForeground:(UIApplication *)application {
50 | /*
51 | Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
52 | */
53 | }
54 |
55 |
56 | - (void)applicationDidBecomeActive:(UIApplication *)application {
57 | /*
58 | 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.
59 | */
60 | }
61 |
62 |
63 | - (void)applicationWillTerminate:(UIApplication *)application {
64 | /*
65 | Called when the application is about to terminate.
66 | See also applicationDidEnterBackground:.
67 | */
68 | }
69 |
70 |
71 | #pragma mark -
72 | #pragma mark Memory management
73 |
74 | - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
75 | /*
76 | Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
77 | */
78 | }
79 |
80 |
81 | - (void)dealloc {
82 | [viewController release];
83 | [window release];
84 | [super dealloc];
85 | }
86 |
87 |
88 | @end
89 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/Classes/FlyweightView.h:
--------------------------------------------------------------------------------
1 | //
2 | // FlyweightView.h
3 | // Flyweight
4 | //
5 | // Created by Carlo Chung on 11/29/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface FlyweightView : UIView
12 | {
13 | @private
14 | NSArray *flowerList_;
15 | }
16 |
17 | @property (nonatomic, retain) NSArray *flowerList;
18 |
19 | @end
20 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/Classes/FlyweightView.m:
--------------------------------------------------------------------------------
1 | //
2 | // FlyweightView.m
3 | // Flyweight
4 | //
5 | // Created by Carlo Chung on 11/29/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "FlyweightView.h"
10 | #import "ExtrinsicFlowerState.h"
11 |
12 | @implementation FlyweightView
13 |
14 | @synthesize flowerList=flowerList_;
15 |
16 | extern NSString *FlowerObjectKey, *FlowerLocationKey;
17 |
18 | // Only override drawRect: if you perform custom drawing.
19 | // An empty implementation adversely affects performance during animation.
20 | - (void)drawRect:(CGRect)rect
21 | {
22 | // Drawing code
23 |
24 | for (NSValue *stateValue in flowerList_)
25 | {
26 | ExtrinsicFlowerState state;
27 | [stateValue getValue:&state];
28 |
29 | UIView *flowerView = state.flowerView;
30 | CGRect area = state.area;
31 |
32 | [flowerView drawRect:area];
33 | }
34 | }
35 |
36 |
37 | - (void)dealloc
38 | {
39 | [flowerList_ release];
40 | [super dealloc];
41 | }
42 |
43 |
44 | @end
45 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/Classes/FlyweightViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // FlyweightViewController.h
3 | // Flyweight
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "FlyweightView.h"
11 |
12 | @interface FlyweightViewController : UIViewController
13 | {
14 |
15 | }
16 |
17 | @end
18 |
19 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/Classes/FlyweightViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // FlyweightViewController.m
3 | // Flyweight
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "FlyweightViewController.h"
10 | #import "FlowerFactory.h"
11 | #import "ExtrinsicFlowerState.h"
12 |
13 |
14 | @implementation FlyweightViewController
15 |
16 |
17 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
18 | - (void)viewDidLoad
19 | {
20 | [super viewDidLoad];
21 |
22 | // construct a flower list
23 | FlowerFactory *factory = [[[FlowerFactory alloc] init] autorelease];
24 | NSMutableArray *flowerList = [[[NSMutableArray alloc]
25 | initWithCapacity:500] autorelease];
26 |
27 | for (int i = 0; i < 500; ++i)
28 | {
29 | // retrieve a shared instance
30 | // of a flower flyweight object
31 | // from a flower factory with a
32 | // random flower type
33 | FlowerType flowerType = arc4random() % kTotalNumberOfFlowerTypes;
34 | UIView *flowerView = [factory flowerViewWithType:flowerType];
35 |
36 | // set up a location and an area for the flower
37 | // to display onscreen
38 | CGRect screenBounds = [[UIScreen mainScreen] bounds];
39 | CGFloat x = (arc4random() % (NSInteger)screenBounds.size.width);
40 | CGFloat y = (arc4random() % (NSInteger)screenBounds.size.height);
41 | NSInteger minSize = 10;
42 | NSInteger maxSize = 50;
43 | CGFloat size = (arc4random() % (maxSize - minSize + 1)) + minSize;
44 |
45 | // assign attributes for a flower
46 | // to an extrinsic state object
47 | ExtrinsicFlowerState extrinsicState;
48 | extrinsicState.flowerView = flowerView;
49 | extrinsicState.area = CGRectMake(x, y, size, size);
50 |
51 | // add an extrinsic flower state
52 | // to the flower list
53 | [flowerList addObject:[NSValue value:&extrinsicState
54 | withObjCType:@encode(ExtrinsicFlowerState)]];
55 | }
56 |
57 | // add the flower list to
58 | // this FlyweightView instance
59 | [(FlyweightView *)self.view setFlowerList:flowerList];
60 | }
61 |
62 |
63 | - (void)didReceiveMemoryWarning
64 | {
65 | // Releases the view if it doesn't have a superview.
66 | [super didReceiveMemoryWarning];
67 |
68 | // Release any cached data, images, etc that aren't in use.
69 | }
70 |
71 | - (void)viewDidUnload
72 | {
73 | // Release any retained subviews of the main view.
74 | // e.g. self.myOutlet = nil;
75 | }
76 |
77 | - (void)dealloc
78 | {
79 | [super dealloc];
80 | }
81 |
82 | @end
83 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/FlowerFactory.h:
--------------------------------------------------------------------------------
1 | //
2 | // FlowerFactory.h
3 | // Flyweight
4 | //
5 | // Created by Carlo Chung on 11/29/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | typedef enum
12 | {
13 | kAnemone,
14 | kCosmos,
15 | kGerberas,
16 | kHollyhock,
17 | kJasmine,
18 | kZinnia,
19 | kTotalNumberOfFlowerTypes
20 | } FlowerType;
21 |
22 | @interface FlowerFactory : NSObject
23 | {
24 | @private
25 | NSMutableDictionary *flowerPool_;
26 | }
27 |
28 | - (UIView *) flowerViewWithType:(FlowerType)type;
29 |
30 | @end
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/FlowerFactory.m:
--------------------------------------------------------------------------------
1 | //
2 | // FlowerFactory.m
3 | // Flyweight
4 | //
5 | // Created by Carlo Chung on 11/29/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "FlowerFactory.h"
10 | #import "FlowerView.h"
11 |
12 | @implementation FlowerFactory
13 |
14 |
15 | - (UIView *) flowerViewWithType:(FlowerType)type
16 | {
17 | // lazy-load a flower pool
18 | if (flowerPool_ == nil)
19 | {
20 | flowerPool_ = [[NSMutableDictionary alloc]
21 | initWithCapacity:kTotalNumberOfFlowerTypes];
22 | }
23 |
24 | // try to retrieve a flower
25 | // from the pool
26 | UIView *flowerView = [flowerPool_ objectForKey:[NSNumber
27 | numberWithInt:type]];
28 |
29 | // if the type requested
30 | // is not available then
31 | // create a new one and
32 | // add it to the pool
33 | if (flowerView == nil)
34 | {
35 | UIImage *flowerImage;
36 |
37 | switch (type)
38 | {
39 | case kAnemone:
40 | flowerImage = [UIImage imageNamed:@"anemone.png"];
41 | break;
42 | case kCosmos:
43 | flowerImage = [UIImage imageNamed:@"cosmos.png"];
44 | break;
45 | case kGerberas:
46 | flowerImage = [UIImage imageNamed:@"gerberas.png"];
47 | break;
48 | case kHollyhock:
49 | flowerImage = [UIImage imageNamed:@"hollyhock.png"];
50 | break;
51 | case kJasmine:
52 | flowerImage = [UIImage imageNamed:@"jasmine.png"];
53 | break;
54 | case kZinnia:
55 | flowerImage = [UIImage imageNamed:@"zinnia.png"];
56 | break;
57 | default:
58 | break;
59 | }
60 |
61 | flowerView = [[[FlowerView alloc]
62 | initWithImage:flowerImage] autorelease];
63 | [flowerPool_ setObject:flowerView
64 | forKey:[NSNumber numberWithInt:type]];
65 | }
66 |
67 | return flowerView;
68 | }
69 |
70 | - (void) dealloc
71 | {
72 | [flowerPool_ release];
73 | [super dealloc];
74 | }
75 |
76 | @end
77 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/FlowerView.h:
--------------------------------------------------------------------------------
1 | //
2 | // Flower.h
3 | // Flyweight
4 | //
5 | // Created by Carlo Chung on 11/29/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface FlowerView : UIImageView
13 | {
14 |
15 | }
16 |
17 | - (void) drawRect:(CGRect)rect;
18 |
19 | @end
20 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/FlowerView.m:
--------------------------------------------------------------------------------
1 | //
2 | // Flower.m
3 | // Flyweight
4 | //
5 | // Created by Carlo Chung on 11/29/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "FlowerView.h"
10 |
11 | @implementation FlowerView
12 |
13 | - (void) drawRect:(CGRect)rect
14 | {
15 | [self.image drawInRect:rect];
16 | }
17 |
18 | @end
19 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/Flyweight-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleDisplayName
8 | ${PRODUCT_NAME}
9 | CFBundleExecutable
10 | ${EXECUTABLE_NAME}
11 | CFBundleIconFile
12 |
13 | CFBundleIdentifier
14 | com.yourcompany.${PRODUCT_NAME:rfc1034identifier}
15 | CFBundleInfoDictionaryVersion
16 | 6.0
17 | CFBundleName
18 | ${PRODUCT_NAME}
19 | CFBundlePackageType
20 | APPL
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | 1.0
25 | LSRequiresIPhoneOS
26 |
27 | NSMainNibFile
28 | MainWindow
29 |
30 |
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/Flyweight_Prefix.pch:
--------------------------------------------------------------------------------
1 | //
2 | // Prefix header for all source files of the 'Flyweight' target in the 'Flyweight' project
3 | //
4 |
5 | #ifdef __OBJC__
6 | #import
7 | #import
8 | #endif
9 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/images/anemone.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/images/anemone.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/images/cosmos.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/images/cosmos.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/images/gerberas.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/images/gerberas.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/images/hollyhock.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/images/hollyhock.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/images/jasmine.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/images/jasmine.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/images/zinnia.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/images/zinnia.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_Ch21_Flyweight_code/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // Flyweight
4 | //
5 | // Created by Carlo Chung on 11/28/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | int main(int argc, char *argv[]) {
12 |
13 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
14 | int retVal = UIApplicationMain(argc, argv, nil, nil);
15 | [pool release];
16 | return retVal;
17 | }
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/CanvasView.h:
--------------------------------------------------------------------------------
1 | //
2 | // CanvasView.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 9/14/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @protocol Mark;
12 |
13 | @interface CanvasView : UIView
14 | {
15 | @private
16 | id mark_; // the main stroke structure
17 | }
18 |
19 | @property (nonatomic, retain) id mark;
20 |
21 | @end
22 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/CanvasView.m:
--------------------------------------------------------------------------------
1 | //
2 | // CanvasView.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 9/14/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "CanvasView.h"
10 | #import "MarkRenderer.h"
11 |
12 | @implementation CanvasView
13 |
14 | @synthesize mark=mark_;
15 |
16 |
17 | - (id)initWithFrame:(CGRect)frame
18 | {
19 | if ((self = [super initWithFrame:frame]))
20 | {
21 | // Initialization code
22 | [self setBackgroundColor:[UIColor whiteColor]];
23 | }
24 | return self;
25 | }
26 |
27 |
28 | // Only override drawRect: if you perform custom drawing.
29 | // An empty implementation adversely affects performance during animation.
30 | - (void)drawRect:(CGRect)rect
31 | {
32 |
33 | // Drawing code
34 | CGContextRef context = UIGraphicsGetCurrentContext();
35 |
36 | // create a renderer visitor
37 | MarkRenderer *markRenderer = [[[MarkRenderer alloc] initWithCGContext:context] autorelease];
38 |
39 | // pass this renderer along the mark composite structure
40 | [mark_ acceptMarkVisitor:markRenderer];
41 |
42 | }
43 |
44 |
45 | - (void)dealloc
46 | {
47 | [mark_ release];
48 | [super dealloc];
49 | }
50 |
51 | @end
52 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/CanvasViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // CanvasViewController.h
3 | // Composite
4 | //
5 | // Created by Carlo Chung on 9/11/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Scribble.h"
11 | #import "CanvasView.h"
12 | #import "CanvasViewGenerator.h"
13 | #import "CommandBarButton.h"
14 | #import "NSMutableArray+Stack.h"
15 |
16 | @interface CanvasViewController : UIViewController
17 | {
18 | @private
19 | Scribble *scribble_;
20 | CanvasView *canvasView_;
21 |
22 | CGPoint startPoint_;
23 | UIColor *strokeColor_;
24 | CGFloat strokeSize_;
25 | }
26 |
27 | @property (nonatomic, retain) CanvasView *canvasView;
28 | @property (nonatomic, retain) Scribble *scribble;
29 | @property (nonatomic, retain) UIColor *strokeColor;
30 | @property (nonatomic, assign) CGFloat strokeSize;
31 |
32 | - (void) loadCanvasViewWithGenerator:(CanvasViewGenerator *)generator;
33 |
34 | - (IBAction) onBarButtonHit:(id) button;
35 | - (IBAction) onCustomBarButtonHit:(CommandBarButton *)barButton;
36 |
37 | - (NSInvocation *) drawScribbleInvocation;
38 | - (NSInvocation *) undrawScribbleInvocation;
39 |
40 | - (void) executeInvocation:(NSInvocation *)invocation withUndoInvocation:(NSInvocation *)undoInvocation;
41 | - (void) unexecuteInvocation:(NSInvocation *)invocation withRedoInvocation:(NSInvocation *)redoInvocation;
42 |
43 | @end
44 |
45 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/CanvasViewGenerator.h:
--------------------------------------------------------------------------------
1 | //
2 | // CanvasViewGenerator.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/16/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "CanvasView.h"
11 |
12 | @interface CanvasViewGenerator : NSObject
13 | {
14 |
15 | }
16 |
17 | - (CanvasView *) canvasViewWithFrame:(CGRect) aFrame;
18 |
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/CanvasViewGenerator.m:
--------------------------------------------------------------------------------
1 | //
2 | // CanvasViewGenerator.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/16/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "CanvasViewGenerator.h"
10 |
11 |
12 | @implementation CanvasViewGenerator
13 |
14 | - (CanvasView *) canvasViewWithFrame:(CGRect) aFrame
15 | {
16 | return [[[CanvasView alloc] initWithFrame:aFrame] autorelease];
17 | }
18 |
19 | @end
20 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ClothCanvasView.h:
--------------------------------------------------------------------------------
1 | //
2 | // ClothCanvasView.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/16/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "CanvasView.h"
11 |
12 | @interface ClothCanvasView : CanvasView
13 | {
14 |
15 | // some private variables.
16 | }
17 |
18 | // some other specific behaviors.
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ClothCanvasView.m:
--------------------------------------------------------------------------------
1 | //
2 | // ClothCanvasView.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/16/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "ClothCanvasView.h"
10 |
11 |
12 | @implementation ClothCanvasView
13 |
14 |
15 | - (id)initWithFrame:(CGRect)frame
16 | {
17 | if ((self = [super initWithFrame:frame]))
18 | {
19 | // Add a cloth image view on top
20 | // as the canvas background
21 | UIImage *backgroundImage = [UIImage imageNamed:@"cloth"];
22 | UIImageView *backgroundView = [[[UIImageView alloc]
23 | initWithImage:backgroundImage]
24 | autorelease];
25 | [self addSubview:backgroundView];
26 | }
27 |
28 | return self;
29 | }
30 |
31 | // implementation for other behaviors
32 |
33 | @end
34 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ClothCanvasViewGenerator.h:
--------------------------------------------------------------------------------
1 | //
2 | // ClothCanvasViewGenerator.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/16/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "CanvasViewGenerator.h"
11 | #import "ClothCanvasView.h"
12 |
13 | @interface ClothCanvasViewGenerator : CanvasViewGenerator
14 | {
15 |
16 | }
17 |
18 | - (CanvasView *) canvasViewWithFrame:(CGRect) aFrame;
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ClothCanvasViewGenerator.m:
--------------------------------------------------------------------------------
1 | //
2 | // ClothCanvasViewGenerator.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/16/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "ClothCanvasViewGenerator.h"
10 |
11 |
12 | @implementation ClothCanvasViewGenerator
13 |
14 | - (CanvasView *) canvasViewWithFrame:(CGRect) aFrame
15 | {
16 | return [[[ClothCanvasView alloc] initWithFrame:aFrame] autorelease];
17 | }
18 |
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/Command.h:
--------------------------------------------------------------------------------
1 | //
2 | // Command.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/19/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface Command : NSObject
13 | {
14 | @protected
15 | NSDictionary *userInfo_;
16 | }
17 |
18 | @property (nonatomic, retain) NSDictionary *userInfo;
19 |
20 | - (void) execute;
21 | - (void) undo;
22 |
23 | @end
24 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/Command.m:
--------------------------------------------------------------------------------
1 | //
2 | // Command.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/19/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "Command.h"
10 |
11 |
12 | @implementation Command
13 | @synthesize userInfo=userInfo_;
14 |
15 | - (void) execute
16 | {
17 | // should throw an exception.
18 | }
19 |
20 | - (void) undo
21 | {
22 | // do nothing
23 | // subclasses need to override this
24 | // method to perform actual undo.
25 | }
26 |
27 | - (void) dealloc
28 | {
29 | [userInfo_ release];
30 | [super dealloc];
31 | }
32 |
33 | @end
34 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/CommandBarButton.h:
--------------------------------------------------------------------------------
1 | //
2 | // CustomBarButton.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/19/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Command.h"
11 |
12 | @interface CommandBarButton : UIBarButtonItem
13 | {
14 | @protected
15 | Command *command_;
16 | }
17 |
18 | @property (nonatomic, retain) IBOutlet Command *command;
19 |
20 | @end
21 |
22 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/CommandBarButton.m:
--------------------------------------------------------------------------------
1 | //
2 | // CustomBarButton.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/19/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "CommandBarButton.h"
10 |
11 | @implementation CommandBarButton
12 |
13 | @synthesize command=command_;
14 |
15 |
16 | - (void) dealloc
17 | {
18 | [command_ release];
19 | [super dealloc];
20 | }
21 |
22 | @end
23 |
24 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/CommandSlider.h:
--------------------------------------------------------------------------------
1 | //
2 | // CommandSlider.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Command.h"
11 |
12 | @interface CommandSlider : UISlider
13 | {
14 | @protected
15 | Command *command_;
16 | }
17 |
18 | @property (nonatomic, retain) IBOutlet Command *command;
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/CommandSlider.m:
--------------------------------------------------------------------------------
1 | //
2 | // CommandSlider.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "CommandSlider.h"
10 |
11 |
12 | @implementation CommandSlider
13 |
14 | @synthesize command=command_;
15 |
16 | - (void) dealloc
17 | {
18 | [command_ release];
19 | [super dealloc];
20 | }
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/CoordinatingController.h:
--------------------------------------------------------------------------------
1 | //
2 | // CoordinatingController.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/18/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "CanvasViewController.h"
11 | #import "PaletteViewController.h"
12 | #import "ThumbnailViewController.h"
13 |
14 | typedef enum
15 | {
16 | kButtonTagDone,
17 | kButtonTagOpenPaletteView,
18 | kButtonTagOpenThumbnailView
19 | } ButtonTag;
20 |
21 | @interface CoordinatingController : NSObject
22 | {
23 | @private
24 | CanvasViewController *canvasViewController_;
25 | UIViewController *activeViewController_;
26 | }
27 |
28 | @property (nonatomic, readonly) UIViewController *activeViewController;
29 | @property (nonatomic, readonly) CanvasViewController *canvasViewController;
30 |
31 | + (CoordinatingController *) sharedInstance;
32 |
33 | - (IBAction) requestViewChangeByObject:(id)object;
34 |
35 | @end
36 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/DeleteScribbleCommand.h:
--------------------------------------------------------------------------------
1 | //
2 | // DeleteScribbleCommand.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/8/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Command.h"
11 |
12 | @interface DeleteScribbleCommand : Command
13 | {
14 |
15 | }
16 |
17 | - (void) execute;
18 |
19 | @end
20 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/DeleteScribbleCommand.m:
--------------------------------------------------------------------------------
1 | //
2 | // DeleteScribbleCommand.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/8/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "DeleteScribbleCommand.h"
10 | #import "CoordinatingController.h"
11 | #import "CanvasViewController.h"
12 |
13 | @implementation DeleteScribbleCommand
14 |
15 | - (void) execute
16 | {
17 | // get a hold of the current
18 | // CanvasViewController from
19 | // the CoordinatingController
20 | CoordinatingController *coordinatingController = [CoordinatingController sharedInstance];
21 | CanvasViewController *canvasViewController = [coordinatingController canvasViewController];
22 |
23 | // create a new scribble for
24 | // canvasViewController
25 | Scribble *newScribble = [[[Scribble alloc] init] autorelease];
26 | [canvasViewController setScribble:newScribble];
27 | }
28 |
29 |
30 | @end
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/Dot.h:
--------------------------------------------------------------------------------
1 | //
2 | // Dot.h
3 | // Composite
4 | //
5 | // Created by Carlo Chung on 9/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Vertex.h"
11 |
12 | @protocol MarkVisitor;
13 |
14 | @interface Dot : Vertex
15 | {
16 | @private
17 | UIColor *color_;
18 | CGFloat size_;
19 | }
20 |
21 | @property (nonatomic, retain) UIColor *color;
22 | @property (nonatomic, assign) CGFloat size;
23 |
24 | // for the Visitor pattern
25 | - (void) acceptMarkVisitor:(id )visitor;
26 |
27 | // for the Prototype pattern
28 | - (id) copyWithZone:(NSZone *)zone;
29 |
30 | // for the Memento pattern
31 | - (id)initWithCoder:(NSCoder *)coder;
32 | - (void)encodeWithCoder:(NSCoder *)coder;
33 |
34 | @end
35 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/Dot.m:
--------------------------------------------------------------------------------
1 | //
2 | // Dot.m
3 | // Composite
4 | //
5 | // Created by Carlo Chung on 9/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "Dot.h"
10 |
11 |
12 | @implementation Dot
13 | @synthesize size=size_, color=color_;
14 |
15 | - (void) acceptMarkVisitor:(id )visitor
16 | {
17 | [visitor visitDot:self];
18 | }
19 |
20 | - (void) dealloc
21 | {
22 | [color_ release];
23 | [super dealloc];
24 | }
25 |
26 | #pragma mark -
27 | #pragma mark NSCopying method
28 |
29 | // it needs to be implemented for memento
30 | - (id)copyWithZone:(NSZone *)zone
31 | {
32 | Dot *dotCopy = [[[self class] allocWithZone:zone] initWithLocation:location_];
33 |
34 | // copy the color
35 | [dotCopy setColor:[UIColor colorWithCGColor:[color_ CGColor]]];
36 |
37 | // copy the size
38 | [dotCopy setSize:size_];
39 |
40 | return dotCopy;
41 | }
42 |
43 |
44 | #pragma mark -
45 | #pragma mark NSCoder methods
46 |
47 | - (id)initWithCoder:(NSCoder *)coder
48 | {
49 | if (self = [super initWithCoder:coder])
50 | {
51 | color_ = [[coder decodeObjectForKey:@"DotColor"] retain];
52 | size_ = [coder decodeFloatForKey:@"DotSize"];
53 | }
54 |
55 | return self;
56 | }
57 |
58 | - (void)encodeWithCoder:(NSCoder *)coder
59 | {
60 | [super encodeWithCoder:coder];
61 | [coder encodeObject:color_ forKey:@"DotColor"];
62 | [coder encodeFloat:size_ forKey:@"DotSize"];
63 | }
64 |
65 | #pragma mark -
66 | #pragma mark An Extended Direct-draw Example
67 |
68 | // for a direct draw example
69 | - (void) drawWithContext:(CGContextRef)context
70 | {
71 | CGFloat x = self.location.x;
72 | CGFloat y = self.location.y;
73 | CGFloat frameSize = self.size;
74 | CGRect frame = CGRectMake(x - frameSize / 2.0,
75 | y - frameSize / 2.0,
76 | frameSize,
77 | frameSize);
78 |
79 | CGContextSetFillColorWithColor (context,[self.color CGColor]);
80 | CGContextFillEllipseInRect(context, frame);
81 | }
82 |
83 | @end
84 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/Mark.h:
--------------------------------------------------------------------------------
1 | //
2 | // Mark.h
3 | // Composite
4 | //
5 | // Created by Carlo Chung on 9/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "MarkVisitor.h"
11 |
12 |
13 | @protocol Mark
14 |
15 | @property (nonatomic, retain) UIColor *color;
16 | @property (nonatomic, assign) CGFloat size;
17 | @property (nonatomic, assign) CGPoint location;
18 | @property (nonatomic, readonly) NSUInteger count;
19 | @property (nonatomic, readonly) id lastChild;
20 |
21 | - (id) copy;
22 | - (void) addMark:(id ) mark;
23 | - (void) removeMark:(id ) mark;
24 | - (id ) childMarkAtIndex:(NSUInteger) index;
25 |
26 | // for the Visitor pattern
27 | - (void) acceptMarkVisitor:(id ) visitor;
28 |
29 | // for the Iterator pattern
30 | - (NSEnumerator *) enumerator;
31 |
32 | // for internal iterator implementation
33 | - (void) enumerateMarksUsingBlock:(void (^)(id item, BOOL *stop)) block;
34 |
35 | // for a bad example
36 | - (void) drawWithContext:(CGContextRef) context;
37 |
38 | @end
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/MarkEnumerator+Internal.h:
--------------------------------------------------------------------------------
1 | //
2 | // MarkEnumerator+Private.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 1/6/11.
6 | // Copyright 2011 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface MarkEnumerator ()
13 |
14 | - (id) initWithMark:(id )mark;
15 | - (void) traverseAndBuildStackWithMark:(id )mark;
16 |
17 | @end
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/MarkEnumerator.h:
--------------------------------------------------------------------------------
1 | //
2 | // MarkEnumerator.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 1/6/11.
6 | // Copyright 2011 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "NSMutableArray+Stack.h"
11 | #import "Mark.h"
12 |
13 | @interface MarkEnumerator : NSEnumerator
14 | {
15 | @private
16 | NSMutableArray *stack_;
17 | }
18 |
19 | - (NSArray *)allObjects;
20 | - (id)nextObject;
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/MarkEnumerator.m:
--------------------------------------------------------------------------------
1 | //
2 | // MarkEnumerator.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 1/6/11.
6 | // Copyright 2011 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "MarkEnumerator.h"
10 | #import "MarkEnumerator+Internal.h"
11 |
12 | @implementation MarkEnumerator
13 |
14 | - (NSArray *)allObjects
15 | {
16 | // returns an array of yet-visited Mark nodes
17 | // i.e. the remaining elements in the stack
18 | return [[stack_ reverseObjectEnumerator] allObjects];
19 | }
20 |
21 | - (id)nextObject
22 | {
23 | return [stack_ pop];
24 | }
25 |
26 | - (void) dealloc
27 | {
28 | [stack_ release];
29 | [super dealloc];
30 | }
31 |
32 | #pragma mark -
33 | #pragma mark Private Methods
34 |
35 | - (id) initWithMark:(id )aMark
36 | {
37 | if (self = [super init])
38 | {
39 | stack_ = [[NSMutableArray alloc] initWithCapacity:[aMark count]];
40 |
41 | // post-orderly traverse the whole Mark aggregate
42 | // and add individual Marks in a private stack
43 | [self traverseAndBuildStackWithMark:aMark];
44 | }
45 |
46 | return self;
47 | }
48 |
49 | - (void) traverseAndBuildStackWithMark:(id )mark
50 | {
51 | // push post-order traversal
52 | // into the stack
53 | if (mark == nil) return;
54 |
55 | [stack_ push:mark];
56 |
57 | NSUInteger index = [mark count];
58 | id childMark;
59 | while (childMark = [mark childMarkAtIndex:--index])
60 | {
61 | [self traverseAndBuildStackWithMark:childMark];
62 | }
63 | }
64 |
65 | @end
66 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/MarkRenderer.h:
--------------------------------------------------------------------------------
1 | //
2 | // MarkRenderer.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 9/14/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "MarkVisitor.h"
11 | #import "Dot.h"
12 | #import "Vertex.h"
13 | #import "Stroke.h"
14 |
15 | @interface MarkRenderer : NSObject
16 | {
17 | @private
18 | BOOL shouldMoveContextToDot_;
19 |
20 | @protected
21 | CGContextRef context_;
22 | }
23 |
24 | - (id) initWithCGContext:(CGContextRef)context;
25 |
26 | - (void) visitMark:(id )mark;
27 | - (void) visitDot:(Dot *)dot;
28 | - (void) visitVertex:(Vertex *)vertex;
29 | - (void) visitStroke:(Stroke *)stroke;
30 |
31 | @end
32 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/MarkRenderer.m:
--------------------------------------------------------------------------------
1 | //
2 | // MarkRenderer.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 9/14/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "MarkRenderer.h"
10 |
11 |
12 | @implementation MarkRenderer
13 |
14 | - (id) initWithCGContext:(CGContextRef)context
15 | {
16 | if (self = [super init])
17 | {
18 | context_ = context;
19 | shouldMoveContextToDot_ = YES;
20 | }
21 |
22 | return self;
23 | }
24 |
25 | - (void) visitMark:(id )mark
26 | {
27 | // default behavior
28 | }
29 |
30 | - (void) visitDot:(Dot *)dot
31 | {
32 | CGFloat x = [dot location].x;
33 | CGFloat y = [dot location].y;
34 | CGFloat frameSize = [dot size];
35 | CGRect frame = CGRectMake(x - frameSize / 2.0,
36 | y - frameSize / 2.0,
37 | frameSize,
38 | frameSize);
39 |
40 | CGContextSetFillColorWithColor (context_,[[dot color] CGColor]);
41 | CGContextFillEllipseInRect(context_, frame);
42 | }
43 |
44 | - (void) visitVertex:(Vertex *)vertex
45 | {
46 | CGFloat x = [vertex location].x;
47 | CGFloat y = [vertex location].y;
48 |
49 | if (shouldMoveContextToDot_)
50 | {
51 | CGContextMoveToPoint(context_, x, y);
52 | shouldMoveContextToDot_ = NO;
53 | }
54 | else
55 | {
56 | CGContextAddLineToPoint(context_, x, y);
57 | }
58 | }
59 |
60 | - (void) visitStroke:(Stroke *)stroke
61 | {
62 | CGContextSetStrokeColorWithColor (context_,[[stroke color] CGColor]);
63 | CGContextSetLineWidth(context_, [stroke size]);
64 | CGContextSetLineCap(context_, kCGLineCapRound);
65 | CGContextStrokePath(context_);
66 | shouldMoveContextToDot_ = YES;
67 | }
68 |
69 |
70 | @end
71 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/MarkVisitor.h:
--------------------------------------------------------------------------------
1 | //
2 | // MarkVisitor.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 9/14/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @protocol Mark;
13 | @class Dot, Vertex, Stroke;
14 |
15 | @protocol MarkVisitor
16 |
17 | - (void) visitMark:(id )mark;
18 | - (void) visitDot:(Dot *)dot;
19 | - (void) visitVertex:(Vertex *)vertex;
20 | - (void) visitStroke:(Stroke *)stroke;
21 |
22 | @end
23 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/NSMutableArray+Stack.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSMutableArray+Private.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/11/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface NSMutableArray (Stack)
13 |
14 | - (void) push:(id)object;
15 | - (id) pop;
16 | - (void) dropBottom;
17 |
18 | @end
19 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/NSMutableArray+Stack.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSMutableArray+Private.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/11/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "NSMutableArray+Stack.h"
10 |
11 |
12 | @implementation NSMutableArray (Stack)
13 |
14 | - (void) push:(id)object
15 | {
16 | if (object != nil)
17 | [self addObject:object];
18 | }
19 |
20 | - (id) pop
21 | {
22 | if ([self count] == 0) return nil;
23 |
24 | id object = [[[self lastObject] retain] autorelease];
25 | [self removeLastObject];
26 |
27 | return object;
28 | }
29 |
30 | - (void) dropBottom
31 | {
32 | if ([self count] == 0) return;
33 |
34 | [self removeObjectAtIndex:0];
35 | }
36 |
37 | @end
38 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/OpenScribbleCommand.h:
--------------------------------------------------------------------------------
1 | //
2 | // OpenScribbleCommand.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Command.h"
11 | #import "ScribbleSource.h"
12 |
13 | @interface OpenScribbleCommand : Command
14 | {
15 | @private
16 | id scribbleSource_;
17 | }
18 |
19 | @property (nonatomic, retain) id scribbleSource;
20 |
21 | - (id) initWithScribbleSource:(id ) aScribbleSource;
22 | - (void) execute;
23 |
24 | @end
25 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/OpenScribbleCommand.m:
--------------------------------------------------------------------------------
1 | //
2 | // OpenScribbleCommand.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "OpenScribbleCommand.h"
10 | #import "CoordinatingController.h"
11 | #import "CanvasViewController.h"
12 |
13 | @implementation OpenScribbleCommand
14 |
15 | @synthesize scribbleSource=scribbleSource_;
16 |
17 | - (id) initWithScribbleSource:(id ) aScribbleSource
18 | {
19 | if (self = [super init])
20 | {
21 | [self setScribbleSource:aScribbleSource];
22 | }
23 |
24 | return self;
25 | }
26 |
27 | - (void) execute
28 | {
29 | // get a scribble from the scribbleSource_
30 | Scribble *scribble = [scribbleSource_ scribble];
31 |
32 | // set it to the current CanvasViewController
33 | CoordinatingController *coordinator = [CoordinatingController sharedInstance];
34 | CanvasViewController *controller = [coordinator canvasViewController];
35 | [controller setScribble:scribble];
36 |
37 | // then tell the coordinator to change views
38 | [coordinator requestViewChangeByObject:self];
39 | }
40 |
41 | @end
42 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/PaletteViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // PaletteViewController.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/18/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "CommandBarButton.h"
11 | #import "CommandSlider.h"
12 | #import "SetStrokeColorCommand.h"
13 | #import "SetStrokeSizeCommand.h"
14 |
15 | @interface PaletteViewController : UIViewController
16 |
18 | {
19 | @private
20 | IBOutlet CommandSlider *redSlider_;
21 | IBOutlet CommandSlider *greenSlider_;
22 | IBOutlet CommandSlider *blueSlider_;
23 | IBOutlet CommandSlider *sizeSlider_;
24 | IBOutlet UIView *paletteView_;
25 | }
26 |
27 | // slider event handler
28 | - (IBAction) onCommandSliderValueChanged:(CommandSlider *)slider;
29 |
30 | @end
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/PaperCanvasView.h:
--------------------------------------------------------------------------------
1 | //
2 | // PaperCanvasView.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/16/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "CanvasView.h"
11 |
12 | @interface PaperCanvasView : CanvasView
13 | {
14 | // some private variables.
15 | }
16 |
17 | // some other specific behaviors.
18 |
19 | @end
20 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/PaperCanvasView.m:
--------------------------------------------------------------------------------
1 | //
2 | // PaperCanvasView.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/16/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "PaperCanvasView.h"
10 |
11 |
12 | @implementation PaperCanvasView
13 |
14 |
15 | - (id)initWithFrame:(CGRect)frame
16 | {
17 | if ((self = [super initWithFrame:frame]))
18 | {
19 | // Add a paper image view on top
20 | // as the canvas background
21 | UIImage *backgroundImage = [UIImage imageNamed:@"paper"];
22 | UIImageView *backgroundView = [[[UIImageView alloc]
23 | initWithImage:backgroundImage]
24 | autorelease];
25 | [self addSubview:backgroundView];
26 | }
27 |
28 | return self;
29 | }
30 |
31 | // implementation for other behaviors
32 |
33 | @end
34 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/PaperCanvasViewGenerator.h:
--------------------------------------------------------------------------------
1 | //
2 | // PaperCanvasViewGenerator.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/16/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "CanvasViewGenerator.h"
11 | #import "PaperCanvasView.h"
12 |
13 | @interface PaperCanvasViewGenerator : CanvasViewGenerator
14 | {
15 |
16 | }
17 |
18 | - (CanvasView *) canvasViewWithFrame:(CGRect) aFrame;
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/PaperCanvasViewGenerator.m:
--------------------------------------------------------------------------------
1 | //
2 | // PaperCanvasViewGenerator.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/16/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "PaperCanvasViewGenerator.h"
10 |
11 |
12 | @implementation PaperCanvasViewGenerator
13 |
14 | - (CanvasView *) canvasViewWithFrame:(CGRect) aFrame
15 | {
16 | return [[[PaperCanvasView alloc] initWithFrame:aFrame] autorelease];
17 | }
18 |
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/PrototypeController.h:
--------------------------------------------------------------------------------
1 | //
2 | // PrototypeController.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/20/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface PrototypeController : UIViewController
13 | {
14 |
15 | }
16 |
17 | @end
18 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/PrototypeController.m:
--------------------------------------------------------------------------------
1 | //
2 | // PrototypeController.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/20/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "PrototypeController.h"
10 | #import "Mark.h"
11 | #import "CanvasView.h"
12 |
13 | @implementation PrototypeController
14 |
15 | /*
16 | // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
17 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
18 | if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
19 | // Custom initialization
20 | }
21 | return self;
22 | }
23 | */
24 |
25 | /*
26 | // Implement loadView to create a view hierarchy programmatically, without using a nib.
27 | - (void)loadView {
28 | }
29 | */
30 |
31 |
32 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
33 | - (void)viewDidLoad
34 | {
35 | [super viewDidLoad];
36 |
37 | // The following code snippets are
38 | // for illustration purposes in
39 | // the book only and not part of the
40 | // app
41 | id selectedMark;
42 | NSMutableArray *templateArray = [[NSMutableArray alloc] initWithCapacity:1];
43 | id patternTemplate = [selectedMark copy];
44 |
45 | // save the patternTemplate in
46 | // a data structure so it can be
47 | // used later
48 | [templateArray addObject:patternTemplate];
49 |
50 | CanvasView *canvasView;
51 | id currentMark;
52 | int patternIndex;
53 |
54 | id patternClone = [templateArray objectAtIndex:patternIndex];
55 | [currentMark addMark:patternClone];
56 | [canvasView setMark:currentMark];
57 | [canvasView setNeedsDisplay];
58 | }
59 |
60 |
61 | /*
62 | // Override to allow orientations other than the default portrait orientation.
63 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
64 | // Return YES for supported orientations
65 | return (interfaceOrientation == UIInterfaceOrientationPortrait);
66 | }
67 | */
68 |
69 | - (void)didReceiveMemoryWarning {
70 | // Releases the view if it doesn't have a superview.
71 | [super didReceiveMemoryWarning];
72 |
73 | // Release any cached data, images, etc that aren't in use.
74 | }
75 |
76 | - (void)viewDidUnload {
77 | [super viewDidUnload];
78 | // Release any retained subviews of the main view.
79 | // e.g. self.myOutlet = nil;
80 | }
81 |
82 |
83 | - (void)dealloc {
84 | [super dealloc];
85 | }
86 |
87 |
88 | @end
89 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/SaveScribbleCommand.h:
--------------------------------------------------------------------------------
1 | //
2 | // SaveScribbleCommand.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/22/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Command.h"
11 |
12 | @interface SaveScribbleCommand : Command
13 | {
14 |
15 | }
16 |
17 | - (void) execute;
18 |
19 | @end
20 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/SaveScribbleCommand.m:
--------------------------------------------------------------------------------
1 | //
2 | // SaveScribbleCommand.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/22/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "SaveScribbleCommand.h"
10 | #import "ScribbleManager.h"
11 | #import "CoordinatingController.h"
12 | #import "UIView+UIImage.h"
13 |
14 | @implementation SaveScribbleCommand
15 |
16 | - (void) execute
17 | {
18 | // get a hold of all necessary information
19 | // from an instance of CanvasViewController
20 | // for saving its Scribble
21 | CoordinatingController *coordinatingController = [CoordinatingController sharedInstance];
22 | CanvasViewController *canvasViewController = [coordinatingController canvasViewController];
23 | UIImage *canvasViewImage = [[canvasViewController canvasView] image];
24 | Scribble *scribble = [canvasViewController scribble];
25 |
26 | // use an instance of ScribbleManager
27 | // to save the scribble and its thumbnail
28 | ScribbleManager *scribbleManager = [[[ScribbleManager alloc] init] autorelease];
29 | [scribbleManager saveScribble:scribble thumbnail:canvasViewImage];
30 |
31 | // finally show an alertbox that says
32 | // after the scribble is saved
33 | UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Your scribble is saved"
34 | message:nil
35 | delegate:nil
36 | cancelButtonTitle:@"OK"
37 | otherButtonTitles:nil];
38 | [alertView show];
39 | [alertView release];
40 | }
41 |
42 | @end
43 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/Scribble.h:
--------------------------------------------------------------------------------
1 | //
2 | // Scribble.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 9/20/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Mark.h"
11 | #import "ScribbleMemento.h"
12 |
13 | @interface Scribble : NSObject
14 | {
15 | @private
16 | id parentMark_;
17 | id incrementalMark_;
18 | }
19 |
20 | // methods for Mark management
21 | - (void) addMark:(id )aMark shouldAddToPreviousMark:(BOOL)shouldAddToPreviousMark;
22 | - (void) removeMark:(id )aMark;
23 |
24 | // methods for memento
25 | - (id) initWithMemento:(ScribbleMemento *)aMemento;
26 | + (Scribble *) scribbleWithMemento:(ScribbleMemento *)aMemento;
27 | - (ScribbleMemento *) scribbleMemento;
28 | - (ScribbleMemento *) scribbleMementoWithCompleteSnapshot:(BOOL)hasCompleteSnapshot;
29 | - (void) attachStateFromMemento:(ScribbleMemento *)memento;
30 |
31 | @end
32 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ScribbleManager.h:
--------------------------------------------------------------------------------
1 | //
2 | // ScribbleManager.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 9/20/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Scribble.h"
11 | #import "ScribbleThumbnailViewImageProxy.h"
12 |
13 | @interface ScribbleManager : NSObject
14 | {
15 |
16 | }
17 |
18 | - (void) saveScribble:(Scribble *)scribble thumbnail:(UIImage *)image;
19 | - (NSInteger) numberOfScribbles;
20 | - (Scribble *) scribbleAtIndex:(NSInteger)index;
21 | - (UIView *) scribbleThumbnailViewAtIndex:(NSInteger)index;
22 |
23 | @end
24 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ScribbleMemento+Friend.h:
--------------------------------------------------------------------------------
1 | //
2 | // ScribbleMemento+Private.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 9/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Mark.h"
11 | #import"ScribbleMemento.h"
12 |
13 | @interface ScribbleMemento ()
14 |
15 | - (id) initWithMark:(id )aMark;
16 |
17 | @property (nonatomic, copy) id mark;
18 | @property (nonatomic, assign) BOOL hasCompleteSnapshot;
19 |
20 | @end
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ScribbleMemento.h:
--------------------------------------------------------------------------------
1 | //
2 | // ScirbbleMemento.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 9/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Mark.h"
11 |
12 |
13 | @interface ScribbleMemento : NSObject
14 | {
15 | @private
16 | id mark_;
17 | BOOL hasCompleteSnapshot_;
18 | }
19 |
20 | + (ScribbleMemento *) mementoWithData:(NSData *)data;
21 | - (NSData *) data;
22 |
23 | @end
24 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ScribbleMemento.m:
--------------------------------------------------------------------------------
1 | //
2 | // ScirbbleMemento.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 9/27/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "ScribbleMemento.h"
10 | #import "ScribbleMemento+Friend.h"
11 |
12 | @implementation ScribbleMemento
13 |
14 | @synthesize mark=mark_;
15 | @synthesize hasCompleteSnapshot=hasCompleteSnapshot_;
16 |
17 | - (NSData *) data
18 | {
19 | NSData *data = [NSKeyedArchiver archivedDataWithRootObject:mark_];
20 | return data;
21 | }
22 |
23 | + (ScribbleMemento *) mementoWithData:(NSData *)data
24 | {
25 | // It raises an NSInvalidArchiveOperationException if data is not a valid archive
26 | id retoredMark = (id )[NSKeyedUnarchiver unarchiveObjectWithData:data];
27 | ScribbleMemento *memento = [[[ScribbleMemento alloc]
28 | initWithMark:retoredMark] autorelease];
29 |
30 | return memento;
31 | }
32 |
33 | - (void) dealloc
34 | {
35 | [mark_ release];
36 | [super dealloc];
37 | }
38 |
39 | #pragma mark -
40 | #pragma mark Private methods
41 |
42 | - (id) initWithMark:(id )aMark
43 | {
44 | if (self = [super init])
45 | {
46 | [self setMark:aMark];
47 | }
48 |
49 | return self;
50 | }
51 |
52 | @end
53 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ScribbleSource.h:
--------------------------------------------------------------------------------
1 | //
2 | // ScribbleSource.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Scribble.h"
11 |
12 | @protocol ScribbleSource
13 |
14 | - (Scribble *) scribble;
15 |
16 | @end
17 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ScribbleThumbnailCell.h:
--------------------------------------------------------------------------------
1 | //
2 | // ScribbleThumbnailCell.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/18/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "ScribbleThumbnailView.h"
11 |
12 | @interface ScribbleThumbnailCell : UITableViewCell
13 | {
14 |
15 | }
16 |
17 | + (NSInteger) numberOfPlaceHolders;
18 | - (void) addThumbnailView:(UIView *)thumbnailView
19 | atIndex:(NSInteger)index;
20 |
21 | @end
22 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ScribbleThumbnailCell.m:
--------------------------------------------------------------------------------
1 | //
2 | // ScribbleThumbnailCell.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/18/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "ScribbleThumbnailCell.h"
10 |
11 |
12 | @implementation ScribbleThumbnailCell
13 |
14 | - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
15 | {
16 | if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
17 | {
18 | // Initialization code
19 | [self setBackgroundColor:[UIColor clearColor]];
20 | }
21 |
22 | return self;
23 | }
24 |
25 |
26 | - (void)setSelected:(BOOL)selected animated:(BOOL)animated
27 | {
28 | //[super setSelected:selected animated:animated];
29 |
30 | // Configure the view for the selected state
31 | }
32 |
33 | + (NSInteger) numberOfPlaceHolders
34 | {
35 | return 3;
36 | }
37 |
38 |
39 | - (void) addThumbnailView:(UIView *)thumbnailView
40 | atIndex:(NSInteger)index
41 | {
42 |
43 | if (index == 0)
44 | {
45 | for (UIView *view in [[self contentView] subviews])
46 | {
47 | [view removeFromSuperview];
48 | }
49 | }
50 |
51 | if (index < [ScribbleThumbnailCell numberOfPlaceHolders])
52 | {
53 | CGFloat x = index *90 + (index + 1) *12;
54 | CGFloat y = 10;
55 | CGFloat width = 90;
56 | CGFloat height = 130;
57 |
58 | [thumbnailView setFrame:CGRectMake(x, y, width, height)];
59 |
60 | [self.contentView addSubview:thumbnailView];
61 | }
62 | }
63 |
64 | - (void)dealloc
65 | {
66 |
67 | [super dealloc];
68 | }
69 |
70 |
71 | @end
72 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ScribbleThumbnailView.h:
--------------------------------------------------------------------------------
1 | //
2 | // ScribbleThumbnail.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/18/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Scribble.h"
11 | #import "ScribbleSource.h"
12 |
13 | @interface ScribbleThumbnailView : UIView
14 | {
15 | @protected
16 | NSString *imagePath_;
17 | NSString *scribblePath_;
18 | }
19 |
20 | @property (nonatomic, readonly) UIImage *image;
21 | @property (nonatomic, readonly) Scribble *scribble;
22 | @property (nonatomic, copy) NSString *imagePath;
23 | @property (nonatomic, copy) NSString *scribblePath;
24 |
25 | @end
26 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ScribbleThumbnailView.m:
--------------------------------------------------------------------------------
1 | //
2 | // ScribbleThumbnail.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/18/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "ScribbleThumbnailView.h"
10 |
11 |
12 | @implementation ScribbleThumbnailView
13 |
14 | @dynamic image;
15 | @dynamic scribble;
16 | @synthesize imagePath=imagePath_;
17 | @synthesize scribblePath=scribblePath_;
18 |
19 | @end
20 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ScribbleThumbnailViewImageProxy.h:
--------------------------------------------------------------------------------
1 | //
2 | // ScribbleThumbnailProxy.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/3/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "ScribbleThumbnailView.h"
11 | #import "Command.h"
12 |
13 | @interface ScribbleThumbnailViewImageProxy : ScribbleThumbnailView
14 | {
15 | @private
16 | Scribble *scribble_;
17 | Command *touchCommand_;
18 | UIImage *realImage_;
19 | BOOL loadingThreadHasLaunched_;
20 | }
21 |
22 | @property (nonatomic, readonly) UIImage *image;
23 | @property (nonatomic, readonly) Scribble *scribble;
24 | @property (nonatomic, retain) Command *touchCommand;
25 |
26 | @end
27 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/SetStrokeColorCommand.h:
--------------------------------------------------------------------------------
1 | //
2 | // StrokeColorCommand.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Command.h"
11 |
12 | typedef void (^RGBValuesProvider)(CGFloat *red, CGFloat *green, CGFloat *blue);
13 | typedef void (^PostColorUpdateProvider)(UIColor *color);
14 |
15 | @class SetStrokeColorCommand;
16 |
17 | @protocol SetStrokeColorCommandDelegate
18 |
19 | - (void) command:(SetStrokeColorCommand *) command
20 | didRequestColorComponentsForRed:(CGFloat *) red
21 | green:(CGFloat *) green
22 | blue:(CGFloat *) blue;
23 |
24 | - (void) command:(SetStrokeColorCommand *) command
25 | didFinishColorUpdateWithColor:(UIColor *) color;
26 |
27 | @end
28 |
29 |
30 | @interface SetStrokeColorCommand : Command
31 | {
32 | @private
33 | id delegate_;
34 | RGBValuesProvider RGBValuesProvider_;
35 | PostColorUpdateProvider postColorUpdateProvider_;
36 | }
37 |
38 | @property (nonatomic, assign) id delegate;
39 | @property (nonatomic, copy) RGBValuesProvider RGBValuesProvider;
40 | @property (nonatomic, copy) PostColorUpdateProvider postColorUpdateProvider;
41 |
42 | - (void) execute;
43 |
44 | @end
45 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/SetStrokeColorCommand.m:
--------------------------------------------------------------------------------
1 | //
2 | // StrokeColorCommand.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "SetStrokeColorCommand.h"
10 | #import "CoordinatingController.h"
11 | #import "CanvasViewController.h"
12 |
13 | @implementation SetStrokeColorCommand
14 |
15 | @synthesize delegate=delegate_;
16 | @synthesize postColorUpdateProvider=postColorUpdateProvider_;
17 | @synthesize RGBValuesProvider=RGBValuesProvider_;
18 |
19 |
20 | - (void) execute
21 | {
22 | CGFloat redValue = 0.0;
23 | CGFloat greenValue = 0.0;
24 | CGFloat blueValue = 0.0;
25 |
26 | // Retrieve RGB values from a delegate or a block
27 |
28 | // Delegation (object adapter) approach:
29 | [delegate_ command:self didRequestColorComponentsForRed:&redValue
30 | green:&greenValue
31 | blue:&blueValue];
32 |
33 | // Block approach:
34 | if (RGBValuesProvider_ != nil)
35 | {
36 | RGBValuesProvider_(&redValue, &greenValue, &blueValue);
37 | }
38 |
39 | // Create a color object based on the RGB values
40 | UIColor *color = [UIColor colorWithRed:redValue
41 | green:greenValue
42 | blue:blueValue
43 | alpha:1.0];
44 |
45 | // Assign it to the current canvasViewController
46 | CoordinatingController *coordinator = [CoordinatingController sharedInstance];
47 | CanvasViewController *controller = [coordinator canvasViewController];
48 | [controller setStrokeColor:color];
49 |
50 | // Forward a post update message
51 |
52 | // Delegation approach:
53 | [delegate_ command:self didFinishColorUpdateWithColor:color];
54 |
55 | // Block approach:
56 | if (postColorUpdateProvider_ != nil)
57 | {
58 | postColorUpdateProvider_(color);
59 | }
60 | }
61 |
62 | - (void) dealloc
63 | {
64 | [RGBValuesProvider_ release];
65 | [postColorUpdateProvider_ release];
66 | [super dealloc];
67 | }
68 |
69 | @end
70 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/SetStrokeSizeCommand.h:
--------------------------------------------------------------------------------
1 | //
2 | // StrokeSizeCommand.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Command.h"
11 |
12 | @class SetStrokeSizeCommand;
13 |
14 | @protocol SetStrokeSizeCommandDelegate
15 |
16 | - (void) command:(SetStrokeSizeCommand *)command
17 | didRequestForStrokeSize:(CGFloat *)size;
18 |
19 | @end
20 |
21 |
22 | @interface SetStrokeSizeCommand : Command
23 | {
24 | @private
25 | id delegate_;
26 | }
27 |
28 | @property (nonatomic, assign) id delegate;
29 |
30 | - (void) execute;
31 |
32 | @end
33 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/SetStrokeSizeCommand.m:
--------------------------------------------------------------------------------
1 | //
2 | // StrokeSizeCommand.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 11/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "SetStrokeSizeCommand.h"
10 | #import "CoordinatingController.h"
11 | #import "CanvasViewController.h"
12 |
13 | @implementation SetStrokeSizeCommand
14 |
15 | @synthesize delegate=delegate_;
16 |
17 | - (void) execute
18 | {
19 | // get the current stroke size
20 | // from whatever it's my delegate
21 | CGFloat strokeSize = 1;
22 | [delegate_ command:self didRequestForStrokeSize:&strokeSize];
23 |
24 | // get a hold of the current
25 | // canvasViewController from
26 | // the coordinatingController
27 | // (see the Mediator pattern chapter
28 | // for details)
29 | CoordinatingController *coordinator = [CoordinatingController sharedInstance];
30 | CanvasViewController *controller = [coordinator canvasViewController];
31 |
32 | // assign the stroke size to
33 | // the canvasViewController
34 | [controller setStrokeSize:strokeSize];
35 | }
36 |
37 | @end
38 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/Stroke.h:
--------------------------------------------------------------------------------
1 | //
2 | // Stroke.h
3 | // Composite
4 | //
5 | // Created by Carlo Chung on 9/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Mark.h"
11 | #import "MarkEnumerator.h"
12 |
13 | @protocol MarkVisitor;
14 |
15 | @interface Stroke : NSObject
16 | {
17 | @private
18 | UIColor *color_;
19 | CGFloat size_;
20 | NSMutableArray *children_;
21 | }
22 |
23 | @property (nonatomic, retain) UIColor *color;
24 | @property (nonatomic, assign) CGFloat size;
25 | @property (nonatomic, assign) CGPoint location;
26 | @property (nonatomic, readonly) NSUInteger count;
27 | @property (nonatomic, readonly) id lastChild;
28 |
29 | - (void) addMark:(id ) mark;
30 | - (void) removeMark:(id ) mark;
31 | - (id ) childMarkAtIndex:(NSUInteger) index;
32 |
33 | // for the Visitor pattern
34 | - (void) acceptMarkVisitor:(id )visitor;
35 |
36 | // for the Prototype pattern
37 | - (id) copyWithZone:(NSZone *)zone;
38 |
39 | // for the Iterator pattern
40 | - (NSEnumerator *) enumerator;
41 |
42 | // for internal iterator implementation
43 | - (void) enumerateMarksUsingBlock:(void (^)(id item, BOOL *stop)) block;
44 |
45 | // for the Memento pattern
46 | - (id)initWithCoder:(NSCoder *)coder;
47 | - (void)encodeWithCoder:(NSCoder *)coder;
48 |
49 | @end
50 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/ThumbnailViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ScribblesViewController.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/18/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "ScribbleThumbnailCell.h"
11 | #import "ScribbleManager.h"
12 | #import "CommandBarButton.h"
13 |
14 | @interface ThumbnailViewController : UIViewController
15 |
17 |
18 | {
19 | @private
20 | IBOutlet UINavigationItem *navItem_;
21 | ScribbleManager *scribbleManager_;
22 | }
23 |
24 | @end
25 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/TouchPainterAppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // TouchPainterAppDelegate.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 8/21/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "CoordinatingController.h"
11 |
12 | @interface TouchPainterAppDelegate : NSObject
13 | {
14 | UIWindow *window_;
15 | }
16 |
17 | @property (nonatomic, retain) IBOutlet UIWindow *window;
18 |
19 | @end
20 |
21 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/TouchPainterAppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // TouchPainterAppDelegate.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 8/21/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "TouchPainterAppDelegate.h"
10 |
11 | @implementation TouchPainterAppDelegate
12 |
13 | @synthesize window=window_;
14 |
15 |
16 | #pragma mark -
17 | #pragma mark Application lifecycle
18 |
19 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
20 |
21 | // Add the view controller's view to the window and display.
22 | CoordinatingController *coordinatingController = [CoordinatingController sharedInstance];
23 | UIView *view = [[coordinatingController activeViewController] view];
24 | [window_ addSubview:view];
25 | [window_ makeKeyAndVisible];
26 |
27 | return YES;
28 | }
29 |
30 |
31 | - (void)applicationWillResignActive:(UIApplication *)application {
32 | /*
33 | 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.
34 | Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
35 | */
36 | }
37 |
38 |
39 | - (void)applicationDidEnterBackground:(UIApplication *)application {
40 | /*
41 | 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.
42 | If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
43 | */
44 | }
45 |
46 |
47 | - (void)applicationWillEnterForeground:(UIApplication *)application {
48 | /*
49 | Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
50 | */
51 | }
52 |
53 |
54 | - (void)applicationDidBecomeActive:(UIApplication *)application {
55 | /*
56 | 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.
57 | */
58 | }
59 |
60 |
61 | - (void)applicationWillTerminate:(UIApplication *)application {
62 | /*
63 | Called when the application is about to terminate.
64 | See also applicationDidEnterBackground:.
65 | */
66 | }
67 |
68 |
69 | #pragma mark -
70 | #pragma mark Memory management
71 |
72 | - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
73 | /*
74 | Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
75 | */
76 | }
77 |
78 |
79 | - (void)dealloc
80 | {
81 | [window_ release];
82 | [super dealloc];
83 | }
84 |
85 |
86 | @end
87 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/UIView+UIImage.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIView+UIImage.h
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/23/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 |
12 | @interface UIView (UIImage)
13 |
14 | - (UIImage*) image;
15 |
16 | @end
17 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/UIView+UIImage.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIView+UIImage.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 10/23/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "UIView+UIImage.h"
10 | #import
11 |
12 | @implementation UIView (UIImage)
13 |
14 |
15 | - (UIImage*)image
16 | {
17 | // Create a graphics context with the target size
18 | // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
19 | // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
20 | CGSize imageSize = [self bounds].size;// [[UIScreen mainScreen] bounds].size;
21 |
22 | if (NULL != UIGraphicsBeginImageContextWithOptions)
23 | UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
24 | else
25 | UIGraphicsBeginImageContext(imageSize);
26 |
27 | CGContextRef context = UIGraphicsGetCurrentContext();
28 |
29 | // Iterate over every window from back to front
30 | for (UIWindow *window in [[UIApplication sharedApplication] windows])
31 | {
32 | if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
33 | {
34 | // -renderInContext: renders in the coordinate space of the layer,
35 | // so we must first apply the layer's geometry to the graphics context
36 | CGContextSaveGState(context);
37 | // Center the context around the window's anchor point
38 | CGContextTranslateCTM(context, [window center].x, [window center].y);
39 | // Apply the window's transform about the anchor point
40 | CGContextConcatCTM(context, [window transform]);
41 | // Offset by the portion of the bounds left of and above the anchor point
42 | CGContextTranslateCTM(context,
43 | -[window bounds].size.width *[[window layer] anchorPoint].x,
44 | -[window bounds].size.height *[[window layer] anchorPoint].y);
45 |
46 | // Render the layer hierarchy to the current context
47 | [[window layer] renderInContext:context];
48 |
49 | // Restore the context
50 | CGContextRestoreGState(context);
51 | }
52 | }
53 |
54 | // Retrieve the screenshot image
55 | UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
56 |
57 | UIGraphicsEndImageContext();
58 |
59 | return image;
60 | }
61 |
62 | @end
63 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/Vertex.h:
--------------------------------------------------------------------------------
1 | //
2 | // Vertex.h
3 | // Composite
4 | //
5 | // Created by Carlo Chung on 9/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "Mark.h"
11 |
12 | @protocol MarkVisitor;
13 |
14 | @interface Vertex : NSObject
15 | {
16 | @protected
17 | CGPoint location_;
18 | }
19 |
20 | @property (nonatomic, retain) UIColor *color;
21 | @property (nonatomic, assign) CGFloat size;
22 | @property (nonatomic, assign) CGPoint location;
23 | @property (nonatomic, readonly) NSUInteger count;
24 | @property (nonatomic, readonly) id lastChild;
25 |
26 | - (id) initWithLocation:(CGPoint) location;
27 | - (void) addMark:(id ) mark;
28 | - (void) removeMark:(id ) mark;
29 | - (id ) childMarkAtIndex:(NSUInteger) index;
30 |
31 | // for the Visitor pattern
32 | - (void) acceptMarkVisitor:(id ) visitor;
33 |
34 | // for the Prototype pattern
35 | - (id) copyWithZone:(NSZone *)zone;
36 |
37 | // for the Iterator pattern
38 | - (NSEnumerator *) enumerator;
39 |
40 | // for internal iterator implementation
41 | - (void) enumerateMarksUsingBlock:(void (^)(id item, BOOL *stop)) block;
42 |
43 | // for the Memento pattern
44 | - (id)initWithCoder:(NSCoder *)coder;
45 | - (void)encodeWithCoder:(NSCoder *)coder;
46 |
47 | @end
48 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Classes/Vertex.m:
--------------------------------------------------------------------------------
1 | //
2 | // Vertex.m
3 | // Composite
4 | //
5 | // Created by Carlo Chung on 9/9/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import "Vertex.h"
10 |
11 |
12 | @implementation Vertex
13 | @synthesize location=location_;
14 | @dynamic color, size;
15 |
16 | - (id) initWithLocation:(CGPoint) aLocation
17 | {
18 | if (self = [super init])
19 | {
20 | [self setLocation:aLocation];
21 | }
22 |
23 | return self;
24 | }
25 |
26 | // default properties do nothing
27 | - (void) setColor:(UIColor *)color {}
28 | - (UIColor *) color { return nil; }
29 | - (void) setSize:(CGFloat)size {}
30 | - (CGFloat) size { return 0.0; }
31 |
32 | // Mark operations do nothing
33 | - (void) addMark:(id ) mark {}
34 | - (void) removeMark:(id ) mark {}
35 | - (id ) childMarkAtIndex:(NSUInteger) index { return nil; }
36 | - (id ) lastChild { return nil; }
37 | - (NSUInteger) count { return 0; }
38 | - (NSEnumerator *) enumerator { return nil; }
39 |
40 |
41 | - (void) acceptMarkVisitor:(id )visitor
42 | {
43 | [visitor visitVertex:self];
44 | }
45 |
46 | #pragma mark -
47 | #pragma mark NSCopying method
48 |
49 | // it needs to be implemented for memento
50 | - (id)copyWithZone:(NSZone *)zone
51 | {
52 | Vertex *vertexCopy = [[[self class] allocWithZone:zone] initWithLocation:location_];
53 |
54 | return vertexCopy;
55 | }
56 |
57 |
58 | #pragma mark -
59 | #pragma mark NSCoder methods
60 |
61 | - (id)initWithCoder:(NSCoder *)coder
62 | {
63 | if (self = [super init])
64 | {
65 | location_ = [(NSValue *)[coder decodeObjectForKey:@"VertexLocation"] CGPointValue];
66 | }
67 |
68 | return self;
69 | }
70 |
71 | - (void)encodeWithCoder:(NSCoder *)coder
72 | {
73 | [coder encodeObject:[NSValue valueWithCGPoint:location_] forKey:@"VertexLocation"];
74 | }
75 |
76 | #pragma mark -
77 | #pragma mark MarkIterator methods
78 |
79 | // for internal iterator implementation
80 | - (void) enumerateMarksUsingBlock:(void (^)(id item, BOOL *stop)) block {}
81 |
82 | #pragma mark -
83 | #pragma mark An Extended Direct-draw Example
84 |
85 | // for a direct draw example
86 | - (void) drawWithContext:(CGContextRef)context
87 | {
88 | CGFloat x = self.location.x;
89 | CGFloat y = self.location.y;
90 |
91 | CGContextAddLineToPoint(context, x, y);
92 | }
93 |
94 | @end
95 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/background_texture.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/background_texture.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/large_dot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/large_dot.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/open.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/open.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/palette.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/palette.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/placeholder.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/placeholder.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/redo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/redo.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/save.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/save.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/small_dot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/small_dot.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/undo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/pro-objective-c-design-patterns-for-ios/51f74d8395b830b5de6021cd9614ad2066822205/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/Resources/Images/undo.png
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/TouchPainter-Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | English
7 | CFBundleDisplayName
8 | ${PRODUCT_NAME}
9 | CFBundleExecutable
10 | ${EXECUTABLE_NAME}
11 | CFBundleIconFile
12 |
13 | CFBundleIdentifier
14 | com.carlochung.${PRODUCT_NAME:rfc1034identifier}
15 | CFBundleInfoDictionaryVersion
16 | 6.0
17 | CFBundleName
18 | ${PRODUCT_NAME}
19 | CFBundlePackageType
20 | APPL
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | 1.0
25 | LSRequiresIPhoneOS
26 |
27 | NSMainNibFile
28 | MainWindow
29 |
30 |
31 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/TouchPainter_Prefix.pch:
--------------------------------------------------------------------------------
1 | //
2 | // Prefix header for all source files of the 'TouchPainter' target in the 'TouchPainter' project
3 | //
4 |
5 | #ifdef __OBJC__
6 | #import
7 | #import
8 | #endif
9 |
--------------------------------------------------------------------------------
/code/978-1-4302-3330-5_Chung_TouchPainter_code/TouchPainter/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // TouchPainter
4 | //
5 | // Created by Carlo Chung on 8/21/10.
6 | // Copyright 2010 Carlo Chung. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | int main(int argc, char *argv[]) {
12 |
13 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
14 | int retVal = UIApplicationMain(argc, argv, nil, nil);
15 | [pool release];
16 | return retVal;
17 | }
18 |
--------------------------------------------------------------------------------
/contributing.md:
--------------------------------------------------------------------------------
1 | # Contributing to Apress Source Code
2 |
3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers.
4 |
5 | ## How to Contribute
6 |
7 | 1. Make sure you have a GitHub account.
8 | 2. Fork the repository for the relevant book.
9 | 3. Create a new branch on which to make your change, e.g.
10 | `git checkout -b my_code_contribution`
11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted.
12 | 5. Submit a pull request.
13 |
14 | Thank you for your contribution!
--------------------------------------------------------------------------------