Design Patterns
Design Patterns
- Delegation
- Model View Controller
- Target-Action
Delegation
Delegation is a pattern where one object periodically sends messages to another object specified as its delegate to ask for input or to notify the delegate that an event is occurring. You use it as an alternative to class inheritance for extending the functionality of reusable objects.
In this application, the application object tells its delegate that the main start-up routines have finished and that the custom configuration can begin. For this application, you want the delegate to create an instance of a controller to set up and manage the view. In addition, the text field will tell its delegate (which in this case will be the same controller) when the user has tapped the Return key.
Delegate methods are typically grouped together into a protocol. A protocol is basically just a list of methods. If a class conforms to a protocol, it guarantees that it implements the required methods of a protocol. (Protocols may also include optional methods.) The delegate protocol specifies all the messages an object might send to its delegate. To learn more about protocols and the role they play in Objective-C, see the “Protocols” chapter in The Objective-C Programming Language.
Model-View-Controller
The Model-View-Controller (MVC) design pattern assigns objects in an application one of three roles: model, view, or controller. The pattern defines not only the roles objects play in the application, it defines the way objects communicate with each other. Each of the three types of objects is separated from the others by abstract boundaries and communicates with objects of the other types across those boundaries. The collection of objects of a certain MVC type in an application is sometimes referred to as a layer—for example, model layer.
MVC is central to a good design for a Cocoa application. The benefits of adopting this pattern are numerous. Many objects in these applications tend to be more reusable, and their interfaces tend to be better defined. Applications having an MVC design are also more easily extensible than other applications. Moreover, many Cocoa technologies and architectures are based on MVC and require that your custom objects play one of the MVC roles.
Model Objects
Model objects encapsulate the data specific to an application and define the logic and computation that manipulate and process that data. For example, a model object might represent a character in a game or a contact in an address book. A model object can have to-one and to-many relationships with other model objects, and so sometimes the model layer of an application effectively is one or more object graphs. Much of the data that is part of the persistent state of the application (whether that persistent state is stored in files or databases) should reside in the model objects after the data is loaded into the application. Because model objects represent knowledge and expertise related to a specific problem domain, they can be reused in similar problem domains. Ideally, a model object should have no explicit connection to the view objects that present its data and allow users to edit that data—it should not be concerned with user-interface and presentation issues.
Communication: User actions in the view layer that create or modify data are communicated through a controller object and result in the creation or updating of a model object. When a model object changes (for example, new data is received over a network connection), it notifies a controller object, which updates the appropriate view objects.
View Objects
A view object is an object in an application that users can see. A view object knows how to draw itself and can respond to user actions. A major purpose of view objects is to display data from the application’s model objects and to enable the editing of that data. Despite this, view objects are typically decoupled from model objects in an MVC application.
Because you typically reuse and reconfigure them, view objects provide consistency between applications. Both the UIKit and AppKit frameworks provide collections of view classes, and Interface Builder offers dozens of view objects in its Library.
Communication: View objects learn about changes in model data through the application’s controller objects and communicate user-initiated changes—for example, text entered in a text field—through controller objects to an application’s model objects.
Controller Objects
A controller object acts as an intermediary between one or more of an application’s view objects and one or more of its model objects. Controller objects are thus a conduit through which view objects learn about changes in model objects and vice versa. Controller objects can also perform setup and coordinating tasks for an application and manage the life cycles of other objects.
Communication: A controller object interprets user actions made in view objects and communicates new or changed data to the model layer. When model objects change, a controller object communicates that new model data to the view objects so that they can display it.
Target-Action
The target-action mechanism enables a view object that presents a control—that is, an object such as a button or slider—in response to a user event (such as a click or a tap) to send a message (the action) to another object (the target) that can interpret the message and handle it as an application-specific instruction.
In this application, when it’s tapped, the button tells the controller to update its model and view based on the user’s input.
Memory management
Memory management
Memory management is the programming discipline of managing the life cycles of objects and freeing them when they are no longer needed. Managing object memory is a matter of performance; if an application doesn’t free unneeded objects, its memory footprint grows and performance suffers. Memory management in a Cocoa application that doesn’t use garbage collection is based on a reference counting model. When you create or copy an object, its retain count is 1. Thereafter other objects may express an ownership interest in your object, which increments its retain count. The owners of an object may also relinquish their ownership interest in it, which decrements the retain count. When the retain count becomes zero, the object is deallocated (destroyed).
To assist you in memory management, Objective-C gives you methods and mechanisms that you must use in conformance with set of rules.
Note: In Mac OS X, you can either explicitly manage memory or use the garbage collection feature of Objective-C. Garbage collection is not available in iPhone OS.
Memory-Management Rules
Memory-management rules, sometimes referred to as the ownership policy, help you to explicitly manage memory in Objective-C code.
- You own any object you create by allocating memory for it or copying it.
Related methods:
alloc
,allocWithZone:
,copy
,copyWithZone:
,mutableCopy
,mutableCopyWithZone:
- If you are not the creator of an object, but want to ensure it stays in memory for you to use, you can express an ownership interest in it.
Related method:
retain
- If you own an object, either by creating it or expressing an ownership interest, you are responsible for releasing it when you no longer need it.
Related methods:
release
,autorelease
- Conversely, if you are not the creator of an object and have not expressed an ownership interest, you must not release it.
If you receive an object from elsewhere in your program, it is normally guaranteed to remain valid within the method or function it was received in. If you want it to remain valid beyond that scope, you should retain or copy it. If you try to release an object that has already been deallocated, your program crashes.
Aspects of Memory Management
The following concepts are essential to understanding and properly managing object memory:
- Autorelease pools. Sending
autorelease
to an object marks the object for later release, which is useful when you want the released object to persist beyond the current scope. Autoreleasing an object puts it in an autorelease pool (an instance ofNSAutoreleasePool
), which is created for a arbitrary program scope. When program execution exits that scope, the objects in the pool are released. - Deallocation. When an object’s retain count drops to zero, the runtime calls the
dealloc
method of the object’s class just before it destroys the object. A class implements this method to free any resources the object holds, including objects pointed to by its instance variables. - Factory methods. Many framework classes define class methods that, as a convenience, create objects of the class for you. These returned objects are not guaranteed to be valid beyond the receiving method’s scope.
Delegation
Delegation
Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.
Delegation and the Cocoa Frameworks
The delegating object is typically a framework object, and the delegate is typically a custom controller object. The delegating object holds a weak reference to its delegate. Examples of delegation abound in the Foundation, UIKit, AppKit, and other Cocoa and Cocoa Touch frameworks.
An example of a delegating object is an instance of the NSWindow
class of the AppKit framework. NSWindow
declares a protocol, among whose methods is windowShouldClose:
. When a user clicks the close box in a window, the window object sends windowShouldClose:
to its delegate to ask it to confirm the closure of the window. The delegate returns a Boolean value, thereby controlling the behavior of the window object.
Delegation and Notifications
The delegate of most Cocoa framework classes is automatically registered as an observer of notifications posted by the delegating object. The delegate need only implement a notification method declared by the framework class to receive a particular notification message. Following the example above, a window object posts an NSWindowWillCloseNotification
to observers but sends a windowShouldClose:
message to its delegate.
Data Source
A data source is almost identical to a delegate. The difference is in the relationship with the delegating object. Instead of being delegated control of the user interface, a data source is delegated control of data. The delegating object, typically a view object such as a table view, holds a reference to its data source and occasionally asks it for the data it should display. A data source, like a delegate, must adopt a protocol and implement at minimum the required methods of that protocol. Data sources are responsible for managing the memory of the model objects they give to the delegating view.
iPad prices in India may begin at Rs 23,000
Apple iPad, the latest tech device routed as that of the next generation gadget, will come to India in April 2010. According to Apple distributors in India, the Apple iPad will be available in India from April onwards.
iPad prices in India may begin at Rs 23,000
That is pretty fast, considering the normal delay for almost all cool gadgets to reach us in India. For example, we are yet to receive the Motorola Droid, or the Nokia N900, or the Nexus One. All of which us gadget freaks in India have to get our favourite gadgets from friends or relatives abroad, or browse the back alleys.
Even the Apple iPhone took ages to reach us here in India. Not so for the just-launched iPad.
Apple launched its much anticipated tech gadget, the Apple iPad, claiming to bridging the gap between smartphones and laptops. The 9.7-inch touch-screen tablet was launched in San Francisco by none other than Steve Jobs at a widely – and wildly – anticipated press event.
According to the distributors of Apple in India, the cost of the Apple iPad will be the same in India as that in the US. The lower-end 16GB model will be available for USD 500 (INR 23000) plus 16% VAT, thereby taking it to INR 27000. High end versions of the Apple iPad will sell for USD $599 (Rs28,000) and $699 (Rs32,500), excluding taxes.
The retail shops are expecting a nice time once the iPad is available in India, with many potential customers calling up the shops to inquire about the product.
According to sector experts, Apple has priced the iPad reasonably, considering the fact that the high price of Apple’s iPhone discouraged many potential buyers from buying the device. It seems like Apple has taken a cue from that experience and according to experts, the price tag of Apple iPad is only reasonable considering the volume of functionalities it incorporates.
The Apple iPad can be used to surf the net, check emails, play games or listen to music. It also comes with Wi-Fi and related applications. The most interesting thing about the iPad is its battery, which is capable of playback up to a cool 10 hours. It is clear that Apple is going after the ebook market so far ruled by the Amazon Kindle and Kindle DX e-readers.
Many however think that the Apple iPad was a disappointment, after all the hype about it being a potential saviour for the publishing industry. True, several book publishers have already signed up for the iBookstore app on the iPad. But there is precious little by way of newspapers and magazines on the iPad optimized for the new interface. That is something content creators will have to work on.
Major complaints about the iPad so far include its name (which remind many of a female hygiene product), the large bezel (which may be useful for holding it properly), the lack of a camera, the fact that it is running a version of the iPhone OS and not the OS X Snow Leopard, that it does not offer proper multitasking… and that there is no phone.
It is also thought that companies like Asus and MSI are in the works of producing a product of the same kind albeit additional features. Even HP has plans to launch a tablet computer – but unlike the iPad, the HP Slate will run a version of Windows 7.
keyboard shortcuts for Word 2002, Word 2003, and Word 2007
Command Name Shortcut Keys ------------------------------------------------------------------------ All Caps CTRL+SHIFT+A Annotation ALT+CTRL+M App Maximize ALT+F10 App Restore ALT+F5 Apply Heading1 ALT+CTRL+1 Apply Heading2 ALT+CTRL+2 Apply Heading3 ALT+CTRL+3 Apply List Bullet CTRL+SHIFT+L Auto Format ALT+CTRL+K Auto Text F3 or ALT+CTRL+V Bold CTRL+B or CTRL+SHIFT+B Bookmark CTRL+SHIFT+F5 Browse Next CTRL+PAGE DOWN Browse Previous CTRL+PAGE UP Browse Sel ALT+CTRL+HOME Cancel ESC Center Para CTRL+E Change Case SHIFT+F3 Char Left LEFT Char Left Extend SHIFT+LEFT Char Right RIGHT Char Right Extend SHIFT+RIGHT Clear DELETE Close or Exit ALT+F4 Close Pane ALT+SHIFT+C Column Break CTRL+SHIFT+ENTER Column Select CTRL+SHIFT+F8 Copy CTRL+C or CTRL+INSERT Copy Format CTRL+SHIFT+C Copy Text SHIFT+F2 Create Auto Text ALT+F3 Customize Add Menu ALT+CTRL+= Customize Keyboard ALT+CTRL+NUM + Customize Remove Menu ALT+CTRL+- Cut CTRL+X or SHIFT+DELETE Date Field ALT+SHIFT+D Delete Back Word CTRL+BACKSPACE Delete Word CTRL+DELETE Dictionary ALT+SHIFT+F7 Do Field Click ALT+SHIFT+F9 Doc Close CTRL+W or CTRL+F4 Doc Maximize CTRL+F10 Doc Move CTRL+F7 Doc Restore CTRL+F5 Doc Size CTRL+F8 Doc Split ALT+CTRL+S Double Underline CTRL+SHIFT+D End of Column ALT+PAGE DOWN End of Column ALT+SHIFT+PAGE DOWN End of Doc Extend CTRL+SHIFT+END End of Document CTRL+END End of Line END End of Line Extend SHIFT+END End of Row ALT+END End of Row ALT+SHIFT+END End of Window ALT+CTRL+PAGE DOWN End of Window Extend ALT+CTRL+SHIFT+PAGE DOWN Endnote Now ALT+CTRL+D Extend Selection F8 Field Chars CTRL+F9 Field Codes ALT+F9 Find CTRL+F Font CTRL+D or CTRL+SHIFT+F Font Size Select CTRL+SHIFT+P Footnote Now ALT+CTRL+F Go Back SHIFT+F5 or ALT+CTRL+Z Go To CTRL+G or F5 Grow Font CTRL+SHIFT+. Grow Font One Point CTRL+] Hanging Indent CTRL+T Header Footer Link ALT+SHIFT+R Help F1 Hidden CTRL+SHIFT+H Hyperlink CTRL+K Indent CTRL+M Italic CTRL+I or CTRL+SHIFT+I Justify Para CTRL+J Left Para CTRL+L Line Down DOWN Line Down Extend SHIFT+DOWN Line Up UP Line Up Extend SHIFT+UP List Num Field ALT+CTRL+L Lock Fields CTRL+3 or CTRL+F11 Macro ALT+F8 Mail Merge Check ALT+SHIFT+K Mail Merge Edit Data Source ALT+SHIFT+E Mail Merge to Doc ALT+SHIFT+N Mail Merge to Printer ALT+SHIFT+M Mark Citation ALT+SHIFT+I Mark Index Entry ALT+SHIFT+X Mark Table of Contents Entry ALT+SHIFT+O Menu Mode F10 Merge Field ALT+SHIFT+F Microsoft Script Editor ALT+SHIFT+F11 Microsoft System Info ALT+CTRL+F1 Move Text F2 New CTRL+N Next Cell TAB Next Field F11 or ALT+F1 Next Misspelling ALT+F7 Next Object ALT+DOWN Next Window CTRL+F6 or ALT+F6 Normal ALT+CTRL+N Normal Style CTRL+SHIFT+N or ALT+SHIFT+CLEAR (NUM 5) Open CTRL+O or CTRL+F12 or ALT+CTRL+F2 Open or Close Up Para CTRL+0 Other Pane F6 or SHIFT+F6 Outline ALT+CTRL+O Outline Collapse ALT+SHIFT+- or ALT+SHIFT+NUM - Outline Demote ALT+SHIFT+RIGHT Outline Expand ALT+SHIFT+= Outline Expand ALT+SHIFT+NUM + Outline Move Down ALT+SHIFT+DOWN Outline Move Up ALT+SHIFT+UP Outline Promote ALT+SHIFT+LEFT Outline Show First Line ALT+SHIFT+L Overtype INSERT Page ALT+CTRL+P Page Break CTRL+ENTER Page Down PAGE DOWN Page Down Extend SHIFT+PAGE DOWN Page Field ALT+SHIFT+P Page Up PAGE UP Page Up Extend SHIFT+PAGE UP Para Down CTRL+DOWN Para Down Extend CTRL+SHIFT+DOWN Para Up CTRL+UP Para Up Extend CTRL+SHIFT+UP Paste CTRL+V or SHIFT+INSERT Paste Format CTRL+SHIFT+V Prev Cell SHIFT+TAB Prev Field SHIFT+F11 or ALT+SHIFT+F1 Prev Object ALT+UP Prev Window CTRL+SHIFT+F6 or ALT+SHIFT+F6 Print CTRL+P or CTRL+SHIFT+F12 Print Preview CTRL+F2 or ALT+CTRL+I Proofing F7 Redo ALT+SHIFT+BACKSPACE Redo or Repeat CTRL+Y or F4 or ALT+ENTER Repeat Find SHIFT+F4 or ALT+CTRL+Y Replace CTRL+H Reset Char CTRL+SPACE or CTRL+SHIFT+Z Reset Para CTRL+Q Revision Marks Toggle CTRL+SHIFT+E Right Para CTRL+R Save CTRL+S or SHIFT+F12 or ALT+SHIFT+F2 Save As F12 Select All CTRL+A or CTRL+CLEAR (NUM 5) or CTRL+NUM 5 Select Table ALT+CLEAR (NUM 5) Show All CTRL+SHIFT+8 Show All Headings ALT+SHIFT+A Show Heading1 ALT+SHIFT+1 Show Heading2 ALT+SHIFT+2 Show Heading3 ALT+SHIFT+3 Show Heading4 ALT+SHIFT+4 Show Heading5 ALT+SHIFT+5 Show Heading6 ALT+SHIFT+6 Show Heading7 ALT+SHIFT+7 Show Heading8 ALT+SHIFT+8 Show Heading9 ALT+SHIFT+9 Shrink Font CTRL+SHIFT+, Shrink Font One Point CTRL+[ Small Caps CTRL+SHIFT+K Space Para1 CTRL+1 Space Para15 CTRL+5 Space Para2 CTRL+2 Spike CTRL+SHIFT+F3 or CTRL+F3 Start of Column ALT+PAGE UP Start of Column ALT+SHIFT+PAGE UP Start of Doc Extend CTRL+SHIFT+HOME Start of Document CTRL+HOME Start of Line HOME Start of Line Extend SHIFT+HOME Start of Row ALT+HOME Start of Row ALT+SHIFT+HOME Start of Window ALT+CTRL+PAGE UP Start of Window Extend ALT+CTRL+SHIFT+PAGE UP Style CTRL+SHIFT+S Subscript CTRL+= Superscript CTRL+SHIFT+= Symbol Font CTRL+SHIFT+Q Thesaurus SHIFT+F7 Time Field ALT+SHIFT+T Toggle Field Display SHIFT+F9 Toggle Master Subdocs CTRL+\ Tool SHIFT+F1 Un Hang CTRL+SHIFT+T Un Indent CTRL+SHIFT+M Underline CTRL+U or CTRL+SHIFT+U Undo CTRL+Z or ALT+BACKSPACE Unlink Fields CTRL+6 or CTRL+SHIFT+F9 Unlock Fields CTRL+4 or CTRL+SHIFT+F11 Update Auto Format ALT+CTRL+U Update Fields F9 or ALT+SHIFT+U Update Source CTRL+SHIFT+F7 VBCode ALT+F11 Web Go Back ALT+LEFT Web Go Forward ALT+RIGHT Word Left CTRL+LEFT Word Left Extend CTRL+SHIFT+LEFT Word Right CTRL+RIGHT Word Right Extend CTRL+SHIFT+RIGHT Word Underline CTRL+SHIFT+W
Apple script – Javascripting webkit thru AppleScript
tell application "Safari" to do JavaScript "alert('Hello, world!')" in document 1
Apple latest creation Events
Will the Apple tablet finally, really be unveiled? We’re at the Yerba Buena Center in San Francisco (see above) patiently waiting to get inside and get this thing underway! Keep reading after the break for the minute by minute coverage!
How to Make a DMG File on a Mac
- Create a New Folder and place the files you would like in your disk image into this new folder.
- Right click (or CTRL-Click) the folder and select “Get Info” and note the size of its contents.
- Open Disk Utility (Applications > Utilities > Disk Utility)
- Click the “New Image” icon to create a new disk image. Enter a name for the Image, and select a size adequate for the size of your folder you created in Step 2. Set the encryption to “none” and Format to “read/write disk image.” or “DVD/CD Master.” See Tips to learn how to encrypt the image.
- Place the contents of the folder from Step 2 into the newly mounted disk image.
- Unmount the Disk Image by dragging its icon to the Trash. In the Finder window, you can also click the Eject symbol next to the mounted image.
You must be logged in to post a comment.