package Patern; public class CommandPattern { public static void main(String [] args){ int a = 10; if(0 <= a && a < 3){ System.out.println(a + "is 0 - 2"); }else if(3 <= a && a < 7){ System.out.println(a + "is 3 - 6"); }else{ System.out.println(a +" is larger than 6"); } /** * Following is a example of using command pattern to extract if-else expressions * into the class. */ ICompare[] compareEvents = new ICompare[]{new LessEvent(), new BetweenEvent() ,new LargeEvent()}; for(int i = 0 ; i < compareEvents.length ; i++){ compareEvents[i].print(a); } } } interface ICompare{ public void print(int number); } class LessEvent implements ICompare{ @Override public void print(int a) { if(0 <= a && a < 3){ System.out.println(a + "is 0 - 2"); } } } class BetweenEvent implements ICompare{ @Override public void print(int a) { if(3 <= a && a < 7){ System.out.println(a + "is 3 - 6"); } } } class LargeEvent implements ICompare{ @Override public void print(int number) { if(number > 6){ System.out.println(number +" is larger than 6"); } } }
網頁
BloggerAds 廣告
標籤
- Java (96)
- Android (27)
- 演算法 (21)
- c++ (19)
- JavaScript (7)
- OpenMp (6)
- Design Pattern (4)
- 日文歌曲 (4)
- 資料結構 (4)
- Foundation Knowledge Of Programming (3)
- QUT (2)
- CodingHomeWork (1)
- Database (1)
- 英文歌詞 (1)
搜尋此網誌
2012年12月30日 星期日
Command design pattern (命令設計模式)
訂閱:
文章 (Atom)
我的網誌清單
標籤
日文歌曲
(4)
股市
(7)
股票
(9)
英文歌詞
(1)
時事
(1)
硬體(hardware)
(1)
資料結構
(4)
演算法
(21)
數學(Math)
(4)
ACM
(3)
ajax
(7)
algorithms
(1)
Android
(27)
Blog Notes(部落格記事)
(6)
C
(9)
c++
(19)
CodingHomeWork
(1)
Database
(1)
Design Pattern
(4)
Foundation Knowledge Of Programming
(3)
GWT
(1)
How
(2)
J2EE
(1)
Java
(96)
Java語言
(4)
JavaScript
(7)
Leetcode
(4)
LOL
(1)
OpenMp
(6)
QUT
(2)
Uva
(2)
Yahoo知識問答
(11)