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

Patterns of Action

阅读更多
Action 模式

写了很长时间的Swing 程序,总结了一些Action 使用方面的模式与大家共享一下。

1、Sensitive Action Pattern

写过Swing 程序的人都知道,根据用户选择的对象来控制某些Action 的状态(Enable/Disable)是比较繁琐的一件事情,通常我们需要监听控件的选择事件,然后再获取到Action的实例,再根据不同的条件来改变它的状态,代码中到处充满了if...else...这些过程性的代码,而且有时为了获取到Action 的实例,还要做很多特殊的处理。

Sensitive Action 模式是我在编写代码时想出的,主要的目的是让代码变得更加简洁,尽量避免重复的代码。接口层的结构图如下:

接口图

上图中各成员的说明如下:

SensitiveAction:该接口定义了selectionChanged 方法,当用户选择对象发生变化时,可以通过该方法通知本Action,

InstanceProvider:该接口定义了一些方法用户获取用户选中的实例。

除了上面定义的接口以外,还有一些帮助类如下:

这些ActionHelper 类主要负责监听事件,然后通知所有被加入的SensitiveAction。

具体应用时的代码如下:

java 代码
 
  1. public void attachAction(JTable table) {
  2. JTableActionHelper helper = new JTableActionHelper(table);
  3. ConcreteAction action1 = new ConcreteAction();
  4. helper.addSensitveAction(action1);
  5. .....//Create popup menu and add it to the JTable
  6. }

2、Alternative Action Pattern

该模式是基于上面的SensitiveAction 模式的,我在开发的经常会遇到一些互斥的Action,例如:Lock/Unlock,Activate/Deactivate 等等。这些Action 同时只能出现一个,AlternativeAction 的代码实现如下:

