Docsity

Design Patterns & Concurrency — Lab Exercise 4, Lecture notes of Design Patterns

Toggle sidebar
bg1
Design Patterns & Concurrency — Lab
Exercise 4
Prof. Dr. Oliver Haase
Problem 4.1
The file exercise4 1.zip contains the following interfaces and classes:
Birthday : Interface that decribes methods to access the year, month, and day fields of
a birthday, as well as a few methods to compare two Birthday instances.
BirthdayFactory : Abstract factory interface for the creation of Birthday instances.
BirthdayClient : Client class that gets uses a BirthdayFactory object to create and
compare a few Birthday instances.
Your task is to make the GregorianCalendar Java library class usable as an imple-
mentation of the Birthday interface. Because GregorianCalendar cannot be modified,
you will need to write an adapter class.
(a) Implement an object adapter DateObjectAdapter that adapts the given class
GregorianCalendar to the new Birthday interface. Write a concrete factory class
DateObjectAdapterFactory that implements BirthdayFactory.
(b) Similarly, implement a class adapter DateClassAdapter and a factory class
DateClassAdapterFactory.
(c) Run the client class BirthdayClient.
Problem 4.2
The file exercise4 2.zip contains the following interfaces and classes:
Fraction : Interface that decribes methods for a datatype representing fractions.
FractionBase : Abstract class that provides an implementation of one of the methods
defined in Fraction, as well as the utility method greatestCommonFactor that
subclasses of FractionBase can use in order to implement the full Fraction in-
terface.
1
pf2

Partial preview of the text

Download Design Patterns & Concurrency — Lab Exercise 4 and more Lecture notes Design Patterns in PDF only on Docsity!

Design Patterns & Concurrency — Lab

Exercise 4

Prof. Dr. Oliver Haase

Problem 4.

The file exercise4 1.zip contains the following interfaces and classes:

Birthday : Interface that decribes methods to access the year, month, and day fields of a birthday, as well as a few methods to compare two Birthday instances.

BirthdayFactory : Abstract factory interface for the creation of Birthday instances.

BirthdayClient : Client class that gets uses a BirthdayFactory object to create and compare a few Birthday instances. Your task is to make the GregorianCalendar Java library class usable as an imple- mentation of the Birthday interface. Because GregorianCalendar cannot be modified, you will need to write an adapter class.

(a) Implement an object adapter DateObjectAdapter that adapts the given class GregorianCalendar to the new Birthday interface. Write a concrete factory class DateObjectAdapterFactory that implements BirthdayFactory.

(b) Similarly, implement a class adapter DateClassAdapter and a factory class DateClassAdapterFactory.

(c) Run the client class BirthdayClient.

Problem 4.

The file exercise4 2.zip contains the following interfaces and classes:

Fraction : Interface that decribes methods for a datatype representing fractions.

FractionBase : Abstract class that provides an implementation of one of the methods defined in Fraction, as well as the utility method greatestCommonFactor that subclasses of FractionBase can use in order to implement the full Fraction in- terface.

FractionNumberClient : Test class, to be used later.

(a) Write an implementation FractionImpl of Fraction as a subclass of FractionBase.

(b) Write an alternative implementation FractionImpl2 of Fraction, this time not as a subclass of FractionBase, but as a subclass of the Java class Number. Write a smart adapter that allows your implementation to take advantage of the (parti- al) implementations in FractionBase. When done, test your alternative solution FractionImpl2 with the test class FractionNumberClient.

Good luck & have fun!