`

ios:设计模式

    博客分类:
  • ios
ios 
阅读更多
========================================
MVVM (mode viewMode view mode)
========================================
MVC (C 管家)
////////////////////////C层
#import "LoginController.h"
#import "LoginContainerView.h"
#import "LoginModel.h"
#import "UIAlertView+show.h"
@interface LoginController ()

@end

@implementation LoginController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

#pragma mark InitView
- (void)initView
{
    LoginContainerView *container = [[LoginContainerView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:container];
    __weak typeof(self) ws = self;
    container.loginHandler = ^(NSString *account,NSString *pwd){
        [ws loginWithAccount:account pwd:pwd];
    };
}

#pragma mark Event Handle
- (void)loginWithAccount:(NSString *)account pwd:(NSString *)pwd
{
    [[LoginModel shareInstance] loginWithAccount:account password:pwd success:^{
        [UIAlertView showAlertWithTitle:@"登录成功"];
    } failure:^{
        [UIAlertView showAlertWithTitle:@"登录失败"];
    }];
}

@end

///////////////////////////////V层
#import "LoginContainerView.h"
@interface LoginContainerView()
@property (nonatomic,strong) UITextField *accountTextField;
@property (nonatomic,strong) UITextField *pwdTextField;
@property (nonatomic,weak) id delegate;

@end
@implementation LoginContainerView
#pragma mark LifeCycle
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}
#pragma mark InitUI
- (void)initUI
{
    self.backgroundColor = [UIColor whiteColor];
    self.accountTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 44)];
    self.accountTextField.center = CGPointMake(self.center.x, 100);
    self.accountTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.accountTextField.placeholder = @"请输入用户名";
   
    self.pwdTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 44)];
    self.pwdTextField.center = CGPointMake(self.center.x, 160);
    self.pwdTextField.secureTextEntry = YES;
    self.pwdTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.pwdTextField.placeholder = @"请输入密码";
   
    [self addSubview:self.accountTextField];
    [self addSubview:self.pwdTextField];
   
    UIButton *loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 60)];
    loginBtn.center = CGPointMake(self.center.x, 220);
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:loginBtn];
}

#pragma mark Event Handle
- (void)login
{
    [self endEditing:YES];
    if (!self.accountTextField.hasText || !self.pwdTextField.hasText) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"账号或密码不能为空" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        return;
    }
    if (self.loginHandler) {
        self.loginHandler(self.accountTextField.text,self.pwdTextField.text);
    }
}
@end

//////////////////////////M层
#import "LoginModel.h"
@interface LoginModel()

@end
@implementation LoginModel
+ (instancetype)shareInstance
{
    static LoginModel *_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}

- (void)loginWithAccount:(NSString *)account password:(NSString *)pwd success:(void (^)())loginSuccess failure:(void (^)())loginFailed;
{
    if ([account isEqualToString:@"test"]&&[pwd isEqualToString:@"123456"]) {
        loginSuccess();
    }else{
        loginFailed();
    }
}

@end

========================================
MVP (P 秘书)
1、View 层比较简单明,就是 View 的一些封装、重用。在一款精心设计过的 App 里面,应该有很多 View 是可以封装重用的。比如一些自己的 TableViewCell,自己设计的 Button,一些 View(包含一些子 View,UI 精心设计过,在项目里多处出现的)等等。

2、Model 层应该不仅仅是创建一个数据对象,还应该包含网络请求,以及数据 SQLite 的 CRUD 操作(比如 iOS 平台,一般以 FMDB 框架直接操作 sql,或者用 CoreData) 。一般可以将数据对象是否需要缓存设计成一个字段 isCache,或者针对整个项目设计一个开存储关,决定整个项目是否需要数据缓存。我们常见的新闻类 App,在离线的时候看到的数据,都是做了缓存处理的。比如一些金融类的 App,实时性比较高,是不做缓存的。

3、Presenter 层并不涉及数据对象的网络请求和 SQLite 操作,只是 Model 层和 View 层的一个桥梁。Presenter 层就不至于太臃肿,容易看懂。
/////////Controller
#import "LoginViewController.h"
#import "UIAlertView+show.h"
#import "LoginPresenter.h"
@interface LoginViewController ()
@property (nonatomic,strong) UITextField *accountTextField;
@property (nonatomic,strong) UITextField *pwdTextField;
@property (nonatomic,strong) LoginPresenter *presenter;

@end

@implementation LoginViewController
- (instancetype)initWithPresenter:(LoginPresenter *)presenter
{
    if (self = [super init]) {
        self.presenter = presenter;
    }
    return self;
}

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

#pragma mark InitUI
- (void)initUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.accountTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 44)];
    self.accountTextField.center = CGPointMake(self.view.center.x, 100);
    self.accountTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.accountTextField.placeholder = @"请输入用户名";
   
    self.pwdTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 44)];
    self.pwdTextField.center = CGPointMake(self.view.center.x, 160);
    self.pwdTextField.secureTextEntry = YES;
    self.pwdTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.pwdTextField.placeholder = @"请输入密码";
   
    [self.view addSubview:self.accountTextField];
    [self.view addSubview:self.pwdTextField];
   
    UIButton *loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 60)];
    loginBtn.center = CGPointMake(self.view.center.x, 220);
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
}

#pragma mark Event Handle
- (void)login
{
    [self.view endEditing:YES];
    if (!self.accountTextField.hasText || !self.pwdTextField.hasText) {
        [UIAlertView showAlertWithTitle:@"账号或密码不能为空"];
        return;
    }
    [self.presenter loginWithAccount:self.accountTextField.text password:self.pwdTextField.text success:^{
        [UIAlertView showAlertWithTitle:@"登录成功"];
    } failure:^{
        [UIAlertView showAlertWithTitle:@"登录失败"];

    }];
}


@end

////////P层
#import "LoginPresenter.h"
#import "LoginModel.h"
@implementation LoginPresenter
- (void)loginWithAccount:(NSString *)account password:(NSString *)pwd success:(void (^)())loginSuccess failure:(void (^)())loginFailed
{
    [[LoginModel shareInstance] loginWithAccount:account password:pwd success:^{
        loginSuccess();
    } failure:^{
        loginFailed();
    }];
}
@end

/////////M层
#import "LoginModel.h"
@interface LoginModel()

@end
@implementation LoginModel
+ (instancetype)shareInstance
{
    static LoginModel *_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}

- (void)loginWithAccount:(NSString *)account password:(NSString *)pwd success:(void (^)())loginSuccess failure:(void (^)())loginFailed;
{
    if ([account isEqualToString:@"test"]&&[pwd isEqualToString:@"123456"]) {
        loginSuccess();
    }else{
        loginFailed();
    }
}


@end
分享到:
评论

相关推荐

    《Objective-C编程之道ios设计模式解析》电子书

    《Objective-C编程之道:iOS设计模式解析》是一本专注于探讨iOS应用开发中设计模式的专著。这本书深入浅出地讲解了如何在Objective-C编程环境中有效地应用设计模式,旨在提升开发者对iOS应用架构的理解和实践能力。...

    IOS 设计模式 + 代码

    在iOS开发中,设计模式是一种解决常见编程问题的模板或最佳实践,它们是经验丰富的...Objective-C编程之道:iOS设计模式解析这本书可能详细介绍了这些模式的原理和在实际项目中的应用,是一份非常有价值的学习资源。

    iOS 设计模式及源码实现

    通过阅读“IOS设计模式解析电子书+源代码”,开发者可以更好地理解和运用这些设计模式,提高代码质量和开发效率。实际的源码实现部分对于加深理解、实践应用以及解决实际问题有着极大的帮助。在iOS开发过程中,熟练...

    Objective-C编程之道:IOS设计模式解析.pdf

    Objective-C编程之道:IOS设计模式解析.pdf

    ios设计模式学习实例

    本实例“ios设计模式学习实例”聚焦于Cocoa框架中的设计模式应用,下面将详细阐述其中涉及到的主要设计模式。 1. 创建型设计模式: - 单例(单态):单例模式确保一个类只有一个实例,并提供全局访问点。在iOS中,...

    Objective-C编程之道IOS设计模式解析.pdf

    最全最新版 Objective-C编程之道IOS设计模式解析.pdf

    ios 简单工厂设计模式

    在iOS开发中,设计模式是一种解决常见编程问题的模板,它们是经验的总结,能够提高代码的可读性、可维护性和复用性。简单工厂设计模式是其中一种创建型设计模式,它提供了一个创建对象的接口,但允许子类决定实例化...

    iOS 设计模式 英文版

    本书《Pro Design Patterns in Swift》以iOS Swift开发为背景,详尽地展示了如何利用Swift语言的特点来应用最核心、持久的设计模式,提升应用程序的结构和可扩展性。设计模式在软件开发中扮演着至关重要的角色,它们...

    编程之道-IOS设计模式解析[www.xiandoudou.com]

    总之,《编程之道-IOS设计模式解析》是一本全面且实用的iOS设计模式指南,它不仅能提升开发者对设计模式的理解,也能帮助他们更好地应对iOS开发中的各种挑战。通过深入学习和实践,开发者可以提升自己的编程技巧,...

    IOS设计模式解析

    "iOS设计模式解析"这个主题涵盖了如何在Objective-C编程中应用这些模式来提高代码质量、可维护性和可扩展性。 设计模式可以分为三类:创建型、结构型和行为型。在iOS开发中,以下是一些关键的设计模式: 1. 单例...

    IOS 设计模式架构设计实例Demo

    通过这个"IOS设计模式架构设计实例Demo",开发者不仅能学习到具体的设计模式如何使用,还能了解到如何在实际项目中选择合适的架构,以及如何将设计模式融入到架构中,以提升应用的整体质量。因此,对于想要深入学习...

    ios三种设计模式

    在iOS应用开发中,设计模式是提升代码可读性、可维护性和可扩展性的重要工具。以下是关于三种主要设计模式——MVC(Model-View-Controller)、MVP(Model-View-Presenter)和MVVM(Model-View-ViewModel)的详细讲解...

    ios设计模式开发23种设计模式OC编程

    本资源“ios设计模式开发23种设计模式OC编程”提供了Objective-C(OC)语言实现的全部23种经典设计模式的实例,可以直接在Xcode项目中导入使用。下面我们将详细探讨这23种设计模式及其在iOS开发中的应用。 1. **...

    iOS MVVM设计模式

    **iOS MVVM设计模式** MVVM(Model-View-ViewModel)设计模式在iOS开发中逐渐流行,尤其是在Objective-C和Swift的环境中。它是一种基于MVC(Model-View-Controller)的改进模式,旨在提高代码的可测试性、可维护性...

    IOS 设计模式抽象工厂实例Demo

    在iOS开发中,设计模式是解决常见编程问题的模板,它们提供了一种标准的方法来组织代码,使得代码更易于理解、扩展和维护。抽象工厂模式是设计模式中的一种,尤其适用于创建一组相关或相互依赖的对象。这个实例Demo...

    iOS开发中的几种设计模式介绍

    本文将深入探讨几种在iOS开发中常用的设计模式:代理模式、观察者模式、MVC模式、单例模式、策略模式以及工厂模式。 1. **代理模式**: 代理模式在iOS开发中广泛应用,主要用于对象间通信。例如,UITableView的...

    读书笔记:IOS设计模式探索(大话设计模式).zip

    读书笔记:IOS设计模式探索(大话设计模式)

    iOS设计模式解析.pdf

    iOS设计模式解析.pdf

    iOS设计模式之原型模式

    在iOS开发中,设计模式是一种重要的编程思想,它将实践中常用的设计策略抽象出来,以便于复用和提高代码质量。本文将深入探讨一种常见的设计模式——原型模式(Prototype Pattern),并结合具体的iOS应用场景进行...

    ios 设计模式

    ### iOS设计模式详解 #### 一、单例模式 单例模式是一种常用的设计模式,它的主要目的是确保一个类仅有一个实例,并且该实例能够被全局访问。这种模式非常适合那些需要频繁访问并保持状态一致的对象,比如全局配置...

Global site tag (gtag.js) - Google Analytics