面向对象技术课件.ppt

上传人(卖家):三亚风情 文档编号:3025993 上传时间:2022-06-23 格式:PPT 页数:76 大小:2.31MB
下载 相关 举报
面向对象技术课件.ppt_第1页
第1页 / 共76页
面向对象技术课件.ppt_第2页
第2页 / 共76页
面向对象技术课件.ppt_第3页
第3页 / 共76页
面向对象技术课件.ppt_第4页
第4页 / 共76页
面向对象技术课件.ppt_第5页
第5页 / 共76页
点击查看更多>>
资源描述

1、模式&框架Pattern & Framework2022-6-23Institute of Computer SoftwareNanjing University1摘要More about PatternFrameworkComparison2022-6-23Institute of Computer SoftwareNanjing University2摘要More about PatternFrameworkComparison2022-6-23Institute of Computer SoftwareNanjing University3More about PatternPatter

2、ns of PatternsAnti-patternJ2EE patterns2022-6-23Institute of Computer SoftwareNanjing University4More about PatternPatterns of PatternsAnti-patternJ2EE patterns2022-6-23Institute of Computer SoftwareNanjing University5Working togetherPatterns are often used together and combined with the same design

3、 solution.A compound pattern combines two or more patterns into a solution that solves a recurring or general problem.2022-6-23Institute of Computer SoftwareNanjing University6Example:设计一个文档编辑器设计问题文档结构格式化修饰用户界面支持多种视感(look-and-feel)标准支持多种窗口系统用户操作拼写检查和连字符2022-6-23Institute of Computer SoftwareNanjing

4、University7使用设计模式Composite表示文档的物理结构:递归组合Strategy允许不同的格式化算法Decorator修饰用户界面Abstract Factory支持多视感标准Bridge允许多个窗口平台Command支持撤销用户操作Iterator访问和遍历对象结构Visitor允许无限扩充分析能力而又不会使文档结构的实现复杂化2022-6-23Institute of Computer SoftwareNanjing University8Duck ExampleStart with a bunch of Quackables.A goose came along and

5、wanted to act like a Quakable too. Then, the Quackologists decided they wanted to count quacks.But the Quackologists were worried theyd forget to add the QuackCounter decorator.We had management problems keeping track of all those ducks and geese and quackables.The Quackologists also wanted to be no

6、tified when any quackable quacked.2022-6-23Institute of Computer SoftwareNanjing University9AdapterDecoratorAbstract factoryCompositeIteratorObserverCompound PatternsMVCa song about MVC by James Dempsey (lyrics)2022-6-23Institute of Computer SoftwareNanjing University10MVCMVC is a compound pattern c

7、onsisting of the Observer (model), Strategy (controller) and Composite (view) patterns.The Adapter pattern can be used to adapt a new model to an existing view and controller.2022-6-23Institute of Computer SoftwareNanjing University11Pattern Definition ReviewA Pattern is a solution to a problem in a

8、 context.Context: the recurring situation in which the pattern appliesProblem: the goal you are trying to achieve in this context and any constrains that occur in the context.Solution: a general design that anyone can apply which resolves the goal and set of constraints2022-6-23Institute of Computer

9、 SoftwareNanjing University12Pattern Categories I - GoalCreational Patterns: involve object instantiation and all provide a way to decouple a client from the objects it needs to instantiate.Structural Patterns: compose classes or objects into larger structures.Behavioral Patterns: concerned with how

10、 classes and objects interact and distribute responsibility2022-6-23Institute of Computer SoftwareNanjing University13Pattern Categories II - ScopeClass PatternsDescribe how relationships between classes are defined via inheritance. Relationships in class patterns are established at compile time.202

11、2-6-23Institute of Computer SoftwareNanjing University14Template MethodInterpreterAdapterFactory MethodPattern Categories II - ScopeObject PatternsDescribe relationships between objects and are primarily defined by composition.Relationships in object patterns are typically created at runtime and are

