Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1698095
  • 博文数量: 210
  • 博客积分: 10013
  • 博客等级: 上将
  • 技术积分: 2322
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-25 15:56
文章分类

全部博文(210)

文章存档

2011年(34)

2010年(121)

2009年(37)

2008年(18)

我的朋友

分类: Java

2011-02-28 17:48:15

command.java

package com.utstar.pattern.command;

public interface Command{
    void excute();
    void setMainBoard(MainBoard mainBoard);
}


OpenCommand.java

 

package com.utstar.pattern.command;

public class OpenCommand implements Command{
    public MainBoard myMainBoard = null;
    
    public OpenCommand(MainBoard mainboard){
        this.myMainBoard = mainboard;
    }
    public void setMainBoard(MainBoard my){
        this.myMainBoard = my;
    }
    public void excute(){
        this.myMainBoard.open();
    }
}


ResetCommand.java

 

package com.utstar.pattern.command;

public class ResetCommand implements Command{
    private MainBoard myMainBoard = null;
    
    public void setMainBoard(MainBoard my){
        this.myMainBoard = my;
    }
    public void excute(){
        this.myMainBoard.Reset();
    }
}


MainBorad.java

 

package com.utstar.pattern.command;

public interface MainBoard{
    void open();
    void Reset();
}


GigaMainBoard.java

 

package com.utstar.pattern.command;

public class GigaMainBoard implements MainBoard{
    public void open(){
        System.out.println("技嘉主板启动中……");
        System.out.println("主板加电……");
        System.out.println("主板检测……");
        System.out.println("主板加载……");
        System.out.println("欢迎使用技嘉主板……");
    }
    public void Reset(){
        System.out.println("重启中……");
    }
}


MsiMainBoard.java

 

package com.utstar.pattern.command;

public class MsiMainBoard implements MainBoard{
    public void open(){
        System.out.println("微星主板启动中……");
        System.out.println("主板加电……");
        System.out.println("主板检测……");
        System.out.println("主板加载……");
        System.out.println("欢迎使用微星主板……");
    }
    public void Reset(){
        System.out.println("重启中……");
    }
}


Box.java

 

package com.utstar.pattern.command;

public class Box{
    public Command command1 = null;
    public Box(Command com){
        this.command1 = com;
    }
    public void pressButton(){
        this.command1.excute();
    }
}


Testcommand.java

 

package com.utstar.pattern.command;

public class TestCommandPattern{
    public static void main(String args[]){
        MainBoard myMainBoard = new GigaMainBoard();
        Command myCommand = new OpenCommand(myMainBoard);
        Box myBox = new Box(myCommand);
        myBox.pressButton();
        
        myMainBoard = new MsiMainBoard();
        myCommand.setMainBoard(myMainBoard);
        myBox.pressButton();
    }
}


阅读(1634) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-05 16:43:05

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com