Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

01 Simple Architectures - Solutions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Tutorial Solutions

6CCS3SAD/7CCSMDAS
Software Architecture & Design

Tutorial 1 Simple Architectures in UML2


This is the solution sheet to the first tutorial.

Exercise 1 Hotel Reservation System


Recall the expedia-style hotel reservation system discussed in lectures.
HolidayReservationSession is a component that provides all the functionality required
to book a holiday. It exposes this functionality via the provided interface IHolidayRes. It
requires three components that implement the interfaces ICarRes, IAirRes and
IHotelRes.
HotelRes is a component that provides the functionality for booking a hotel from a selection
of possible hotels (of the same hotel chain). It implements the IHotelRes interface,
consisting of operations relating to hotel-booking. It requires two components that implement
the ILoyaltyProgram and IBilling.
CreditCarBilling is a component that implements the IBilling interface, providing
operations related to charging a credit card.
LoyaltyProgram is a component that implements the ILoyaltyProgram interface,
providing operations related to a loyalty program for guests use the hotel chain
CarRes implements ICarRes, providing functionality related to booking a car
AirRes implements IAirRes, provding functionality related to booking an airplane.
1.

Define detailed interface descriptions in UML 2 for the HolidayReservationSession


component, the HotelRes component and the LoyaltyProgram component! Make the
interfaces as realistic as possible! You can use the space on the next page for providing
your definitions.
LoyaltyProgram

ILoyaltyProgram

IHolidayRes

Holiday
Reservation
Session

ICarRes

CarRes

Last changed: 11 December 2013

HotelRes

IHotelRes

CreditCardBilling

IBilling

IAirRes
AirRes

Answer: A correct answer is any UML 2 interface description that is cohesive and clearly
performs the functions of the component. Methods are clearly related to each other and to the
purpose of the component. 3 or 4 methods per interface is realistic. If this were an exam, half
marks would be obtained for getting the UML2 interface written; full marks for making an attempt
to describe the semantics of the methods in the interface (in English).
2.

Write Java code to implement the HolidayReservationSession component using the


mapping from the lecture. Note that, in the lecture, we have already written code for the
constructor and the instantiation of the components. In this exercise, you are asked to
provide an implementation of one of the methods from IHolidayRes. You can use the
space below to write your code.

Answer: A good method to include in IHolidayRes might be getOffers (place: String,


from: Date, to: Date, numPeople: int) : OfferDetail[]. Here, OfferDetail is
a helper structure that combines information about a hotel stay, car reservation and flight
availability. I do not show details of this class here, but it is easy to imagine what it would look
like. A possible implementation of getOffers would be:
public class HotelResSession implements IHolidayRes {
// ... code as on slide 48 of the lecture
public List<OfferDetail> getOffers (String place,
Date from, Date to,
int numPeople) {
List<HotelOffers> hotels = myHotelRes.getHotelOffers (
place, from, to, numPeople);
List<CarOffer> cars = myCarRes.getCarOffers (
place, from, to, numPeople);
List<AirOffer> planes = myAirRes.getAirOffers (
place, from, to, numPeople);
// combineMatchingOffers is a private method that selects matching
// hotel, car, and plane offers and returns all sensible
// combinations.
return combineMatchingOffers (hotels, cars, planes);
}
}

Exercise 2 Exam Marking Component


Consider the following specification of an exam marking component, ExamMarking. This
component is to be used within the Department of Informatics new student administration system
(not depicted here). Users of the system will be academics, administrators and students.
The component exposes one provided interface, IExam, defining functionality for entering marks
for students, performing standardization, and viewing marks in various kinds of presentation.
The component also has one required interface, IDiskUtil, defining functions relating to
storing marks on disk and emailing marks.
Here is how the component is represented in UML 2 with an interface specification included.
IExam

IDiskUtil
ExamMarking

Last changed: 11 December 2013

<<interface>>
IExam
+InputGrade(ID:int,mark:float,code:SubjectCode)
+CalculateOverallAverage():float
+CalculateOverallDistribution(code:SubjectCode):Distribution
+Standardize():void
+ViewMark(ID:int,code:SubjectCode):float
+CalculateStudentAverage(ID : int):float
+FixNiceSnack()
IExam interface semantics:
InputGrade takes in the final grade for a student for a subject. ID is the student's ID, mark
is the student's grade and code is the code of the subject being graded.
CalculateOverallAverage computes the overall average grade for all students in all
subjects this is returned as a floating point.
CalculateDistribution computes the overall mark distribution for a subject this is
returned as an element of the Distribution data structure. code is the code of the
subject.
Standardize is a function that standardizes mark distributions for all subjects.
ViewMark retrieves the grade of a student this is returned as a float. ID is the student's
ID, code is the subject code.
CalculateStudentAverage computes the average grade for a particular student,
returning this as a float. ID is the student's ID.
FixNiceSnack orders a nice snack for Steffen.

Last changed: 11 December 2013

