工厂模式

工厂模式

1.
2.#import <UIKit/UIKit.h>
3.
4.@interface LXYUIFactory : NSObject
5.
6./**创建Window*/
7.+ (UIWindow *)createWindow;
8.
9./**创建一个试图控制器*/
10.+ (UIViewController *)createViewController:(NSString *)controllerClassName;
11.
12./**创建一个带背景色的视图控制器*/
13.+ (UIViewController *)createViewController:(NSString *)controllerClassName
14.                       withBackgroundColor:(UIColor *)bgColor;
15.
16.@end
17.
1.#import "LXYUIFactory.h"
2.
3.
4.@implementation LXYUIFactory
5.
6.+ (UIWindow *)createWindow {
7.  static UIWindow *window = nil;
8.  if (!window) {
9.    window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
10.    window.backgroundColor = [UIColor whiteColor];
11.  }
12.  return window;
13.}
14.
15.+ (UIViewController *)createViewController:(NSString *)controllerClassName {
16.
17.  Class cls = NSClassFromString(controllerClassName);
18.
19.  return cls ? [[cls alloc] init] : nil;
20.}
21.
22.+ (UIViewController *)createViewController:(NSString *)controllerClassName
23.                       withBackgroundColor:(UIColor *)bgColor {
24.
25.  UIViewController *controller =
26.      [self createViewController:controllerClassName];
27.  if (controller) {
28.    controller.view.backgroundColor = bgColor;
29.  }
30.  return controller;
31.}
32.
33.@end
34.
 
posted @   Emerys  阅读(165)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示