2015年1月3日土曜日

開発環境

Head First Object-Oriented Analysis and Design: A Brain Friendly Guide to OOA&D (Brett McLaughlin (著)、 Gary Pollice (著)、 David West (著) 、 O'Reilly Media)のChapter 4. Analysis: Taking Your Software into the Real World、DESIGN PUZZLE(No. 2560)を解いてみる。

その他参考書籍

DESIGN PUZZLE(No. 2560)

コード(BBEdit, Emacs)

DogDoor.java

import java.util.Timer;
import java.util.TimerTask;
import java.util.List;
import java.util.ArrayList;

public class DogDoor {
    private boolean open;
    private ArrayList<Bark> barks;

    public DogDoor() {
        this.open = false;
        this.barks = new ArrayList<Bark>();
    }

    public void open() {
        System.out.println("The dog door opens.");
        open = true;
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
                public void run() {
                    close();
                    timer.cancel();
                }
            }, 5000);
    }

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

    public boolean isOpen() {
        return open;
    }

    public void addAllowedBark(Bark bark) {
        this.barks.add(bark);
    }

    public List<Bark> getAllowedBarks() {
        return barks;
    }
}

入出力結果(Terminal)

$ jrun.sh DogDoorSimulator
Bruce starts barking.
 BarkRecognizer: Heard a 'rowlf'
The dog door opens.

Bruce has gone outside...
The dog door closes.

Bruce's all done...
...but he's stuck outside!
A small dog starts barking.
 BarkRecognizer: Heard a 'yip'
This dog is not allowed.
Bruce starts barking.
 BarkRecognizer: Heard a 'rooowlf'
The dog door opens.

Bruce's back inside...
The dog door closes.
$

0 コメント:

コメントを投稿