JiveJdon Community Forums
在线84人 J道首页 | 论坛首页 | 培训咨询 | 开源框架 | 精华 | 查搜 | 注册 | 登陆 |
首页 » 论坛 » 设计模式、框架和架构
???en_US.forumThreadPrev.name??? 上一主题
Go back to the topic listing   返回主题列表
???en_US.forumThreadNext.name??? 下一主题
这个主题共有 6 回复 / 1 页 [ ]  发表新帖子  回复该主题贴
hxz

发表文章: 89
注册时间: 2002年12月03日 10:16
这个用什么模式 发表: 2002年12月07日 09:21 回复
是这样的:
通过按几个按钮中的一个会在另一个区块出现几个按钮,出现的这几个按钮又负责不同的逻辑!
我用swing处理这个界面!

hxz

发表文章: 89
注册时间: 2002年12月03日 10:16
Re: 这个用什么模式 发表: 2002年12月07日 09:40 回复
hxzEF02wMy0K3.jpg
可不可以这样子
hxz

发表文章: 89
注册时间: 2002年12月03日 10:16
Re: 这个用什么模式 发表: 2002年12月07日 09:46 回复
hxzL7124N0806.jpg
这个样子的界面
banq

发表文章: 8929
注册时间: 2002年08月03日 17:08
Re: 这个用什么模式 发表: 2002年12月07日 13:02 回复
当然使用Command模式。

你看看几个英文资料 command主要在你这个案例中体现威力的。
hxz

发表文章: 89
注册时间: 2002年12月03日 10:16
Re: 这个用什么模式 发表: 2002年12月09日 09:22 回复
多谢banq!我先去看看
hxz

发表文章: 89
注册时间: 2002年12月03日 10:16
Re: 这个用什么模式 发表: 2002年12月10日 16:53 回复
命令模式:
命令模式,我的理解就是把一个命令封装成一个类,看例子把。

***************************************
开始
缺点:
if语句繁多,代码太长,不容易维护
***************************************

public class CommandDemo{
public static void main(String args[]){
String commandName = args[0];
process(commandName);
}


void process(String commandName){
if(commandName.equals("a")){
//do a1
//do a2
//do a3
}
if(commandName.equals("b")){
//do b1
//do b2
//do b3
//do b4
//do b5
}
if(commandName.equals("c")){
//do c1
//do c2
//do c3
//do c4
//do c5
//do c6
//do c7
//do c8
//do c9
}
}
}

***************************************
改进一:
改进后的优点:
每个if分支交给了一个command类处理,清晰多了
改进后仍然的缺点:
还是一堆的if语句,看着就烦
***************************************
public class CommandDemo{

public static void main(String args[]){
String commandName = args[0];
if(commandName.equals("a")){
new CommandA().execute();
}
if(commandName.equals("b")){
new CommandB().execute();
}
if(commandName.equals("c")){
new CommandC().execute();
}
}

}

//定义Command接口
inteface Command{
void execute();
}

class CommandA implements Command{
void execute(){
//do a1
//do a2
//do a3
}
}
class CommandB implements Command{
void execute(){
//do b1
//do b2
//do b3
//do b4
//do b5

}
}
class CommandC implements Command{
void execute(){
//do c1
//do c2
//do c3
//do c4
//do c5
//do c6
//do c7
//do c8
//do c9
}
}


***************************************
改进二:
改进后的优点:
if分支少勒!!!清晰多了
改进后仍然的缺点:
还需要注册,恩,有点烦
***************************************
public class CommandDemo{
static Hashtable hashTable = new Hashtable();
static registCommand(String commandName,Commmand command){
if(hashTable.get(commandName)==null){
hashTable.add(commandName,command);
}
}
static Command findCommand(String commandName){
return (Command)hashTable.get(commandName);
}

public static void main(String args[]){
//注册
registCommand("a",new CommandA());
registCommand("b",new CommandB());
registCommand("c",new CommandC());


String commandName = args[0];
Command command = findCommand(commandName);
command.execute();
}
}

//定义Command接口
inteface Command{
void execute();
}

class CommandA implements Command{
void execute(){
//do a1
//do a2
//do a3
}
}
class CommandB implements Command{
void execute(){
//do b1
//do b2
//do b3
//do b4
//do b5

}
}
class CommandC implements Command{
void execute(){
//do c1
//do c2
//do c3
//do c4
//do c5
//do c6
//do c7
//do c8
//do c9
}
}

***************************************
改进三:
改进后的优点:
// registCommand("a",new CommandA());
// registCommand("b",new CommandB());
// registCommand("c",new CommandC());
这些东西不用写了

改进后仍然的缺点:
嗯,差不多了,再改就有点变态了
***************************************

XML外部配置文件:
<commands>
<command name="a" class="CommandA"/>
<command name="b" class="CommandB"/>
<command name="c" class="CommandC"/>
</commands>


public class CommandDemo{
static Hashtable hashTable = new Hashtable();
static loadXMLConfig(){
//load external XML file
//动态加载所有的CommandX类
//放到hashTable中
}
static Command findCommand(String commandName){
return (Command)hashTable.get(commandName);
}

public static void main(String args[]){
//注册
// registCommand("a",new CommandA());
// registCommand("b",new CommandB());
// registCommand("c",new CommandC());


String commandName = args[0];
Command command = findCommand(commandName);
command.execute();
}

void registerCommand(Command c){
v.add(c);
}
}

//定义Command接口
inteface Command{
void execute();
}

class CommandA implements Command{
void execute(){
//do a1
//do a2
//do a3
}
}
class CommandB implements Command{
void execute(){
//do b1
//do b2
//do b3
//do b4
//do b5

}
}
class CommandC implements Command{
void execute(){
//do c1
//do c2
//do c3
//do c4
//do c5
//do c6
//do c7
//do c8
//do c9
}
}

//从cn_java上找来的!
hxz

发表文章: 89
注册时间: 2002年12月03日 10:16
Re: 这个用什么模式 发表: 2002年12月10日 16:54 回复
不知可不可以用工厂方法
这个主题有 6 回复 / 1 页 [ ]
???en_US.forumThreadPrev.name??? 上一主题
Go back to the topic listing   返回主题列表    返回页首  返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Spring Struts
查询本论坛内 回复超过的热门帖子
快速发表回复
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
 

解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-07 jdon.com

anti spam