- 浏览: 27442 次
- 性别:
- 来自: 上海
-
最新评论
这两天死了不少人,南北呼应。厦门的兄弟们别挤brt了,公交有风险,挤车需谨慎!
继续介绍Bee的mvc,框架中已经为MVC做了一定程度的封装,我们查看Bee的源码会发现对应着BeeController、BeeModel以及各种常用的View和ViewController,另外ViewLayout添加了View界面布局的灵活性,从v0.3.0版本开始更是加入了基于XML的UI模板模块。
还是先上个例子比较直观些。
这个例子,通过访问国家气象局天气预报JSON数据接口,http://www.weather.com.cn/data/sk/101010100.html,返回天气数据。
看下目录结构
把MVC分离出来放到不同的目录下。
Model层代码:
在.h中定义了一个Weather的实体,与天气数据对应,用到了BeeActiveRecord,这是Bee中的ORM框架,实现数据库与BeeActiveRecord的序列和反序列化。通常我们在做离线缓存时采用CoreData,bee采用的是DB的方式。感谢老郭为我们封装了原来的SQL语法,免去了封装枯燥的SQL语句。查看BeeActiveRecord的父类BeeDatabase,可以看到一些开源软件上用的很欢的以Block为参数的写法。
在.m中通过
再上Controller的实现
WeatherController完成与天气预报服务器交互的工作,和UISignle类似,在.h中通过AS_MESSAGE(LOAD_WEATHER)定义了一个LOAD_WEATHER的方法声明,在.m中用DEF_MESSAGE(LOAD_WEATHER)对应,并完成LOAD_WEATHER的方法实现。在bee中网络层使用的是ASIHTTPRequest框架,至于为什么不用AFNetworking,这实际上是个人习惯的问题,虽然ASIHTTPRequest已经N久没更新过了,不过也够用了。BeeMessage中对ASIHTTPRequest做了很多工作,通过BeeRequestQueue,对post和get过程中状态的分离。
再来看view层,其实在这个例子中做真正做逻辑的是ViewController,注意它的父类为BeeUIBoard,正是这个类做了UIMessage的消息转发,还实现了ViewController生命周期中的UISignal化。
这里应注意ViewController.m中的代码:
如果把
本篇通过天气预报介绍了Bee中的MVC的实现场景,当然你也可以根据自己的需要选择适用的模块,你可以在Model中直接实现网络层交互,也可以去掉model层,直接在UIBoard中调用BeeController,怎么选择还是看个人习惯。
以上代码下载:https://github.com/ilikeido/BeeFrameworkTest/tree/master/lesson4
继续介绍Bee的mvc,框架中已经为MVC做了一定程度的封装,我们查看Bee的源码会发现对应着BeeController、BeeModel以及各种常用的View和ViewController,另外ViewLayout添加了View界面布局的灵活性,从v0.3.0版本开始更是加入了基于XML的UI模板模块。
还是先上个例子比较直观些。

这个例子,通过访问国家气象局天气预报JSON数据接口,http://www.weather.com.cn/data/sk/101010100.html,返回天气数据。
看下目录结构

