软件模式课程设计_第1页
软件模式课程设计_第2页
软件模式课程设计_第3页
软件模式课程设计_第4页
软件模式课程设计_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 软件设计模式 课程设计 课程名称: 软件设计模式 院 系: 计算机科学与工程学院 学生姓名: 学 号: 组 员: 专业班级: 软件 122 指导教师: 王竹荣老师 2015 年 秋季 学期 目 录一、 系统目标.1 设计目的.1 需求描述.1二、 系统模式选择.1 策略模式.1 单例模式.3 外观模式.4 代理模式.5三、 个人完成的工作设计.9四、实验结果.10五、心得体会.11六、 参考文献.121、 系统目标1. 设计目的设计模式的提出,是面向对象程序设计演化过程中一个重要的里程碑,设计模式使得人们可以更加简单和方便地去复用成功的软件设计和体系结构,从而能够帮助设计者更快更好地完成系统

2、设计。希望通过本次课程设计,可以更加了解设计模式的概念、设计模式的基本要素、设计模式的原则和设计模式的类型,以及各种设计模式的特点和适用情形,会根据不同的场景去灵活运用各种模式,加深对软件设计课程的理解。通过运用设计模式来解决实际系统中遇到的问题,巩固所学的理论知识,提高程序设计能力,从而具备利用计算机编程分析解决综合性实际问题的初步能力。2. 需求描述该系统是一个画板程序,我们用设计模式的思想来设计系统结构,然后实现基本图形的绘制功能。系统中总共用到以下设计模式,分别是代理模式,单例模式,外观模式,策略模式。2.1设计模式要求该模式应用到4种模式,其中涉及到的模式有单例模式、代理模式、策略模

3、式、外观模式。2.2画图基本要求可以实现画任意的曲线、图形的基本功能。2.3画图的高级要求二系统模式选择(1)策略模式策略模式,又叫算法簇模式,就是定义了不同的算法簇,并且可以之间互相替换,此模式让算法的变化独立于使用算法的客户。设计原则是把一个类中经常改变或者将来可能改变的部分提取出来,作为一个接口,然后在类中包含这个对象的实例,这样类的实例在运行时就可以随意调用实现了这个接口的类的行为。策略模式属于対象行为型模式,主要针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得他们可以相互替换。策略模式可以使得算法可以在不影响到客户端的情况下发生变化。策略模式适用于当一个应用程序需要

4、实现一种特定的服务或者功能,而且该程序有多种实现方式时使用。具体代码实现如下: -(void)createToolView:(NSArray *)titleArray CGFloat buttonWidth = (self.bounds.size.width - (titleArray.count + 1) * KtoolButtonSpace) / titleArray.count; for (NSInteger i = 0; i < titleArray.count; i +) ToolButton *button = ToolButton alloc initWithFrame:C

5、GRectMake(i * (buttonWidth + KtoolButtonSpace) + KtoolButtonSpace, 0, buttonWidth, self.bounds.size.height); button setTitle:titleArrayi forState:UIControlStateNormal; button setTag:i; button addTarget:self action:selector(toolButtonClick:) forControlEvents:UIControlEventTouchDown; self addSubview:b

6、utton; -(void)toolButtonClick:(ToolButton *)sender self.recordButton setSelected:NO; sender setSelected:YES; self.recordButton = sender; switch (sender.tag) case 0: self hideOtherSelectedView:self.colorView; self createSelectedColorView; break; case 1: self hideOtherSelectedView:self.widthView; self

7、 createSelectedWidthView; break; case 2: self hideOtherSelectedView:self.eraserView; self createSelectedEraserView; break; case 3: self hideOtherSelectedView:nil; _cancelBlock(); break; case 4: self hideOtherSelectedView:nil; _deleteAllBlock(); break; case 5: self hideOtherSelectedView:nil; _photoBl

8、ock(); break; case 6: self hideOtherSelectedView:nil; _saveBlock(); break; default: break; (2)单例模式单例模式是一种常用的软件设计模式。在他的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易与外界访问,从而便对实例个数的控制并节约系统资源。单例模式的目的是使得类的一个对象成为系统中的唯一实例。要实现这一点,可以从客户端对其进行实例化开始。因此需要用一种只允许生成对象类的唯一实例的机制,“阻止”所有想要生成对象的访问。使用工厂方法来限制实例化过程。这个方法

9、应该是静态方法(类方法),因为让类的实例去生成另一个唯一实例毫无意义。具体代码实现如下: typedef void(SelectedColorViewBlock)(UIColor * color);#pragma mark - 单例模式 +(SelectedColorView *)shareViewWithFrame:(CGRect)frame andBlock:(SelectedColorViewBlock)colorblock synchronized(shareInstance) static dispatch_once_t onceView; dispatch_once(&on

