SOFT2201/COMP9201 Week 7 Tutorial
## Adapter & Observer Adapter Pattern
Adapter is the first pattern where using it in an initial design is almost guaranteed to be a bad idea - itis an extension pattern, used to allow code to be extended in a maintainable way without expending alarge amount of refactoring effort. To understand why this is a useful thing you must first understandtwo fundamental concepts of programming in real world projects - every time someone touches code,
it costs time & money, but also they have a chance to introduce bugs into that code. Because of thesereasons once code is working there are very good reasons to not want to touch it (there are reasons to
want to touch it too, refactoring is often a good thing - but sometimes you want to avoid it).With Adapter we allow an existing service used by a client to be swapped out for another already
existing service that has a different API. We leverage dependency inversion (and dependency injection1) in order to do this - if the client code depends on an interface, it is very easy to swap in anotherimplementation of that interface. If the client is given that implementation rather than creating it itselfthen it doesn’t even need to know anything has changed.
### Question 1: Revisiting Adapter
Consider the following given code including client, existing old service, existing new service, andmain driving class, which could also be found on Canvas module 7.The lecture covered the 2 different ways to implement Adapter - both of these methods will allowyou to have the client code use the new service where the only existing class that gets even a singlecharacter modified is the main driving class. Discuss within the class about: The differences between two versions of the adapter. How you would have to do it without Adapter?
1Dependency injection is a design pattern which is not covered in ourunit. Please feel free to find moreresources about it if you are interested in knowing it.
SOFT2201/COMP9201 Adapter & Observer
public interface Service{
int getValue();
void printDescriptionToSTDOut();
void toggle();
}
public class Client {
private Service service;
public Client(Service service) {
this.service = service;
}
public void useService() {
service.toggle();
service.printDescriptionToSTDOut();
System.out.println(service.getValue());
}
}
public class OldService implements Service {
private boolean toggled = false;
public OldService(){}
public int getValue(){
return 42;
}
public void printDescriptionToSTDOut() {
System.out.println("Toggle is currently: " + toggled);
}
public void toggle(){
toggled = !toggled;
}
}
// Note: this deliberately does NOT implement Service
public class NewService {
public static final int VALUE = 42;
private boolean toggled = false;
public NewService(){}
public String getDesc() {
Software Design and Construction 1 Page 2 of 4
SOFT2201/COMP9201 Adapter & Observer
return "Hey, this is " + toggled;
}
public void setToggled(boolean toggled) {
this.toggled = toggled;
}
public boolean getToggled() {
return toggled;
}
}
public class MainDriver{
public static void main(String[] args) {
Client client = new Client(new OldService());
client.useService();
}
}
### Question 2: Implementing Adapter
Determine the best choice between 2 ways for Question 1, and implement your choice based on theabove code.ObserverObserver is quite different from the structural pattern Adapter because it is something to include ’inadvance’, rather than as a modification to an existing system like Adapter - though it can be involvedin extensions or refactoring as well. Observer handles issues of deep and complex coupling, not byremoving the coupling (the line on a UML diagram will still be there) - but by lessening how involvedthat coupling is (the technical term for this is improving the ’connascence’ between the classes).
### Question 3: Modeling Observer in a New System
Consider the following requirements of an autonomous vehicle fleet monitoring system. The mon-itoring system is based on Vehicle objects which track current vehicle speed, fuel, and occupancy.Theapplication should provide a FleetDisplay with each vehicle shown asa separate line or tile, allupdated in real time as the Vehicle objects update their associated vehicle’s status via the internet(your implementation doesn’t need to actually use the internet, you can just get random integers atrandom intervals from 5 to 10 seconds in length (think 300 to 600 ticks if you use a 60 fps keyframe,
don’t try anything involving Thread.sleep or similar).You will notice far less of this system has been provided to you (aka none of it) -use this as practiceSoftware Design and Construction 1 Page 3 of 4
SOFT2201/COMP9201 Adapter & Observerfor including a design pattern in an entirely new system where there is no provided code to start you
off.Because this is quite an involved activity you should ensure you first model it in UML - aside fromthis being a good idea every time you model a system, that way your tutor has a chance to reviewyour design within the tutorial time.
### Question 4: Implementing Observer in the New System
Based on the UML you have modelled in Question 3, please implement this system. When imple-menting it you should start with a version that does nothing but print updates to System.out - this
should be a model package with a console view package. Once you havethis working with theObserver pattern, then swap out the console view package with a JavaFX view.