Java Reference
In-Depth Information
// ...
public void notify (String message) {
notificationPreference.notify(this, message);
}
}
An implementation of the notification strategy then looks like this:
public class EmailNotifyStrategy implements NotifyStrategy {
@Override
public void notify(Customer customer, String message) {
// Send mail to customer.getEmail();
System.out.println("Sending an email: "+message);
}
}
You can test the program like so:
public class StrategyTest {
public static void main(String[] args) {
EmailNotifyStrategy emailNotifier = new EmailNotifyStrategy();
Customer a = new Customer(emailNotifier);
Customer b = new Customer(emailNotifier);
a.notify("Your product has shipped!");
}
}
This concludes the tour of the creational, structural, and behavioral design patterns. It is easy to
feel overwhelmed by these concepts, especially if you're a newbie. Some of these patterns might look
confusing, and some of them might look so nicely designed that you want to use them right away. Be
careful not to overuse them.
Most of the patterns that were discussed deal, ultimately, with abstracting concepts and algorithms
into more classes and objects. Most programmers will go through an “abstraction vision quest”
sometime in their career, looking roughly like this:
1.
Beginner programmers don't care too much about patterns and architecture. Their programs
are small. They take care to put every concept into an appropriate class, but things stay man-
ageable and nimble.
2.
As programmers undertake bigger and bigger projects, they become at one point annoyed by
a problem that one of the patterns tries to solve. Why do I have to keep adding subclasses to
define product types? Maybe I should think at a higher level? What if I make a product type
itself a class? Programmers start looking into design patterns, and suddenly, they seem like
the perfect solution to all their problems.
3.
Programmers are so happy with their use of patterns and the elegance they bring along, they
start “thinking” ahead and start working out beautiful, elegant, and abstract architectures
for their new projects.
Search WWH ::




Custom Search