面向对象技术-计算机系主页.ppt

上传人:ga****84 文档编号:315979 上传时间:2018-09-21 格式: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 & Framework,2018/9/21,Institute of Computer SoftwareNanjing University,1,摘要,More about PatternFrameworkComparison,2018/9/21,Institute of Computer SoftwareNanjing University,2,摘要,More about PatternFrameworkComparison,2018/9/21,Institute of Computer SoftwareNanjing University,3,More abou

2、t Pattern,Patterns of PatternsAnti-patternJ2EE patterns,2018/9/21,Institute of Computer SoftwareNanjing University,4,More about Pattern,Patterns of PatternsAnti-patternJ2EE patterns,2018/9/21,Institute of Computer SoftwareNanjing University,5,Working together,Patterns are often used together and com

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

4、tute of Computer SoftwareNanjing University,7,使用设计模式,Composite表示文档的物理结构:递归组合Strategy允许不同的格式化算法Decorator修饰用户界面Abstract Factory支持多视感标准Bridge允许多个窗口平台Command支持撤销用户操作Iterator访问和遍历对象结构Visitor允许无限扩充分析能力而又不会使文档结构的实现复杂化,2018/9/21,Institute of Computer SoftwareNanjing University,8,Duck Example,Start with a bu

5、nch of Quackables.A goose came along and 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 quackabl

6、es.The Quackologists also wanted to be notified when any quackable quacked.,2018/9/21,Institute of Computer SoftwareNanjing University,9,Adapter,Decorator,Abstract factory,Composite,Iterator,Observer,Compound Patterns,MVCa song about MVC by James Dempsey (lyrics),2018/9/21,Institute of Computer Soft

7、wareNanjing University,10,MVC,MVC is a compound pattern consisting 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.,2018/9/21,Institute of Computer SoftwareNanjing University,11,Patte

8、rn Definition Review,A Pattern is a solution to a problem in a 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

9、 the goal and set of constraints,2018/9/21,Institute of Computer SoftwareNanjing University,12,Pattern Categories I - Goal,Creational 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 obj

10、ects into larger structures.Behavioral Patterns: concerned with how classes and objects interact and distribute responsibility,2018/9/21,Institute of Computer SoftwareNanjing University,13,Pattern Categories II - Scope,Class PatternsDescribe how relationships between classes are defined via inherita

11、nce. Relationships in class patterns are established at compile time.,2018/9/21,Institute of Computer SoftwareNanjing University,14,Pattern Categories II - Scope,Object PatternsDescribe relationships between objects and are primarily defined by composition.Relationships in object patterns are typica

12、lly created at runtime and are more dynamic and flexible,2018/9/21,Institute of Computer SoftwareNanjing University,15,More about Pattern,Patterns of PatternsAnti-patternJ2EE patterns,2018/9/21,Institute of Computer SoftwareNanjing University,16,Anti-Patterns,Andrew Koenig 1995, Michael Akroyd 1996A

13、n 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.Suggest other patterns that are applicable which may provide good solutions.,2018/9/21,Institute of Computer SoftwareNanjing Univers

14、ity,17,Wiki,反面模式分类,组织结构的反面模式项目管理的反面模式团队管理的反面模式分析方式的反面模式一般设计上的反模式 面向对象设计的反面模式编程上的反模式方法学上的反面模式测试反面模式配置管理反面模式,2018/9/21,Institute of Computer SoftwareNanjing University,18,Example,胖球反模式通过描述一个或几个类不断的膨胀,以至吞食掉整个面向对象架构。一般胖球的出现是由于一个类垄断了处理过程,而其他的类只是数据的封装体。症状和后果单个类拥有大量的属性或操作。单个类中封装了异类的、不相关的属性和操作集。单个控制器类和几个简单的

15、数据对象联系在一起。缺乏面向对象的设计,一个控制器类几乎封装了所有的应用功能。控制器类通常过于复杂,无法复用和测试。把这么个大类加载如内存中的代价可能会很高。 胖球反模式有两种形式:行为形式和数据形式。,2018/9/21,Institute of Computer SoftwareNanjing University,19,Example,如何重构 (Refactor)把一些行为重新分配到某些封装了数据的对象上,并对对象之间的关系重新调整。确定代表契约的关系操作和属性集合,也就是把相关的属性和方法归类。寻找这些根据契约的到了集合的“自然的家”,并把它们迁移过去。移除所有的“远耦合”或者说冗余

