Bark,狗叫声类import java.util.Collection; import java.util.List; public class Bark { private String sound ; private DogDoor dogDoor ; public Bark(DogDoor dogDoor){ this.dogDoor = dogDoor ; } public Bark(String sound) { this.sound = sound ; } public Bark(DogDoor dogDoor,String sound) { this(dogDoor) ; this.sound = sound ; } public boolean equals(Bark bark) { if(bark!=null && bark.sound.equals(this.sound)){ return true ; } return false ; } public String getSound() { return sound; } public void setSound(String sound) { this.sound = sound; } public Bark() {} }
BarkRecognizer,狗叫识别器import java.util.Collection; import java.util.Iterator; public class BarkRecognizer { private DogDoor dogDoor ; public BarkRecognizer(DogDoor dogDoor) { super(); this.dogDoor = dogDoor; } public void recognizer(Bark bark) { Collection barks = dogDoor.getAllowedBarks() ; Iterator it = barks.iterator() ; while(it.hasNext()){ if(bark.equals((Bark)it.next()) ){ dogDoor.open() ; System.out.println("the door opens"); break ; } } System.out.println(bark.getSound() + "is not bruce"); // if(barks != null && barks.size() >=1){ // if(barks.contains(bark)){ // dogDoor.open() ; // } // } } public BarkRecognizer() {} }
狗门import java.util.LinkedList; import java.util.List; import java.util.Timer; import java.util.TimerTask; public class DogDoor { private boolean open ; List barks = new LinkedList(); public void open() { if(!open){ this.open = true ; } final Timer timer = new Timer() ; timer.schedule(new TimerTask(){ public void run() { close() ; timer.cancel() ; } },5000); } public void close() { this.open = false ; System.out.println("the door close"); } public void addAllowedBark(Bark bark) { if(bark!=null){ this.barks.add(bark) ; } } public List getAllowedBarks() { return this.barks ; } public boolean isOpen() { return open ; } }
遥控器 public class Remote { private DogDoor dogDoor ; public Remote(DogDoor dogDoor) { super(); this.dogDoor = dogDoor; } public void pressButton() { dogDoor.open() ; } public Remote() {} ; }
测试类import org.junit.Test; public class TestDogDoor { @Test public void dogDoorTest() { DogDoor door = new DogDoor() ; door.addAllowedBark(new Bark("rowlf")) ; door.addAllowedBark(new Bark("roooowlf")) ; door.addAllowedBark(new Bark("woof")) ; door.addAllowedBark(new Bark("rawlf")) ; BarkRecognizer recognizer = new BarkRecognizer(door) ; Remote remote = new Remote(door); System.out.println("bruce starts barkings") ; recognizer.recognizer(new Bark("rowlf")) ; System.out.println("bruce has gone outside") ; try{ Thread.currentThread().sleep(1000) ; }catch(InterruptedException e){ System.out.println("error") ; } System.out.println("bruce' all done") ; System.out.println("but he's stuck outside!") ; Bark smallDogBark = new Bark("yip") ; System.out.println("a small dog starts barking"); recognizer.recognizer(smallDogBark) ; try{ Thread.currentThread().sleep(1000) ; }catch(InterruptedException e){ System.out.println("error") ; } System.out.println("bruce starts barking"); recognizer.recognizer(new Bark("woof")) ; System.out.println("bruce 'back inside"); } }
posted on 2010-12-21 12:01 jack zhai 阅读(160) 评论(0) 编辑 收藏
Powered by: BlogJava Copyright © jack zhai