把MVC分离出来放到不同的目录下。
Model层代码:
// // WeatherModel.h // BeeFrameWorkTest // // Created by he songhang on 13-6-8. // Copyright (c) 2013年 he songhang. All rights reserved. // #import <BeeFramework/Bee.h> @interface Weather : BeeActiveRecord @property(nonatomic,strong) NSString *city;//城市 @property(nonatomic,strong) NSString *temp;//气温 @property(nonatomic,strong) NSString *WD;//风向 @property(nonatomic,strong) NSString *WS;//风力 @property(nonatomic,strong) NSString *time;//发布时间 @end @interface WeatherModel : BeeModel @property(nonatomic,strong) Weather *weather; -(void)loadOnline;//查询天气 -(void)loadCache;//读取历史记录 @end
// // WeatherModel.m // BeeFrameWorkTest // // Created by he songhang on 13-6-8. // Copyright (c) 2013年 he songhang. All rights reserved. // #import "WeatherModel.h" #import "WeatherController.h" @implementation Weather +(void)mapRelation{ [super mapPropertyAsKey:@"city"]; [super mapRelation]; } @end @interface WeatherModel (private) - (void)saveCache; - (void)asyncSaveCache; @end @implementation WeatherModel -(void)loadOnline{ [self sendMessage:WeatherController.LOAD_WEATHER]; } -(void)loadCache{ int count = Weather.DB.WHERE(@"city",@"北京").COUNT().resultCount; if (count>0) { self.weather = [Weather.DB.WHERE(@"city",@"北京").GET_RECORDS() lastObject]; } } - (void)saveCache { [NSObject cancelPreviousPerformRequestsWithTarget:self]; [self performSelector:@selector(asyncSaveCache) withObject:nil afterDelay:0.1f]; } - (void)asyncSaveCache { _weather.SAVE(); } - (void)handleMessage:(BeeMessage *)msg { [super handleMessage:msg]; } - (void)handleWeatherController:(BeeMessage *)msg { if ( [msg is:WeatherController.LOAD_WEATHER] ) { if ( msg.succeed ) { NSDictionary *dict = [msg.output dictAtPath:@"result"]; if (dict) { self.weather = (Weather *)[dict objectForClass:[Weather class]]; [self saveCache]; } } } [super handleMessage:msg]; } @end
在.h中定义了一个Weather的实体,与天气数据对应,用到了BeeActiveRecord,这是Bee中的ORM框架,实现数据库与BeeActiveRecord的序列和反序列化。通常我们在做离线缓存时采用CoreData,bee采用的是DB的方式。感谢老郭为我们封装了原来的SQL语法,免去了封装枯燥的SQL语句。查看BeeActiveRecord的父类BeeDatabase,可以看到一些开源软件上用的很欢的以Block为参数的写法。
在.m中通过
[self sendMessage:WeatherController.LOAD_WEATHER];向Weathercontroller发送了一个LOAD_WEATHER的请求,在handleWeatherController中响应BeeMessage的不同状态,并完成离线数据的保存工作
[self saveCache];
再上Controller的实现
// // WeatherController.h // BeeFrameWorkTest // // Created by he songhang on 13-6-8. // Copyright (c) 2013年 he songhang. All rights reserved. // #import "Bee_Controller.h" @interface WeatherController : BeeController AS_MESSAGE(LOAD_WEATHER) @end
// // WeatherController.m // BeeFrameWorkTest // // Created by he songhang on 13-6-8. // Copyright (c) 2013年 he songhang. All rights reserved. // #import "WeatherController.h" #import "JSONKit.h" #import <BeeFramework/Bee.h> @implementation WeatherController DEF_MESSAGE(LOAD_WEATHER) -(void)LOAD_WEATHER:(BeeMessage *)msg{ if ( msg.sending ) { NSString * callURL = @"http://www.weather.com.cn/data/sk/101010100.html" ; msg.HTTP_GET( callURL ); } else if ( msg.progressed ) { } else if ( msg.succeed ) { NSDictionary * jsonData = [msg.response objectFromJSONData]; if ( nil == jsonData ) { [msg setLastError]; return; } NSDictionary *weatherinfo = (NSDictionary *)[jsonData objectAtPath:@"weatherinfo"]; [msg output:@"result", weatherinfo, nil]; } else if ( msg.failed ) { } else if ( msg.cancelled ) { } } @end
WeatherController完成与天气预报服务器交互的工作,和UISignle类似,在.h中通过AS_MESSAGE(LOAD_WEATHER)定义了一个LOAD_WEATHER的方法声明,在.m中用DEF_MESSAGE(LOAD_WEATHER)对应,并完成LOAD_WEATHER的方法实现。在bee中网络层使用的是ASIHTTPRequest框架,至于为什么不用AFNetworking,这实际上是个人习惯的问题,虽然ASIHTTPRequest已经N久没更新过了,不过也够用了。BeeMessage中对ASIHTTPRequest做了很多工作,通过BeeRequestQueue,对post和get过程中状态的分离。
再来看view层,其实在这个例子中做真正做逻辑的是ViewController,注意它的父类为BeeUIBoard,正是这个类做了UIMessage的消息转发,还实现了ViewController生命周期中的UISignal化。
// // ViewController.h // BeeFrameWorkTest // // Created by he songhang on 13-6-3. // Copyright (c) 2013年 he songhang. All rights reserved. // #import <UIKit/UIKit.h> #import <BeeFramework/Bee.h> #import "WeatherModel.h" @interface ViewController : BeeUIBoard{ __strong WeatherModel *_weatherModel; __weak IBOutlet UITextView *textView; } @end
// // ViewController.m // BeeFrameWorkTest // // Created by he songhang on 13-6-3. // Copyright (c) 2013年 he songhang. All rights reserved. // #import "ViewController.h" #import "WeatherController.h" @implementation ViewController -(void)load{ _weatherModel = [[WeatherModel alloc]init]; [_weatherModel addObserver:self]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)handleUISignal:(BeeUISignal *)signal { [super handleUISignal:signal]; } - (void)handleUISignal_BeeUIBoard:(BeeUISignal *)signal { [super handleUISignal:signal]; if ( [signal is:BeeUIBoard.CREATE_VIEWS] ) { [self showNavigationBarAnimated:YES]; [self showBarButton:UINavigationBar.BARBUTTON_RIGHT system:UIBarButtonSystemItemRefresh]; self.title = @"天气预报"; } else if ( [signal is:BeeUIBoard.DELETE_VIEWS] ) { } else if ( [signal is:BeeUIBoard.LOAD_DATAS] ) { [_weatherModel loadCache]; [self refushTextView]; } else if ( [signal is:BeeUIBoard.WILL_APPEAR] ) { } else if ( [signal is:BeeUIBoard.DID_DISAPPEAR] ) { } } - (void)handleUISignal_UINavigationBar:(BeeUISignal *)signal { if ( [signal is:UINavigationBar.BACK_BUTTON_TOUCHED] ) { } else if ( [signal is:UINavigationBar.DONE_BUTTON_TOUCHED] ) { [self loadOnline]; } } -(void)loadOnline{ [_weatherModel loadOnline]; } -(void)refushTextView{ if (_weatherModel.weather) { Weather *weather = _weatherModel.weather; NSString *weatherInfo = [NSString stringWithFormat:@"城市:%@\n气温:%@\n风向:%@\n风力%@\n发布时间:%@,",weather.city,weather.temp,weather.WD,weather.WS,weather.time]; textView.text = weatherInfo; } } - (void)handleMessage:(BeeMessage *)msg { [super handleMessage:msg]; } - (void)handleWeatherController:(BeeMessage *)msg { [super handleMessage:msg]; if ( [msg is:WeatherController.LOAD_WEATHER] ) { if ( msg.sending ) { BeeUIActivityIndicatorView * indicator = [BeeUIActivityIndicatorView spawn]; [self showBarButton:UINavigationBar.BARBUTTON_RIGHT custom:indicator]; [indicator startAnimating]; } else { [self showBarButton:UINavigationBar.BARBUTTON_RIGHT system:UIBarButtonSystemItemRefresh]; } if ( msg.succeed ) { [self refushTextView]; } } } @end

