• 讲讲 MVC、MVVM、MVP,以及你在项目里具体是怎么写的?

    MVC - Apple版
    设计模式与架构 - 图1

MVC – 变种
设计模式与架构 - 图2
MVP

设计模式与架构 - 图3

  1. @interface MJAppPresenter() <MJAppViewDelegate>
  2. @property (weak, nonatomic) UIViewController *controller;
  3. @end
  4. @implementation MJAppPresenter
  5. - (instancetype)initWithController:(UIViewController *)controller
  6. {
  7. if (self = [super init]) {
  8. //把UIViewController传到MJAppPresenter中
  9. self.controller = controller;
  10. // 创建View
  11. MJAppView *appView = [[MJAppView alloc] init];
  12. appView.frame = CGRectMake(100, 100, 100, 150);
  13. appView.delegate = self;
  14. [controller.view addSubview:appView];
  15. // 加载模型数据
  16. MJApp *app = [[MJApp alloc] init];
  17. app.name = @"QQ";
  18. app.image = @"QQ";
  19. // 赋值数据
  20. [appView setName:app.name andImage:app.image];
  21. }
  22. return self;
  23. }
  24. @end

MVVM
设计模式与架构 - 图4

ViewModel与View之间形成一个双向绑定,ViewModel的里面属性的更新会及时通知到View上,可以用KVO、RAC、KVOController来实现。

  1. //view中
  2. // 设置数据
  3. self.name = app.name;
  4. self.image = app.image;
  5. //ViewModel中
  6. - (void)setViewModel:(MJAppViewModel *)viewModel
  7. {
  8. _viewModel = viewModel;
  9. __weak typeof(self) waekSelf = self;
  10. [self.KVOController observe:viewModel keyPath:@"name" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
  11. waekSelf.nameLabel.text = change[NSKeyValueChangeNewKey];
  12. }];
  13. [self.KVOController observe:viewModel keyPath:@"image" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
  14. waekSelf.iconView.image = [UIImage imageNamed:change[NSKeyValueChangeNewKey]];
  15. }];
  16. }

你自己用过哪些设计模式?

设计模式与架构 - 图5

一般开始做一个项目,你的架构是如何思考的?

设计模式与架构 - 图6

  • 何为架构?

架构(Architecture)
软件开发中的设计方案
类与类之间的关系、模块与模块之间的关系、客户端与服务端的关系

经常听到的架构名词
MVC、MVP、MVVM、VIPER、CDD
三层架构、四层架构
……