`
wangyonghe
  • 浏览: 42257 次
社区版块
存档分类
最新评论

Presentation Context

阅读更多

1. 目的

使界面代码承担的职责更少,便于代码的维护,提高代码可读性。

2. 动机

我们在编写客户端代码的时候,经常会用到字符资源、图片资源以及多个View 间共享实例,这些职责如果由View 自己负责,一方面,降低了代码的重用性(资源加载的代码),另一方面,也使View 承担了更多的职责,不利用View 的维护。如果这部分职责由外部实现,View 又如何访问这些资源。这就是Presentation Context 的模式所要解决的问题。

3.参与者

1. Presentation Context:它封装了View 在创建时所需要的上下文信息,并且可以作为多个View 之间共享对象的容器。

2. Component Factory:它负责创建Swing 控制和平台自定义的控制。使用Componet Factory 的好处是可以统一的控制系统内部的控件,例如:通过Component Factory 创建的对话框都可以点击Esc 键关闭,点击F1 出现Help 对话模式。

4. 特点

1. 优点:(1)使用Presentation Context 可是分解View 的职责,使View 的代码更容易维护。 (2) 使加载资源的代码可以被重用。(3)使多个View 间可以方便的共享对象。(4)使View 更容易访问客户端平台开放的接口。

5. 实现

1) ComponentFactory 接口代码

java 代码
 
  1. public interface ComponentFactory {
  2. public JLabel createJLabel(String labelText);
  3. public JDialog createJDialog(JComponent content);
  4. // ...
  5. }

2) ImageResourceFactory 接口代码

java 代码
 
  1. public interface ImageResourceFactory {
  2. public Image getImage(String imageId);
  3. public Icon getIcon(String iconId);
  4. }

3) PresentationContext 接口代码

java 代码
 
  1. public interface PresentationContext {
  2. public ResourceBundle getResourceBundle();
  3. public ComponentFactory getComponentFactory();
  4. public ImageResourceFactory getImageResourceFactory();
  5. public void setProperty(String name, Object value);
  6. public Object getProperty(String name);
  7. }

4) SampleView 代码

java 代码
 
  1. public class SampleView extends JPanel {
  2. private PresentationContext presentationContext;
  3. private ComponentFactory componentFactory;
  4. public SampleView(PresentationContext presentationContext) {
  5. this.presentationContext = presentationContext;
  6. componentFactory = presentationContext.getComponentFactory();
  7. constructUI();
  8. }
  9. protected void constructUI() {
  10. ResourceBundle resourceBundle = presentationContext.getResourceBundle();
  11. ImageResourceFactory imageResourceFactory = presentationContext
  12. .getImageResourceFactory();
  13. // Create a label with component factory that is injected by
  14. // presentation context.
  15. JLabel label = componentFactory.createJLabel(resourceBundle
  16. .getString("label.text"));
  17. label.setIcon(imageResourceFactory.getIcon("label.icon"));
  18. }
  19. public void showAsDialog() {
  20. JDialog dialog = componentFactory.createJDialog(this);
  21. dialog.pack();
  22. dialog.setVisible(true);
  23. }
  24. }

说明:

1) SampleView 中的文本资源和图片资源都是外部进行加载的,一般应用时可以由平台提供代码实现,然后各模块自己注入相应的路径。

2) SampleView 中的dialog 都是由平台提供的ComponentFactory 进行创建的,实现时平台可以在创建时,默认注册Esc 键的Listener。

6. 注意事项

1.Presentation Context 可以包含更多与特定客户端平台相关的内容,在应用时可以根据具体情况使用,但是要注意它里面不应该包含和领域逻辑相关的任何内容。

2.一个模块应该只有一个Presentation Context 实例,最好由平台创建或者有模块的Facade 负责创建。

  • 大小: 14.9 KB
分享到:
评论
8 楼 wangyonghe 2006-12-28  
ComponentFactory 是一个抽象工厂,用于创建平台系列的组件,当我们的模块被加入到不同的平台时组件可以有不同的实现,这样的好处有是:平台的一些特性可以统一被处理,不需要在模块代码中零碎增加处理平台特性的代码。
7 楼 shaucle 2006-12-28  
很不错的架构.

Context就像是在Model和View之间再加了一层.
只是像ComponentFactory这种会不会显得很庸肿?
6 楼 wangyonghe 2006-12-27  
有人关心吗?这个模式我已经在我的项目中应该了多次,
如果大家不能够看懂模式,可以告诉我,我再修改一下。
5 楼 wangyonghe 2006-12-27  
代码示例补充完毕。
4 楼 wangyonghe 2006-12-27  
shaucle 写道
给个具体的例子吧...
我会尽快补充一个代码示例。
3 楼 shaucle 2006-12-27  
给个具体的例子吧...
2 楼 wangyonghe 2006-12-26  
怎么没人回应呀?这种模式是好还是坏,还是根本就不值得一提呀?
1 楼 wangyonghe 2006-12-26  
没有人参与讨论吗?

相关推荐

    Windows Presentation Foundation 4.5 Cookbook的源码

    Windows Presentation Foundation 4.5 Cookbook的源码 Chapter 1: Foundations 7 Introduction 7 Creating custom type instances in XAML 9 Creating a dependency property 15 Using an attached property 25 ...

    Android Presentation实现双屏异显

    1.简述:首先从它的继承关系上来看Presentation是继承自Dialog的,就是说它其实就是一种特殊的Dialog用于在第二个屏幕上显示内容的,它在创建时会和它的目标展示屏幕相关联,包括它的context和一些配置参数等。...

    Applied WPF 4 in Context

    Windows Presentation Foundation (WPF) is a graphical computer platform built and distributed by Microsoft to create rich client applications for Windows systems. With WPF, you can build rich ...

    开源项目-dkondratovych-golang-ua-meetup.zip

    开源项目-dkondratovych-golang-ua-meetup.zip,Presentation about Context in Go 1.7. Review, examples, thoughts.

    seam_reference.pdf

    There is no distinction between presentation tier components and business logic components in Seam. You can layer your application according to whatever architecture you devise, rather than being ...

    Understanding LTE and its Performance

    The presentation starts from basic principles, and proceeds to the most advanced topics. Provided schemes are developed and oriented in the context of actual closed standards of the IEEE working ...

    Lectures on Categorical Data Analysis-Springer(2018)

    This book offers a relatively self-contained presentation of the fundamental results in categorical data analysis, which plays a central role among the statistical techniques applied in the social, ...

    Data Structures And Algorithms With Object-Oriented Design Patterns In C Sharp.pdf

    1 The primary goal of this book is to promote object-oriented design using C# and to illustrate the use of ...presentation fosters intuitive understanding of the concepts rather than mathematical rigor.

    神经网络结构优化算法

    Abstract— Training neural networks is a complex task of great importance in the supervised learning field of research. We intend to show the ...the other algorithms in the context of eLearning

    Java Programming, 7th edition

    The text explains concepts clearly and reinforces the reader-friendly presentation with meaningful real-world exercises. Full programming examples emphasize learning in context. Updated "You Do It" ...

    python运行命令快捷键-Pycharm快捷键python基础教程python入门pyt .pdf

    Ctrl + Shift + Q 全屏 Ctrl + Shift + Alt + Q presentation mode Shift + ESC 关闭除编辑区域的其他区域 Ctrl + Shift + Z Run替换 Ctrl +Shift + F10 Run context configuration Ctrl + Alt + Shift + X 替换 ...

    英文原版-A Practical Guide to Basic Laboratory Andrology 1st Edition

    Each technical section includes pertinent quality control considerations, as well as the optimum presentation of results. In addition to the comprehensive 'basic' semen analysis, incorporating ...

    Retail Credit Risk Modeling and the Basel Capital Accord

    community in the context of developing an internal ratings-based (IRB) approach to capital requirements. While the broader effort encompasses both wholesale and retail lending, Calem's comments and ...

    RequirementsPaper

    The basis of a good system architecture is the availability and understanding of the requirements....the requirements and how to use these requirements in the context of the product creation process.

    Visualizing.Financial.Data.111890785X

    Solve data presentation problems using efficient visualization techniques Use the provided companion website to follow along with examples The companion website gives you the illustration files and ...

    Packt.MVVM.Survival.Guide.for.Enterprise.Architectures.in.Silverlight.And.WPF

    Discover the evolution of presentation patterns-by example-and see the benefits of MVVM in the context of the larger picture of presentation patterns Customize the MVVM pattern for your projects ...

    Echo Quick Start Guide [True PDF]

    The presentation has been streamlined to concentrate on the things you really need to know, rather than everything. What you will learn Key design considerations for high performance Echo ...

    F

    " @context " : " http://iiif.io/api/presentation/2/context.json " , " @id " : " https://raw.githubusercontent.com/altomator/IIIF/main/collection/cb343833568.json " , " @type " : " sc:Collection " ,...

    ppk on Javascript (pdf)

    Context Section A. The CSS revolution Section B. Separation of presentation and structure Section C. Separation of behavior and structure Section D. Separation of behavior and presentation ...

    ASN.1 Communication between Heterogeneous Systems

    Many case studies of real-world applications illustrate this presentation., The text also replaces the language in its historical background and, describes the context in which it is used, both from ...

Global site tag (gtag.js) - Google Analytics