軟件設(shè)計式一_第1頁
軟件設(shè)計式一_第2頁
軟件設(shè)計式一_第3頁
軟件設(shè)計式一_第4頁
軟件設(shè)計式一_第5頁
已閱讀5頁,還剩60頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、軟件設(shè)計模式(一)潘愛民http:/ patterns?內(nèi)容 從一個例子看模式 關(guān)于模式的研究情況 介紹一些重要的模式(部分) 第三次作業(yè)一個設(shè)計例子 vc/samples/mfc/ole/drawclidrawcli的基礎(chǔ) mfc提供的基礎(chǔ) doc/view結(jié)構(gòu) doc提供了強大的數(shù)據(jù)管理功能 view提供了強大的顯示功能 splitwnd功能 cwinapp/cmainframe 提供了一套命令處理流程 對ole的封裝 active container ole clipboard property page 數(shù)據(jù)結(jié)構(gòu)管理功能 cobject/coblist我們的設(shè)計焦點 用c+對象來表示每

2、一個圖元 定義圖元的公共接口 如何處理用戶的操作 鼠標的動作 圖元對象的創(chuàng)建和管理圖元基類class cdrawobj : public cobject/ attributescdrawdoc* m_pdocument;/ ownervirtual int gethandlecount();virtual cpoint gethandle(int nhandle);virtual hcursor gethandlecursor(int nhandle);virtual void setlinecolor(colorref color);virtual void setfillcolor(col

3、orref color);/ operationsvirtual void draw(cdc* pdc);virtual void drawtracker(cdc* pdc, trackerstate state);virtual void moveto(const crect& positon, cdrawview* pview = null);virtual int hittest(cpoint point, cdrawview* pview, bool bselected);virtual bool intersects(const crect& rect);virtua

4、l void movehandleto(int nhandle, cpoint point, cdrawview* pview = null);virtual void onopen(cdrawview* pview);virtual void oneditproperties();virtual cdrawobj* clone(cdrawdoc* pdoc = null);virtual void remove();virtual void serialize(carchive& ar);/ ;圖元層次結(jié)構(gòu) cdrawobj cdrawrect cdrawpoly cdrawoleo

5、bj cdrawobjcdrawrectcdrawpoly cdrawoleobj圖元的創(chuàng)建 永久支持: cdocument:serialize-coblist:serialize-cobject:serialize cdrawobj:clone drawtool:創(chuàng)建圖元對象交互操作 鍵盤和菜單命令 mfc內(nèi)部機制 鼠標操作 在cdrawview的鼠標處理函數(shù)中處理 工具箱:工具鏈,有一個當前活動工具 轉(zhuǎn)交給當前活動工具來處理工具箱和cdrawtoolclass cdrawtool/ constructorspublic:cdrawtool(drawshape ndrawshape);/ o

6、verridablesvirtual void onlbuttondown(cdrawview* pview, uint nflags, const cpoint& point);virtual void onlbuttondblclk(cdrawview* pview, uint nflags, const cpoint& point);virtual void onlbuttonup(cdrawview* pview, uint nflags, const cpoint& point);virtual void onmousemove(cdrawview* pvie

7、w, uint nflags, const cpoint& point);virtual void oneditproperties(cdrawview* pview);virtual void oncancel();/ attributesdrawshape m_drawshape;static cdrawtool* findtool(drawshape drawshape);static cptrlist c_tools;static drawshape c_drawshape;/ ;drawtool層次結(jié)構(gòu) cdrawtool cselecttool crecttool cpol

8、ytool cdrawtoolcselecttoolcrecttoolcpolytool例子中的模式 cdrawobj和cdrawtool合起來構(gòu)成了factory method模式 cdrawtool:clone用到了原型創(chuàng)建模式 每一個cdrawtool都是一個singleton adapter模式:把ole對象封裝成cdrawobj * 可以增加composite模式 facade模式:通過cdrawdoc/view與mfc通信 chain of responsibility:如鼠標處理工作 * 用command模式增加undo/redo功能patterns 定義: a physical

