GOF Patterns
NOTE: We are sure this page will save you a lot of time trying
to figure out what we’ve done.
We’ve implemented all GOFs except Composite (which our project didn’t
really call for or accomodate).
Adapter
- A few adapter classes have been created, but the main one too look at is
IDBConnenctionAdapter (in the database
package). Both MySqlDBConnectionAdapter (which connects to
MySQL databases) and MsAccessDBConnectionAdapter (which can
connect to Microsoft Access databases) extend the IDBConnenctionAdapter
interface.
- Definition: Different systems you need
to call. Provide stable interface to similar components with different interfaces.
Factory
- DBConnectionFactory (in the database
package) is our factory. It contains a static IDBConnenctionAdapter
object. Nothing too complicated here. Have a look at how _Test_Known_Databases.java
uses this method to connect to different databases and (more importantly)
uses different adapters.
- Definition: Facilitate separation of
concerns – group’s similar adapters together (like an index).
Responsible for creating objects when special considerations like complex
creation logic.
Singleton
- Global (in the common package)
is the best example of a singleton (although there are others). It contains
the type of data and methods which numerous other classes want to access &
change in a global context.
- Definition: Single static instance
- accessible from anywhere.
Strategy
- Note: We understand the concept of strategy, but had trouble
incorporating it in our project. We weren’t even thinking our project
didn't really warranted the use of a strategy.
- PollRoomBookings, implements the interfaceIPollCondition
(in the gui package). By feeding a different IPollCondition
class into the PollTimer's setIPollCondition()
method you can use a different "algorithm" as the poll condition.
At the moment RoomPanel uses PollTimer to
periodically check if the number of room bookings (records) has changed since
the last poll. I could now write PollRoomBookingAdvanced
(also implementing IPollCondition) to check if, say, any
room bookings have been added *OR* modified since the last poll.
- Definition: Define varying, but algorithms
in separate classes with a common interface. Builds/based on polymorphism
– often ties in with factory
Composite
- *NOT IMPLEMENTED
- We had trouble seeing how composite could possibly relate to us. We have
so little business logic in our project, and what little we had is now thrown
out the window, because we have a new client.
- Definition: Define conflict resolution
strategy. Treat group of objects the same way (poloymorphically) as a non-composite
(atomic) object.
Facade
- Look at the BookRoomController first (under control)
and look at the makeBooking() method. Although we don’t
explicitly have a rule engine, you will notice that we have to check the date
before trying to make a booking.
- The class Global (under common)
demonstrates a facade. We have date checking methods in this class, which
are used by an entire subsystem - many different controllers have to call
this method - which creates a date and then checks that it is valid.
- Definition: “Front end”
object - hides a subsystem behind an object. Lowers coupling. Facades are
often accessed via singletons. Provides a single point of entry for the services
of a subsystem. Provide protected variations from changes in the implementation
of a subsystem.
Observer
- I was able to implement observer. Have a look at PollTimer (under gui),
and see how RoomPanel uses/calls this.
- Every ten seconds the database is polled – if the number of records
has changed then (according to PollRoomBookings.java), then the JTable is
“refreshed”.
- To see this working, start the gui (run_gui.bat), make a new booking (or
go into the database through manning to do this.) and notice that the change
will be detected by the next poll, and the database will refresh.
- PROBLEM: I didn’t make this observer pattern work
for our JSP site because (as far as I know) there is no way to make it work.
Once the JSP page is generated at the server side it is sent to the browser.
As soon as you send the closing HTML tag the browser is done. So I can’t
see how a timer thread can then tell the browser to refresh, because the client
is already finished it’s request to the server.
- Definition: Interested in state change.
Define subscriber/listener interface, which subscribers implement. Publisher
can dynamically register subscribers who are interested in an even, and notify
them when even occurs.