16、的、间接的联系。最后,移除所有的瞬时联系。 总之,把一个控制器类变成了一个协调器类,让开始的数据类扩充一些处理逻辑,数据类在协调类的指导下进行操作,这也只是职责的迁移。,2018/9/21,Institute of Computer SoftwareNanjing University,20,More about Pattern,Patterns of PatternsAnti-patternJ2EE patterns,2018/9/21,Institute of Computer SoftwareNanjing University,21,J2EE Patterns,特点与平台、语言相关,描

17、述的是J2EE平台上利用Java如何解决设计问题所解决问题的规模较大,在J2EE框架的基础上解决构件技术的选择、构件之间的协作等问题模式抽象层次较高,同时提供了若干具体实现的细节,称为策略。注重性能的优化,2018/9/21,Institute of Computer SoftwareNanjing University,22,J2EE Patterns Classification,根据来源分为SJC设计模式和TheServerSide设计模式两大类J2EE: JSP, Servlet, EJB, JMS, JDBC, JNDISJC (Sun Java Center)设计模式Web层模式:

18、JSP,ServletIntercepting Filter, Front Controller, View Helper业务层模式:EJBBusiness Delegate, Value Object, Session Faade集成层模式:JMS,JDBCData Access Object, Service Activator,2018/9/21,23,J2EE Patterns Classification,TheServerSide设计模式EJB层体系结构模式Session Faade, Message Faade, EJB Command层间数据传输模式Data Transfer

19、Object, Data Transfer HashMap事务与持久性模式Version Number, JDBC for Reading客户端EJB交互模式EJB Home Factory, Business Delegate,2018/9/21,Institute of Computer SoftwareNanjing University,24,Example,EJB调用是工厂模式的实现调用EJB 语法:EJBHome em = JNDIServer.getRemoteHome(EJB-JNDI-NAME); EJBObject myEJB = em.create();em.create

20、()类似Afactory.create();EJBObject 是接口,2018/9/21,Institute of Computer SoftwareNanjing University,25,Example,会话 Bean和实体Bean: Faade模式一个会话Bean中调用多个实体Bean该会话Bean是一个Faade类/Manager类使用Faade 会话Bean优点:提供性能,节省客户端直接调用实体Bean的网络开销解耦分层,利于扩展变化。,2018/9/21,Institute of Computer SoftwareNanjing University,26,Example,DT

21、O(Data Transfer Object)模式DTO模式是指将数据封装成普通的JavaBeans,在J2EE多个层次之间传输。DTO类似信使,是同步系统中的Message该JavaBeans可以是一个数据模型Model,2018/9/21,Institute of Computer SoftwareNanjing University,27,Example,MVC模式MVC模式是J2EE Web层的主要实现,2018/9/21,Institute of Computer SoftwareNanjing University,28,Example,Proxy模式代理模式可以强迫客户端对一个对

22、象的方法调用间接通过代理类进行。通常代理模式有以下几种:访问代理(Access Proxy)、虚拟代理和远程代理等。动态Proxy模式动态代理利用Java的反射(Reflect)机制,可以在运行时刻将一个对象实例的方法调用分派到另外一个对象实例的调用。动态代理模式可以在运行时刻创建继承某个接口的类型安全的代理对象,而无需在代码编译时编译这些代理类代码。,2018/9/21,Institute of Computer SoftwareNanjing University,29,J2EE Anti-Pattern,无EJB不叫J2EE过度分层 频繁的往返调用过度使用有状态的Session Bean

23、过度会话万能Servlet或者万能JSP,2018/9/21,Institute of Computer SoftwareNanjing University,30,NowOO Design Toolbox,OO BasicsOO PrinciplesOO PatternsYour patterns here!,2018/9/21,Institute of Computer SoftwareNanjing University,31,摘要,More about PatternFramework: why, what, howComparison,2018/9/21,Institute of C

24、omputer SoftwareNanjing University,32,Forms of design-level reuse,Sharing of consistency: programming and scripting languageSharing concrete solution fragments: librariesSharing contracts: interfacesSharing individual architecture: patternsSharing subsystem architectures: frameworksSharing overall s

25、tructure: system architectures.,2018/9/21,Institute of Computer SoftwareNanjing University,33,Framework,GoF: 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 abstraction in which com

26、mon 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 contain some key disti

