《契约式设计》PPT课件.ppt_第1页
《契约式设计》PPT课件.ppt_第2页
《契约式设计》PPT课件.ppt_第3页
《契约式设计》PPT课件.ppt_第4页
《契约式设计》PPT课件.ppt_第5页
已阅读5页,还剩80页未读 继续免费阅读

下载本文档

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

文档简介

契约式设计,Design by Contract,2019/7/23,Institute of Computer Software Nanjing University,摘要,引言 Eiffel 的 DbC 机制 DbC与继承 如何应用DbC,2019/7/23,Institute of Computer Software Nanjing University,2,引言,Design by Contract (DbC) 契约式设计 方法学层面的思想 以尽可能小的代价开发出可靠性出众的软件系统 Eiffel语言的直接支持 Bertrand Meyer:DbC是构建面向对象软件系统方法的核心! James McKim:“只要你会写程序,你就会写契约”,2019/7/23,Institute of Computer Software Nanjing University,3,引言,A discipline of analysis, design, implementation, management (可以贯穿于软件创建的全过程,从分析到设计,从文档到调试,甚至可以渗透到项目管理中) Viewing the relationship between a class and its clients as a formal agreement, expressing each partys rights and obligations. (把类和它的客户程序之间的关系看做正式的协议,描述双方的权利和义务),2019/7/23,Institute of Computer Software Nanjing University,4,引言,Every software element is intended to satisfy a certain goal, for the benefit of other software elements (and ultimately of human users). This goal is the elements contract. The contract of any software element should be Explicit. Part of the software element itself.,2019/7/23,Institute of Computer Software Nanjing University,5,A human contract,2019/7/23,Institute of Computer Software Nanjing University,6,Client,Supplier,(Satisfy precondition:) Bring package before 4 p.m.; pay fee.,(Satisfy postcondition:) Deliver package by 10 a.m. next day.,OBLIGATIONS(义务),(From postcondition:) Get package delivered by 10 a.m. next day.,(From precondition:) Not required to do anything if package delivered after 4 p.m., or fee not paid.,BENEFITS(权益/权利),deliver,A view of software construction,Constructing systems as structured collections of cooperating software elements suppliers and clients cooperating on the basis of clear definitions of obligations and benefits. These definitions are the contracts.,2019/7/23,Institute of Computer Software Nanjing University,7,Properties of contracts,A contract: Binds two parties (or more): supplier, client. Is explicit (written). Specifies mutual obligations and benefits. Usually maps obligation for one of the parties into benefit for the other, and conversely. Has no hidden clauses: obligations are those specified. Often relies, implicitly or explicitly, on general rules applicable to all contracts (laws, regulations, standard practices).,2019/7/23,Institute of Computer Software Nanjing University,8,2019/7/23,Institute of Computer Software Nanjing University,9,deferred class PLANE inherit AIRCRAFT feature start_take_off is - Initiate take-off procedures. require controls.passed assigned_runway.clear deferred ensure assigned_runway.owner = Current moving end start_landing, increase_altitude, decrease_altitude, moving, altitude, speed, time_since_take_off . Other features . invariant (time_since_take_off 10) end,Contracts for analysis,2019/7/23,Institute of Computer Software Nanjing University,10,deferred class VAT inherit TANK feature in_valve, out_valve: VALVE fill is - Fill the vat. require in_valve.open out_valve.closed deferred ensure in_valve.closed out_valve.closed is_full end empty, is_full, is_empty, gauge, maximum, . Other features . invariant is_full = (gauge = 0.97 * maximum) and (gauge = 1.03 * maximum) end,Contracts for analysis (contd),Contracts for analysis (contd),2019/7/23,Institute of Computer Software Nanjing University,11,Client,Supplier,(Satisfy precondition:) Make sure input valve is open, output valve is closed.,(Satisfy postcondition:) Fill the vat and close both valves.,OBLIGATIONS,(From postcondition:) Get filled-up vat, with both valves closed.,(From precondition:) Simpler processing thanks to assumption that valves are in the proper initial position.,BENEFITS,fill,So, is it like “assert.h”?,(Source: Reto Kramer) Design by Contract goes further: “Assert” does not provide a contract. Clients cannot see asserts as part of the interface. Asserts do not have associated semantic specifications. Not explicit whether an assert represents a precondition, post-conditions or invariant. Asserts do not support inheritance. Asserts do not yield automatic documentation.,2019/7/23,Institute of Computer Software Nanjing University,12,Contracts,契约就是“规范和检查”! Precondition:针对method,它规定了在调用该方法之前必须为真的条件 Postcondition:针对method,它规定了方法顺利执行完毕之后必须为真的条件 Invariant:针对整个类,它规定了该类任何实例调用任何方法都必须为真的条件,2019/7/23,Institute of Computer Software Nanjing University,13,Correctness in software,Correctness is a relative notion: consistency of implementation vis-a-vis specification. (This assumes there is a specification!) Basic notation: (P, Q: assertions, i.e. properties of the state of the computation. A: instructions). P A Q “Hoare triple” What this means (total correctness): Any execution of A started in a state satisfying P will terminate in a state satisfying Q.,2019/7/23,Institute of Computer Software Nanjing University,14,Hoare triples: a simple example,n 5 n := n + 9 n 13 Most interesting properties: Strongest postcondition (from given precondition). Weakest precondition (from given postcondition). “P is stronger than or equal to Q” means: P implies Q QUIZ: What is the strongest possible assertion? The weakest?,2019/7/23,Institute of Computer Software Nanjing University,15,Software correctness,Consider P A Q Take this as a job ad in the classifieds. Should a lazy employment candidate hope for a weak or strong P? What about Q? Two special offers: 1. False A . 2. . A True,2019/7/23,Institute of Computer Software Nanjing University,16,Strongest precond.,Weakest postcond.,“We are looking for someone whose work will be to start from initial situations as characterized by P, and deliver results as defined by Q,摘要,引言 Eiffel 的 DbC 机制 DbC与继承 如何应用DbC,2019/7/23,Institute of Computer Software Nanjing University,17,Design by Contract: The Mechanism,Preconditions and Postconditions Class Invariant Run-time effect,2019/7/23,Institute of Computer Software Nanjing University,18,A contract (from EiffelBase),2019/7/23,Institute of Computer Software Nanjing University,19,extend (new: G; key: H) - Assuming there is no item of key key, - insert new with key; set inserted. require key_not_present: not has (key) ensure insertion_done: item (key) = new key_present: has (key) inserted: inserted one_more: count = old count + 1,The contract,2019/7/23,Institute of Computer Software Nanjing University,20,Client,Supplier,PRECONDITION,POSTCONDITION,OBLIGATIONS,POSTCONDITION,PRECONDITION,BENEFITS,Routine,A class without contracts,class ACCOUNT feature - Access balance: INTEGER - Balance Minimum_balance: INTEGER is 1000 - Minimum balance feature NONE - Implementation of deposit and withdrawal add (sum: INTEGER) is - Add sum to the balance (secret procedure). do balance := balance + sum end,2019/7/23,Institute of Computer Software Nanjing University,21,Without contracts (contd),2019/7/23,Institute of Computer Software Nanjing University,22,feature - Deposit and withdrawal operations deposit (sum: INTEGER) is - Deposit sum into the account. do add (sum) end withdraw (sum: INTEGER) is - Withdraw sum from the account. do add (sum) end may_withdraw (sum: INTEGER): BOOLEAN is - Is it permitted to withdraw sum from the account? do Result := (balance - sum = Minimum_balance) end end,Introducing contracts,class ACCOUNT create make feature NONE - Initialization make (initial_amount: INTEGER) is - Set up account with initial_amount. require large_enough: initial_amount = Minimum_balance do balance := initial_amount ensure balance_set: balance = initial_amount end,2019/7/23,Institute of Computer Software Nanjing University,23,Introducing contracts (contd),2019/7/23,Institute of Computer Software Nanjing University,24,feature - Access balance: INTEGER - Balance Minimum_balance: INTEGER is 1000 - Minimum balance feature NONE - Implementation of deposit and withdrawal add (sum: INTEGER) is - Add sum to the balance (secret procedure). do balance := balance + sum ensure increased: balance = old balance + sum end,With contracts (contd),2019/7/23,Institute of Computer Software Nanjing University,25,feature - Deposit and withdrawal operations deposit (sum: INTEGER) is - Deposit sum into the account. require not_too_small: sum = 0 do add (sum) ensure increased: balance = old balance + sum end,With contracts (contd),withdraw (sum: INTEGER) is - Withdraw sum from the account. require not_too_small: sum = 0 not_too_big: sum = balance Minimum_balance do add ( sum) - i.e. balance := balance sum ensure decreased: balance = old balance - sum end,2019/7/23,Institute of Computer Software Nanjing University,26,The contract,2019/7/23,Institute of Computer Software Nanjing University,27,Client,Supplier,(Satisfy precondition:) Make sure sum is neither too small nor too big.,(Satisfy postcondition:) Update account for withdrawal of sum.,OBLIGATIONS,(From postcondition:) Get account updated with sum withdrawn.,(From precondition:) Simpler processing: may assume sum is within allowable bounds.,BENEFITS,withdraw,The imperative and the applicative,2019/7/23,Institute of Computer Software Nanjing University,28,do balance := balance - sum,ensure balance = old balance - sum,PRESCRIPTIVE,DESCRIPTIVE,How?,Operational,Implementation,Command,Instruction,Imperative,What?,Denotational,Specification,Query,Expression,Applicative,With contracts (end),may_withdraw (sum: INTEGER): BOOLEAN is - Is it permitted to withdraw sum from the - account? do Result := (balance - sum = Minimum_balance) end invariant not_under_minimum: balance = Minimum_balance end,2019/7/23,Institute of Computer Software Nanjing University,29,The class invariant,Consistency constraint applicable to all instances of a class. Must be satisfied: After creation. After execution of any feature by any client. (Qualified calls only: a.f (.),2019/7/23,Institute of Computer Software Nanjing University,30,The correctness of a class,For every creation procedure cp: precp docp postcp and INV For every exported routine r: INV and prer dor postr and INV The worst possible erroneous run-time situation in object-oriented software development: Producing an object that does not satisfy the invariant of its own class.,2019/7/23,Institute of Computer Software Nanjing University,31,Example,balance = deposits.total withdrawals.total,2019/7/23,Institute of Computer Software Nanjing University,32,deposits,withdrawals,balance,deposits,withdrawals,(A1),(A2),A more sophisticated version,2019/7/23,Institute of Computer Software Nanjing University,33,class ACCOUNT create make feature NONE - Implementation add (sum: INTEGER) is - Add sum to the balance (secret procedure). do balance := balance + sum ensure balance_increased: balance = old balance + sum end deposits: DEPOSIT_LIST withdrawals: WITHDRAWAL_LIST,New version (contd),feature NONE - Initialization make (initial_amount: INTEGER) is - Set up account with initial_amount. require large_enough: initial_amount = Minimum_balance do balance := initial_amount create deposits.make create withdrawals.make ensure balance_set: balance = initial_amount end feature - Access balance: INTEGER - Balance Minimum_balance: INTEGER is 1000 - Minimum balance,2019/7/23,34,New version (contd),feature - Deposit and withdrawal operations deposit (sum: INTEGER) is - Deposit sum into the account. require not_too_small: sum = 0 do add (sum) deposits.extend (create DEPOSIT.make (sum) ensure increased: balance = old balance + sum end,2019/7/23,Institute of Computer Software Nanjing University,35,New version (contd),2019/7/23,Institute of Computer Software Nanjing University,36,withdraw (sum: INTEGER) is - Withdraw sum from the account. require not_too_small: sum = 0 not_too_big: sum = balance Minimum_balance do add (sum) withdrawals.extend (create WITHDRAWAL.make (sum) ensure decreased: balance = old balance sum one_more: withdrawals.count = old withdrawals.count + 1 end,New version (end),2019/7/23,Institute of Computer Software Nanjing University,37,may_withdraw (sum: INTEGER): BOOLEAN is - Is it permitted to withdraw sum from the - account? do Result := (balance - sum = Minimum_balance) end invariant not_under_minimum: balance = Minimum_balance consistent: balance = deposits.total withdrawals.total end,The correctness of a class,For every creation procedure cp: precp docp postcp and INV For every exported routine r: INV and prer dor postr and INV,2019/7/23,Institute of Computer Software Nanjing University,38,a.f (),a.g (),a.f (),create a.make (),S1,S2,S3,S4,Initial version,2019/7/23,Institute of Computer Software Nanjing University,39,feature NONE - Initialization make (initial_amount: INTEGER) is - Set up account with initial_amount. require large_enough: initial_amount = Minimum_balance do balance := initial_amount create deposits.make create withdrawals.make ensure balance_set: balance = initial_amount end,Correct version,2019/7/23,Institute of Computer Software Nanjing University,40,feature NONE - Initialization make (initial_amount: INTEGER) is - Set up account with initial_amount. require large_enough: initial_amount = Minimum_balance do create deposits.make create withdrawals.make deposit (initial_amount) ensure balance_set: balance = initial_amount end,40,Contracts: run-time effect,Compilation options (per class, in Eiffel): No assertion checking Preconditions only Preconditions and postconditions Preconditions, postconditions, class invariants All assertions,2019/7/23,Institute of Computer Software Nanjing University,41,41,摘要,引言 Eiffel 的 DbC 机制 DbC与继承 如何应用DbC,2019/7/23,Institute of Computer Software Nanjing University,42,继承与 Design by Contract,问题: 子类中的断言与父类中的断言是什么关系? 依据 子类乃父类的特化,子类的实例也是父类的合法实例。 申明为父类的引用运行时可能指向子类实例 因而 ?,2019/7/23,Institute of Computer Software Nanjing University,43,Inheritance and assertions,2019/7/23,Institute of Computer Software Nanjing University,44,Correct call: if a1. then a1.r (.) else . end,r is,ensure,r is,ensure,C,A,B,a1: A,a1.r (),Contract,2019/7/23,Institute of Computer Software Nanjing University,45,Client,Supplier,(Satisfy precondition:) 不得要求投递超过5kg的包裹,(Satisfy postcondition:) 在3个工作日内投送到位,OBLIGATIONS,(From postcondition:) 3个工作日内包裹到位,(From precondition:) 不受理超过5kg的包裹,BENEFITS,delivery,Contract,class COURIER feature deliver(p:Package, d:Destination) require -包裹重量不超过5kg ensure -3个工作日内投送到指定地点 end,2019/7/23,Institute of Computer Software Nanjing University,46,More desirable contract,2019/7/23,Institute of Computer Software Nanjing University,47,Client,Supplier,(Satisfy precondition:) 不得要求投递超过8kg的包裹,(Satisfy postcondition:) 在2个工作日内投送到位,OBLIGATIONS,(From postcondition:) 2个工作日内包裹到位,(From precondition:) 不受理超过8kg的包裹,BENEFITS,delivery,More desirable contract,class DIFFERENT_COURIER Inherit COURIER redefine deliver feature deliver(p:Package, d:Destination) require -包裹重量不超过5kg require else -包裹重量不超过8kg ensure -3天内投送到指定地点 ensure then -2天内投送到指定地点 end,2019/7/23,Institute of Computer Software Nanjing University,48,require -包裹重量不超过8kg,ensure - 2天内投送到指定地点,Assertion redeclaration rule,Redefined version may not have require or ensure. May have nothing (assertions kept by default), or require else new_pre ensure then new_post Resulting assertions are: original_precondition or new_pre original_postcondition and new_post,2019/7/23,Institute of Computer Software Nanjing University,49,Invariant accumulation,Every class inherits all the invariant clauses of its parents. These clauses are conceptually “and”-ed.,2019/7/23,Institute of Computer Software Nanjing University,50,简言之,可以使用require else削弱先验条件 可以使用ensure then加强后验条件 用and把不变式子句和你所继承的不变式子句结合起来,就可以加强不变式,2019/7/23,Institute of Computer Software Nanjing University,51,摘要,引言 Eiffel 的 DbC 机制 DbC与继承 如何应用DbC 其它,2019/7/23,Institute of Computer Software Nanjing University,52,Design by Contract: How to apply,目的:构造高质量的程序 理解Contract violation DbC与Quality Assurance(QA) Precondition Design Not defensive programming Class Invariants and business logic,2019/7/23,Institute of Computer Software Nanjing University,53,What are contracts good for?,Writing correct software (analysis, design, implementation, maintenance, reengineering). Documentation (the “contract” form of a class). Effective reuse. Controlling inheritance. Preserving the work of the best developers. Quality assurance, testing, debugging (especially in connection with the use of libraries) . Exception handling .,2019/7/23,Institute of Computer Software Nanjing University,54,Some benefits: technical,Development process becomes more focused. Writing to spec. Sound basis for writing reusable software. Exception handling guided by precise definition of “normal” and “abnormal” cases. Interface documentation always up-to-date, can be trusted. Documentation generated automatically. Faults occur close to their cause. Found faster and more easily. Guide for black-box test case generation.,2019/7/23,Institute of Computer Software Nanjing University,55,Some benefits: managerial,Library users can trust documentation. They can benefit from preconditions to validate their own software. Test manager can benefit from more accurate estimate of test effort. Black-box specification for free. Designers who leave bequeath not only code but intent. Common vocabulary between all actors of the process: developers, managers, potentially customers. Component-based development possible on a solid basis.,2019/7/23,Institute of Computer Software Nanjing University,56,A contract violation is not a special case,For special cases (e.g. “if the sum is negative, report an error.”) use standard control structures (e.g. if . then . else.). A run-time assertion violation is something else: the manifestation of A DEFECT (“BUG”),2019/7/23,Institute of Computer Software Nanjing University,57,Contracts and quality assurance,Precondition violation: Bug

温馨提示

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

评论

0/150

提交评论