工厂模式
#import <UIKit/UIKit.h>
@interface LXYUIFactory : NSObject
+ (UIWindow *)createWindow;
+ (UIViewController *)createViewController:(NSString *)controllerClassName;
+ (UIViewController *)createViewController:(NSString *)controllerClassName
withBackgroundColor:(UIColor *)bgColor;
@end
#import "LXYUIFactory.h"
@implementation LXYUIFactory
+ (UIWindow *)createWindow {
static UIWindow *window = nil;
if (!window) {
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.backgroundColor = [UIColor whiteColor];
}
return window;
}
+ (UIViewController *)createViewController:(NSString *)controllerClassName {
Class cls = NSClassFromString(controllerClassName);
return cls ? [[cls alloc] init] : nil;
}
+ (UIViewController *)createViewController:(NSString *)controllerClassName
withBackgroundColor:(UIColor *)bgColor {
UIViewController *controller =
[self createViewController:controllerClassName];
if (controller) {
controller.view.backgroundColor = bgColor;
}
return controller;
}
@end