底层原理:架构设计

2021-12-08  本文已影响0人  飘摇的水草

1. 架构

2. 经常听到的架构名词

3. MVC

@interface CustomCell

@property (nonatomic, strong, readOnly) UILabel *nameLabel;
@property (nonatomic, strong, readOnly) UILabel *addressLabel;

@end

@implementation ViewController

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomCell *cell = [tableView 
  dequeueReusableCellWithIdentifier:@“CustomCell” 
  forIndexPath:indexPath];
  cell.nameLabel.text = @"旺旺";
  cell.addressLabel.text = @"北京市";
  return cell;
}

@end
@interface MJAppView : UIView

@property (strong, nonatomic) MJApp *app;

@end

@interface MJAppView()

@property (weak, nonatomic) UIImageView *iconView;
@property (weak, nonatomic) UILabel *nameLabel;

@end

@implementation MJAppView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIImageView *iconView = [[UIImageView alloc] init];
        iconView.frame = CGRectMake(0, 0, 100, 100);
        [self addSubview:iconView];
        _iconView = iconView;
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.frame = CGRectMake(0, 100, 100, 30);
        nameLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:nameLabel];
        _nameLabel = nameLabel;
    }
    return self;
}

- (void)setApp:(MJApp *)app
{
    _app = app;
    self.iconView.image = [UIImage imageNamed:app.image];
    self.nameLabel.text = app.name;
}

// Controller

#import "ViewController.h"
#import "MJApp.h"
#import "MJAppView.h"

@interface ViewController () <MJAppViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建view
    MJAppView *appView = [[MJAppView alloc] init];
    appView.frame = CGRectMake(100, 100, 100, 150);
    appView.delegate = self;
    [self.view addSubview:appView];
    
    // 加载模型数据
    MJApp *app = [[MJApp alloc] init];
    app.name = @"QQ";
    app.image = @"QQ";
    
    // 设置数据到view上
    appView.app = app;
}

4. MVP(Model-View-Presenter)

这里Presenter的作用类似于MVC里的控制器的作用,这里面model和view依旧不直接通信:

View <==> Presenter <==> Model

5. 分层架构

界面层(新闻列表界面)-->业务层(加载新闻数据)-->数据层(通过网络、本地数据)

主要有三层架构和四层架构(加了网络层)两种,而MVC、MVP、MVVM这主要是针对界面来说的,再往上才是分层架构


以上部分为底层原理部分有关架构设计的课程,比较简单

上一篇 下一篇

猜你喜欢

热点阅读