2008 Exam answers

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m (Question 6a)
(Question 6a)
Line 107: Line 107:
  
 
'''Find as many Gang of Four design patterns as you can in the Irrigation design. Name each pattern and describe where and how it is used in this design. Provide just enough information to make it clear how and why the pattern is applied here. Note any important variations from the standard pattern. There is no need to comment on the value of the pattern.'''
 
'''Find as many Gang of Four design patterns as you can in the Irrigation design. Name each pattern and describe where and how it is used in this design. Provide just enough information to make it clear how and why the pattern is applied here. Note any important variations from the standard pattern. There is no need to comment on the value of the pattern.'''
 +
 +
* [[Singleton]]: The two subclasses of HoseType, RigidHoseType and FlexHoseType are both singletons. They have a static getType() / getHoseType() method which returns an instance of the class. They also each contain a single static instance, the singleton object. The constructors of HoseType, RigidHoseType and FlexHoseType are all protected to stop clients outside the class hierarchy creating new instances. This is a deviation from the normal singleton pattern, where the constructors are usually made private.

Revision as of 02:04, 27 September 2009

Contents

Question 1

Possible improvements to the design:

  • Replace the Pair class with a Transaction class and two subclasses CreditTransaction and DebitTransaction. These classes will contain the information that was contained in Pair, and any other behavior related to transactions which is likely to have leaked out into the rest of the system.
    • Currently, the Pair class is used to keep track of the Stock a Stockholder owns (price and date of purchase or sale) and to record transactions (the stock that was traded and the date of the trade).
    • Violations:
  • Replace Stock class with CompanyStock and OwnedStock. These classes will not share a common super class as they differ greatly in what they represent.
    • The Stock class currently tries to represent both a single share (OwnedStock) and the master share with information about the stock for a particular company (CompanyStock).
    • Violations:
  • Broker and StockHolder should both hold a reference to an Account object. This would replace the brokersAccount property of Broker. The Account class would manage Transactions and include a reference to the account holder (Broker or StockHolder).
  • Broker and StockHolder should both hold a reference to ContactDetails object. Alternatively they could both inherit from a Person class, but if we favor composition over inheritance this would seem the less preferred solution. This also wouldn't be a good solution if a Broker can be a company as well as a person. This would allow personal details of Brokers and StockHolders to be stored. In the current design the notion of a Person is mixed with that of an Account. The improved design separates these two and would allow someone to have multiple accounts.
  • Once the notion of a person (Stockholder) and an account has been separated, all the accounts for one particular person can be stored as a Set by the object representing the person.
    • At the moment, accounts are chained together through the next link in the StockHolder class. This further shows that two different concepts have been muddled up. This also means that redundant data will be stored since for each account the data for the person is stored separately, rather than that data being stored only once.
    • Violations:
  • The trades field should be moved out of the GUI class. The GUI should concentrate on the visual display of information rather than recording trades and transactions.
    • The trades field could potentially be moved to the Broker class to record the trades made by one particular Broker.
    • Violations:
      • Separation of concerns
      • [[Single responsibility principle]
      • The GUI is part of the presentation layer of the program while the trades field belongs in the Business layer. The two should not be mixed.
  • The Price class should not be a subclass of Stock. This inheritance relationship makes no sense in the given domain as the two concepts are totally different. The Price class has very different behaviour and data and should therefore not inherit from Stock.
    • We can easily see that the inheritance relationship makes no sense in this case because the behaviour of the Stock class (which is inherited by Price) makes little sense for a Price object.
    • Violations:
  • Decouple the Broker from the GUI. This could be done using an observer design pattern.
    • At the moment, the Broker calls on the GUI to display itself. Broker contains a display method which knows about different ways of displaying a Broker. This behaviour does not belong in the Broker class but in the GUI. The GUI is the only part of the system which should be concerned with displaying information.
    • Violations:
  • The getters in the Stock class should be separated into getters and setters. They currently fulfill the role of both accessors and mutators.

Question 2

a) A maxim that contrasts with the culture of software reuse

  • I think that You ain't gonna need it potentially conflicts with the culture of software reuse. YAGNI basically says not to worry about functionality or a fancy design that you don't really need yet. On the other hand, in order to achieve software reuse you need to develop modules in a very general way, keeping in mind possible future needs. -- Janina

b) A maxim that might be measured automatically by software tools

  • There are a lot of maxims like this that can be measured automatically. I think essentially any maxim which can be evaluated by simply looking at the syntax of a program or the relationships between parts of a program can be easily measured. Examples include:
    • Acyclic dependencies principle - This is easy to measure by looking at which parts of the program depend on each other.
    • Avoid downcasting - This can be easily measured by looking for casts that go down the inheritance hierarchy.
    • Law of Demeter - This can be measured by looking at method calls to check that they all comply with the law
    • ...