12、 more dynamic and flexible2022-6-23Institute of Computer SoftwareNanjing University15CompositeDecoratorVisitorCommandIteratorMementoProxyFacadeChain of ResponsibilityObserverBuilderSingletonPrototype FlyweightStateMediatorBridgeAbstract FactoryStrategyMore about PatternPatterns of PatternsAnti-patte

13、rnJ2EE patterns2022-6-23Institute of Computer SoftwareNanjing University16Anti-PatternsAndrew Koenig 1995, Michael Akroyd 1996An anti-pattern (反面模式) tells you how to go from a problem to a BAD solution.Tell you why a bad solution is attractive.Tell you why that solution in the long term is bad.Sugge

14、st other patterns that are applicable which may provide good solutions.2022-6-23Institute of Computer SoftwareNanjing University17Wiki反面模式分类组织结构的反面模式项目管理的反面模式团队管理的反面模式分析方式的反面模式一般设计上的反模式 面向对象设计的反面模式面向对象设计的反面模式编程上的反模式方法学上的反面模式测试反面模式配置管理反面模式2022-6-23Institute of Computer SoftwareNanjing University18Ane

15、mic Domain ModelBaseBeanCall superCircle-ellipse problemEmpty subclass failureGod objectObject cesspoolObject orgyPoltergeistsSequential couplingSingletonitiesYet another fucking layerYo-yo problemExample胖球反模式通过描述一个或几个类不断的膨胀,以至吞食掉整个面向对象架构。一般胖球的出现是由于一个类垄断了处理过程,而其他的类只是数据的封装体。症状和后果单个类拥有大量的属性或操作。单个类中封装了

16、异类的、不相关的属性和操作集。单个控制器类和几个简单的数据对象联系在一起。缺乏面向对象的设计,一个控制器类几乎封装了所有的应用功能。控制器类通常过于复杂,无法复用和测试。把这么个大类加载如内存中的代价可能会很高。 胖球反模式有两种形式:行为形式和数据形式。2022-6-23Institute of Computer SoftwareNanjing University19Example如何重构 (Refactor)把一些行为重新分配到某些封装了数据的对象上,并对对象之间的关系重新调整。确定代表契约的关系操作和属性集合,也就是把相关的属性和方法归类。寻找这些根据契约的到了集合的“自然的家”,并把

17、它们迁移过去。移除所有的“远耦合”或者说冗余的、间接的联系。最后,移除所有的瞬时联系。 总之,把一个控制器类变成了一个协调器类,让开始的总之,把一个控制器类变成了一个协调器类,让开始的数据类扩充一些处理逻辑,数据类在协调类的指导下进数据类扩充一些处理逻辑,数据类在协调类的指导下进行操作,这也只是职责的迁移。行操作,这也只是职责的迁移。2022-6-23Institute of Computer SoftwareNanjing University20More about PatternPatterns of PatternsAnti-patternJ2EE patterns2022-6-23I

18、nstitute of Computer SoftwareNanjing University21J2EE Patterns特点与平台、语言相关,描述的是J2EE平台上利用Java如何解决设计问题所解决问题的规模较大,在J2EE框架的基础上解决构件技术的选择、构件之间的协作等问题模式抽象层次较高,同时提供了若干具体实现的细节,称为策略策略。注重性能的优化2022-6-23Institute of Computer SoftwareNanjing University22J2EE Patterns Classification根据来源分为SJC设计模式和TheServerSide设计模式两大类J

19、2EE: JSP, Servlet, EJB, JMS, JDBC, JNDISJC (Sun Java Center)设计模式Web层模式:JSP,ServletIntercepting Filter, Front Controller, View Helper业务层模式:EJBBusiness Delegate, Value Object, Session Faade集成层模式:JMS,JDBCData Access Object, Service Activator2022-6-2323J2EE Patterns ClassificationTheServerSide设计模式EJB层体系

