




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Elaboration Iteration 2- GOF PatternsCh24. Applying GoF Design Patterns1Chapter 24. Iteration 2-More PatternsObjectivesIntroduce and apply some GoF design patterns.Show GRASP principles as a generalization of other design patterns.2DefinitionIn software engineering, a design pattern is a general reu
2、sable solution to a commonly occurring problem in software design.In OO design, a pattern is a named description of a problem and solution that can be applied in new contexts; ideally, a pattern advises us on how to apply the solution in varying circumstances and considers the forces and tradeoffs.3
3、Design PatternsA design pattern is not a finished design that can betransformed directly into code.It is a description or template for how to solve aproblem that can be used in many different situations.OO design patterns typically show relationships andinteractions between classes or objects, witho
4、utspecifying the final application classes or objects thatare involved.4Gang-of-Four Design Patterns(23)Scope: domain over which a pattern appliesPurpose: reflects what a pattern does5PurposeCreationalStructuralBehavioralScopeClassFactory Method AdapterInterpreter Template MethodObjectAbstract Facto
5、ry Builder Prototype Singleton Adapter Bridge Composite DecoratorFacade ProxyFlyweight Chain of ResponsibilityCommand Iterator MediatorMemento Observer State Strategy Visitor 1. Adapter (GoF)Name: AdapterProblem:How to resolve incompatible interfaces, or provide a stable interface to similar compone
6、nts with different interfaces?Solution: (advice)Convert the original interface of a component into another interface, through an intermediate adapter object.67Using an Adapter8Some GRASP Principles as a Generalization of Other PatternsThe previous use of the Adapter pattern can be viewed as a specia
7、lization of some GRASP building blocks.Protected Variation Indirection Polymorphism (via interface) Whats the Problem? Pattern Overload! The Pattern Almanac 2000 lists around 500 design patterns Many hundreds more have been published since then A Solution: See the Underlying Principles Its important
8、 for an experienced designer to know in detail and by memory 50+ of the most important design patterns Most design patterns can be seen as specializations of a few basic GRASP principles9Example: Relating Adapter to some core GRASP principles10Analysis Discoveries During Design: Domain ModelIt is no
9、rmal and common to discover noteworthy domain concepts and refined understanding of the requirements during design or programming.iterative development support this kind of incremental discovery 112. FactorySimple Factory or Concrete FactoryIn the prior Adapter pattern solution for external services
10、 with varying interfaceswho creates the adapters? And how to determine which class of adapter to create, such as TaxMaster-Adapter or GoodAsGoldTaxProAdapter?One solutionchoosing a domain object (such as a Register) to create the adapters does not support the goal of a separation of concerns, and lo
11、wers its cohesion12Factory PatternName: FactoryProblem:Who should be responsible for creating objects when there are special considerations, such as complex creation logic, a desire to separate the creation responsibilities for better cohesion, and so forth?Solution: (advice)Create a Pure Fabricatio
12、n object called a Factory that handles the creation.13The Factory pattern143. Singleton (GoF)The ServicesFactory raises another new problem in the designWho creates the factory itself, and how is it accessed?only one instance of the factory is needed how to get visibility to this single ServiceFacto
13、ry instance? One solutionpass the ServicesFactory instance around as a parameter to wherever a visibility need is discovered for it, or to initialize the objects that need visibility to it, with a permanent reference.15Singleton PatternName: SingletonProblem:Exactly one instance of a class is allowe
14、d, it is a singleton. Objects need a global and single point of access.Solution: (advice)Define a static method of the class that returns the singleton.16The Singleton pattern in the ServicesFactory class17Using factory pattern18visibility to public classes is global-SingletonClass.getInstance() Imp
15、licit getInstance Singleton pattern message indicated in the UML19Conclusion of the External Services with Varying Interfaces Problem204. Strategy (GoF)The new design problemProvide more complex pricing logic, such as a store-wide discount for the day, senior citizen discounts, and so forth.The pric
16、ing strategy (which may also be called a rule, policy, or algorithm) for a sale can vary. During one period it may be 10% off all sales, later it may be $10 off if the sale total is greater than $200, and myriad other variations. How do we design for these varying pricing algorithms?21Strategy Patte
17、rnName: StrategyProblem:How to design for varying, but related, algorithms or policies? How to design for the ability to change these algorithms or policies?Solution: (advice)Define each algorithm/policy/strategy in a separate class, with a common interface.22Pricing Strategy classes.23Strategy in c
18、ollaboration24A Strategy object is attached to a context object the object to which it applies the algorithm It is common that the context object pass a reference to itself on to the strategy objectContext object needs attribute visibility to its strategy25Creating a Strategy with a FactoryThere are
19、 different pricing algorithms or strategies, and they change over time. Who should create the strategy? 26Creating a strategy275.Composite (GoF) How do we handle the case of multiple, conflicting pricing policies? For example, suppose a store has the following policies in effect today (Monday):20% s
20、enior discount policypreferred customer discount of 15% off sales over $400on Monday, there is $50 off purchases over $500buy 1 case of Darjeeling tea, get 15% discount off of everythingIs there a way to change the design so that the Sale object does not know if it is dealing with one or many pricin
21、g strategies, and also offer a design for the conflict resolution?28Composite PatternName: CompositeProblem:How to treat a group or composition structure of objects the same way (polymorphically) as a non-composite (atomic) object?Solution: (advice)Define classes for composite and atomic objects so
22、that they implement the same interface.2930The Composite patternCollaboration with a Composite31Sample code3233Sample codeAbstract superclasses, abstract methods, and inheritance in the UML34Creating Multiple SalePricingStrategies 35customer type discount36Creating the Pricing Strategy for a Custome
23、r Discount -part 137Creating the pricing strategy for a customer discount -Part 2386. Facade (GoF)How to suport pluggable business rulesWhen makeNewSale or enterItem occurs in the Process Sale use case, or when a cashier starts cashing in, different customers who wish to purchase the NextGen POS wou
24、ld like to customize its behavior slightly.39Facade PatternName: FacadeProblem:A common, unified interface to a disparate set of implementations or interfacessuch as within a subsystemis required. There may be undesirable coupling to many things in the subsystem, or the implementation of the subsyst
25、em may change. Solution: (advice)Define a single point of contact to the subsystema facade object that wraps the subsystem. This facade object presents a single unified interface and is responsible for collaborating with the subsystem components.40UML package diagram with a Facade.41Sample code427.
26、Observer/Publish-Subscribe/Delegation Event Model (GoF)For a GUI window to refresh its display of the sale total when the total changesModel-View Separation principle 43Updating the interface when the sale total changesObserver (Publish-Subscribe) PatternName: Observer (Publish-Subscribe)Problem:Dif
27、ferent kinds of subscriber objects are interested in the state changes or events of a publisher object, and want to react in their own unique way when the publisher generates an event. Moreover, the publisher wants to maintain low coupling to the subscribers. Solution: (advice)Define a subscriber or listener interface. Subscribers imple
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 无人机应用技术1.9.无人机高速应急车道违章抓拍
- 真理题目及答案解析图解
- 2025年中国磨钢球专用砂轮行业投资前景及策略咨询研究报告
- 2025年中国电站铸锻件行业投资前景及策略咨询研究报告
- 2025年中国智能电话收音机行业投资前景及策略咨询研究报告
- 2025年中国抛光无纺布行业投资前景及策略咨询研究报告
- 2025年中国工具包装膜行业投资前景及策略咨询研究报告
- 2025年中国全光亮酸性镀锡添加剂行业投资前景及策略咨询研究报告
- 湖北省武汉市青山区2025届高一化学第二学期期末统考试题含解析
- 河南省安阳第三十六中学2025届高一下化学期末复习检测试题含解析
- 电工厂搬迁方案(3篇)
- 2025年南京市中考数学真题试卷
- 老年人眼科疾病
- 钢板配送设计方案(3篇)
- 中医基础学课件护理情志
- 小学三年级科学下册教案
- T/CBMCA 039-2023陶瓷大板岩板装修镶贴应用规范
- 2025-2030中国美容美发行业市场现状供需分析及投资评估规划分析研究报告
- 2025年中国不锈钢蚀刻板数据监测研究报告
- 免疫检查点抑制剂相关肺炎诊治和管理专家共识(2025)要点解读
- (统编版2025)历史七年级下册新教材变化及教学建议
评论
0/150
提交评论