Todd and Gina’s Dog Door, Requirements List要求:1. The dog door opening must be at least 12” tall.
        2. A button on the remote control opens the dog door if the
           door is closed, and closes the dog door if the door is open.
        3. Once the dog door has opened, it should close
           automatically if the door isn’t already closed.
        4. A bark recognizer must be able to tell when a dog is
           barking.
        5. The bark recognizer must open the dog door when it
           hears barking.新要求:Todd and Gina asked that they could determine the
        waiting time needed to close the door automatically
        after it is open.
Modify the application and update the test to satisfy
this new requirement.
Give me a code THANKS!

解决方案 »

  1.   

    不知道楼主要做成什么....swing界面么...需求大概看懂了一点,一个自动开关的狗门,
    可以手动开和关,
    开了之后一段时间后如果没有手动关就自动关
    可以监听狗叫,听到狗叫就开门然后新需求是要求可以控制自动关门的时长不过...这些要求和java有啥关系啊 囧!
      

  2.   

    to simulate the scenario rightafter several objects are determined, it's nearly finishedDogDoorBarkRecognizerDog
    and Dog and BarkRecognizer would become observer and observable for each other
      

  3.   

    根据新要求修改下面的程序:
    Test drive: update the DogDoorSimulator
    //DogDoorSimulator.java
    public class DogDoorSimulator {
    public static void main(String[] args) {
    DogDoor door = new DogDoor();
    BarkRecognizer recongnizer = new BarkRecognizer(door);
    Remote remote = new Remote(door);
    System.out.println("Fido starts barking.");
    recognizer.recognize(“Woof”);
    System.out.println("\nFido has gone outside...");
    System.out.println("\nFido's all done...");
    try{ Thread.currentThread().sleep(10000);
    } catch (InterruptedException e) {}
    System.out.println("...but he's stuck outside!");
    System.out.println(“Fido starts barking.");
    recognizer.recognize(“Woof”);
    System.out.println("\nFido's back inside...");
    }
    }
      

  4.   

    DogDoorSimulator.javapublic class DogDoorSimulator {
    public static void main(String[] args) {
    DogDoor door = new DogDoor();
    BarkRecognizer recognizer = new BarkRecognizer(door);
    // here remote is nothing but a show up
    Remote remote = new Remote(door);
    // DogDoor cannot be directly controlled
    door = null;
    remote.open();
    remote.close();
    Dog fido = new Dog("Fido");
    fido.addBarkRecognizer(recognizer);
        fido.bark();
    System.out.println("Fido has gone outside...");
    System.out.println("Fido's all done...");
    try {
    Thread.sleep(1000);
    } catch(InterruptedException e) {
    }
    if(remote.getDogDoorState().equals(DogDoor.DogDoorState.open)) {
    remote.close();
    }
    System.out.println("...but he's stuck outside!");
    fido.bark();
    System.out.println("Fido's back inside...");
    }
    }
    Dog.javapublic class Dog {
    private BarkRecognizer br = null;
    private String name;

    public Dog(String name) {
    this.name = name;
    }

    public Dog(String name, BarkRecognizer br) {
    this.name = name;
    this.br = br;
    }

        public void bark() {
         System.out.println(this.name + " starts barking.");
         if(br != null) {
         br.recognize(this);
         }
        }
        
        public void addBarkRecognizer(BarkRecognizer br) {
         this.br = br;
        } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    }}
    DogDoor.javapublic class DogDoor {

    DogDoorState state = DogDoorState.closed; public enum DogDoorState { closed(0), open(1); int value; DogDoorState(int value) {
    this.value = value;
    } public int getValue() {
    return value;
    } public static DogDoorState valueOf(int value) {
    switch(value) {
    case 0:
    return closed;
    case 1:
    return open;
    default:
    return closed;
    }
    }
    } public DogDoorState getState() {
    return state;
    } public void setState(DogDoorState state) {
    this.state = state;
    }

    public void open() {
    setState(DogDoorState.open);
    System.out.println("dogdoor now is opened...");
    }

    public void close() {
    setState(DogDoorState.closed);
    System.out.println("dogdoor now is closed...");
    }}
    BarkRecognizer.javapublic class BarkRecognizer {
    DogDoor dogdoor;

    public BarkRecognizer(DogDoor dogdoor) {
    this.dogdoor = dogdoor;
    } public void recognize(Dog dog) {
    System.out.println("BarkRecognizer recognized that " + dog.getName() + " is barking...");
    dogdoor.open();
    }
    }
    Remote.javapublic class Remote {
    private DogDoor dogdoor;

    public Remote(DogDoor dogdoor) {
    this.dogdoor = dogdoor;
    } public void open() {
    dogdoor.open();
    } public void close() {
    dogdoor.close();
    }

    public DogDoor.DogDoorState getDogDoorState() {
    return dogdoor.getState();
    } public DogDoor getDogdoor() {
    return dogdoor;
    } public void setDogdoor(DogDoor dogdoor) {
    this.dogdoor = dogdoor;
    }}
    result:
    dogdoor now is opened...
    dogdoor now is closed...
    Fido starts barking.
    BarkRecognizer recognized that Fido is barking...
    dogdoor now is opened...
    Fido has gone outside...
    Fido's all done...
    dogdoor now is closed...
    ...but he's stuck outside!
    Fido starts barking.
    BarkRecognizer recognized that Fido is barking...
    dogdoor now is opened...
    Fido's back inside...there're obviously some places left to make better
    like the design is too tight-coupling so that it's not easy to extend as
    interface here has not been used
    or it would face the abstraction and improve its applicability
    refer to the observer pattern though here for the current problem it seems enough