20、结构模式Session Faade, Message Faade, EJB Command层间数据传输模式Data Transfer Object, Data Transfer HashMap事务与持久性模式Version Number, JDBC for Reading客户端EJB交互模式EJB Home Factory, Business Delegate2022-6-23Institute of Computer SoftwareNanjing University24ExampleEJB调用是工厂模式的实现调用EJB 语法:EJBHome em = JNDIServer.getRemo

21、teHome(EJB-JNDI-NAME); EJBObject myEJB = em.create();em.create()类似Afactory.create();EJBObject 是接口2022-6-23Institute of Computer SoftwareNanjing University25Example会话 Bean和实体Bean: Faade模式一个会话Bean中调用多个实体Bean该会话Bean是一个Faade类/Manager类使用Faade 会话Bean优点:提供性能,节省客户端直接调用实体Bean的网络开销解耦分层,利于扩展变化。2022-6-23Institu

22、te of Computer SoftwareNanjing University26ExampleDTO(Data Transfer Object)模式DTO模式是指将数据封装成普通的JavaBeans,在J2EE多个层次之间传输。DTO类似信使,是同步系统中的Message该JavaBeans可以是一个数据模型Model2022-6-23Institute of Computer SoftwareNanjing University27ExampleMVC模式MVC模式是J2EE Web层的主要实现2022-6-23Institute of Computer SoftwareNanjing

23、 University28ExampleProxy模式代理模式可以强迫客户端对一个对象的方法调用间接通过代理类进行。通常代理模式有以下几种:访问代理(Access Proxy)、虚拟代理和远程代理远程代理等。动态Proxy模式动态代理利用Java的反射(Reflect)机制,可以在运行时刻将一个对象实例的方法调用分派到另外一个对象实例的调用。动态代理模式可以在运行时刻创建继承某个接口的类型安全的代理对象,而无需在代码编译时编译这些代理类代码。 2022-6-23Institute of Computer SoftwareNanjing University29J2EE Anti-Pattern

24、无EJB不叫J2EE过度分层 频繁的往返调用过度使用有状态的Session Bean过度会话万能Servlet或者万能JSP2022-6-23Institute of Computer SoftwareNanjing University30NowOO Design ToolboxOO BasicsOO PrinciplesOO PatternsYour patterns here!2022-6-23Institute of Computer SoftwareNanjing University31摘要More about PatternFramework: why, what, howCom

25、parison2022-6-23Institute of Computer SoftwareNanjing University32Forms of design-level reuseSharing of consistency: programming and scripting languageSharing concrete solution fragments: librariesSharing contracts: interfacesSharing individual architecture: patternsSharing subsystem architectures:

26、frameworksSharing overall structure: system architectures.2022-6-23Institute of Computer SoftwareNanjing University33FrameworkGoF: A framework is a set of cooperating classes, some of which may be abstract, that make up a reusable design for a specific class of software.A software framework is an ab

27、straction in which common code providing generic functionality can be selectively overridden or specialized by user code, thus providing specific functionality. Frameworks are a special case of software libraries in that they are reusable abstractions of code wrapped in a well-defined API, yet they

28、contain some key distinguishing features that separate them from normal libraries.2022-6-23Institute of Computer SoftwareNanjing University34Framework框架,其实就是某种应用的半成品,就是一组组件,供你选用完成你自己的系统。简单说就是使用别人搭好的舞台,你来做表演。而且,框架一般是成熟的,不断升级的软件。框架一般处在低层应用平台(如J2EE)和高层业务逻辑之间的中间层。2022-6-23Institute of Computer SoftwareN

29、anjing University35Framework2022-6-23Institute of Computer SoftwareNanjing University36应用程序专业领域框架一般性应用框架支持性框架作业系统应用框架应用框架FrameworkFirst commercial application framework: MacApp by Apple Computer for MacintoshWell-known frameworksMFCStruts, Spring, Avalon, PicoContainerHibernateJdon2022-6-23Institute

30、 of Computer SoftwareNanjing University37Framework 特点Inversion of control - The overall programs flow of control is not dictated by the caller, but by the framework.Default behavior - A framework has a default behavior. This default behavior must actually be some useful behavior and not a series of

