Question
15. Reimplement the voice mail system of chapter ref Chapter 2 so that the Law of Demeter holds. In particular, the MailSystem class should not
15. Reimplement the voice mail system of chapter ref Chapter 2 so that the Law of Demeter holds. In particular, the MailSystem class should not give out Mailbox objects. Instead, you need to add additional methods to the MailSystem class.
Law of Demeter
LoD tells us that it is a bad idea for single functions to know the entire navigation structure of the system.
http://cs.stmarys.ca/~porter/csc/465/code/horstmann/slides/Ch2/Ch2.html
^ in this link, bottom of page you can find all the MailSystem Files.
-----------------------------------------------------------------------------------------------------------
01: import java.util.ArrayList; 02: 03: /** 04: A system of voice mail boxes. 05: */ 06: public class MailSystem 07: { 08: /** 09: Constructs a mail system with a given number of mailboxes 10: @param mailboxCount the number of mailboxes 11: */ 12: public MailSystem(int mailboxCount) 13: { 14: mailboxes = new ArrayList(); 15: 16: // Initialize mail boxes. 17: 18: for (int i = 0; i < mailboxCount; i++) 19: { 20: String passcode = "" + (i + 1); 21: String greeting = "You have reached mailbox " + (i + 1) 22: + ". Please leave a message now."; 23: mailboxes.add(new Mailbox(passcode, greeting)); 24: } 25: } 26: 27: /** 28: Locate a mailbox. 29: @param ext the extension number 30: @return the mailbox or null if not found 31: */ 32: public Mailbox findMailbox(String ext) 33: { 34: int i = Integer.parseInt(ext); 35: if (1 <= i && i <= mailboxes.size()) 36: return mailboxes.get(i - 1); 37: else return null; 38: } 39: 40: private ArrayList mailboxes; 41: }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started