模式框架.ppt

上传人:本田雅阁 文档编号:3187481 上传时间:2019-07-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页
点击查看更多>>
资源描述

《模式框架.ppt》由会员分享,可在线阅读,更多相关《模式框架.ppt(76页珍藏版)》请在三一文库上搜索。

1、模式&框架,Pattern & Framework,2019/7/23,Institute of Computer Software Nanjing University,1,摘要,More about Pattern Framework Comparison,2019/7/23,Institute of Computer Software Nanjing University,2,摘要,More about Pattern Framework Comparison,2019/7/23,Institute of Computer Software Nanjing University,3,Mo

2、re about Pattern,Patterns of Patterns Anti-pattern J2EE patterns,2019/7/23,Institute of Computer Software Nanjing University,4,More about Pattern,Patterns of Patterns Anti-pattern J2EE patterns,2019/7/23,Institute of Computer Software Nanjing University,5,Working together,Patterns are often used tog

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

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

5、,8,Duck Example,Start with a bunch 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 t

6、hose ducks and geese and quackables. The Quackologists also wanted to be notified when any quackable quacked.,2019/7/23,Institute of Computer Software Nanjing University,9,Adapter,Decorator,Abstract factory,Composite,Iterator,Observer,Compound Patterns,MVC a song about MVC by James Dempsey (lyrics),

7、2019/7/23,Institute of Computer Software Nanjing 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.,2019/7/23,Institute of Comput

8、er Software Nanjing University,11,Pattern Definition Review,A Pattern is a solution to a problem in a context. Context: the recurring situation in which the pattern applies Problem: the goal you are trying to achieve in this context and any constrains that occur in the context. Solution: a general d

9、esign that anyone can apply which resolves the goal and set of constraints,2019/7/23,Institute of Computer Software Nanjing 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.

10、 Structural Patterns: compose classes or objects into larger structures. Behavioral Patterns: concerned with how classes and objects interact and distribute responsibility,2019/7/23,Institute of Computer Software Nanjing University,13,Pattern Categories II - Scope,Class Patterns Describe how relatio

11、nships between classes are defined via inheritance. Relationships in class patterns are established at compile time.,2019/7/23,Institute of Computer Software Nanjing University,14,Pattern Categories II - Scope,Object Patterns Describe relationships between objects and are primarily defined by compos

12、ition. Relationships in object patterns are typically created at runtime and are more dynamic and flexible,2019/7/23,Institute of Computer Software Nanjing University,15,More about Pattern,Patterns of Patterns Anti-pattern J2EE patterns,2019/7/23,Institute of Computer Software Nanjing University,16,

13、Anti-Patterns,Andrew Koenig 1995, Michael Akroyd 1996 An 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 solution

14、s.,2019/7/23,Institute of Computer Software Nanjing University,17,Wiki,反面模式分类,组织结构的反面模式 项目管理的反面模式 团队管理的反面模式 分析方式的反面模式 一般设计上的反模式 面向对象设计的反面模式 编程上的反模式 方法学上的反面模式 测试反面模式 配置管理反面模式,2019/7/23,Institute of Computer Software Nanjing University,18,Example,胖球反模式 通过描述一个或几个类不断的膨胀,以至吞食掉整个面向对象架构。一般胖球的出现是由于一个类垄断了处理过

15、程,而其他的类只是数据的封装体。 症状和后果 单个类拥有大量的属性或操作。 单个类中封装了异类的、不相关的属性和操作集。 单个控制器类和几个简单的数据对象联系在一起。 缺乏面向对象的设计,一个控制器类几乎封装了所有的应用功能。 控制器类通常过于复杂,无法复用和测试。 把这么个大类加载如内存中的代价可能会很高。 胖球反模式有两种形式:行为形式和数据形式。,2019/7/23,Institute of Computer Software Nanjing University,19,Example,如何重构 (Refactor) 把一些行为重新分配到某些封装了数据的对象上,并对对象之间的关系重新调整

16、。 确定代表契约的关系操作和属性集合,也就是把相关的属性和方法归类。 寻找这些根据契约的到了集合的“自然的家”,并把它们迁移过去。 移除所有的“远耦合”或者说冗余的、间接的联系。 最后,移除所有的瞬时联系。 总之,把一个控制器类变成了一个协调器类,让开始的数据类扩充一些处理逻辑,数据类在协调类的指导下进行操作,这也只是职责的迁移。,2019/7/23,Institute of Computer Software Nanjing University,20,More about Pattern,Patterns of Patterns Anti-pattern J2EE patterns,201

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

18、heServerSide设计模式两大类 J2EE: JSP, Servlet, EJB, JMS, JDBC, JNDI SJC (Sun Java Center)设计模式 Web层模式:JSP,Servlet Intercepting Filter, Front Controller, View Helper 业务层模式:EJB Business Delegate, Value Object, Session Faade 集成层模式:JMS,JDBC Data Access Object, Service Activator,2019/7/23,23,J2EE Patterns Classi

19、fication,TheServerSide设计模式 EJB层体系结构模式 Session Faade, Message Faade, EJB Command 层间数据传输模式 Data Transfer Object, Data Transfer HashMap 事务与持久性模式 Version Number, JDBC for Reading 客户端EJB交互模式 EJB Home Factory, Business Delegate,2019/7/23,Institute of Computer Software Nanjing University,24,Example,EJB调用是工

20、厂模式的实现 调用EJB 语法: EJBHome em = JNDIServer.getRemoteHome(EJB-JNDI-NAME); EJBObject myEJB = em.create(); em.create()类似Afactory.create(); EJBObject 是接口,2019/7/23,Institute of Computer Software Nanjing University,25,Example,会话 Bean和实体Bean: Faade模式 一个会话Bean中调用多个实体Bean 该会话Bean是一个Faade类/Manager类 使用Faade 会话B

21、ean优点: 提供性能,节省客户端直接调用实体Bean的网络开销 解耦分层,利于扩展变化。,2019/7/23,Institute of Computer Software Nanjing University,26,Example,DTO(Data Transfer Object)模式 DTO模式是指将数据封装成普通的JavaBeans,在J2EE多个层次之间传输。 DTO类似信使,是同步系统中的Message 该JavaBeans可以是一个数据模型Model,2019/7/23,Institute of Computer Software Nanjing University,27,Exa

22、mple,MVC模式 MVC模式是J2EE Web层的主要实现,2019/7/23,Institute of Computer Software Nanjing University,28,Example,Proxy模式 代理模式可以强迫客户端对一个对象的方法调用间接通过代理类进行。 通常代理模式有以下几种:访问代理(Access Proxy)、虚拟代理和远程代理等。 动态Proxy模式 动态代理利用Java的反射(Reflect)机制,可以在运行时刻将一个对象实例的方法调用分派到另外一个对象实例的调用。 动态代理模式可以在运行时刻创建继承某个接口的类型安全的代理对象,而无需在代码编译时编译这

23、些代理类代码。,2019/7/23,Institute of Computer Software Nanjing University,29,J2EE Anti-Pattern,无EJB不叫J2EE 过度分层 频繁的往返调用 过度使用有状态的Session Bean 过度会话 万能Servlet或者万能JSP,2019/7/23,Institute of Computer Software Nanjing University,30,NowOO Design Toolbox,OO Basics OO Principles OO Patterns Your patterns here!,2019

24、/7/23,Institute of Computer Software Nanjing University,31,摘要,More about Pattern Framework: why, what, how Comparison,2019/7/23,Institute of Computer Software Nanjing University,32,Forms of design-level reuse,Sharing of consistency: programming and scripting language Sharing concrete solution fragme

25、nts: libraries Sharing contracts: interfaces Sharing individual architecture: patterns Sharing subsystem architectures: frameworks Sharing overall structure: system architectures.,2019/7/23,Institute of Computer Software Nanjing University,33,Framework,GoF: A framework is a set of cooperating classe

26、s, 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 common code providing generic functionality can be selectively overridden or specialized by user code, thus providing specific functionality. Frameworks

27、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 distinguishing features that separate them from normal libraries.,2019/7/23,Institute of Computer Software Nanjing University,34,Framework,框架,其实就是某种应用的半成品,

28、就是一组组件,供你选用完成你自己的系统。简单说就是使用别人搭好的舞台,你来做表演。而且,框架一般是成熟的,不断升级的软件。 框架一般处在低层应用平台(如J2EE)和高层业务逻辑之间的中间层。,2019/7/23,Institute of Computer Software Nanjing University,35,Framework,2019/7/23,Institute of Computer Software Nanjing University,36,应用程序,专业领域框架,一般性应用框架,支持性框架,作业系统,应用框架,Framework,First commercial appli

29、cation framework: MacApp by Apple Computer for Macintosh Well-known frameworks MFC Struts, Spring, Avalon, PicoContainer Hibernate Jdon ,2019/7/23,Institute of Computer Software Nanjing University,37,Framework 特点,Inversion of control - The overall programs flow of control is not dictated by the call

30、er, 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 no-ops. Extensibility - A framework can be extended by the user usually by selective overriding or specialized by user code providing speci

31、fic 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 code.,2019/7/23,Institute of Computer Software Nanjing University,38,How to design a good framework?,领域驱动设计 (Domain Driven Design) 控制反转

32、IoC (Inversion of Control) or 依赖注入DI (Dependence Injection) AOP (Aspect Oriented Programming) ,2019/7/23,Institute of Computer Software Nanjing University,39,Domain Driven Design,简称DDD http:/domaindrivendesign.org/ The premise of domain-driven design is two-fold: For most software projects, the prim

33、ary focus should be on the domain and domain logic; and Complex domain designs should be based on a model.,2019/7/23,Institute of Computer Software Nanjing University,40,DDD Background,开始有人抱怨使用Java开发Web网站系统,类似大炮轰蚊子。 “Ruby on Rails (RoR)是Web系统主打选择”对Java世界提出冲击和挑战。但是使用另外一种语言有其他陷阱。 对Java世界提出:吸取DDD框架(RoR

34、)优点,真正简化Java企业系统开发,不能打着简化反简化,不能因为IoC/AOP导致复杂化。,2019/7/23,Institute of Computer Software Nanjing University,41,DDD,Domain-driven design is not a technology or a methodology. It is a way of thinking and a set of priorities, aimed at accelerating software projects that have to deal with complicated dom

35、ains.,2019/7/23,Institute of Computer Software Nanjing University,42,大师说DDD,2019/7/23,Institute of Computer Software Nanjing University,43,DDD,核心思想: 面向领域模型(Domain Model)编程,尽可能减少层次之间混乱调用,化多层编码为多层配置,提供多层编码中各层的缺省实现 前提: 不能丧失多层结构,否则返回Delphi/VB时代;保证多层之间的松耦合。,2019/7/23,Institute of Computer Software Nanjin

36、g University,44,DDD特点,统一语言:一个无处不在(ubiquitous )的语言,项目中所有人统一交流的语言。减少沟通疑惑,减少传达走样。使得软件更加适合需求。 统一领域模型:领域专家和程序员统一使用一种模型,没有数据库数据表等专业软件技术干扰。 专门的业务领域层:领域层除了业务没有其他,没有软件架构 框架等等底层技术。,以模型为核心的卫星图,Domain Model,Domain Service,Logging,GUI,Persistence,坏设计之一:失血模型,尽管使用MVC模式和框架,但是将大部分业务逻辑写在控制器Controller中,如Struts的Action。

37、甚至一个Action有几千行。 尽管使用SOA的服务,但是将大部分业务逻辑写在服务中。 在上面两种设计中,模型是只有setter/getter方法失血模型。模型对象成了纯粹的数据包装,没有业务行为和方法。 失血模型导致软件难于拓展和维护,重新回到面向过程的编程老思路。随着时间推移,开发效率降低。,坏设计之二:基于数据表的设计,最初订单有OrderItemId, OrderId, ProductId 和 Qty。 后来添加 MinDeliveryQty 和 PendingQty字段,这是和订单交货有关。 订单和订单交货是两个概念,但是我们把这些字段都混合在一个类中了。 尽管采取了OO设计,但这还

38、是一种带有数据库设计影子的坏设计。 DDD设计:将实体的职责分离到不同限定场景 。,领域设计和数据库设计不同,领域不是把实体看成铁板一块,一开始就把它分解到各种场景。 下订单和订单交货交付是两个场景,它们应该有彼此独立的接口,由实体来实现。 在数据库中它们是一个,也就是说,从ER模型上看,它们是一个整体,但是从domain model领域模型角度看,它们是分离的。,DDD advantage,左图:跨层混乱 ; 右图:以Model为线索有条理,2019/7/23,Institute of Computer Software Nanjing University,50,以Jdon Framewo

39、rk (JF)为例,Domain Model是JF系统的第一个设计开发对象。 每个Model都必须有一个主键;或唯一标识。 由Domain Model延伸界面模型和持久实体。,2019/7/23,Institute of Computer Software Nanjing University,51,以Domain为核心,http:/ of Computer Software Nanjing University,52,Model Configuration,使用配置替代传统MVC模式Controller编码:,2019/7/23,Institute of Computer Software

40、Nanjing University,53,IoC,控制反转 Inversion of Control Review Design Principles: DIP (dependence inversion) Hollywood Principle: Dont call us, well call you. 依赖注入 Dependence Injection (by Martin Fowler) http:/ of Computer Software Nanjing University,54,IoC Motivation,First We knew from basic Java tutor

41、ial, that all objects should be created first before use. Then We knew how to create objects by usage of factory patterns. Now We will study how to get an object from an IoC container and use it without creating it by ourselves.,2019/7/23,Institute of Computer Software Nanjing University,55,abc =new

42、 ABC(),abc =Factory.createInstanceOfABC(),Example,2019/7/23,Institute of Computer Software Nanjing University,56,public class JDBCDataManger public void accessData() DataSource dataSource = new DataSource(); /access data . ,public class JDBCDataManger public void accessData() DataSource dataSource =

43、 ApplciationResources.getDataSource(); /access data . ,In either case, the JDBCDataManager has to fetch the DataSource itself!,http:/ Motivation,应用控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用,传递给它。也可以说,依赖被注入到对象中。所以,控制反转是,关于一个对象如何获取它所依赖的对象的引用,这个责任的反转。,2019/7/23,Institute of Computer Software Nanjing

44、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,2019/7/23,58,import org.apache.avalon.framework.*; public class JDBCDataManger implements Serviceable DataSourc

45、e 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 give code a “bloated“ feel, while at the same time coupling your a

46、pplication code to the underlying framework.,Avalon,IoC Approach II,Setter Injection (Type 2) some external metadata is used to resolve dependencies.,2019/7/23,Institute of Computer Software Nanjing University,59,public class JDBCDataManger private DataSource dataSource; public void setDataManager(D

47、ataSource 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.,2019/7/23,Institute of Computer Software Nanjing University,60, com.mydb.jdbc.Driv

48、er 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,2019/7/23,Institute of Computer Software Nanjing University,61,Make unit test easier! Application code is not tied to the c

49、ontainer.,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

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

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1