I just finished reading up to ch4 I was totally confused about this issue in ch4:
It seems to me that the list of allowedBarks and all its associated methods ought to belong to BarkRecognizer and not and not in DogDoor as they are in the example. The door's responsibility is just to deal with all the opening and shutting related tasks. Its really the BarkRecognizer's job to deal with the allowedBarks and such.
Is this a flawed design or have I completely missed the point?
Thanks in advance for comments.
APE
O'Reilly Forums: Chapter 4 DogDoor design question - O'Reilly Forums
Page 1 of 1
Chapter 4 DogDoor design question
#2
Posted 31 March 2007 - 03:34 AM
I was also confused by this.
The constructor for BarkRecognizer takes a DogDoor as a paramater, and therefore the allowedbarks become 'part' of the BarkRegonizer object when instansiated. That is, when you instantiate BarkRecongizer, it gets the list of allowed barks for a door.
The recognize method in BarkRecognizer takes an external bark (from any dog) as a parameter, and compares it to the allowed barks on the list for the particular door with which it was associated when instantiated. But I should point out, BarkRecongizer does not return a boolean value in this example as I would have expected. Instead, BarkRecongizer prints out whether the bark is recognized or not. ... a bit of a silly program now that I try to explain it - but perhaps I have not understood and not explained correctly, as I am new to this.
as an aside to the authors
.... it becomes a little more annoying in later chapters for which no code is made available, even though the book explicitly creates the contract that the code is available on the http://www.headfirstlabs.com/books/hfooad/ site - so be prepared and take a defensive learning strategy for this particular book!
:x
The constructor for BarkRecognizer takes a DogDoor as a paramater, and therefore the allowedbarks become 'part' of the BarkRegonizer object when instansiated. That is, when you instantiate BarkRecongizer, it gets the list of allowed barks for a door.
The recognize method in BarkRecognizer takes an external bark (from any dog) as a parameter, and compares it to the allowed barks on the list for the particular door with which it was associated when instantiated. But I should point out, BarkRecongizer does not return a boolean value in this example as I would have expected. Instead, BarkRecongizer prints out whether the bark is recognized or not. ... a bit of a silly program now that I try to explain it - but perhaps I have not understood and not explained correctly, as I am new to this.
as an aside to the authors
.... it becomes a little more annoying in later chapters for which no code is made available, even though the book explicitly creates the contract that the code is available on the http://www.headfirstlabs.com/books/hfooad/ site - so be prepared and take a defensive learning strategy for this particular book!
:x
#3
Posted 09 April 2007 - 11:02 AM
Marten, i definitely agree that barkrecognizer should return boolean, this was my implementation for that:
I also updated the DogDoorSimulator to take advantage from that:
public class BarkRecognizer {
private DogDoor door;
public BarkRecognizer (DogDoor door) {
this.door = door;
}
public boolean recognize(Bark bark) {
System.out.println("\tBarkRecognizer: Heard a " + bark.getSound());
boolean allowed = false;
allowed = door.getAllowedBarks().contains(bark);
if (allowed) {
door.open();
} else {
System.out.println("This dog is not allowed");
}
return allowed;
}
}
private DogDoor door;
public BarkRecognizer (DogDoor door) {
this.door = door;
}
public boolean recognize(Bark bark) {
System.out.println("\tBarkRecognizer: Heard a " + bark.getSound());
boolean allowed = false;
allowed = door.getAllowedBarks().contains(bark);
if (allowed) {
door.open();
} else {
System.out.println("This dog is not allowed");
}
return allowed;
}
}
I also updated the DogDoorSimulator to take advantage from that:
public class DogDoorSimulator {
public static void main(String[] args) {
DogDoor door = new DogDoor();
door.addAllowedBark(new Bark("rowlf"));
door.addAllowedBark(new Bark("rooowlf"));
door.addAllowedBark(new Bark("rawlf"));
door.addAllowedBark(new Bark("woof"));
BarkRecognizer recognizer = new BarkRecognizer(door);
Remote remote = new Remote(door);
// Simulate the hardware hearing a bark
System.out.println("Bruce starts barking.");
if ( recognizer.recognize(new Bark("rowlf")) ) {
System.out.println("\nBruce has gone outside...");
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e) { }
System.out.println("\nBruce all done...");
System.out.println("...but he's stuck outside!");
// Simulate the hardware hearing a bark (not Bruce!)
Bark smallDogBark = new Bark("yip");
System.out.println("Bitsie starts barking.");
recognizer.recognize(smallDogBark);
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) { }
// Simulate the hardware hearing a bark again
System.out.println("\nBruce starts barking.");
recognizer.recognize(new Bark("Rooowlf"));
System.out.println("\nBruce's back inside...");
} else {
System.out.println("That's not bruce, let's ignore this dog.");
}
}
}
[/code]
public static void main(String[] args) {
DogDoor door = new DogDoor();
door.addAllowedBark(new Bark("rowlf"));
door.addAllowedBark(new Bark("rooowlf"));
door.addAllowedBark(new Bark("rawlf"));
door.addAllowedBark(new Bark("woof"));
BarkRecognizer recognizer = new BarkRecognizer(door);
Remote remote = new Remote(door);
// Simulate the hardware hearing a bark
System.out.println("Bruce starts barking.");
if ( recognizer.recognize(new Bark("rowlf")) ) {
System.out.println("\nBruce has gone outside...");
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e) { }
System.out.println("\nBruce all done...");
System.out.println("...but he's stuck outside!");
// Simulate the hardware hearing a bark (not Bruce!)
Bark smallDogBark = new Bark("yip");
System.out.println("Bitsie starts barking.");
recognizer.recognize(smallDogBark);
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) { }
// Simulate the hardware hearing a bark again
System.out.println("\nBruce starts barking.");
recognizer.recognize(new Bark("Rooowlf"));
System.out.println("\nBruce's back inside...");
} else {
System.out.println("That's not bruce, let's ignore this dog.");
}
}
}
#4
Posted 09 April 2007 - 11:07 AM
APE,
I agree with you that the allowedBarks list could be stored on the BarkRecognizer.
But if you think that the allowedBarks are like "allowed passwords" or "allowed keys" they make sense to be stored on the DogDoor. A Door should be able to select what keys are allowed and what are not.
For example, if the barkRecognizer stored the Barks, if you took your bark recognizer hardware to you neighbor's home (that also has a dogdoor installed), your dogs would be able to enter in his house. see?
hope that helps,
bruno
I agree with you that the allowedBarks list could be stored on the BarkRecognizer.
But if you think that the allowedBarks are like "allowed passwords" or "allowed keys" they make sense to be stored on the DogDoor. A Door should be able to select what keys are allowed and what are not.
For example, if the barkRecognizer stored the Barks, if you took your bark recognizer hardware to you neighbor's home (that also has a dogdoor installed), your dogs would be able to enter in his house. see?
hope that helps,
bruno
#5
Posted 02 February 2012 - 11:50 AM
I faced the same design question and I chose to let the BarkRecognizer be responsible for "recording" the allowable barks.
I like Bruno's scenario, he makes a good point. However, I believe my choice is correct because, think about it, the DogDoor has to have some mechanism for recording the actual dog barks. That doesn't make sense. We might argue that if the customer has to replace the door, they have to re-record the Barks. The DogDoor should just be responsible for opening and closing.
Here's my implementation.
DogDoorSimulator.java
BarkRecognizer.java
Notice that to make this change at the end of chapter 4 I only had to create a Bark class and change BarkRecognizer. If I had chosen to store the allowed barks in the DogDoor class, this change would have affected more classes (both the BarkRecognizer and the DogDoor).
I would assert that this design is better than the what's-her-name character in the book who won the MB Pro.
Anyone care to debate this?
I like Bruno's scenario, he makes a good point. However, I believe my choice is correct because, think about it, the DogDoor has to have some mechanism for recording the actual dog barks. That doesn't make sense. We might argue that if the customer has to replace the door, they have to re-record the Barks. The DogDoor should just be responsible for opening and closing.
Here's my implementation.
DogDoorSimulator.java
public class DogDoorSimulator {
public static void main(String[] args) {
DogDoor door = new DogDoor();
BarkRecognizer recognizer = new BarkRecognizer(door);
recognizer.addAllowedBark(new Bark("rowlf"));
recognizer.addAllowedBark(new Bark("rooowlf"));
recognizer.addAllowedBark(new Bark("rawlf"));
recognizer.addAllowedBark(new Bark("woof"));
Remote remote = new Remote(door);
// etc...
BarkRecognizer.java
import java.util.Vector;
public class BarkRecognizer {
private DogDoor door;
private Vector<Bark> allowedBarks;
public BarkRecognizer(DogDoor door) {
this.door = door;
this.allowedBarks = new Vector<Bark>();
}
public void recognize(Bark bark) {
System.out.println(" BarkRecognizer: Heard a '" +
bark.toString() + "'");
for (Bark b : allowedBarks) {
if (b == bark) {
door.open();
return;
}
}
}
public void addAllowedBark(Bark bark) {
this.allowedBarks.add(bark);
}
}
Notice that to make this change at the end of chapter 4 I only had to create a Bark class and change BarkRecognizer. If I had chosen to store the allowed barks in the DogDoor class, this change would have affected more classes (both the BarkRecognizer and the DogDoor).
I would assert that this design is better than the what's-her-name character in the book who won the MB Pro.
Anyone care to debate this?
Share this topic:
Page 1 of 1


















