南京大学-软件工程-16 Design Patterns

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
responsibility, collaboration Consequences: results and tradeoffs
Main Contents
Design Pattern Introduction Information Hiding AND Strategy Object Creation AND Factory, Factory
Design Patterns Introduction —— Why?
Expert designers know not to solve every problem from first principles
They reuse solutions
These patterns make OO designs more flexible, elegant, and ultimately reusable
Leabharlann Baidu
设计模式
“设计模式描述了一个在我们周围不断重复发 生的问题,以及该问题的解决方案的核心” Christopher Alexander,《建筑的永恒之道》
是经过实践反复检验、能解决关键技术难题、 有广泛应用前景和能够显著提高软件质量的有 效设计经验总结
5
What is a Design Pattern?
Computing and SE II
Chapter 16: Design Patterns
Software Institute, NJU
Main Contents
Design Pattern Introduction Information Hiding AND Strategy Object Creation AND Factory, Factory
distills design experience
Elements of Design Patterns
Design patterns have 4 essential elements:
Pattern name: increases vocabulary of designers Problem: intent, context, when to apply Solution: UML-like structure, abstract code,
Method, Abstract Factory, Singleton Programming to Interface AND Iterator
Design Patterns Introduction
—— Why?
Designing OO software is hard
Designing reusable OO software – harder
The lightswitching policy changes by the hour
•The “dumb” policy: change the green route every 5 seconds •Midnight policy: change to yellow always •Rush hour policy: double the “green time” in the busy route
A design pattern
abstracts a recurring design structure comprises class and/or object
dependencies, structures, interactions, or conventions
Next you isolate (encapsulate) each secret so that if it does change, the change doesn’t affect the rest of the program.
10
Intersection Traffic Lights Control
case dumb: next_time := time + constant_time;
case midnight: if (not the_green_route.is_busy) then …
Method, Abstract Factory, Singleton Programming to Interface AND Iterator
Information Hiding(1)
Each module hides the implementation of an important design decision so that only the constituents of that module know the details
Experienced OO designers make good design
New designers tend to fall back on non-OO techniques used before
Experienced designers know something – what is it?
Information Hiding(2)
Modules may have additional secrets: Changes
Anticipate changes
You then separate each design secret by assigning it to its own class, subroutine, or other design unit.
A Bad Solution
Use multiple conditional statement to set the behavior according to current policy
intersection::next_green(the_green_route r) {
switch (current_policy) {
相关文档
最新文档