This lab uses the following patterns:
Strategy, Decorator
Composite, Visitor
Publisher-Subscriber
Adapter, Bridge
Draw a class diagram showing how you would connect the given classes and apply the given patterns. In some cases you may need to add classes, interfaces, fields, operations, and associations.
Suggestion: Create these classes using StarUML, then use the "Apply Pattern" tool to assign rolls to the classes.
(a) Patterns: Composite, Strategy
Classes: Worker, Supervisor, Task
All of the methods and fields of a utility class are static. There is no point in creating instances of a utility class. For example, Java's Math class is a utility class containing standard mathematical functions:
class Math {
�� public static double sin(double x) { ...
}
�� public static double cos(double x) {
... }
�� public static double log(double x) {
... }
�� // etc.
}
Only one instance of a singleton class can be created. Often a developer can choose between a utility class and a singleton class.
For example, a white page server contains a hash table that associates server names to server locations. The white page server provides a method that allows servers to register their names and locations, and provides a method that allows clients to locate servers using only their names. Of course all servers and clients will somehow need to know where the white page server is located. It might be confusing if an application has more than one white page server.
Here are the possible designs. First using Singleton:
Second, using a utility class:
UML note: An underlined method or field is static.
(a) Sketch an implementation of WhitePageServer, Server, and Client two times. In both cases your sketch should show how the client and server use the white page server. First implement WhitePageServer as a utility class, and then as a singleton class.
(b) Can you think of any advantages or disadvantages of singleton over utility or vice-versa?
We can have many non-mutable objects representing the same real world object because there's no danger that they will get out of synch with each other. For example, it's not a problem having multiple non-mutable Student objects representing the same student.
Having several mutable Student objects representing the same student is a problem, however, because they can be mutated in different ways and therefore become out of synch with each other.
Use interning to implement a mutable Student class that doesn't allow two student objects to be created that represent the same student.
There should be only on instance of a GUI. Show how you would use the Singleton pattern in the GUI class.