9、 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 solve a general design problem in a particular cont

10、ext design patterns represent solutions to problems that arise when developing software within a particular context 幾個近義詞 idiom、technique、paradigmabout patternsabout patterns documentation of expert software engineers behavior documentation of specific reoccurring problems (and solutions) abstractio

11、n 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. patterns provide a common vocabulary and understanding for

12、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支持軟件結(jié)構(gòu)和設(shè)計的重用 抓住了特定領(lǐng)域中問題的成功解決方案中的靜態(tài)、動態(tài)結(jié)構(gòu)和相互之間的協(xié)作關(guān)系 patterns與開發(fā)語言無關(guān),但是建立在一定的環(huán)境

13、基礎(chǔ)上 例如:經(jīng)典的mvc、factory method frameworks支持細節(jié)設(shè)計和代碼的重用 framework是一組組件的綜合,這些組件相互協(xié)作,為一族相關(guān)應(yīng)用提供了一個可重用的框架結(jié)構(gòu) 例如:mmc、ms script engine 兩者結(jié)合起來, design patterns and frameworks有助于提高軟件的質(zhì)量 比如:重用性,擴展性,性能,可維護性design pattern與framework(續(xù)) a framework supplies the infrastructure and mechanisms that execute a policy for

14、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 architectural elements than frameworks design patterns are

15、 less specialized than frameworks framework與class library(toolkit)pattern的研究情況 關(guān)于pattern研究的歷史 a pattern language,christopher alexander,1977 “advanced c+:programming styles and idioms”,james coplien,1992 “design patterns: elements of reusable object-oriented software”,gof,1995 “pattern-oriented softw

16、are architecture: a system of patterns” (簡稱為“posa”) ,gov,1996 .pattern is a hot topic 在amazon上查找包含patterns的書(2002.12.12)pattern的研究現(xiàn)狀 pattern與java pattern與corba pattern與系統(tǒng)結(jié)構(gòu) pattern與generic programming結(jié)合 其他(例如uml等)posa中的模式分類 architectural patterns 表達了軟件系統(tǒng)的基本結(jié)構(gòu)組織形式或者結(jié)構(gòu)方案 它包含一組預(yù)定義的子系統(tǒng),規(guī)定了這些子系統(tǒng)的責任,同時還提供

17、了用于組織和管理這些子系統(tǒng)的規(guī)則和向?qū)?design patterns 為軟件系統(tǒng)的子系統(tǒng)、組件或者組件之間的關(guān)系提供一個精煉之后的解決方案 它描述了在特定環(huán)境下,用于解決通用軟件設(shè)計問題的組件以及這些組件相互通信時的可重現(xiàn)結(jié)構(gòu) idioms 是一個與編程語言相關(guān)的低級模式 它描述了如何實現(xiàn)組件的某些功能,或者利用編程語言的特性來實現(xiàn)組件內(nèi)部要素之間的通信功能posa: architectural patterns(1) architectural patterns are very high-level structural patterns. also called “conceptual

18、 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 task i is the input of task i+1. blackboard: several indepen

19、dent 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 re

20、sults 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 controller parts are usually observers of the model via th

21、e 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 component adaptable systems : design for change microkerne

