Conventional MVC

Advantages:

1.Model is reusable.

2.View is reusable.

3. Low coupling.

Disadvantages:

1. The controller is bloated.

2.View control exposed, unsafe, poor encapsulation,

The realization of the Model

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MVCModel : NSObject

/** url */
@property (nonatomic, copy) NSString *photoUrl;

/** title */
@property (nonatomic, copy) NSString *titleStr;

@end

NS_ASSUME_NONNULL_END

Copy the code

The implementation of the View

/ / header files#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN @interface MVCView: UIView /** image */ @property (nonatomic, strong) UIImageView *imageView; @property (nonatomic, strong) UILabel *titleLbl; NS_ASSUME_NONNULL_END // Implementation file#import "MVCView.h"

@implementation MVCView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self createUI];
    }
    returnself; } /** Create control */ - (void)createUI{UIImageView *imageView = [[UIImageView alloc] init]; [self addSubview:imageView]; self.imageView = imageView; UILabel *titleLbl = [[UILabel alloc] init]; titleLbl.textColor = [UIColor blackColor]; titleLbl.font = [UIFont systemFontOfSize:16]; titleLbl.textAlignment = NSTextAlignmentCenter; [self addSubview:titleLbl]; self.titleLbl = titleLbl; } - (void)layoutSubviews{ [super layoutSubviews]; self.imageView.frame = CGRectMake(0, 0, 100, 100); self.titleLbl.frame = CGRectMake(0, 100, 100, 50); } @endCopy the code

The realization of the Controller

#import "MVCViewController.h"
#import "MVCView.h"
#import "MVCModel.h"

@interface MVCViewController ()

@end

@implementation MVCViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createUI];
}

#pragma Mark - Build interface- (void)createUI{// Create view MVCView *view = [[MVCView alloc] initWithFrame:CGRectMake(100, 100, 100, 150)]; [self.view addSubview:view]; MVCModel *model = [[MVCModel alloc] init]; model.photoUrl = @"classStudent";
    model.titleStr = @"Morning self-study"; / / assignment view. ImageView. Image = [UIImage imageNamed: model. PhotoUrl]; view.titleLbl.text = model.titleStr; } @endCopy the code

MVC variant

Advantages:

1. Reduce the amount of code in the controller.

2 View encapsulation is strong, the outside world can not see the implementation of internal controls.

Disadvantages:

1. High coupling.

2. Model and View cannot be reused separately.

The realization of the Model

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface VariantModel : NSObject

/** url */
@property (nonatomic, copy) NSString *photoUrl;

/** title */
@property (nonatomic, copy) NSString *titleStr;

@end

NS_ASSUME_NONNULL_END
Copy the code

The implementation of the View

/ / header files#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN @class VariantModel; @interface VariantView : UIView /** model */ @property (nonatomic, strong) VariantModel *model; NS_ASSUME_NONNULL_END // Implementation file#import "VariantView.h"
#import "VariantModel.h"/ / @property (nonatomic, strong) UIImageView *imageView; @property (nonatomic, strong) UILabel *titleLbl; @end @implementation VariantView - (instancetype)initWithFrame:(CGRect)frame{if (self = [super initWithFrame:frame]) {
        
        [self createUI];
    }
    returnself; } /** Create control */ - (void)createUI{UIImageView *imageView = [[UIImageView alloc] init]; [self addSubview:imageView]; self.imageView = imageView; UILabel *titleLbl = [[UILabel alloc] init]; titleLbl.textColor = [UIColor blackColor]; titleLbl.font = [UIFont systemFontOfSize:16]; titleLbl.textAlignment = NSTextAlignmentCenter; [self addSubview:titleLbl]; self.titleLbl = titleLbl; } /** Set frame */ - (void)layoutSubviews{[super layoutSubviews]; self.imageView.frame = CGRectMake(0, 0, 100, 100); self.titleLbl.frame = CGRectMake(0, 100, 100, 50); } /** assign */ - (void)setModel:(VariantModel *)model{
    
    _model = model;
    self.imageView.image = [UIImage imageNamed:model.photoUrl];
    self.titleLbl.text = model.titleStr;
}

Copy the code

The realization of the Controller

#import "VariantViewController.h"
#import "VariantModel.h"
#import "VariantView.h"

@interface VariantViewController ()

@end

@implementation VariantViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createUI];
}

#pragma Mark - Build interfaceVoid createUI{// Create view VariantView *view = [[VariantView alloc] initWithFrame:CGRectMake(100, 100, 100, 150)]; [self.view addSubview:view]; // Create model VariantModel *model = [VariantModel alloc] init]; model.photoUrl = @"classStudent";
    model.titleStr = @"Medium self-study"; // Assign view.model = model; } @endCopy the code

MVP

Advantages:

1.Model is reusable.

2.View is reusable.

3. Low coupling.

4. Reduce the amount of code in the controller.

Disadvantages:

1. The total amount of code increases.

The realization of the Model

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MVPModel : NSObject

/** url */
@property (nonatomic, copy) NSString *photoUrl;

/** title */
@property (nonatomic, copy) NSString *titleStr;

@end

NS_ASSUME_NONNULL_END
Copy the code

The implementation of the View

/ / header files#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN @interface MVPView: UIView /** image */ @property (nonatomic, strong) UIImageView *imageView; @property (nonatomic, strong) UILabel *titleLbl; NS_ASSUME_NONNULL_END // Implementation file#import "MVPView.h"

@implementation MVPView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self createUI];
    }
    returnself; } /** Create control */ - (void)createUI{UIImageView *imageView = [[UIImageView alloc] init]; [self addSubview:imageView]; self.imageView = imageView; UILabel *titleLbl = [[UILabel alloc] init]; titleLbl.textColor = [UIColor blackColor]; titleLbl.font = [UIFont systemFontOfSize:16]; titleLbl.textAlignment = NSTextAlignmentCenter; [self addSubview:titleLbl]; self.titleLbl = titleLbl; } - (void)layoutSubviews{ [super layoutSubviews]; self.imageView.frame = CGRectMake(0, 0, 100, 100); self.titleLbl.frame = CGRectMake(0, 100, 100, 50); } @endCopy the code

The realization of the Controller

#import "MVPViewController.h"
#import "MVPPresenter.h"

@interface MVPViewController ()

@property (strong, nonatomic) MVPPresenter *presenter;

@end

@implementation MVPViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createUI];
}

#pragma Mark - Build interface
- (void)createUI{

    self.presenter = [[MVPPresenter alloc] initWithViewController:self];
}

@end
Copy the code

The realization of the Presenter

/ / header files#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN @interface MVPPresenter : NSObject /** start method */ - (instancetype)initWithViewController:(UIViewController *)controller; NS_ASSUME_NONNULL_END // Implementation file#import "MVPPresenter.h"
#import "MVPView.h"
#import "MVPModel.h"

@interface MVPPresenter ()

/** viewController */
@property (nonatomic, weak) UIViewController *controller;

@end

@implementation MVPPresenter

- (instancetype)initWithViewController:(UIViewController *)controller{
    
    if (self = [super init]) {
        
        self.controller = controller;
        
        [self createUI];
    }
    returnself; } /** create control */ - (void)createUI{// create view MVPView *view = [[MVPView alloc] initWithFrame:CGRectMake(100, 100, 100, 150)]; [self.controller.view addSubview:view]; MVPModel *model = [[MVPModel alloc] init]; model.photoUrl = @"classStudent";
    model.titleStr = @"Evening study"; / / assignment view. ImageView. Image = [UIImage imageNamed: model. PhotoUrl]; view.titleLbl.text = model.titleStr; } @endCopy the code