1 java设计模式中有一个模式:命令模式。该模式是通过一个公共命令类来统一控制多个命令的。这个很像门面模式。
2 命令模式。是通过一个命令类来统一控制不同的命令方法。和门面模式很类似(区别:门面模式是控制不同的多个子系统)。
3 代码如下:
public class Receive {
public Receive() {
super();
// TODO Auto-generated constructor stub
}
public void action1(){
System.out.println("执行命令1");
}
public void action2(){
System.out.println("执行命令2");
}
}
public interface Commond {
public void action1();
public void action2();
}
public class JutiCommond implements Commond{
private Receive receive=null;
public JutiCommond() {
super();
// TODO Auto-generated constructor stub
}
public JutiCommond(Receive receive) {
super();
this.receive = receive;
}
public void action1() {
// TODO Auto-generated method stub
this.receive.action1();
}
public void action2() {
// TODO Auto-generated method stub
this.receive.action2();
}
}
public class CommondUtils {
private Commond commond=null;
public CommondUtils() {
super();
// TODO Auto-generated constructor stub
}
public CommondUtils(Commond commond) {
super();
this.commond = commond;
}
public void action1(){
this.commond.action1();
}
public void action2(){
this.commond.action2();
}
}
public static void main(String[] args) {
Receive receive=new Receive();
Commond commond=new JutiCommond(receive);
CommondUtils commondUtils=new CommondUtils(commond);
commondUtils.action1();
commondUtils.action2();
}
综述:命令模式(关键字:命令,一个命令控制类)
2 命令模式。是通过一个命令类来统一控制不同的命令方法。和门面模式很类似(区别:门面模式是控制不同的多个子系统)。
3 代码如下:
public class Receive {
public Receive() {
super();
// TODO Auto-generated constructor stub
}
public void action1(){
System.out.println("执行命令1");
}
public void action2(){
System.out.println("执行命令2");
}
}
public interface Commond {
public void action1();
public void action2();
}
public class JutiCommond implements Commond{
private Receive receive=null;
public JutiCommond() {
super();
// TODO Auto-generated constructor stub
}
public JutiCommond(Receive receive) {
super();
this.receive = receive;
}
public void action1() {
// TODO Auto-generated method stub
this.receive.action1();
}
public void action2() {
// TODO Auto-generated method stub
this.receive.action2();
}
}
public class CommondUtils {
private Commond commond=null;
public CommondUtils() {
super();
// TODO Auto-generated constructor stub
}
public CommondUtils(Commond commond) {
super();
this.commond = commond;
}
public void action1(){
this.commond.action1();
}
public void action2(){
this.commond.action2();
}
}
public static void main(String[] args) {
Receive receive=new Receive();
Commond commond=new JutiCommond(receive);
CommondUtils commondUtils=new CommondUtils(commond);
commondUtils.action1();
commondUtils.action2();
}
综述:命令模式(关键字:命令,一个命令控制类)