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 代码
- public interface ComponentFactory {
-
- public JLabel createJLabel(String labelText);
-
- public JDialog createJDialog(JComponent content);
-
- }
2) ImageResourceFactory 接口代码
java 代码
- public interface ImageResourceFactory {
-
- public Image getImage(String imageId);
-
- public Icon getIcon(String iconId);
- }
3) PresentationContext 接口代码
java 代码
- public interface PresentationContext {
-
- public ResourceBundle getResourceBundle();
-
- public ComponentFactory getComponentFactory();
-
- public ImageResourceFactory getImageResourceFactory();
-
- public void setProperty(String name, Object value);
-
- public Object getProperty(String name);
- }
4) SampleView 代码
java 代码
- public class SampleView extends JPanel {
-
- private PresentationContext presentationContext;
-
- private ComponentFactory componentFactory;
-
- public SampleView(PresentationContext presentationContext) {
- this.presentationContext = presentationContext;
- componentFactory = presentationContext.getComponentFactory();
- constructUI();
- }
-
- protected void constructUI() {
- ResourceBundle resourceBundle = presentationContext.getResourceBundle();
- ImageResourceFactory imageResourceFactory = presentationContext
- .getImageResourceFactory();
-
-
-
- JLabel label = componentFactory.createJLabel(resourceBundle
- .getString("label.text"));
- label.setIcon(imageResourceFactory.getIcon("label.icon"));
- }
-
- public void showAsDialog() {
- JDialog dialog = componentFactory.createJDialog(this);
- dialog.pack();
- dialog.setVisible(true);
- }
- }
说明:
1) SampleView 中的文本资源和图片资源都是外部进行加载的,一般应用时可以由平台提供代码实现,然后各模块自己注入相应的路径。
2) SampleView 中的dialog 都是由平台提供的ComponentFactory 进行创建的,实现时平台可以在创建时,默认注册Esc 键的Listener。
6. 注意事项
1.Presentation Context 可以包含更多与特定客户端平台相关的内容,在应用时可以根据具体情况使用,但是要注意它里面不应该包含和领域逻辑相关的任何内容。
2.一个模块应该只有一个Presentation Context 实例,最好由平台创建或者有模块的Facade 负责创建。
- 大小: 14.9 KB
分享到:
相关推荐
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 ...
1.简述:首先从它的继承关系上来看Presentation是继承自Dialog的,就是说它其实就是一种特殊的Dialog用于在第二个屏幕上显示内容的,它在创建时会和它的目标展示屏幕相关联,包括它的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,Presentation about Context in Go 1.7. Review, examples, thoughts.
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 ...
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 ...
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, ...
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
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" ...
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 替换 ...
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 ...
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 ...
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.
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 ...
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 ...
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 ...
" @context " : " http://iiif.io/api/presentation/2/context.json " , " @id " : " https://raw.githubusercontent.com/altomator/IIIF/main/collection/cb343833568.json " , " @type " : " sc:Collection " ,...
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 ...
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 ...