《软件设计模式(一)》由会员分享,可在线阅读,更多相关《软件设计模式(一)(65页珍藏版)》请在金锄头文库上搜索。
1、软件设计模式(一),潘爱民 http:/ 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
3、SetFillColor(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
4、-CObject:Serialize CDrawObj:Clone DrawTool:创建图元对象,交互操作,键盘和菜单命令 MFC内部机制 鼠标操作 在CDrawView的鼠标处理函数中处理 工具箱:工具链,有一个当前活动工具 转交给当前活动工具来处理,工具箱和CDrawTool,class CDrawTool / Constructors public: CDrawTool(DrawShape nDrawShape); / Overridables virtual void OnLButtonDown(CDrawView* pView, UINT nFlags, const CPoint,
5、DrawTool层次结构,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
7、 to 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
8、of specific 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.
9、Patterns 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
11、the infrastructure 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 smal
12、ler architectural 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 Reus
13、able Object-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中的模式分类,Architectu
14、ral Patterns 表达了软件系统的基本结构组织形式或者结构方案 它包含一组预定义的子系统,规定了这些子系统的责任,同时还提供了用于组织和管理这些子系统的规则和向导 Design Patterns 为软件系统的子系统、组件或者组件之间的关系提供一个精炼之后的解决方案 它描述了在特定环境下,用于解决通用软件设计问题的组件以及这些组件相互通信时的可重现结构 Idioms 是一个与编程语言相关的低级模式 它描述了如何实现组件的某些功能,或者利用编程语言的特性来实现组件内部要素之间的通信功能,POSA: Architectural Patterns(1),Architectural Patter
15、ns are very 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 out
16、put of task 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 clients and forward the requests to servers, then return the results back to the clients.,POSA: Architectural Patterns(2),In