标签:
和已有组件保持一致(例如: 使用标准的API, 模型,模式)可以使Developer更易理解。
Class interface
在编码前学习该平台的习俗或者惯例;学习并理解protocol, delegate, category分别是什么;遵循内存管理规则;
"Any component should be designed such that it’s not coupled to the project you created it for,
and if it’s a GUI control or view, it should at least display something by default." Ref[1]
- (id)initWithDelegate:(id<MGTileMenuDelegate>)theDelegate; // required parameter; cannot be nil.
1 @property (nonatomic, weak, readonly) id<MGTileMenuDelegate> delegate; // must be specified via initializer method.
"you should briefly note default values beside properties or accessors" Ref[1]
1 @property (nonatomic) CGGradientRef tileGradient; // gradient to apply to tile backgrounds (default: a lovely blue) 2 @property (nonatomic) NSInteger selectionBorderWidth; // default: 5 pixels 3 @property (nonatomic) CGGradientRef selectionGradient; // default: a subtle white (top) to grey (bottom) gradient
这条值得商榷!
1 // Instantiate. 2 tileController = [[MGTileMenuController alloc] initWithDelegate:self]; 3 4 // Configure. 5 tileController.dismissAfterTileActivated = NO; // to make it easier to play with in the demo app. 6 7 // Display. 8 [tileController displayMenuCenteredOnPoint:loc inView:self.view];
"I approach this by trying not to think of customisation at the instance-variable level,
but rather at the “aspect” level." Ref[1]
"What’s not OK, though, is needlessly putting mysterious raw values throughout your code, and it’s
especially not OK to expose that in the API." Ref[1]
Delegate and data-source protocols
Data-source Protocol关注的事项:
Delegate 关注的事项:
should, will, did
"A well-designed component should need very, very few required delegate methods - just the
bare minimum to do whatever it does." Ref[1]
"make things accessible" Ref[1]
accessibility programming VoiceOver
"If you’re asking for a date, don’t accept numbers - get an actual NSDate object. " Ref[1]
例如: "A contact list implemented with a table should have a contacts-related API" Ref[1]
"Many of us approach optional delegate methods as an either-or situation: if you don’t implement them,
you get the default behaviour, and if you do, then you’re totally responsible for what happens.
That’s not ideal." ref[1]
可选方法可能并没有返回我们期望的值。比如:我们期望可选方法返回一个UIColor对象,但是可选方法返回了nil。那么我们
跳回默认逻辑是更好的选择。
1 - (void)tileMenu:(MGTileMenuController *)tileMenu didActivateTile:(NSInteger)tileNumber; // zero-based tileNumber
1 - (void)tileMenuDidActivateTile:(NSInteger)tileNumber; // zero-based tileNumber 2 // Um, WHICH menu?
1 - (UIImage *)imageForTile:(NSInteger)tileNumber inMenu:(MGTileMenuController *)tileMenu; // zero-based tileNumber
1 - (UIImage *)tileMenu:(MGTileMenuController *)tileMenu imageForTile:(NSInteger)tileNumber;
但是Apple API确实和该Rule相反。例如:
1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
"The One True Delegate Protocol, however, isn’t for queries but rather for notifications." Ref[1]
1 - (void)tileMenu:(MGTileMenuController *)tileMenu willSwitchToPage:(NSInteger)pageNumber; // zero-based pageNumber
Notification
"Notifications are the other half of delegate protocols. My position is that, if you’re using a delegate
protocol (you should, if it’s at all appropriate), then it’s incomplete until you add the notifications
that naturally follow from it." Ref[1]
"If you have a delegate method that tells the delegate about something happening, you should
usually provide a notification for that same purpose." Ref[1]
"At the very least, you must ensure that all arguments provided to the corresponding delegate
method are wrapped up in the userInfo object." Ref[1]
如果时间紧迫,可以减少feature来支持测试
Todo: article on releasing open source code
"The design goals for an API are similar to those of a user interface: exposing the product’s features
and communicating how to use them." Ref[2]
"This manifests itself in long, descriptive method names, the organization of functionality around view controllers,
and design patterns like delegation and target-action. " Ref[2]
遵守Apple的Cocoa Touch规范: naming conventions and design patterns
"Choosing the best pattern makes your code both easier to understand and to use."
Delegate
"the delegate version breaks up the processing code into pieces and makes it more difficult to read and understand."
"Delegation is best suited for situations when the response to a notification relies primarily on the information
contained in the delegate method’s parameters."
"By providing a convenience initializer, the integrating developer does not need to consider the more advanced
options until their application demands them."
1. API Design
http://mattgemmell.com/api-design/
主要是UI方面库和API的设计
2. Best Practices for iOS API Design
https://blog.creativesdk.com/2015/03/best-practices-for-ios-api-design/
"Aviary team’s best practices for developing APIs"
Todo
1. google "how to design interface of library for iOS"
2. WWDC 2014 Session 416
3. adobe creative sdk
4. Delegate vs. Block
SoftwareEngineering.APIDesign.iOS
标签:
原文地址:http://www.cnblogs.com/cwgk/p/4624696.html