10、ceView, shareInstance = SelectedColorView alloc initWithFrame:frame andSelectedColorViewBlock:colorblock; ); return shareInstance;+(instancetype)allocWithZone:(struct _NSZone *)zone synchronized(shareInstance) static dispatch_once_t onceView; dispatch_once(&onceView, shareInstance = super allocW

11、ithZone:zone; ); return shareInstance;(3)外观模式外观模式(Facade),为子系统中的一组接口提供一个一致的界面,定义一个高层接口,这个接口使得这一子系统更加容易使用。适用场景 。在以下情况下使用外观模式:1.设计初期阶段,应该有意识的将不同阶层分离,层与层之间建立外观模式。2.开发阶段子系统越来越复杂,增加外观模式提供一个简单的调用接口。 3.维护一个大型遗留系统的时候,可能这个系统难以维护和扩展,但又包含非常重要的功能,为其开发一个外观类,以便新系统与其交互。使用外观模式实现了子系统与客户端之间的松耦合关系,客户端屏蔽了子系统组件,减少了客户端所需

12、处理的对象数目,使得子系统使用起来更加容易。(4)代理模式代理模式为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。抽象角色:通过接口或抽象类声明真实角色实现的业务方法。代理角色:实现抽象角色,是真实角色的代理,通过真实角色的业务逻辑方法来实现抽象方法,并可以附加自己的操作。真实角色:实现抽象角色,定义真实角色所要实现的业务逻辑,供代理角色调用。具体代码实现如下:typedef void(SelectedColorViewBlock)(UIColor * color);-(instancet

13、ype)initWithFrame:(CGRect)frame andSelectedColorViewBlock:(SelectedColorViewBlock)colorViewBlock if (self = super initWithFrame:frame) _colorBlock = colorViewBlock; self setBackgroundColor:UIColor yellowColor; self createToolItem; return self;typedef void(SelectedEraserViewBlock)(NSInteger width);-(

14、instancetype)initWithFrame:(CGRect)frame andSelectedEraserViewBlock:(SelectedEraserViewBlock)eraserBlock if (self = super initWithFrame:frame) _eraserBlock = eraserBlock; self setBackgroundColor:UIColor purpleColor; self createToolItemWithArray; return self;typedef void(SelectedWidthViewBlock)(NSInt

15、eger width);-(instancetype)initWithFrame:(CGRect)frame andSelectedWidthViewBlock:(SelectedWidthViewBlock)widthBlock if (self = super initWithFrame:frame) _widthBlock = widthBlock; self setBackgroundColor:UIColor orangeColor; NSArray *itemArray = 1,2,3,4,5,6,10,15,20,25; self createToolItemWithArray:

16、itemArray; return self;ToolView()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITableViewDataSource,UITableViewDelegate> SelectedColorViewBlock _colorBlock; SelectedWidthViewBlock _widthBlock; SelectedEraserViewBlock _eraserBlock; ToolViewBlock _cancelBlock; ToolViewBlock _d

17、eleteAllBlock; ToolViewBlock _photoBlock; ToolViewBlock _saveBlock;-(instancetype)initWithFrame:(CGRect)frame andSelectedColorViewBlock:(SelectedColorViewBlock)colorViewBlock andSelectedWidthViewBlock:(SelectedWidthViewBlock)widthBlock andSelectedEraserViewBlock:(SelectedEraserViewBlock)eraserBlock

18、andToolViewCancelBlock:(ToolViewBlock)cancelBlock andToolViewDeleteAllBlock:(ToolViewBlock)deleteAllBlock andPhotoBlock:(ToolViewBlock)photoBlock andSaveBlock:(ToolViewBlock)saveBlock if (self = super initWithFrame:frame) _cancelBlock = cancelBlock; _deleteAllBlock = deleteAllBlock; _photoBlock =pho

19、toBlock; _saveBlock = saveBlock; _colorBlock = colorViewBlock; _eraserBlock = eraserBlock; _widthBlock = widthBlock; self setBackgroundColor:UIColor lightGrayColor; NSArray *titleArray = "颜色","线宽","橡皮","撤销","清屏","相机","保存" self cre

20、ateToolView:titleArray; return self;-(void)toolButtonClick:(ToolButton *)sender self.recordButton setSelected:NO; sender setSelected:YES; self.recordButton = sender; switch (sender.tag) case 0: self hideOtherSelectedView:self.colorView; self createSelectedColorView; break; case 1: self hideOtherSele

21、ctedView:self.widthView; self createSelectedWidthView; break; case 2: self hideOtherSelectedView:self.eraserView; self createSelectedEraserView; break; case 3: self hideOtherSelectedView:nil; _cancelBlock(); break; case 4: self hideOtherSelectedView:nil; _deleteAllBlock(); break; case 5: self hideOtherSelectedView:nil; _photoBlock(); break; case 6: self hideOtherSelectedView:nil; _saveBlock(); break; default: break; 3 个人完成的工作设计外观模式(Facade),为子系统中的一组接口提供一个一致的界面,定义一个高层接口,这个接口使得这一子系统更加容易使用。策略模式,设计原则是把一个类中经常改变或者将来可能改变的部分提取出来,作为一个接口,然后在类中包含这个对象的实例,这样类的实例在运行时就可以随意调用实现了这个接

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论