22、l 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 encapsulates knowledge about the problem domainposa: design patterns(

23、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 interface for the aggregate. organization of work : compone

24、nts 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 services and components. proxy: clients communicate with a represen

25、tative (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 patternposa: design patterns(2) management: handle homogenous collections of o

26、bjects, 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 control specific views. similar to gamma et als abstract factory

27、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 client - dispatcher - server: a dispatcher acts as an intermedia

28、te 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” idioms are low-level patterns specific to a programming lang

29、uage. counted pointer: simplifies memory management of shared objects providing reference counting. 其他 virtual constructor smart pointer handle/body關(guān)于“design pattern” 對已有模式的整理、分類 一套描述模式的詞匯,可用于交流和文檔化 為軟件設(shè)計總結(jié)了寶貴的經(jīng)驗,這些設(shè)計經(jīng)驗可以被重用,但不是簡單的代碼重用 分類: creational patterns structural patterns behavioral patterns

30、在軟件設(shè)計模式領(lǐng)域,起到先驅(qū)的作用重提:指導(dǎo)模式設(shè)計的三個概念 重用(reuse):是目標 兩種重要的重用手段 inheritance & composition 接口與實現(xiàn)分離 接口保持不變,分離帶來靈活性 多態(tài)性(polymorphism) decouple 降低復(fù)雜性如何描述一個模式 關(guān)鍵要素 design pattern name,aliases or also known as problem,intent or goal forces,constraints,motivation context, applicability solution structure parti

31、cipants collaboration implementation evaluation,resulting context,consequences related patterns examples,known usescreational patterns abstract factory(kit) builder factory method(virtual constructor) prototype singleton * finder模式 1:factory method (一) aliases:virtual constructor intent define an in

32、terface for creating an object, but let subclasses decide which class to instantiate. factory method lets a class defer instantiation to subclasses. motivation frameworks use abstract classes to define and maintain relationships between objects. a framework is often responsible for creating these ob

33、jects as well.factory method模式(二) applicability:use the factory method pattern when a class cant anticipate the class of objects it must create. a class wants its subclasses to specify the objects it creates. classes delegate responsibility to one of several helper subclasses, and you want to locali

34、ze the knowledge of which helper subclass is the delegate. 插:virtual constructor intent: 在一個類層次中,客戶在runtime要創(chuàng)建一個對象,對象的子類型未確定,根據(jù)客戶的需要和環(huán)境情況,確定對象的類型 problem: 只知道對象的一般類型,不知道確切類型(需要從環(huán)境中獲取類型信息) forces : 隱藏對象的類型層次,只發(fā)布基接口 如何確定最合適的派生類 客戶必須有辦法使用派生類的服務(wù)插:virtual constructor(續(xù)) solution: 使用envelope/letter或者handl

35、e/body pattern 由envelope或者handle根據(jù)環(huán)境信息選擇適當?shù)呐缮愋?例子: 1 根據(jù)stream動態(tài)創(chuàng)建對象 2 com對象factory method模式(三) struct participants product、concreteproduct、creator、concretecreator collaborationsfactory method模式(四) evaluation 多態(tài)性:客戶代碼可以做到與特定應(yīng)用無關(guān),適用于任何實體類 缺點:需要creator和相應(yīng)的子類作為factory method的載體,如果應(yīng)用模型確實需要creator和子類存在,則

36、很好;否則的話,需要增加一個類層次 優(yōu)點: (1) provides hooks for subclasses?;悶閒actory method提供缺省實現(xiàn),子類可以重寫新的實現(xiàn),也可以繼承父類的實現(xiàn)。體現(xiàn)了:加一層間接性,增加了靈活性 (2) connects parallel class hierarchiesfactory method模式(五) connects parallel class hierarchiesfactory method模式(六) implementation (1) 父類是否提供缺省的實現(xiàn) (2) factory method的參數(shù) (3) language-

37、specific variants and issues smalltalk,使用類型 c+,使用lazy initialization技術(shù) (4) using templates to avoid subclassingfactory method模式(七) related patterns abstract factory prototype examples模式 2 :abstract factory(一) aliases:kit intent provide an interface for creating families of related or dependent objec

38、ts without specifying their concrete classes. motivation 為了解決一族相關(guān)或者相依對象的創(chuàng)建工作,專門定義一個用于創(chuàng)建這些對象的接口(基類)。客戶只需與這個基接口打交道,不必考慮實體類的類型。abstract factory(二) applicability,use the abstract factory pattern when a system should be independent of how its products are created, composed, and represented. a system shou

39、ld be configured with one of multiple families of products. a family of related product objects is designed to be used together, and you need to enforce this constraint. you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations. abstract

40、 factory(三) struct participants: client、abstractfactory、concretefactory、abstractproduct、concreteproduct collaborations abstract factory(四) evaluation 與factory method的關(guān)系 多個factory method合在一起 factory method一定是virtual的abstract factory(五) evaluation(續(xù)) 優(yōu)點: factory把product的類型封裝起來,分離了具體的類 易于變換product族 保證不

41、同族之間的product相互不會碰撞,即保證products的一致性 缺點: factory對象的方法數(shù)目對應(yīng)product數(shù)目,增加新的product種類比較困難,要影響到factory的基類,進而影響到所有的子類abstract factory(六) implementation factories as singletons, 每個product族往往只需要一個factory對象就可以了 creating the products, 對于product族比較多的情況,可以使用prototype模式來實現(xiàn)這些factories,而不必對于每一個具有細微差別的product族都使用一個con

42、crete factory class defining extensible factories,針對evaluation中提到的缺點,通過參數(shù)化技術(shù)提高factory的適應(yīng)能力和擴展性 問題在于,返回給客戶什么樣的類型?abstract factory(七) related patterns factory method、prototype 、singleton examples widgetfactory插:com中的class factory 兼有兩種模式:factory method和abstract factory iclassfactory是abstract factory的接口

43、 createinstance是factory method 對于每一個coclass,class object就是concrete factory, 每一個產(chǎn)品的抽象接口為iunknown,com對象是真正的concrete product ipsfactorybuffer也是abstract factory的接口 通過factory method創(chuàng)建對象 比客戶直接創(chuàng)建對象,要靈活 concretefactory也是一個concrete product,所以可以重用底層的許多機制,如套間機制、跨進程機制等模式三:builder (一) intent separate the constru

44、ction of a complex object from its representation so that the same construction process can create different representations motivation 在復(fù)雜對象的構(gòu)造過程中,允許同樣的構(gòu)造過程能夠加入新的被構(gòu)造元素 “結(jié)構(gòu)化構(gòu)造過程” applicability, use the builder pattern when the algorithm for creating a complex object should be independent of the part

45、s that make up the object and how theyre assembled. the construction process must allow different representations for the object thats constructed.builder (二) structure participants director、 builder、concretebuilder、 productbuilder (三) collaborationsbuilder (四) evaluation it lets you vary a products

46、 internal representation it isolates code for construction and representation it gives you finer control over the construction process implementation builder interface(assembly and construction) why no abstract class for products? empty methods as default in builder. related patterns abstract factor

47、y 區(qū)別:(1) builder重在構(gòu)造過程,最后一步返回結(jié)果; (2) builder構(gòu)造許多復(fù)雜對象builder (五) examples readers、parsers、converters the persistence of ole documents模式四:prototype(一) intent specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. motivation 以一個已有的對象作為原型,

48、通過它來創(chuàng)建新的對象。在增加新的對象的時候,新對象的細節(jié)創(chuàng)建工作由自己來負責,從而使新對象的創(chuàng)建過程與框架隔離開來 applicability 當產(chǎn)品的創(chuàng)建過程要獨立于系統(tǒng)時 當產(chǎn)品的類型是在runtime時被指定的情況下 避免創(chuàng)建一個與product層次平行的factory層次時prototype(二) structure participants prototype、concreteprototype、client collaborationsprototype(三) evaluation adding and removing products at run-time specifyin

49、g new objects by varying values,降低系統(tǒng)中類的數(shù)目 configuring an application with classes dynamically 要求:每一個product類都必須實現(xiàn)clone操作 對于c+語言特別有意義:c+的class不是first-class objects implementation using a prototype manager implementing the clone operation shallow copy versus deep copy save & load initializing clon

50、es 兩階段構(gòu)造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 point of access to it. motivation its important for some classes to have exactly one instance. insta

51、nce-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 instance 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 sin

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論