版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、软件设计模式(一),潘爱民 ,Why Patterns?,内容,从一个例子看模式 关于模式的研究情况 介绍一些重要的模式(部分) 第三次作业,一个设计例子,VC/Samples/MFC/OLE/DrawCli,DrawCli的基础,MFC提供的基础 Doc/View结构 Doc提供了强大的数据管理功能 View提供了强大的显示功能 splitwnd功能 CWinApp/CMainFrame 提供了一套命令处理流程 对OLE的封装 Active Container OLE Clipboard Property Page 数据结构管理功能 CObject/CObList,我们的设计焦点,用C+对象
2、来表示每一个图元 定义图元的公共接口 如何处理用户的操作 鼠标的动作 图元对象的创建和管理,图元基类,class CDrawObj : public CObject / Attributes CDrawDoc* m_pDocument;/ owner virtual int GetHandleCount(); virtual CPoint GetHandle(int nHandle); virtual HCURSOR GetHandleCursor(int nHandle); virtual void SetLineColor(COLORREF color); virtual void Set
3、FillColor(COLORREF color); / Operations virtual void Draw(CDC* pDC); virtual void DrawTracker(CDC* pDC, TrackerState state); virtual void MoveTo(const CRect,图元层次结构,CDrawObj CDrawRect CDrawPoly CDrawOleObj ,CDrawObj,CDrawRect,CDrawPoly,CDrawOleObj,图元的创建,永久支持: CDocument:Serialize-CObList:Serialize-COb
4、ject:Serialize CDrawObj:Clone DrawTool:创建图元对象,交互操作,键盘和菜单命令 MFC内部机制 鼠标操作 在CDrawView的鼠标处理函数中处理 工具箱:工具链,有一个当前活动工具 转交给当前活动工具来处理,工具箱和CDrawTool,class CDrawTool / Constructors public: CDrawTool(DrawShape nDrawShape); / Overridables virtual void OnLButtonDown(CDrawView* pView, UINT nFlags, const CPoint,Draw
5、Tool层次结构,CDrawTool CSelectTool CRectTool CPolyTool ,CDrawTool,CSelectTool,CRectTool,CPolyTool,例子中的模式,CDrawObj和CDrawTool合起来构成了Factory Method模式 CDrawTool:Clone用到了原型创建模式 每一个CDrawTool都是一个singleton Adapter模式:把OLE对象封装成CDrawObj * 可以增加Composite模式 facade模式:通过CDrawDoc/View与MFC通信 chain of responsibility:如鼠标处理工
6、作 * 用Command模式增加undo/redo功能,patterns,定义: A physical arrangement of elements Repeating;with some degree of correspondence in successive trials or observations 典范、范例,事物的标准样式 In the book “Design Patterns”, the design patterns are descriptions of communicating objects and classes that are customized to
7、solve a general design problem in a particular context Design patterns represent solutions to problems that arise when developing software within a particular context 几个近义词 idiom、technique、paradigm,About patterns,About patterns Documentation of expert software engineers behavior Documentation of spe
8、cific reoccurring problems (and solutions) Abstraction of common design occurrences Properties of design patterns A pattern addresses a recurring design problem that arises in specific design situations, and presents a solution to it. Patterns document existing, well-proven design experience. Patter
9、ns provide a common vocabulary and understanding for design principles. Patterns are a means of documenting software architectures. Patterns support the construction of software with define properties. Patterns help you to manage software complexity.,pattern与framework,Patterns支持软件结构和设计的重用 抓住了特定领域中问题
10、的成功解决方案中的静态、动态结构和相互之间的协作关系 patterns与开发语言无关,但是建立在一定的环境基础上 例如:经典的MVC、Factory Method frameworks支持细节设计和代码的重用 framework是一组组件的综合,这些组件相互协作,为一族相关应用提供了一个可重用的框架结构 例如:MMC、MS Script Engine 两者结合起来, design patterns and frameworks有助于提高软件的质量 比如:重用性,扩展性,性能,可维护性,Design pattern与framework(续),a framework supplies the in
11、frastructure and mechanisms that execute a policy for interaction between abstract components with open implementations. frameworks are often said to abide by the Hollywood Principle (Dont call us, well call you.) 比较: Design patterns are more abstract than frameworks Design patterns are smaller arch
12、itectural elements than frameworks Design patterns are less specialized than frameworks framework与class library(toolkit),Pattern的研究情况,关于pattern研究的历史 A Pattern Language,Christopher Alexander,1977 “Advanced C+:Programming Styles and Idioms”,James Coplien,1992 “Design Patterns: Elements of Reusable Obj
13、ect-Oriented Software”,GOF,1995 “Pattern-Oriented Software Architecture: A System of Patterns” (简称为“POSA”) ,GoV,1996 .,Pattern is a hot topic,在amazon上查找包含patterns的书(2002.12.12),Pattern的研究现状,pattern与Java pattern与CORBA pattern与系统结构 pattern与generic programming结合 其他(例如UML等),POSA中的模式分类,Architectural Patt
14、erns 表达了软件系统的基本结构组织形式或者结构方案 它包含一组预定义的子系统,规定了这些子系统的责任,同时还提供了用于组织和管理这些子系统的规则和向导 Design Patterns 为软件系统的子系统、组件或者组件之间的关系提供一个精炼之后的解决方案 它描述了在特定环境下,用于解决通用软件设计问题的组件以及这些组件相互通信时的可重现结构 Idioms 是一个与编程语言相关的低级模式 它描述了如何实现组件的某些功能,或者利用编程语言的特性来实现组件内部要素之间的通信功能,POSA: Architectural Patterns(1),Architectural Patterns are v
15、ery high-level structural patterns. Also called “Conceptual Patterns ” From Mud to Structure: Organize components. Layers: Organize components into layers where layer is services are only used by layer i+1. Pipes and Filters: Divide the task into several sequential processing steps - the output of t
16、ask i is the input of task i+1. Blackboard: Several independent programs work cooperatively on a common data structure. Distributed Systems :Handle distributed computation. Broker: Introduce a broker component to to achieve better decoupling of clients and servers - brokers accept requests from clie
17、nts and forward the requests to servers, then return the results back to the clients.,POSA: Architectural Patterns(2),Interactive Systems: Keep a programs functional core independent of the user interface Model - View - Controller: Divides the application into processing, output, and input. View and
18、 controller parts are usually observers of the model via the observer pattern Presentation - Abstract - Control: Divides the application up to heirarchies or MVC-like components. Each component is dependent upon and provides functionality for the a higher-level component. There is only one top-level
19、 component Adaptable Systems : Design for change Microkernel Encapsulate the fundamental services of the application Reflection Divide the application into a meta-level and a base level to make the application self-aware. The meta level encapsulates knowledge of the system; the base level encapsulat
20、es knowledge about the problem domain,POSA: Design Patterns(1),Structure Decomposition: Decompose subsystems and complex components into cooperating parts. Whole - Part: Define a component that encapsulates smaller objects. Prevent clients from directly accessing the contained objects, but provide a
21、 interface for the aggregate. Organization of Work : Components collaborate to solve complex problems. Master - Slave The master divides a task among identical (but independent) slaves, the combines the slaves partial results to arrive a solution. Access Control : Guard and control access to service
22、s and components. Proxy: Clients communicate with a representative (proxy) rather than the target object itself. The proxy can perform pre- and post-processing to provide validation checking, access control, remote object access, extra computation, etc. See also Gamma et als proxy pattern,POSA: Desi
23、gn Patterns(2),Management: Handle homogenous collections of objects, services and components in their entirety. Command Processor: Extends Gamma et als command pattern by adding an explicit command processor View Handler: Separate the the management of views from the code required to present or cont
24、rol specific views. Similar to Gamma et als Abstract Factory and Mediator. Communication :Organize communication between components. Forward - Receiver: Contain all system-specific communication functionality in separate components so distributed peers can communicate without loosing portability Cli
25、ent - Dispatcher - Server: A dispatcher acts as an intermediate layer between clients and servers. The dispatcher provides the communication channel and a name service to hide physical locations. Publisher - Subscriber: Same as Gamma et als Observer pattern.,Idioms,Also called “Programming Patterns”
26、 Idioms are low-level patterns specific to a programming language. Counted Pointer: Simplifies memory management of shared objects providing reference counting. 其他 Virtual constructor smart pointer handle/body,关于“Design Pattern”,对已有模式的整理、分类 一套描述模式的词汇,可用于交流和文档化 为软件设计总结了宝贵的经验,这些设计经验可以被重用,但不是简单的代码重用 分类
27、: Creational Patterns Structural Patterns Behavioral Patterns 在软件设计模式领域,起到先驱的作用,重提:指导模式设计的三个概念,重用(reuse):是目标 两种重要的重用手段 Inheritance (2) builder构造许多复杂对象,Builder (五),Examples readers、parsers、converters the persistence of OLE documents,模式四:Prototype(一),Intent Specify the kinds of objects to create using
28、 a prototypical instance, and create new objects by copying this prototype. Motivation 以一个已有的对象作为原型,通过它来创建新的对象。在增加新的对象的时候,新对象的细节创建工作由自己来负责,从而使新对象的创建过程与框架隔离开来 Applicability 当产品的创建过程要独立于系统时 当产品的类型是在runtime时被指定的情况下 避免创建一个与product层次平行的factory层次时,Prototype(二),Structure Participants Prototype、ConcreteProt
29、otype、Client Collaborations,Prototype(三),Evaluation Adding and removing products at run-time Specifying new objects by varying values,降低系统中类的数目 Configuring an application with classes dynamically 要求:每一个product类都必须实现Clone操作 对于C+语言特别有意义:C+的class不是first-class objects Implementation Using a prototype ma
30、nager Implementing the Clone operation shallow copy versus deep copy Save & Load Initializing clones 两阶段构造,Prototype(四),Related patterns Prototype与Abstract Factory往往是相互竞争的 factory method Examples DrawCli,music editor,模式五:Singleton(一),Intent Ensure a class only has one instance, and provide a global
31、point of access to it. Motivation Its important for some classes to have exactly one instance. Instance-controlled class Applicability, Use the Singleton pattern when there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point. when the sole ins
32、tance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.,Singleton(二),Structure Participants Singleton Collaborations Clients access a Singleton instance solely through Singletons Instance operation.,Singleton(三),Evaluation Contr
33、olled access to sole instance Reduced name space Permits refinement of operations and representation,允许子类化 Permits a variable number of instances More flexible than class operations (static member functions in C+ ) 这种思想比较适用于Object-Based中的许多情形 Implementation Ensuring a unique instance 考虑使用lazy initialize 使用global/static object的缺点 Subclassing the Singleton class,Singleton(四),Related patterns Singleton与其他创建型模式并不矛盾,可以用singleton来实现其他模式中的对象。包括Abstract Factory、Builder、Prototype等。 多个实例对于构造过程往往并无意义,所以在许多情况下singleton模式比
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 护理部质量持续改进总结
- 2023年人教版七7年级下册数学期末复习卷附答案
- 2026-2027学年统编版八年级道德与法治 第二单元 维护社会秩序测试卷(含答案)
- 中信银行信用卡中心宝鸡分中心招聘考试真题及答案
- 中国平安并购汽车之家的绩效分析
- Rabaichromone-生命科学试剂-MCE
- PCR定量分析HPV感染宫颈癌筛查
- 危机管理在医学生殖中心护理中的应用
- 综治维稳工作情况汇报
- 妇科肿瘤日间病房管理规范
- 2026年徐州市中考道德与法治试卷(含答案)
- 慢性痛风性关节炎 疑难病例教学查房|多维度诊疗护理深度研讨
- 2026年秋季学期中小学1530安全教育记录
- 慢阻肺患者氧疗护理要点
- 浙江省强基联盟2025-2026学年高一上学期12月月考物理试题(图片版)
- 肿瘤患者的安宁疗护
- 杀猪宴活动策划方案(3篇)
- 投资发展部门年度工作总结报告
- 机械通气患者脱机训练方案
- 三年(2023-2025)中考化学真题分类汇编(全国):专题22 实验探究题(原卷版)
- 鲁教版高中地理必修第一册期末复习全册知识点提纲
评论
0/150
提交评论