31、no-ops.Extensibility - A framework can be extended by the user usually by selective overriding or specialized by user code providing specific functionality.Non-modifiable framework code - The framework code, in general, is not allowed to be modified. Users can extend the framework, but not modify it

32、s code.2022-6-23Institute of Computer SoftwareNanjing University38How to design a good framework?领域驱动设计 (Domain Driven Design)控制反转IoC (Inversion of Control) or 依赖注入DI (Dependence Injection)AOP (Aspect Oriented Programming)2022-6-23Institute of Computer SoftwareNanjing University39Domain Driven Desig

33、n简称DDD http:/domaindrivendesign.org/The premise of domain-driven design is two-fold: For most software projects, the primary focus should be on the domain and domain logic; and Complex domain designs should be based on a model. 2022-6-23Institute of Computer SoftwareNanjing University40DDD Backgroun

34、d开始有人抱怨使用Java开发Web网站系统,类似大炮轰蚊子。“Ruby on Rails (RoR)是Web系统主打选择”对Java世界提出冲击和挑战。但是使用另外一种语言有其他陷阱。对Java世界提出:吸取DDD框架(RoR)优点,真正简化Java企业系统开发,不能打着简化反简化,不能因为IoC/AOP导致复杂化。2022-6-23Institute of Computer SoftwareNanjing University41DDDDomain-driven design is not a technology or a methodology. It is a way of thin

35、king and a set of priorities, aimed at accelerating software projects that have to deal with complicated domains. 2022-6-23Institute of Computer SoftwareNanjing University42大师说DDD2022-6-23Institute of Computer SoftwareNanjing University43DDD核心思想: 面向领域模型(Domain Model)编程,尽可能减少层次之间混乱调用,化多层编码为多层配化多层编码为多

36、层配置置,提供多层编码中各层的缺省实现前提: 不能丧失多层结构,否则返回Delphi/VB时代;保证多层之间的松耦合保证多层之间的松耦合。2022-6-23Institute of Computer SoftwareNanjing University44DDD特点统一语言:统一语言:一个无处不在(ubiquitous )的语言,项目中所有人统一交流的语言。减少沟通疑惑,减少传达走样。使得软件更加适合需求。统一领域模型统一领域模型:领域专家和程序员统一使用一种模型,没有数据库数据表等专业软件技术干扰。专门的业务领域层专门的业务领域层:领域层除了业务没有其他,没有软件架构 框架等等底层技术。以模

37、型为核心的卫星图Domain Model Domain ServiceLoggingGUIPersistence坏设计之一:失血模型尽管使用MVC模式和框架,但是将大部分业务逻辑写在控制器Controller中,如Struts的Action。甚至一个Action有几千行。尽管使用SOA的服务,但是将大部分业务逻辑写在服务中。在上面两种设计中,模型是只有setter/getter方法失血模型。模型对象成了纯粹的数据包装,没有业务行为和方法。失血模型导致软件难于拓展和维护,重新回到面向过程的编程老思路。随着时间推移,开发效率降低。坏设计之二:基于数据表的设计最初订单有OrderItemId, Or

38、derId, ProductId 和 Qty。后来添加 MinDeliveryQty 和 PendingQty字段,这是和订单交货有关。订单和订单交货是两个概念,但是我们把这些字段都混合在一个类中了。尽管采取了OO设计,但这还是一种带有数据库设计影子的坏设计。 DDD设计:将实体的职责分离到不同限定场景设计:将实体的职责分离到不同限定场景 。领域设计和数据库设计不同领域不是把实体看成铁板一块,一开始就把它分解到各种场景。下订单和订单交货交付是两个场景,它们应该有彼此独立的接口,由实体来实现。在数据库中它们是一个,也就是说,从ER模型上看,它们是一个整体,但是从domain model领域模型角