<<interface>>
IDiskUtil
+storeList(fileName:string,grades:List)
+readList(fileName:string):List
+emailStudentTranscript(grades:List)
+emailWarning(grades:List)
+orderCoffee(strength:int)
+orderChocolate(type:ChocolateType)
IDiskUtil
storeList writes the list of grades to a file. fileName is the name of the file. grades is
the list of grades for all students in all subjects.
readList reads a list of grades for all students in all subjects from a file, returning them as
an element of the data structure List. fileName is the name of the file to be read from.
emailStudentTranscript emails a transcript of grades to each student mentioned in
the grades list. grades is the list of grades for all students in all subjects.
emailWarning emails threats of expulsion from the College to all students mentioned in
the grades list whose average falls below a certain point. grades is the list of grades for
all students in all subjects.
orderCoffee() utilizes the Departments automated telephony system and calls Caff
Nero to order Steffen his favourite espresso. strength is the number of espresso shots
required.
orderChocolate() uses the telephony system to order Steffen a bar of chocolate. type
is kind of chocolate required.
What is wrong with this component specification? Fix it, bearing in mind the discussion on what
makes a component from last weeks lecture. (Note there are several things that are obviously
wrong with this component, and several more subtle problems.) Use the space below to write
down your answers.
Answer: The interfaces are not cohesive.
1. IExam and IDiskUtil are about examination results and storing data, according to the
system description. The FixNiceSnack function of IExam and orderCoffee and
orderChocolate functions have nothing to do with the objective of the interfaces or
components they should be removed from these interfaces and put into different
interfaces. But even when put into different interfaces, they should also be removed from the
components, as they have nothing to do with the component functionality as a whole.
2. IExam can be split into two different interfaces to ensure better cohesion. InputGrade,
CalculateOverallAverage, CalculateDistribution, and Standardize methods
are functions to do with examination mark administration performed by teachers and
administrators, while ViewMark, and CalculateStudentAverage are functions to do with
individual student-oriented views on the system. A possible solution is to divide these two
sets of functions into two interfaces, IMarkAdmin, to do with administration, and
IStudentView, to do with providing views to students.
3. Similarly, sending marks and storing marks are two different kinds of functionality it would
be a better design to place storeList and readList in a new required interface
IDiskAdmin
and
put
emailStudentTranscript(grades:List)
and
emailWarning(grades:List) in a new required interface IEmail.

Last changed: 11 December 2013

Exercise 3 Component Substitution


Consider the following two components written by rival software houses. The components are
meant to work as plug-ins with a GUI-based email client.
EmailChecker is a component that scans incoming emails for viruses. The interface that
provides this functionality is called ICheckEmail. The component requires the use of
another component that provides an up-to-date list of current viruses. This required
functionality is defined in an interface called ICurrentVirusInfo.
JunkAlert does the same kind of thing as EmailChecker, by providing an alternative
implementation of the ICheckEmail interface. Also, it has additional spam checking
functionality, implemented by the ISpamCheck interface. It does not require another
component to provide a list of viruses, as it has a magic algorithm inside it that can detect
viruses and spam on the fly.
The components are drawn in UML2 as follows:
ICheckEmail
JunkAlert
ISpamCheck
ICurrentVirusInfo
ICheckEmail

EmailChecker

1. Can EmailChecker be substituted by JunkAlert within an application?


2. Can JunkAlert be substituted by EmailChecker?
3. Draw two example architectures to back up your reasons (i.e., one application consisting of
other components interacting with EmailChecker and the same application but with
JunkAlert replacing EmailChecker)!
Answer: EmailChecker can be substituted with JunkAlert, because JunkAlert provides
the ICheckEmail interface so any components using EmailChecker can also use
JunkAlert.
A possible picture to back up this reason would consist of
1. A before picture of a client component C using EmailCheckers ICheckEmail
interface and a server component S providing the required interface
ICurrentVirusInfo to EmailChecker.
2. An after picture of a C using JunkAlerts ICheckEmail interface, and S no longer
connected (b/c it is no longer needed).
The opposite does not hold, because a component using JunkAlert might require its
ISpamCheck interface, which is not provided by EmailChecker. Any picture of a client
component using JunkAlerts two provided interfaces will back up this reason.

Exercise 4 Reverse Engineering an Architecture (advanced)


At the end of this document is a rather intimidating looking Java program. The purpose of the
code is to send emails from the President of Marcuul to a list of people, alerting potential financial
partners of a great business opportunity.
The President has heard that software architecture aids maintenanceunfortunately, his current
team of hackers didnt learn about architecture when they studied at the University of South-East
Marcuul. He has therefore employed you as a consultant brought in to help with documenting the
architecture of this code.

Last changed: 11 December 2013

This is a common consulting role: to work backwards from code to design, and then (possibly) to
aid in making improvements.
Look at the Java code and draw a simple components-and-connector architectural diagram in
UML2.
Remember to abstract away parts of the code that are not architecturally relevant. In the lectures
we saw how abstract components can be implemented as classes. However, always remember
that not all classes correspond to components!
You dont need to understand the code in much detail. You are a senior consultant, and havent
actually written a program in yearsand that was written in Smalltalk. The main thing is to focus
on the coarse grain aspects of the code, ignoring fine grain stuff like algorithms, method content
and data structures as much as you can.
The code begins on the next page. Use the space below to draw your architecture diagram.
Answer: This is a trick question most of the code is not important from an architectural
perspective.
The Invitation class references an object of type Transportable, and the code instantiates
this reference with a Transport object. The architecture diagram simply consists of
1. An Invitation component. The provided interface is an interface that has all the public
methods of the Invitation class. You can give this interface any name you like. For
example, you could define an interface IInvitation with one operation
public void sendInvitation().
The required interface is the Transportable interface, written in UML2 instead of Java.
2. A Transport component. The provided interface is the Transportable interface,
written in UML2 instead of Java.
3. A connection between the two components provided and required interfaces.
The other classes have to do with data structures (e.g., the Address class) or starting up the
system (e.g., the System class). These things are not coarse grain and so do not correspond to
components.

Last changed: 11 December 2013

You might also like