In the process of writing the application, suppose I get a new requirement to take the following actions whenever a new customer is entered into the system:
Send a welcome e-mail to the customer.
Verify the customer's address with the post office.
Are these all of the requirements? Will things change in the future?
If I am reasonably certain that I know every requirement, then I could solve the problem by hard-coding the notification behavior into the Customer class, such as shown in Figure 17-1.
For example, using the same method that adds a new customer into the database, I will also make calls to the objects that generate welcome letters and verify post office addresses.
These classes have the following responsibilities:
Class | Responsibility |
---|---|
Customer | When a customer is added, this object will make calls to the other objects to have the corresponding actions take place. |
WelcomeLetter | Creates welcome letters for customers that let them know they were added to the system. |
AddrVerification | This object will verify the address of any customer that asks it to. |
The hard-coding approach works fine—the first time. But requirements always change. I know that another requirement will come that will require another change to Customer's behavior. For example, I might have to support different companies' welcome letters, which would require a different Customer object for each company. Surely, I can do better.