c) A maxim that can never be measured automatically be software tools

  • Again, there are a number of principles that we will probably never be able to measure properly. Especially maxims which require an understanding of the domain semantics will be very hard to measure directly, though it is possible that we may be able to measure them indirectly through some metric. Examples include:
    • Model the real world - Evaluating whether this maxim has been followed requires domain knowledge.
    • Single responsibility principle - Again, domain knowledge is required to measure this one though we may be able to indirectly measure the effects that not following this rule has on our program.
    • Premature optimization - This would require knowledge about when a system is fully working, what constitutes optimization etc which is likely to be hard to automate.
    • ...

d) A maxim that is violated by the GoF Strategy pattern

e) A GoF pattern that allows a single object to do the job of multiple similar objects by externalising their changeable properties.

  • Flyweight: "A flyweight is a shared object that can be used in multiple contexts simultaneously. The flyweight acts as an independent object in each context - it's indistinguishable from an instance of the object that's not shared." (From GoF Design Patterns book) This decreases the overall number of object instances.

f) A pattern that encourages the use of abstract getters and setters instead of accessing attributes.

  • Encapsulate concrete state pattern - This pattern tells developers to never access data directly but to instead go through getters and setters, both from within and from outside a class. This improves the maintainability of the system since the data can be changed without affecting the code accessing it.

g) A pattern likely to be most helpful when developing a system to record the contents of a warehouse and the associated income and expenditure.

Question 3

A well known quote from Kent Beck says: “Make it work. Make it right. Make it fast.” What does he mean, and why?

  • He means that before doing any optimisations, you should make sure that your system works as it should. Once it does, you can refactor, change the design and make the code clearer to make it "right". Only once you have the functionality and design in place should you try to optimise it to make it faster. This is in line with the Premature optimization maxim.
  • It can be very tempting to write a piece of code and then spend lots of time making it "right" and optimising it. This is generally not a good idea because this code may be changed later. If instead we leave the optimisation to the end, it is less likely that the code will need to be changed later. In addition, optimisation and refactoring can often introduce bugs so it's best to make sure everything works and passes the tests before messing around with it.
  • This quote probably refers to test-driven design. For test-driven design you first write tests. While the new tests don't pass, you then write the simplest possible code to make the test pass (Do the simplest thing that could possibly work). This can involve committing all sorts of serious "sins" but the aim while tests aren't passing is to "make it work". Then, when the tests pass, you go into refactoring mode and "repent from your sins" to improve the design and "make it right".

Question 4

What is the principal message of Foote and Yoder’s Big Ball of Mud paper? Write as concise a synopsis as possible.

In the paper, "Big Ball of Mud", Foote and Yoder state that the most commonly used software architecture is the "big ball of mud", a "casually, even haphazardly, structured system". They say that big balls of mud occur for example when throw-away code becomes permanent or when the design of a system and its requirements continually changed but they also concede that big balls of mud are created because they work. Foote and Yoder present a total of seven patterns (including "Big ball of mud") either to explain where big balls of mud originate from or to show how they can be improved.

Question 5

Cohesion and coupling are influential software design concepts. Identify maxims (as many as you can) that encourage high cohesion and low coupling. Explain your answers.

  • Keep related data and behavior in one place - By following this principle, the behaviour is close to the data it needs rather than having to get data from other modules, which reduces coupling between modules. In addition, it obviously encourages us to keep together parts of the system that belong together, which leads to high cohesion of modules.
  • Separation of concerns - This maxim encourages developers to separate parts of the system that don't belong together, thus avoiding coincidental cohesion, the lowest form of cohesion.
  • Behavioral completeness - This principle says that any behavior that is related to an object and that is needed by the system should be implemented as part of the object rather than elsewhere in the system. In this way, related data and behavior is kept in one place, increasing the cohesion of the object in question and reducing coupling with other parts of the system which may have otherwise implemented behavior associated with the object's data.
  • Encapsulate that which varies - By encapsulating parts of the system that are likely to change, we reduce the coupling between those parts of the system and the rest of the system, meaning that the rest of the system will not need to change if the encapsulated part changes.

Question 6a

The following questions refer to the UML class diagram in Figure 3 and explanatory notes in Figure 4. Some getters and setters and other details are omitted from the diagram, but may be assumed where necessary. (Recall that a # in UML means protected, and an underline means static.) Document any non-trivial assumptions you need to make.

Find as many Gang of Four design patterns as you can in the Irrigation design. Name each pattern and describe where and how it is used in this design. Provide just enough information to make it clear how and why the pattern is applied here. Note any important variations from the standard pattern. There is no need to comment on the value of the pattern.

  • Singleton: The two subclasses of HoseType, RigidHoseType and FlexHoseType are both singletons. They have a static getType() / getHoseType() method which returns an instance of the class. They also each contain a single static instance, the singleton object. The constructors of HoseType, RigidHoseType and FlexHoseType are all protected to stop clients outside the class hierarchy creating new instances. This is a deviation from the normal singleton pattern, where the constructors are usually made private.
Personal tools