1.iOSä¸isKindOfClassåisMemberOfClassçåºå«
2.关于DROOLS规则引擎的图书那里有卖的,急需啊!
3.iOSå¼åä¸isKindOfClassåisMemberOfClassçåºå«
4.ios performselector å respondstoselectorçåºå«
5.isKindOfClassåisMemberOfClassçåºå«
iOSä¸isKindOfClassåisMemberOfClassçåºå«
对象å¨è¿è¡æ¶è·åå ¶ç±»åçè½å称为å çãå çå¯ä»¥æå¤ç§æ¹æ³å®ç°ãå¤æ对象类å-(BOOL) isKindOfClass: classObjå¤ææ¯å¦æ¯è¿ä¸ªç±»æè è¿ä¸ªç±»çåç±»çå®ä¾-(BOOL) isMemberOfClass: classObj å¤ææ¯å¦æ¯è¿ä¸ªç±»çå®ä¾ UIView *view = [[UIView alloc]init]if ([view isKindOfClass:[UIView class]]){ //viewæ¯UIViewç±»æè æ¯UIViewçåç±»}
关于DROOLS规则引擎的图书那里有卖的,急需啊!
InfoQ发布了一个关于规则引擎的介绍Real-World Rule Engines,原文 /articles/Rule-Engines
ONJava上也有两篇文章:
Give Your Business Logic a Framework with Drools /lpt/a/
Using Drools in Your Enterprise Java Application /lpt/a/
下面总结一下其中的精华:
大多数web和企业Java应用可以分成三个部分:一个和用户交互的前台, 一个和后台系统,例如数据库交互的服务层,以及他们中间的业务逻辑。 现在使用框架构建前台和后台系统已经成为普遍共识(例如,微米源码免费下载 Struts, Cocoon, Spring, Hibernate, JDO, 和 Entity Beans), 但却没有一个标准的方法来构建业务逻辑。一些框架,html源码seo例如 EJB 和 Spring 只在一个高层实现业务逻辑,但对于我们组织逻辑代码没有任何帮助,所以,为什么没有一个框架来替换冗繁,易错的if...then语句呢,这个框架应该和其它前台或后台框架一样,易于配置,看懂app源码具有可读性和重用性。下面我们将介绍Drools 规则引擎,这个来解决我们问题的框架。
下面是小米视频源码一个典型的Java业务逻辑的代码
if ((user.isMemberOf(AdministratorGroup)
&& user.isMemberOf(teleworkerGroup))
|| user.isSuperUser(){
// more checks for specific cases
if((expenseRequest.code().equals("B")
||(expenseRequest.code().equals("A")
&&(totalExpenses<)
&&(bossSignOff> totalExpenses))
&&(deptBudget.notExceeded)) {
//issue payments
} else if {
//check lots of other conditions
}
} else {
// even more business logic
}
这是大多数程序员写业务逻辑的方法,但是有以下的问题:
如果用户有另一个选项,例如("C") ,那么需要修改上面的代码,当代码很长的xla源码分析时候,修改代码是难于维护的
我们如何保证代码的正确性,这个代码只有程序员能够看到,真正的使用者,业务人员无法看到这个代码
很多应用程序有相同的业务逻辑,如果某个业务改变了,如何保证其他业务逻辑的一致性
业务逻辑能否不合java语言绑定?
业务逻辑能否用其他的脚本语言。
Java下的规则引擎标准就是JSR,它的实现主要有Jess Jena Drools
Drools中,一个典型的业务逻辑的配置,如下
<?xml version="1.0"?>
<rule-set name="BusinessRulesSample"
xmlns="mendPurchase());
}
</java:functions>
<rule-set>
<!-- Ensure stock price is not too high-->
<rule name="Stock Price Low Enough">
<!-- Params to pass to business rule -->
<parameter identifier="stockOffer">
<class>StockOffer</class>
</parameter>
<!-- Conditions or 'Left Hand Side'
(LHS) that must be met for
business rule to fire -->
<!-- note markup -->
<java:condition>
stockOffer.getRecommendPurchase() == null
</java:condition>
<java:condition>
stockOffer.getStockPrice() <
</java:condition>
<!-- What happens when the business
rule is activated -->
<java:consequence>
stockOffer.setRecommendPurchase(
StockOffer.YES);
printStock(stockOffer);
</java:consequence>
</rule>
</rule-set>
一条规则就是rule-set中的rule,如果有很多规则,就要写很多的rule。
上面规则的意思是判断股票价格是否小于,如果这个标准改变了,那么只要修改这个规则文件,而不用去修改源代码了
iOSå¼åä¸isKindOfClassåisMemberOfClassçåºå«
isKindOfClassåisMemberOfClass é½æ¯NSObjectçæ¯è¾Classçæ¹æ³
ããä½ä¸¤ä¸ªæå¾å¤§åºå«:
ããisKindOfClassæ¥ç¡®å®ä¸ä¸ªå¯¹è±¡æ¯å¦æ¯ä¸ä¸ªç±»çæåï¼æè æ¯æ´¾çèªè¯¥ç±»çæå
ããisMemberOfClassåªè½ç¡®å®ä¸ä¸ªå¯¹è±¡æ¯å¦æ¯å½åç±»çæå
ããä¾å¦ï¼æ们已ç»æNSObjectæ´¾çäºèªå·±çç±»ï¼isMemberOfClassä¸è½æ£æµä»»ä½çç±»é½æ¯åºäºNSObjectç±»è¿ä¸äºå®ï¼èisKindOfClasså¯ä»¥ã
ãã[[NSMutableData data] isKindOfClass:[NSData class]]; // YES
[[NSMutableData data] isMemberOfClass:[NSData class]]; // NO
å¸æå¯¹ä½ æ帮å©
ios performselector å respondstoselectorçåºå«
respondsToSelectoræ¯å®ä¾æ¹æ³ä¹æ¯ç±»æ¹æ³,ç¨äºå¤ææ个类/å®ä¾æ¯å¦è½å¤çæ个æ¹æ³(å æ¬åºç±»æ¹æ³)ãconformsToProtocol:@protocol()æ¯ç¨æ¥æ£æ¥å¯¹è±¡æ¯å¦å®ç°äºæå®å议类çæ¹æ³. respondsToSelectorç¸å ³çæ¹æ³: -(BOOL) isKindOfClass: classObj ç¨æ¥å¤ææ¯å¦æ¯æ个类æå ¶åç±»çå®ä¾ -(BOOL) isMemberOfClass: classObj ç¨æ¥å¤ææ¯å¦æ¯æ个类çå®ä¾ -(BOOL) respondsToSelector: selector ç¨æ¥å¤ææ¯å¦æ以æ个ååå½åçæ¹æ³(被å°è£ å¨ä¸ä¸ªselectorç对象éä¼ é) +(BOOL) instancesRespondToSelector: selector ç¨æ¥å¤æå®ä¾æ¯å¦æ以æ个ååå½åçæ¹æ³. åä¸é¢ä¸ä¸ªä¸åä¹å¤å¨äº, åé¢è¿ä¸ªæ¹æ³å¯ä»¥ç¨å¨å®ä¾åç±»ä¸ï¼èæ¤æ¹æ³åªè½ç¨å¨ç±»ä¸. -(id) performSelector: selector SEL sel = @selector (start:) ; // æå®action if ([obj respondsToSelector:sel]) { //å¤æ该对象æ¯å¦æç¸åºçæ¹æ³ [obj performSelector:sel withObject:self]; //è°ç¨éæ©å¨æ¹æ³ } 使ç¨[[UIApplication sharedApplication] keyWindow]æ¥æ¾åºç¨ç¨åºç主çªå£å¯¹è±¡ respondsToSelectorå¤ææ¯å¦å®ç°äºææ¹æ³ #import <Foundation/Foundation.h> @interface Tester : NSObject { } -(void) test:(NSString*) msg; -(void) notImp; @end Tester.m #import "Tester.h" @implementation Tester -(void) test:(NSString*) msg { NSLog(@"%@", msg); } @end 注æï¼æ²¡æå®ç°notImpæ¹æ³ main.m #import <Foundation/Foundation.h> #import "Tester.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; id tester = [[Tester alloc] init];//注æï¼è¿é使ç¨id SEL testSelector = @selector(test:); SEL notImpSelector = @selector(notImp:); if([tester respondsToSelector:testSelector]) { //tester.mä¸å®ç°äºtestæ¹æ³ [tester test:@"invoke test method"]; } if([tester respondsToSelector:notImpSelector]) { //test.mä¸æ²¡æå®ç°æ¤ä¸»å°±å» [tester notImp]; } [pool drain]; return 0; } conformsToProtocol:@protocol()æ¯ç¨æ¥æ£æ¥å¯¹è±¡æ¯å¦å®ç°äºæå®å议类çæ¹æ³ //ä¾åå¦ä¸ï¼å¨ä¾åå½ä¸ä¼æéå½ç注éï¼ä»¥å©ç解è¿ä¸ªæ¹æ³ï¼ @protocol MyProtocol - (void) doSomething; @end @interface MyClass : NSObject<MyProtocol>//ç´æ¥ç¬¦ååè®®çç±» { } @end @implementation MyClass - (void) doSomething { } @end @interface MyOtherClass : MyClass//继æ¿äºç¬¦ååè®®çç±»ï¼å³å ¶ç¶ç±»ç¬¦ååè®®ã { } @end @implementation MyOtherClass - (void) doSomething { } @end int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; MyClass *obj_one = [MyClass new]; BOOL one_conforms = [obj_one conformsToProtocol:@protocol(MyProtocol)]; MyOtherClass *obj_two = [MyOtherClass new]; //obj_twoæ¯ç±»çå®ä¾å¯¹è±¡ï¼åç¶ç±»ç¸å ³ï¼å ¶ç¶ç±»ç¬¦ååè®®ï¼åå ¶äº¦ç¬¦åã BOOL two_conforms = [obj_two conformsToProtocol:@protocol(MyProtocol)]; NSLog(@"obj_one conformsToProtocol: %d", one_conforms);//output:YES NSLog(@"obj_two conformsToProtocol: %d", two_conforms);//output:YES [pool drain]; return 0; } //Output: obj_one conformsToProtocol: 1 obj_two conformsToProtocol: 1 //Whereas: MyOtherClass *obj_two = [MyOtherClass new]; //class_conformsToProtocolæ¯åªæ£æ¥å½å类符ä¸ç¬¦ååè®®ï¼åå ¶ç¶ç±»æ å ³ã BOOL conforms_two = class_conformsToProtocol([obj_two class], @protocol(MyProtocol)); NSLog(@"obj_two conformsToProtocol: %d", conforms_two);//output:NO //Output: obj_two conformsToProtocol: 0 å¨ä»£çè°ç¨æ¯ï¼æ£æ¥å ¶ä»£çæ¯å¦ç¬¦ååè®®ï¼æè ä½¿ç¨ respondsToSelector æ£æ¥å¯¹è±¡è½å¦ååºæå®çæ¶æ¯ï¼ æ¯é¿å 代çå¨åè°æ¶å 为没æå®ç°ä»£çå½æ°èç¨åºå´©æºçä¸ä¸ªææçæ¹å¼
isKindOfClassåisMemberOfClassçåºå«
ããisKindOfClassåisMemberOfClass é½æ¯NSObjectçæ¯è¾Classçæ¹æ³
ããä½ä¸¤ä¸ªæå¾å¤§åºå«:
ããisKindOfClassæ¥ç¡®å®ä¸ä¸ªå¯¹è±¡æ¯å¦æ¯ä¸ä¸ªç±»çæåï¼æè æ¯æ´¾çèªè¯¥ç±»çæå
ããisMemberOfClassåªè½ç¡®å®ä¸ä¸ªå¯¹è±¡æ¯å¦æ¯å½åç±»çæå
ããä¾å¦ï¼æ们已ç»æNSObjectæ´¾çäºèªå·±çç±»ï¼isMemberOfClassä¸è½æ£æµä»»ä½çç±»é½æ¯åºäºNSObjectç±»è¿ä¸äºå®ï¼èisKindOfClasså¯ä»¥ã
ãã[[NSMutableData data] isKindOfClass:[NSData class]]; // YES
[[NSMutableData data] isMemberOfClass:[NSData class]]; // NO
ä¾:
- (void) testIsKindOfClass {
Class1 *c1=[Class1 new];
if ([c1 isKindOfClass: [NSObject class]]) {
NSLog(@"c1 is a kind of NSObject.");
}
if ([c1 isMemberOfClass:[Class1 class]]) {
NSLog(@"c1 is Member of Class1 !");
}
if (![c1 isMemberOfClass:[NSObject class]]) {
NSLog(@"c1 is't Member of NSObject !");
}
}
è¾åºç»æï¼
c1 is a kind of NSObject .
c1 is Member of Class1 !
c1 is't Member of NSObject !