Action 模式
写了很长时间的Swing 程序,总结了一些Action 使用方面的模式与大家共享一下。
1、Sensitive Action Pattern
写过Swing 程序的人都知道,根据用户选择的对象来控制某些Action 的状态(Enable/Disable)是比较繁琐的一件事情,通常我们需要监听控件的选择事件,然后再获取到Action的实例,再根据不同的条件来改变它的状态,代码中到处充满了if...else...这些过程性的代码,而且有时为了获取到Action 的实例,还要做很多特殊的处理。
Sensitive Action 模式是我在编写代码时想出的,主要的目的是让代码变得更加简洁,尽量避免重复的代码。接口层的结构图如下:
接口图
上图中各成员的说明如下:
SensitiveAction:该接口定义了selectionChanged 方法,当用户选择对象发生变化时,可以通过该方法通知本Action,
InstanceProvider:该接口定义了一些方法用户获取用户选中的实例。
除了上面定义的接口以外,还有一些帮助类如下:
这些ActionHelper 类主要负责监听事件,然后通知所有被加入的SensitiveAction。
具体应用时的代码如下:
java 代码
- public void attachAction(JTable table) {
- JTableActionHelper helper = new JTableActionHelper(table);
- ConcreteAction action1 = new ConcreteAction();
- helper.addSensitveAction(action1);
- .....
- }
2、Alternative Action Pattern
该模式是基于上面的SensitiveAction 模式的,我在开发的经常会遇到一些互斥的Action,例如:Lock/Unlock,Activate/Deactivate 等等。这些Action 同时只能出现一个,AlternativeAction 的代码实现如下:
java 代码
- public class AlternativeAction extends AbstractAction implements SensitiveAction{
-
- public AlternativeAction(SensitiveAction action1,SensitiveAction actions2) {
- this.action1 = action1;
- this.action2 = action2;
- }
-
- public void actionPerformed(ActionEvent event) {
- if(action1.isEnable())
- action1.actionPerformed(event);
- else if(action2.isEnable())
- action2.actionPerformed(event);
-
- }
-
- public void selectionChanged(InstanceProvider provider) {
- action1.selectionChanged(provider);
- action2.selectionChanged(provider);
- if(action1.isEnabled())
- putValue(NAME,action1.getValue(NAME);
- else if(action2.isEnable())
- putValue(NAME, action2.getValue(NAME));
- )
- }
3、Safe Action
在Action 的actionPerformed 方法中经常会出现一些 RuntimeException,这些异常如果不被捕捉,就会出现用户点击了一个菜单或按钮后没有任何反应;有时候,我们也需要在actionPerformed 方法中处理一些业务相关的Exception,例如:用户去删除一个用户组下有用户的用户组时,系统应该弹出相应的警告对话框。像这类的问题如果让每个Action 自己去处理会出现很多重复的代码,应该只需要一个类去处理这类问题,然后所有的Action 从该中继承就可以了,代码如下:
java 代码
- public abstract class SafeAction extends AbstractAction{
-
- private Container parent;
-
- public SafeAction(Container parent) {
- this.parent = parent;
- }
-
- public void actionPerformed(ActionEvent e) {
- try {
- performed(e);
- } catch (Throwable e1) {
-
- }
- }
-
- public abstract void performed(ActionEvent e)throws Throwable;
-
- }
该类定义了一个模板方法performed 方法,子类应该在该方法中编写Action 的执行代码。
- 描述: SensitiveAction接口图
- 大小: 24.8 KB
- 大小: 43.4 KB
分享到:
相关推荐
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
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 ...
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 ...
Cocoa设计模式源自经典的软件设计模式,如GOF(GoF,Gang of Four)的设计模式,但更专注于Apple生态系统,特别是iOS和macOS。这些模式是解决特定问题或实现特定功能的经过验证的最佳实践,它们使开发者能够利用...
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...
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 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...
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 ...
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 ...
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 ...
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 ...
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 providing the sound conceptual basis that developers need to ...
Machine Learning in Action is unique book that blends the foundational theories of machine learning with the practical realities of building tools for everyday data analysis. You'll use the flexible ...
Mule in Action, Second Edition is a totally-revised guide covering Mule 3 fundamentals and best practices. It starts with a quick ESB overview and then dives into rich examples covering core concepts ...