39、度看,它们是分离的。 DDD advantage左图:跨层混乱 ; 右图:以Model为线索有条理2022-6-23Institute of Computer SoftwareNanjing University50以Jdon Framework (JF)为例Domain Model是JF系统的第一个设计开发对象。每个Model都必须有一个主键;或唯一标识。由Domain Model延伸界面模型和持久实体。2022-6-23Institute of Computer SoftwareNanjing University51以以Domain为核心为核心http:/ of Computer Sof

40、twareNanjing University52Model Configuration使用配置替代传统MVC模式Controller编码:2022-6-23Institute of Computer SoftwareNanjing University53IoC控制反转 Inversion of ControlReviewDesign Principles: DIP (dependence inversion)Hollywood Principle: Dont call us, well call you.依赖注入 Dependence Injection (by Martin Fowler

41、)http:/ of Computer SoftwareNanjing University54IoC MotivationFirstWe knew from basic Java tutorial, that all objects should be created first before use.ThenWe knew how to create objects by usage of factory patterns.NowWe will study how to get an object from an IoC container and use it without creat

42、ing it by ourselves. 2022-6-23Institute of Computer SoftwareNanjing University55abc =new ABC()abc =Factory.createInstanceOfABC()Example2022-6-23Institute of Computer SoftwareNanjing University56public class JDBCDataManger public void accessData() DataSource dataSource = new DataSource(); /access dat

43、a . public class JDBCDataManger public void accessData() DataSource dataSource = ApplciationResources.getDataSource(); /access data . In either case, the JDBCDataManager has to fetch the DataSource itself!http:/ Motivation应用控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用,传递给它。也可以说,依赖被注入到对象中。所以,控制反转是,关于

44、一个对象如何获取它所依赖的对象的引用,这个责任的反转。2022-6-23Institute of Computer SoftwareNanjing University57IoC Approach IInterface Injection / Contextualized Dependency Lookup (Type 1)components implement specific interfaces provided by their containers in order to be configured2022-6-2358import org.apache.avalon.framew

45、ork.*;public class JDBCDataManger implements Serviceable DataSource dataSource; public void service (ServiceManager sm) throws ServiceException dataSource = (DataSource)sm.lookup(dataSource); public void getData() /use dataSource for something The requirement to implement specific interfaces can giv

46、e code a bloated feel, while at the same time coupling your application code to the underlying framework. Avalon IoC Approach IISetter Injection (Type 2)some external metadata is used to resolve dependencies. 2022-6-23Institute of Computer SoftwareNanjing University59public class JDBCDataManger priv

47、ate DataSource dataSource; public void setDataManager(DataSource dataSource this.dataSource = dataSource; public void getData() /use dataSource for something Looks like a normal bean!Spring IoC Approach IIMetadata: takes the form of an XML configuration file in Spring.2022-6-23Institute of Computer

48、SoftwareNanjing University60 com.mydb.jdbc.Driver jdbc:mydb:/server:port/mydb root Metadata can be reused by multiple components!IoC Approach IIdefine an instance of our manager and pass in a reference to the data source 2022-6-23Institute of Computer SoftwareNanjing University61 Make unit test easi

49、er! Application code is not tied to the container.Potential downsides: Component relationship unclear; Encapsulation brokenIoC Approach IIIConstructor Injection (Type 3)Based on the principle of “Good Citizen”Register an object with the framework, specify the parameters to use (which can in turn be

50、created by the framework itself) and then just request an instance. The components being registered just have to implement a constructor, which can be used to inject the dependencies. 2022-6-23Institute of Computer SoftwareNanjing University62IoC Approach IIINeed a constructor2022-6-23Institute of C

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公、行业 > 各类PPT课件(模板)
版权提示 | 免责声明

1,本文(面向对象技术课件.ppt)为本站会员(三亚风情)主动上传,163文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。
2,用户下载本文档,所消耗的文币(积分)将全额增加到上传者的账号。
3, 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(发送邮件至3464097650@qq.com或直接QQ联系客服),我们立即给予删除!


侵权处理QQ:3464097650--上传资料QQ:3464097650

【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。


163文库-Www.163Wenku.Com |网站地图|