27、nguishing features that separate them from normal libraries.,2018/9/21,Institute of Computer SoftwareNanjing University,34,Framework,框架,其实就是某种应用的半成品,就是一组组件,供你选用完成你自己的系统。简单说就是使用别人搭好的舞台,你来做表演。而且,框架一般是成熟的,不断升级的软件。框架一般处在低层应用平台(如J2EE)和高层业务逻辑之间的中间层。,2018/9/21,Institute of Computer SoftwareNanjing Universi

28、ty,35,Framework,2018/9/21,Institute of Computer SoftwareNanjing University,36,应用程序,专业领域框架,一般性应用框架,支持性框架,作业系统,应用框架,Framework,First commercial application framework: MacApp by Apple Computer for MacintoshWell-known frameworksMFCStruts, Spring, Avalon, PicoContainerHibernateJdon,2018/9/21,Institute of

29、Computer SoftwareNanjing University,37,Framework 特点,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 n

30、o-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 its

31、 code.,2018/9/21,Institute of Computer SoftwareNanjing University,38,How to design a good framework?,领域驱动设计 (Domain Driven Design)控制反转IoC (Inversion of Control) or 依赖注入DI (Dependence Injection)AOP (Aspect Oriented Programming),2018/9/21,Institute of Computer SoftwareNanjing University,39,Domain Driv

32、en Design,简称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.,2018/9/21,Institute of Computer SoftwareNanjing University,40,D

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

34、t is a way of thinking and a set of priorities, aimed at accelerating software projects that have to deal with complicated domains.,2018/9/21,Institute of Computer SoftwareNanjing University,42,大师说DDD,2018/9/21,Institute of Computer SoftwareNanjing University,43,DDD,核心思想: 面向领域模型(Domain Model)编程,尽可能减

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

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

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

38、advantage,左图:跨层混乱 ; 右图:以Model为线索有条理,2018/9/21,Institute of Computer SoftwareNanjing University,50,以Jdon Framework (JF)为例,Domain Model是JF系统的第一个设计开发对象。每个Model都必须有一个主键;或唯一标识。由Domain Model延伸界面模型和持久实体。,2018/9/21,Institute of Computer SoftwareNanjing University,51,以Domain为核心,http:/ of Computer SoftwareNan

39、jing University,52,Model Configuration,使用配置替代传统MVC模式Controller编码:,2018/9/21,Institute of Computer SoftwareNanjing University,53,IoC,控制反转 Inversion of ControlReviewDesign Principles: DIP (dependence inversion)Hollywood Principle: Dont call us, well call you.依赖注入 Dependence Injection (by Martin Fowler

40、)http:/ of Computer SoftwareNanjing University,54,IoC Motivation,FirstWe 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 cr

41、eating it by ourselves.,2018/9/21,Institute of Computer SoftwareNanjing University,55,abc =new ABC(),abc =Factory.createInstanceOfABC(),Example,2018/9/21,Institute of Computer SoftwareNanjing University,56,public class JDBCDataManger public void accessData() DataSource dataSource = new DataSource();

42、 /access data . ,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,应用控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用,传递给它。也可以说,依赖被注入

43、到对象中。所以,控制反转是,关于一个对象如何获取它所依赖的对象的引用,这个责任的反转。,2018/9/21,Institute of Computer SoftwareNanjing University,57,IoC Approach I,Interface Injection / Contextualized Dependency Lookup (Type 1)components implement specific interfaces provided by their containers in order to be configured,2018/9/21,58,import

44、org.apache.avalon.framework.*;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 sp

45、ecific interfaces can give code a bloated feel, while at the same time coupling your application code to the underlying framework.,Avalon,IoC Approach II,Setter Injection (Type 2)some external metadata is used to resolve dependencies.,2018/9/21,Institute of Computer SoftwareNanjing University,59,pub

46、lic class JDBCDataManger private 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 II,Metadata: takes the form of an XML configuration file in Spring

47、.,2018/9/21,Institute of Computer SoftwareNanjing University,60, com.mydb.jdbc.Driver jdbc:mydb:/server:port/mydb root ,Metadata can be reused by multiple components!,IoC Approach II,define an instance of our manager and pass in a reference to the data source,2018/9/21,Institute of Computer Software

48、Nanjing University,61, ,Make unit test easier! Application code is not tied to the container.,Potential downsides: Component relationship unclear; Encapsulation broken,IoC Approach III,Constructor 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 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.,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 重点行业资料库 > 1

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。