论坛首页 Java企业应用论坛

面向对象学习笔记JAVA 01(客户想要狗门)—2014_4_19

浏览 4455 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-04-19  
OO

******************************

成为首席程序员后

 

******************************

内容来自于"head first OO"

 

You’ve just been hired as the lead programmer at a new start-up, Doug’s Dog Doors.

Doug’s got a pretty high-tech door under development, and he’s decided you’re the programmer that can write all the software to make his killer hardware work.



 

 

Let’s start with the dog door                                                                                          

 

The first thing we need is a class to represent the dog door.

Let’s call this class DogDoor, and add just a few simple methods:

 

public class DogDoor {

  private boolean open;

  public DogDoor() {
    this.open = false;
  }

  public void open() {
    System.out.println("The dog door opens.");
    open = true;
  }

  public void close() {
    System.out.println("The dog door closes.");
    open = false;
  }

  public boolean isOpen() {
    return open;                      
  }                  
}

 

This is pretty simple.

 

Test drive                                                                                                                                

 

Let’s see if everything works.

Go ahead and take your new dog door for a test drive.

 

public class DogDoorSimulator {

  public static void main(String[] args) {
    DogDoor door = new DogDoor();
    Remote remote = new Remote(door);

    System.out.println("Fido barks to go outside...");
    remote.pressButton();

    System.out.println("\nFido has gone outside...");
    remote.pressButton();

    System.out.println("\nFido's all done...");
    remote.pressButton();

    System.out.println("\nFido's back inside...");
    remote.pressButton();
  }
}

 

 
 
It works! Let’s go show Todd and Gina
 

But when Gina tried it...                                                                                                                        

 


 

There’s nothing wrong with our code!

Gina must have forgotten to press the button on the remote again after Fido came back in.

It’s not my fault she’s using the door incorrectly!

 

But the door doesn’t work the way Todd and Gina want it to!

 

Todd and Gina didn’t expect to have to close the dog door, so they pressed the button on the remote only once: to let Fido out.
Even worse, in this case, the way they used the door created new problems.

Rats and rabbits started coming into their house through the open door, and you’re taking the blame.
Let’s tackle Todd and Gina’s dog door again, but this time, we’ll do things a little bit differently.

 

Here’s our plan:

 

  1. Gather requirements for the dog door.
  2. Figure out what the door should really do.
  3. Get any additional information we need from Todd and Gina.
  4. Build the door RIGHT!

Listen to the customer                                                                            



 

1. Fido barks to be let out.
2. Todd or Gina hears Fido barking.
3. Todd or Gina presses the button on the remote control .
4. The dog door opens.
5. Fido goes outside.
6. Fido does his business.
6.1 The door shuts automatically.
6.2 Fido barks to be le t back inside .
6.3 Todd or Gina hears Fido barking ( again).
6.4 Todd or Gina presses the button on the remote control .
6.5 The dog door opens (again).
7. Fido goes back inside .
8. The door shuts automatically.

 

public class Remote {

  private DogDoor door;

  public Remote(DogDoor door) {
    this.door = door;
  }

  public void pressButton() {
    System.out.println("Pressing the remote control button...");
    if (door.isOpen()) {
      door.close();
    } else {
      door.open();

      final Timer timer = new Timer();
      timer.schedule(new TimerTask() {
        public void run() {
          door.close();
          timer.cancel();
        }
      }, 5000);
    }
  }
}

 

The remote already has code to handle closing the door if it’s open.

 

Our old simulator isn’t that useful anymore... it assumes Todd and Gina are closing the door manually, and not letting the timer do its work. Let’s update our simulator to make it work with the updated Remote class

 

public class DogDoorSimulator {

  public static void main(String[] args) {
    DogDoor door = new DogDoor();
    Remote remote = new Remote(door);

    System.out.println("Fido barks to go outside...");
    remote.pressButton();

    System.out.println("\nFido has gone outside...");

    System.out.println("\nFido's all done...");

    System.out.println("\nFido's back inside...");
  }
}

 In the new improved dog door, Gina doesn’t need to press a button to close the door.

That will happenautomatically now.

 

It’s time to see if all our hard work is going to pay off.

Let’s test out the new and improved dog door.



 

  • 大小: 45.1 KB
  • 大小: 482.7 KB
  • 大小: 42.1 KB
  • 大小: 57.8 KB
  • 大小: 3.1 KB
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics