2015-08-27 49 views
1

我有一個具有多個XML文件的Typhoon 1.x實現。我有多個應用程序依賴於一個公共庫。我有一個默認的xml文件,其中包含用於各種應用程序的「默認」實現的公共庫。然後每個應用程序都有自己的xml文件,可以覆蓋爲某個鍵定義的對象類型。該覆蓋通過自定義工廠完成。Typhoon - 將XML 1.x實現遷移到2.x

庫的xml:

<!-- DEFAULT MENU --> 
<component class="my_menu" key="menu_default"/> 

應用1 XML:

<!-- APP 1 MENU --> 
<component class="my_app1_menu" key="menu"/> 

應用2 XML:

<!-- APP 2 MENU --> 
<!-- None - use default --> 

然後我定製廠發生在一個關鍵值,試圖訪問颱風爲「鑰匙」。如果這是'零',然後再次嘗試訪問:「key」+「_default」。

我的工廠方法:

- (id) componentForKey:(NSString *)key 
{ 
    TyphoonComponentFactory *factory = [TyphoonComponentFactory defaultFactory]; 
    @try { 
     id obj = [factory componentForKey:key]; 
     if (obj) { 
      return obj; 
     } 
    } 
    @catch (NSException *exception) { 
     DLog(@"Couldn't find %@", key); 
     DLog(@"Nope"); 
    } 
    // Try Default 
    @try { 
     key = [NSString stringWithFormat:@"%@_default", key]; 
     id obj = [factory componentForKey:key]; 
     if (obj) { 
      return obj; 
     } 
    } 
    @catch (NSException *exception) { 
     DLog(@"Couldn't find %@", key); 
     DLog(@"No Default"); 
     DLog(@"Exception: %@", exception); 
    } 

    return nil; 
} 

由於颱風的XML實現中止了與2.x版本發佈。我想超越1.x和我們目前的XML文件。但是,我很難找到可行的實施方案。

這很重要的原因是,每個應用程序本質上都是一個自定義的皮膚,超過了由庫行爲定義的基本應用程序。隨着新功能的添加,圖書館的颱風xml會填入「_default」定義。然後,當我們用最新版本的庫啓動每個應用程序時,至少我們定義了一個基本實現,所以我們無需在任何地方追蹤'零'值。

回答

0

是的,不幸的是,從Typhoon 2.x開始不支持XML。然而,你希望得到的是'組裝'風格的配置'。總結:

  • 定義可以模塊化和組織成多個彙編文件。這些文件可能是refer to each other
  • 在啓動時,可以在一個組件中替換另一個組件。這可以是程序集的子類,或者程序集是由協議支持的,也可以是該協議的另一個實現。 (事實上​​,只要組件響應相同的消息,即提供了命名定義,Typhoon就不會在意)。

例子:

比方說,你有以下頂級部件:

@interface MyAppAssembly : TyphoonAssembly 

@property(nonatomic, strong, readonly) LibraryAssembly *library; 

@end 

您可以激活此如下:

MyAppAssembly *assembly = [[MyAppAssembly new] 
    activateWithCollaboratingAssemblies:[CustomizedLibraryAssembly new]]; 

在啓動颱風會說:

CustomizedLibraryAssembly will act in place of LibraryAssembly 

CustomizedLibraryAssembly中,您可以覆蓋任何您喜歡的定義。

感謝您成爲颱風早期採用者。如果您需要幫助移植到最新版本作爲電子郵件發送,我們很樂意提供幫助。

+0

對不起,請您澄清一下三個提到的程序集:LibraryAssembly,CustomizedLibraryAssembly和MyAppAssembly。謝謝。 – FishStix

+0

我已經在這裏做了,其實:https://github.com/appsquickly/Typhoon/wiki/Modularizing-Assemblies –

+0

感謝您的例子! – FishStix

0

所以我能夠想出一個解決方案,我們幾乎完全移動,使用颱風3.x.

@interface LibAssembly : TyphoonAssembly 
@end 

@implementation LibAssembly 

- (id) menu { 
    return [TyphoonDefinition withClass:[MyMenu class]]; 
} 

@end 

然後爲每個應用程序:

@interface AppAssembly : LibAssembly 
@end 

@implementation AppAssembly 

- (id) menu { 
    return [TyphoonDefinition withClass:[AppMenu class]]; 
} 

@end 

我的工廠方法改爲:

- (id) componentForKey:(NSString *)key 
{ 
    // Check Init 
    if (!self.assembly) { 
     @throw @"Assembly not initialized properly - please call 'initWithAssemblerClass:' first"; 
    } 

    SEL selector = NSSelectorFromString(key); 
    if ([self.assembly respondsToSelector:selector]) { 
     return [self.assembly performSelector:selector]; 
    } 
    return nil; 
} 

+ (void) initWithAssemblerClass:(Class)assemblerClass 
{ 
    TyphoonAssembly *assembly = [assemblerClass new]; 
    [assembly activate]; 
    [MyFactory setAssembly: assembly]; 
} 

然後,我們根本就加入到我們的每個應用程序的代表的初始化步驟:

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // INIT TYPHOON 
    [MyFactory initWithAssemblerClass:[AppAssembly class]]; 
... 
}