Homework 1 Design Patterns and UML

advertisement
HOMEWORK 1 - Design Patterns CSC 2053 Name ___________________________
UML
1. Implement a UML diagram for classes A, B and C, D and G
a) A extends B and implements C
b) D extends A and implements G
c) Class B contains two objects and four methods: two of the methods are private
2. Fill in them meaning of the following UML keys:
+ _________________________
# _________________________
- _____________________________
<blank>
static variables are ____________________
Model View Controller
Note: Model, View, Controller Design pattern will be designated as MVC
1. What is the relationship between the Controller and Model and Controller and View in
the MVC Design Pattern: The Controller
A. manipulates the model
B. receives user input and invokes models methods
C. changes the View in response to user input
D. Updates view based on changes in state of model
E. captures the state of the system and notifies the model
F. A, B, C,D
G. All of the above
The MVC Design Pattern splits application design into three parts.
2. Which of the following is NOT the job of the Model
_________________
A. The model is used to manage information and notify observers when that
information changes. The Model holds an application’s data and defines the
logic that manipulates that data.
B. It contains only data and functionality that are related by a common purpose .
C. A model is meant to serve as an abstraction of some real world process or system
e.g. a queue of people in a security line
D. It captures the state of a process or system, and how the system works.
E. The model object acts as the intermediary between the application’s view and
the model’s services(methods).
3. Which of these is NOT true of the Controller __________
A. The Controller is the means by which the user interacts with the application.
B. A Controller accepts input from the user and instructs the model and view to perform
actions based on that input.
C. A controller object acts as the intermediary between the application’s view and its
model.
D. The Controller is the most reusable part of the application’s MVC layers.
E. If the user clicks the mouse button or chooses a menu item, the Controller is
responsible for determining how the application should respond.
1
HOMEWORK 1 - Design Patterns CSC 2053 Name ___________________________
4. Which of these is NOT true of the View:
A. A view object knows how to display and possibly edit data from the application’s
model e.g. painting people objects in a line in a bank, formatting a date
B. The view should not be responsible for storing the data it is displaying, the model
contains the data.
C. When the model changes, the view automatically redraws the affected part of the
image to reflect those changes.
D. There is only one version of the view for each Controller object
5. Several problems can arise when applications contain a mixture of data access code,
business logic code, and presentation code. Such applications are difficult to maintain, because
interdependencies between all of the components cause strong ripple effects whenever a change is
made anywhere. High coupling makes classes difficult or impossible to reuse because they
depend on so many other classes. Adding new data views often requires re-implementing or
cutting and pasting business logic code, which then requires maintenance in multiple places. Data
access code suffers from the same problem, being cut and pasted among business logic methods.
How does the MVC design pattern solve these problems?
________________________________________________________________________
Design Patterns
1. What are Design patterns?
__________________________________________________________
2. Objects that may be null present problems when calling methods that use them, give two
examples
3. The Null Object design pattern is used to create a class that represents null objects, give
an example from my slides how this pattern might work.
4. The BigFish ocean example of a Null Object uses another technique for ensuring that a
working program is not jeapordized. What is this technique and explain the main
elements used.
5. In the BigFish/LittleFish scenario, the subclasses of Fish have methods of the same name
with very little difference in code. How can refactoring help avoid the code duplication?
6. Which of the following steps taken to refactor in the Fish example is not true?
A. Extract the check on whether it’s ok to move into a method
B. In the Fish class, put the actual (template) move() method
C. Create an okToMove() method in the Fish class and implement a general
movement pattern for all fish
D. Implement okToMove() in each subclass with a specific movement pattern
7. Below is the inheritance structure of the BigFish/LittleFish scenario before refactoring.
Draw it again after the refactoring is done.
2
HOMEWORK 1 - Design Patterns CSC 2053 Name ___________________________
Fish
<<abstract>>move()
BigFish
LittleFish
move()
move()
8. Write the pseudocode for the new move method
public void move()
{
}
9. Why is the okToMove method abstract? What does labeling okToMove abstract require of the
of the Fish class?
_________________________________________________________________
10. Give an example of when a Singleton Design method might be used. What is a Singleton
class?
11. Which of the following is/are NOT true of when a Singleton class should be used:
a. You may want just one instance of a null object, which you use in many places
b. You may want to create just one AudioStream, so you can only play one tune at a
time
c. You may want to prohibit any changes to instance variables in the class
d. You may want to have many printers but one spooler
e. You might want to have one copy of alphabetic characters in a document
12. Which of the following are true steps in designing a Singleton class:
A. Define a static reference variable of the class type which serves as the single
instance of the class.
B. The constructor should be private so a user can not instantiate the class
C. Use a static public method that will return the reference to the object
D. Make all other methods in the class static and private
E. A,B, C, D
F. A, B, C
13. In the Dice example in my slides illustrating the Singleton Design Patternm, how is the
single object created? Give specific code.
14. You should use the Factory Design Pattern when which of the following are true:
A. You write a class that works with several different kinds of objects
B. You may want to be able to create objects, without being dependent on the kind
of object
C. You want to provide a simple interface to a complex subsystem.
D. Your object is an image
E. All of the above
3
HOMEWORK 1 - Design Patterns CSC 2053 Name ___________________________
F. A and B
G. A and D
15. In order to write a factory method that creates several objects of different classes
depending on a parameter and return one of them, all the objects must have the same
_____________
16. Give an example of a factory design method -other than one in my slides.
__________________________________________________________________
17. The Façade Design Pattern does not provide which of the following services:
A. Provide access to services of multiple related classes without requiring the
user to know all the internal details
B. Allows access two or more applications (say, Oracle, MySql, and Access
2000) so you can write a program that works with any of them
C. Allows the creation of a class that accepts many different kinds of requests,
and “forwards” them to the appropriate internal class
D. The Façade pattern provides a simple interface to a complex subsystem.
E. Allows user to create objects, without being dependent on the kind of object,
so multiple services can be used
18. Draw the class and interface structure for the Ocean classes as described in my slides on
the Façade Design Pattern.
19. Which of the following is/are true about using the Immutable object Design Pattern
A. Objects that refer to an immutable object never have to worry about whether
that object has been changed
B. Immutable objects are thread-safe
C. Immutable objects prevent the user from creating other objects of that type.
D. In order to change an immutable object you must create a new object
incorporating the changes.
E. A,B, C
F. A,B,D
20. What are the two steps to create an immutable object:
1.__________________________
2. ____________________________
21. Which of the following are necessary to ensure that your inheritance structure is correct.
A. When you create a subclass, you agree to inherit all its (non-private)
fields and methods
B. If you want to inherit just some of the functionality of a class, you can
probably add some of your own
C. Your class wants to use most of the methods of a superclass but may
need to hide variables or methods
D. Your class will always keep its same relationship with the super class
E. Your class is of the same type as the super class.
F. A,B,C
G. A, D, E
H. A, B, D E
I. A, C, D, E
4
HOMEWORK 1 - Design Patterns CSC 2053 Name ___________________________
Fill in the blanks using Design Patterns we have studied
1. A ________________ method can create instances of different classes, depending (say)
on its parameters
2. _____________________If you have an application that has three images to be
distributed over hundreds of cards, you can have just one copy of each image rather than
create an instance for each card.
3. ______________ is fundamental to agile programming whereby working code is
restructured using a Design Pattern
4. The_________________ Design pattern provides a model for building a class that can
have only one instance
5. _____________ is sometimes necessary in a traditional process, when the design is
found to be flawed
Short Questions
1. If the Stack class extends the Vector class, what functionality will it inherit that might
violate its ADT profile?
__________________________________________________________
2. The definition of refactoring includes three elements:
1) ___________________________________
2) ____________________________________
3) _____________________________________
3. What must you do in order to successfully refactor (restructure) working code.
4. When should you refactor?
6. When should you not refactor?
7. What assumptions are the basis for Agile Engineering?
1.
2.
3
8. Give some examples of code that might indicate that refactoring might be needed
9. Explain the benefits of restructuring the switch statement below in the example in
my slides of class animal.
class Animal
{
final int MAMMAL = 0, BIRD = 1, REPTILE = 2;
int myKind; // set in constructor
...
String getSkin()
{
switch (myKind)
{
case MAMMAL: return "hair";
case BIRD: return "feathers";
case REPTILE: return "scales";
5
HOMEWORK 1 - Design Patterns CSC 2053 Name ___________________________
default: return "integument";
}
}
}
Restructured switch
class Animal
{
String getSkin() { return "integument"; }
}
class Mammal extends Animal
{
String getSkin() { return "hair"; }
}
class Bird extends Animal
{
String getSkin() { return "feathers"; }
}
class Reptile extends Animal
{
String getSkin() { return "scales"; }
}
6
Download