Last active
June 9, 2017 04:26
-
-
Save fernandojinzenji/04132dd6c61526f7d131e94bb984c143 to your computer and use it in GitHub Desktop.
Week 2, Thursday
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Question 1: | |
A) Programmer needs to set up the Observee (Subject) object adding a new notification to NSNotificationCenter defaultCenter. | |
After doing this, each Observer who needs to be notified needs to subscribe to that notification. To do that, each Observer | |
object needs to be added as a Observer in NotificationCenter and define a method that will be called each time that it receives | |
a new notification. | |
B) Notification Center uses two different design patterns: the Observer and the Singleton pattern. The observer defines that | |
any object that wants to subscribe to receive notifications (that could be also considered delegates for that observer, because | |
they can execute a specific task when receive a notification) can do that. In the delegation pattern, only one object can | |
receive a "notification" by the delegator. Notification Center uses a Singleton Pattern to define that only one instance | |
of this class will be created during the execution of the application. | |
C) Asynchronouly. All subscribers of the notification receives the message in the same time, and can execute their specific | |
tasks by their own. | |
Question 2: | |
Self should avoid reference itself, because this may cause a retain cycle. | |
Question 3: | |
A) Based on the code provided, it looks like both classes are View Controllers (the first is a subclass of UIViewController and the second directly access a input view - so it is not a model - and also wants to receive a notification when a person class is saved - so it is not a view, according to MVC patterns). In this case, the best option would be use the delegation pattern. Simplified code below: | |
@protocol ZZPersonViewControllerDelegate | |
- (void) personWasSaved:(Person*)p; | |
- (void) personWasDiscarded:(Person*)p; | |
@end | |
@interface ZZPersonViewController | |
@property id<ZZPersonViewControllerDelegate> delegate; | |
@end | |
@implementation ZZPersonViewController | |
- (void) savePerson | |
{ | |
if(ok) | |
[self.delegate personWasSaved:p]; | |
else | |
[self.delegate personWasDiscarded:p]; | |
} | |
@end | |
// in the other class... | |
// first, remove line codes where notification center is being referred | |
@interface .... <ZZPersonViewControllerDelegate> | |
@end | |
@implementation | |
- (void) personWasSaved:(Person*)p | |
{ | |
} | |
- (void) personWasDiscarded:(Person*)p | |
{ | |
} | |
@end | |
B) Considering that in a MVC pattern the responsible to maintain all object data is the Model, all code that is referencing the cloud manager should be transferred to the ZZPerson class. | |
Question 4: Properties encapsulates the object information from external user (more safety). Also, through the getter and | |
setter methods, you could do execute specific tasks before returning the value (integrity). For example, you could create | |
a "Chance of Rain %" and does not allow values outside 0 and 100 in the setter. Or before returning a name, you could | |
return in Uppercase in the getter, without having to change the instance variable real value. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment