1 / 76

Pattern & Framework

Pattern & Framework. 模式 & 框架. 摘要. More about Pattern Framework Comparison. 摘要. More about Pattern Framework Comparison. More about Pattern. Patterns of Patterns Anti-pattern J2EE patterns. More about Pattern. Patterns of Patterns Anti-pattern J2EE patterns. Working together.

reed
Download Presentation

Pattern & Framework

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pattern & Framework 模式&框架 Institute of Computer Software Nanjing University

  2. 摘要 • More about Pattern • Framework • Comparison Institute of Computer Software Nanjing University

  3. 摘要 • More about Pattern • Framework • Comparison Institute of Computer Software Nanjing University

  4. More about Pattern • Patterns of Patterns • Anti-pattern • J2EE patterns Institute of Computer Software Nanjing University

  5. More about Pattern • Patterns of Patterns • Anti-pattern • J2EE patterns Institute of Computer Software Nanjing University

  6. Working together • Patterns are often used together 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. Institute of Computer Software Nanjing University

  7. Example:设计一个文档编辑器 • 设计问题 • 文档结构 • 格式化 • 修饰用户界面 • 支持多种视感(look-and-feel)标准 • 支持多种窗口系统 • 用户操作 • 拼写检查和连字符 Institute of Computer Software Nanjing University

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

  9. 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 they’d 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 notified when any quackable quacked. Adapter Decorator Abstract factory Composite Iterator Observer Institute of Computer Software Nanjing University

  10. Compound Patterns • MVC • a song about MVC by James Dempsey (lyrics) Institute of Computer Software Nanjing University

  11. 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. Institute of Computer Software Nanjing University

  12. 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 design that anyone can apply which resolves the goal and set of constraints Institute of Computer Software Nanjing University

  13. 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 objects into larger structures. • Behavioral Patterns: concerned with how classes and objects interact and distribute responsibility Institute of Computer Software Nanjing University

  14. Template Method Factory Method Interpreter Adapter Pattern Categories II -- Scope • Class Patterns • Describe how relationships between classes are defined via inheritance. • Relationships in class patterns are established at compile time. Institute of Computer Software Nanjing University

  15. Visitor Command Composite Iterator Decorator Proxy Strategy Facade Observer Chain of Responsibility Prototype Flyweight Mediator Bridge State Builder Memento Singleton Abstract Factory Pattern Categories II -- Scope • Object Patterns • Describe relationships between objects and are primarily defined by composition. • Relationships in object patterns are typically created at runtime and are more dynamic and flexible Institute of Computer Software Nanjing University

  16. More about Pattern • Patterns of Patterns • Anti-pattern • J2EE patterns Institute of Computer Software Nanjing University

  17. 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 solutions. Wiki Institute of Computer Software Nanjing University

  18. 反面模式分类 • 组织结构的反面模式 • 项目管理的反面模式 • 团队管理的反面模式 • 分析方式的反面模式 • 一般设计上的反模式 • 面向对象设计的反面模式 • 编程上的反模式 • 方法学上的反面模式 • 测试反面模式 • 配置管理反面模式 Anemic Domain Model BaseBean Call super Circle-ellipse problem Empty subclass failure God object Object cesspool Object orgy Poltergeists Sequential coupling Singletonities Yet another fucking layer Yo-yo problem Institute of Computer Software Nanjing University

  19. Example • 胖球反模式 • 通过描述一个或几个类不断的膨胀,以至吞食掉整个面向对象架构。一般胖球的出现是由于一个类垄断了处理过程,而其他的类只是数据的封装体。 • 症状和后果 • 单个类拥有大量的属性或操作。 • 单个类中封装了异类的、不相关的属性和操作集。 • 单个控制器类和几个简单的数据对象联系在一起。 • 缺乏面向对象的设计,一个控制器类几乎封装了所有的应用功能。 • 控制器类通常过于复杂,无法复用和测试。 • 把这么个大类加载如内存中的代价可能会很高。 • 胖球反模式有两种形式:行为形式和数据形式。 Institute of Computer Software Nanjing University

  20. Example • 如何重构 (Refactor) • 把一些行为重新分配到某些封装了数据的对象上,并对对象之间的关系重新调整。 • 确定代表契约的关系操作和属性集合,也就是把相关的属性和方法归类。 • 寻找这些根据契约的到了集合的“自然的家”,并把它们迁移过去。 • 移除所有的“远耦合”或者说冗余的、间接的联系。 • 最后,移除所有的瞬时联系。 • 总之,把一个控制器类变成了一个协调器类,让开始的数据类扩充一些处理逻辑,数据类在协调类的指导下进行操作,这也只是职责的迁移。 Institute of Computer Software Nanjing University

  21. More about Pattern • Patterns of Patterns • Anti-pattern • J2EE patterns Institute of Computer Software Nanjing University

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

  23. J2EE Patterns Classification • 根据来源分为SJC设计模式和TheServerSide设计模式两大类 • 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 Façade… • 集成层模式:JMS,JDBC • Data Access Object, Service Activator

  24. J2EE Patterns Classification • TheServerSide设计模式 • EJB层体系结构模式 • Session Façade, Message Façade, EJB Command… • 层间数据传输模式 • Data Transfer Object, Data Transfer HashMap… • 事务与持久性模式 • Version Number, JDBC for Reading… • 客户端EJB交互模式 • EJB Home Factory, Business Delegate Institute of Computer Software Nanjing University

  25. Example • EJB调用是工厂模式的实现 • 调用EJB 语法:EJBHome em = JNDIServer.getRemoteHome(EJB-JNDI-NAME); EJBObject myEJB = em.create(); • em.create()类似Afactory.create(); • EJBObject 是接口 Institute of Computer Software Nanjing University

  26. Example • 会话 Bean和实体Bean: Façade模式 • 一个会话Bean中调用多个实体Bean • 该会话Bean是一个Façade类/Manager类 • 使用Façade 会话Bean优点: • 提供性能,节省客户端直接调用实体Bean的网络开销 • 解耦分层,利于扩展变化。 Institute of Computer Software Nanjing University

  27. Example • DTO(Data Transfer Object)模式 • DTO模式是指将数据封装成普通的JavaBeans,在J2EE多个层次之间传输。 • DTO类似信使,是同步系统中的Message • 该JavaBeans可以是一个数据模型Model Institute of Computer Software Nanjing University

  28. Example • MVC模式 • MVC模式是J2EE Web层的主要实现 Institute of Computer Software Nanjing University

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

  30. J2EE Anti-Pattern • 无EJB不叫J2EE • 过度分层 • 频繁的往返调用 • 过度使用有状态的Session Bean • 过度会话 • 万能Servlet或者万能JSP Institute of Computer Software Nanjing University

  31. Now…OO Design Toolbox • OO Basics • OO Principles • OO Patterns • Your patterns here! Institute of Computer Software Nanjing University

  32. 摘要 • More about Pattern • Framework: why, what, how • Comparison Institute of Computer Software Nanjing University

  33. Forms of design-level reuse • Sharing of consistency: programming and scripting language • Sharing concrete solution fragments: libraries • Sharing contracts: interfaces • Sharing individual architecture: patterns • Sharing subsystem architectures: frameworks • Sharing overall structure: system architectures. Institute of Computer Software Nanjing University

  34. 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 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 contain some key distinguishing features that separate them from normal libraries. Institute of Computer Software Nanjing University

  35. Framework • 框架,其实就是某种应用的半成品,就是一组组件,供你选用完成你自己的系统。简单说就是使用别人搭好的舞台,你来做表演。而且,框架一般是成熟的,不断升级的软件。 • 框架一般处在低层应用平台(如J2EE)和高层业务逻辑之间的中间层。 Institute of Computer Software Nanjing University

  36. Framework 应用程序 专业领域框架 应用框架 一般性应用框架 支持性框架 作业系统 Institute of Computer Software Nanjing University

  37. Framework • First commercial application framework: MacApp by Apple Computer for Macintosh • Well-known frameworks • MFC • Struts, Spring, Avalon, PicoContainer • Hibernate • Jdon • … Institute of Computer Software Nanjing University

  38. Framework 特点 • Inversion of control - The overall program's 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 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 its code. Institute of Computer Software Nanjing University

  39. How to design a good framework? • 领域驱动设计 (Domain Driven Design) • 控制反转IoC (Inversion of Control) or 依赖注入DI (Dependence Injection) • AOP (Aspect Oriented Programming) • …… Institute of Computer Software Nanjing University

  40. Domain Driven 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. Institute of Computer Software Nanjing University

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

  42. 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 domains. Institute of Computer Software Nanjing University

  43. 大师说DDD Institute of Computer Software Nanjing University

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

  45. DDD特点 • 统一语言:一个无处不在(ubiquitous )的语言,项目中所有人统一交流的语言。减少沟通疑惑,减少传达走样。使得软件更加适合需求。 • 统一领域模型:领域专家和程序员统一使用一种模型,没有数据库数据表等专业软件技术干扰。 • 专门的业务领域层:领域层除了业务没有其他,没有软件架构 框架等等底层技术。

  46. 以模型为核心的卫星图 Persistence Domain Service Domain Model GUI Logging

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

  48. 坏设计之二:基于数据表的设计 • 最初订单有OrderItemId, OrderId, ProductId 和 Qty。 • 后来添加 MinDeliveryQty 和 PendingQty字段,这是和订单交货有关。 • 订单和订单交货是两个概念,但是我们把这些字段都混合在一个类中了。 • 尽管采取了OO设计,但这还是一种带有数据库设计影子的坏设计。 • DDD设计:将实体的职责分离到不同限定场景 。

  49. 领域设计和数据库设计不同 • 领域不是把实体看成铁板一块,一开始就把它分解到各种场景。 • 下订单和订单交货交付是两个场景,它们应该有彼此独立的接口,由实体来实现。 • 在数据库中它们是一个,也就是说,从ER模型上看,它们是一个整体,但是从domain model领域模型角度看,它们是分离的。

  50. DDD advantage • 左图:跨层混乱 ; 右图:以Model为线索有条理 Institute of Computer Software Nanjing University

More Related