java 代码
 
  1. public class AlternativeAction extends AbstractAction implements SensitiveAction{
  2. public AlternativeAction(SensitiveAction action1,SensitiveAction actions2) {
  3. this.action1 = action1;
  4. this.action2 = action2;
  5. }
  6. public void actionPerformed(ActionEvent event) {
  7. if(action1.isEnable())
  8. action1.actionPerformed(event);
  9. else if(action2.isEnable())
  10. action2.actionPerformed(event);
  11. }
  12. public void selectionChanged(InstanceProvider provider) {
  13. action1.selectionChanged(provider);
  14. action2.selectionChanged(provider);
  15. if(action1.isEnabled())
  16. putValue(NAME,action1.getValue(NAME);
  17. else if(action2.isEnable())
  18. putValue(NAME, action2.getValue(NAME));
  19. )
  20. }

3、Safe Action

在Action 的actionPerformed 方法中经常会出现一些 RuntimeException,这些异常如果不被捕捉,就会出现用户点击了一个菜单或按钮后没有任何反应;有时候,我们也需要在actionPerformed 方法中处理一些业务相关的Exception,例如:用户去删除一个用户组下有用户的用户组时,系统应该弹出相应的警告对话框。像这类的问题如果让每个Action 自己去处理会出现很多重复的代码,应该只需要一个类去处理这类问题,然后所有的Action 从该中继承就可以了,代码如下:

java 代码
 
  1. public abstract class SafeAction extends AbstractAction{
  2. private Container parent;
  3. public SafeAction(Container parent) {
  4. this.parent = parent;
  5. }
  6. public void actionPerformed(ActionEvent e) {
  7. try {
  8. performed(e);
  9. } catch (Throwable e1) {
  10. //Show error dialog;
  11. }
  12. }
  13. public abstract void performed(ActionEvent e)throws Throwable;
  14. }

该类定义了一个模板方法performed 方法,子类应该在该方法中编写Action 的执行代码。

  • 描述: SensitiveAction接口图
  • 大小: 24.8 KB
  • 大小: 43.4 KB
分享到:
评论
8 楼 icedcoco 2007-01-05  
写的还是很好的,对于设计模式而言,有自己的理解,这是最重要的。
其实action模式在web应用中是最基本的东西,目前来说是放置四海而皆准的东西了。很多的处理都用action来驱动,去触发相应的操作,非常的方便易懂。
7 楼 ahuaxuan 2006-12-17  
我们公司做swing的真的也没有几个,去51job上,招java的大多是做web的
6 楼 icefire 2006-12-04  
总感觉用java开发桌面程序不是太爽!
不过计划帮同学写个小程序,打算用用Swing,正学习!
同时打算用db4o做数据库!

效率和部署,这是让人不想用java的原因!
5 楼 wangyonghe 2006-12-04  
今天补充了一个SafeAction 模式,在以后的时间里我会补充更多有关Action 的模式,
其实,模式本身并不是针对某个语言或者某个平台的,我使用Swing 来描述Action 相关的模式只是一种描述的手段而已,在Web 中这些模式同样可以适用。只不过我已经有好几年没做Web 了,都忘得差不多了,所以还是使用我最擅长的方式的描述更加准确一点。
4 楼 wangyonghe 2006-12-03  
如果学习设计模式,Swing 还是挺不错的范例呀???
3 楼 lighter 2006-12-02  
wangyonghe 写道
不会吧,Swing 那么经典,怎么没人学呀?

那你觉得用java开发web的多,还是swing的多啊?
是有人学swing的;但一般的都用java开发web,去搞swing相对来说比较少的.
btw:文章写不错...
2 楼 wangyonghe 2006-12-02  
不会吧,Swing 那么经典,怎么没人学呀?
1 楼 wangyonghe 2006-12-02  
怎么没人关注呀?

相关推荐

    Agile Principles Patterns and Practices in C#

    The introductory chapters lay out the basics of the agile movement, while the later chapters show proven techniques in action. The book includes many source code examples that are also available for ...

    Selenium Design Patterns and Best Practices 最新 原版

    Stabilize your tests by using patterns such as the Action Wrapper and Black Hole Proxy patterns In Detail Selenium WebDriver is a global leader in automated web testing. It empowers users to ...

    Selenium Design Patterns and Best Practices(PACKT,2014)

    Selenium WebDriver is a global leader in automated web testing. It empowers users to perform complex ...Stabilize your tests by using patterns such as the Action Wrapper and Black Hole Proxy patterns

    Manning Microservices in action

    Microservices in Action is a practical book about building and deploying microservice-based applications. Written for developers and architects with a solid grasp of service-oriented development, it ...

    Camel in action(camel实战)

    Camel in Action is a Camel tutorial full of small examples showing how to work with the integration patterns. It starts with core concepts like sending, receiving, routing, and transforming data. It ...

    Pro Objective-C Design Patterns for iOS

    The purpose of this book is to show you how to put design patterns into action in iOS application development. I will focus on applicability of various design patterns with the Cocoa Touch framework ...

    spring microservices in action

    Spring Microservices in Action was written for the practicing Java/Spring ...patterns discussed in almost every chapter, along with examples of the patterns implemented using Spring Boot and Spring

    Entity Framework Core in Action

    Entity Framework Core in Action teaches you how to access and update relational data from .NET applications. Following the crystal-clear explanations, real-world examples, and around 100 diagrams, you...

    Spring Microservices in Action.pdf

    Spring Microservices in Action teaches you how to build microservice-based applications using Java and the Spring platform. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub ...

    Hadoop in Action

    Hadoop in Action is for programmers, architects, and project managers who have to process large amounts of data offline. The book begins with several simple examples that illustrate the basic idea ...

    Java Reflection in Action

    Java Reflection in Action is unique in presenting a clear account of all the cool things you can do with reflection, and at the same time pro- viding the sound conceptual basis that developers need to...

    [Camel实战].(Camel.in.Action).Claus.Ibsen&Jonathan;.Anstey.文字版

    Camel in Action is a Camel tutorial full of small examples showing how to work with the integration patterns. It starts with core concepts like sending, receiving, routing, and transforming data. It ...

    spring.microservices.in.action

    Spring Microservices in Action consists of 10 chapters and two appendixes: Chapter 1 introduces you to why the microservices architecture is an important and relevant approach to building ...

    Entity Framework Core in Action-原版

    Summary Entity Framework Core in Action teaches you how to access and update relational data from .NET applications. Following the crystal-clear explanations, real-world examples, and around 100 ...

    Amazon Web Services in Action(Manning,2015)

    Overview of cloud concepts and patterns Deploy applications on AWS Integrate Amazon's pre-built services Manage servers on EC2 for cost-effectiveness About the Reader Written for developers and ...

    Microservices in Action

    Master a few important new patterns and processes, and you’ll be ready to develop, deploy, and run production-quality microservices. Microservices in Action teaches you how to write and maintain ...

    Oculus.Rift.in.Action.161729

    you can deliver powerful immersive games, simulations, and other virtual experiences that finally nail the feeling of being in the middle of the action. Oculus Rift in Action teaches you how to ...

    PHP in Action.pdf

    This book’s purpose involves a kind of bigamy. It introduces state-of-the art objectoriented design principles, patterns, and techniques. Then it weds these to two different partners. The first ...

Global site tag (gtag.js) - Google Analytics