这里应注意ViewController.m中的代码:
-(void)load{ _weatherModel = [[WeatherModel alloc]init]; [_weatherModel addObserver:self];//注意点!!! },结合WeatherModel.m中的
- (void)handleWeatherController:(BeeMessage *)msg { if ( [msg is:WeatherController.LOAD_WEATHER] ) { if ( msg.succeed ) { NSDictionary *dict = [msg.output dictAtPath:@"result"]; if (dict) { self.weather = (Weather *)[dict objectForClass:[Weather class]]; [self saveCache]; } } } [super handleMessage:msg];//注意点 }
如果把
[super handleMessage:msg];去掉,那么ViewController中的handleMessage和handleWeatherController将无法响应。
本篇通过天气预报介绍了Bee中的MVC的实现场景,当然你也可以根据自己的需要选择适用的模块,你可以在Model中直接实现网络层交互,也可以去掉model层,直接在UIBoard中调用BeeController,怎么选择还是看个人习惯。
以上代码下载:https://github.com/ilikeido/BeeFrameworkTest/tree/master/lesson4
发表评论
-
xcode模拟器运行僵死在 attaching to 的解决法子
2013-07-19 15:45 1119修改Scheme为GDB可以运行时。 检查你的网络127.0 ... -
BeeFramework 系列二 UISignal篇下
2013-06-04 12:42 2355上篇,我们讲了UISignal的工作原理,以及Be ... -
BeeFramework 系列二 UISignal篇上
2013-06-03 16:28 3204BeeFramework中用的最多的就是UISignal了,这 ... -
BeeFramework 系列一 安装篇(Arc)
2013-06-03 10:00 3794Beeframework 是一款iOS快速开发框架,它以UIS ... -
发布程序 通过safari直接下载手机安装
2012-01-30 22:37 2173. Installing Apps Wirelessly ...
相关推荐
BeeFramework 在实现 MVC 上有其独特之处: 1. **Model**:模型层负责处理数据和业务逻辑,BeeFramework 提供了数据模型的抽象和管理,使得数据操作更加规范和高效。 2. **View**:视图层负责展示用户界面,...
在三层MVC架构中,View层并不直接与Model交互,而是通过Controller获取Model的数据并渲染到用户界面上。这样可以确保视图只关注数据的显示,不涉及数据的处理。 **控制器(Controller)**作为Model和View之间的桥梁...
MVC三层架构基础介绍简单来说,Design Patten 就是一个常用的方案。 在我们的开发过程中,经常会遇到一些相同或者相近的问题,每次我们都会去寻找一个新的解决方法,为了节省时间提高效率,我们提供一些能够解决这些...
【C# MVC 三层架构详解】 C# MVC(Model-View-Controller)是一种设计模式,广泛应用于.NET框架下的Web应用程序开发。它将应用程序分为三个主要部分:模型(Model)、视图(View)和控制器(Controller),以实现...
C#本科期末大作业MVC三层架构亮灯的仓库管理系统源码。MVC+MYSQL+EasyuiC#本科期末大作业MVC三层架构亮灯的仓库管理系统源码。MVC+MYSQL+EasyuiC#本科期末大作业MVC三层架构亮灯的仓库管理系统源码。MVC+MYSQL+...
1. **MVC模式**:MVC模式是软件设计中的一种经典模式,它将应用程序分为三个主要组件:模型(Model)、视图(View)和控制器(Controller)。模型负责业务逻辑和数据管理,视图负责展示数据,而控制器处理用户交互并...
三层架构是一种更高级别的架构设计,它在MVC的基础上增加了数据访问层(Data Access Layer, DAL)。三层架构包括表现层(Presentation Layer,对应MVC中的Controller+View)、业务逻辑层(Business Logic Layer,...
MVC 模式是软件设计中一种非常重要的模式,它将软件或组件分为三个部分:模型(Model)、视图(View)和控制器(Controller)。这种模式已经 menjadi 必备的开发模式之一,广泛应用于 Web 开发、移动应用开发等领域...
Model(模型)是应用程序中用于处理应用程序数据逻辑的部分。 通常模型对象负责在数据库中存取数据。 View(视图)是应用程序中处理数据显示的部分。...这个适合刚接触三层架构的来学习,比较简单。
本程序一个基于三层架构的MVC模式应用的完整示例项目源码,基于Asp.net 3.5开发, 结构更简洁,提供更多有效的示例源码参考。 方便用户更好的理解和使用该架构进行开发,配合动软.Net代码生成器,可以使开发效率...
BeeFramework是一款iOS平台的MVC应用快速开发框架,使用Objective-C开发。 其早期原型曾经被应用在QQ空间 、QQ游戏大厅 等多款精品APP中。 BeeFramework 从根本上解决了iOS开发者长期困扰的各种问题,诸如:分层架构...
对于本实例中的"ASP.NET MVC1.0 BBS 简易Demo教学版源码Ajax",可能包含数据库实体类(Models)以及数据库上下文(DbContext)来实现数据访问层。 BBS(Bulletin Board System,电子公告板系统)是在线讨论和信息...
在本项目中,"EF+MVC+三层(MVC +三层架构+EF对数据库对数据库进行增删改查小案例).zip"是一个综合性的示例,它演示了如何利用Entity Framework(EF)、Model-View-Controller(MVC)架构以及三层架构来实现对...
本课程“ASP.NET MVC框架开发系列课程2_一个简单的ASP.NET MVC应用程序WebCast20080425Video”旨在深入讲解如何使用该框架来构建实际的Web应用程序。 首先,我们要理解MVC模式的基本概念。模型(Model)负责处理...
在本课程中,我们将深入探讨ASP.NET MVC的正式版本,这个版本通常代表着稳定性和性能的优化,以及一系列新功能的引入。 ASP.NET MVC框架的核心理念在于分离关注点,将业务逻辑、数据处理和用户界面三者有效地解耦。...
MVC模式是一种软件设计模式,它将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller)。这种分离提高了代码的组织性和可维护性。 1. **模型(Model)**:模型负责处理业务逻辑和数据管理...
综上所述,MVC模式与三层架构各有侧重,它们在软件开发的不同阶段发挥着重要作用。MVC模式更专注于表现层的设计与实现,适用于快速迭代和灵活调整用户界面的场景;而三层架构则关注整个应用的结构设计,适用于构建大...
在本示例中,我们将探讨如何在MVC(Model-View-Controller)框架下实现三层架构。 **一、MVC框架介绍** MVC是一种广泛应用于Web应用开发的设计模式,由模型(Model)、视图(View)和控制器(Controller)三部分...
2. **MVC架构支持**:遵循Model-View-Controller的设计模式,BeeFramework提供了对MVC的强力支持,帮助开发者更好地组织代码,保持良好的代码结构。 3. **网络通信**:框架内置了网络请求处理模块,支持HTTP/HTTPS...
UML图设计模式、三层架构、MVC.EAP