W10: Design patterns, Management, and Programming Languages
Please submit your answers to the questions as comments in a W10.md file you’ll be writing in this lecture.
Grading rubric and submission
When you are done, submit your W10.md file to BB.
You will be graded on the following:
| Item | Points |
|---|---|
| Answers are correct | 100 |
Questions
1
What is the difference between a design pattern and a java library?
Answer
A design pattern is a template, not code.
2
Using the Singleton pattern, write code/pseudocode that ensures that only one database connection object is ever instantiated.
Answer
public class DBSingleton {
private static DBSingleton singletonInstance;
private static DBConnection connection;
private DBSingleton(String user, String password, String schema) {
connection = new DBConnection(user, password, schema);
}
public static synchronized DBSingleton getInstance() {
if (singletonInstance == null) {
singletonInstance = new DBSingleton("kinga", "passy", "book_database");
}
return singletonInstance;
}
}3
Assume there is an Animal abstract parent class that has the following constructor: public Animal(String name, int weight).
Using the Factory pattern, write code/pseudocode that sets up a factory for two child classes of Animal: Bird and Mammal. Each of these has a constructor with the same arguments as those of the parent class.
Answer
public class AnimalFactory {
public static Animal getAnimal(String type, String name, int weight){
switch(type){
case "bird":
return new Bird(name, weight);
case "beast":
return new Mammal(name, weight);
}
return null;
}
}4
Imagine we have some code for an aquarium that works nicely to schedule feeding the fish in all the tanks, and cleaning the tanks:
public class Aquarium {
public void feedAll(ArrayList<Fish> tanks, int numFishInTank) {...}
public void cleanAll(ArrayList<Fish> tanks)
}This code works well with the following Fish class:
public class Fish{
public void feed(String food, int weight) {...}
public void clean(String cleaningProduct) {...}
}We also, one day, inherit an animal hospital, where each patient is solo in a cage. We want to be able to use the code above to feed and clean the cages of all the animals, but our hospital has the following API for the mammals they serve:
public class Mammal{
public void feed(String food, int weight, String medication) {...}
public void clean() {...}
}Use the Adapter pattern to allow us to use the Mammal class with the Aquarium class above. Each animal is housed alone in its cage during its visit. All mammals are given the same medication, namely, "antibiotics".
Answer
public class MammalAdapter extends Fish {
private Mammal mammal;
public MammalAdapter(Mammal m){
mammal = m;
}
public void feed(String food, int weight) {
mammal.feed(food, weight, "antibiotics");
}
public void clean(String cleaningProduct) {
mammal.clean();
}
}5
What is the difference between a functional and non-functional requirement?
Answer
Both must be testable and capture behavior of the system, but a non-functional requirement is usually some kind of “-ity” (usability, compatibility, security, speed, performance, etc.) that isn’t generally something that a single developer could code up (like, for example, a widget on a GUI).
6
Give two example functional requirements for Instagram.com.
Answer
Many options here;
- The system shall allow the user to upload a photo to their account.
- The system shall allow the user to ‘like’ photos of users they are following
7
Give two example non-functional requirements for Instagram.com.
Answer
Many options here;
- The system shall encrypt all passwords stored in the database
- The system shall load each image within one second
8
Draw a use case diagram for Instagram.com with at least three uses cases shown, where at least one use case extends another, and there are two actors.
Answer
Answers given in class.
9
Why has software engineering evolved to often embrace agile development models over waterfall ones?
Answer
It is generally impossible to obtain clear, consistent, and complete requirements up-front at the start of a software project, as the waterfall model assumes. Therefore, agile approaches can be used to have flexibility around the requirements discovery that inevitably happens during the software development process.
10
What is a critical path in software project planning? Why should you care?
Answer
Any task along the critical path that gets delayed will delay the entire project.
11
When would you use Java over C++? C++ over Java?
Answer
Many answers here, but commonly Java is slower but better for large group projects that would benefit from OOP and garbage collection. C++ is often used in places where computational speed matters and folks don’t want to rely on garbage collection.
12
What is static versus dynamic typing? How is dynamic binding related to these concepts?
Answer
Static typing, like in Java, defines and checks variable types at compile time. Dynamic typing, like in Python, decides a variable’s type at runtime depending on what was assigned to it (and this type can change during the run of the program). Dynamic binding, in Java, still relies on the compiler to check for types statically, but through inheritance binds the code that will be run for various overridden methods at runtime depending on the runtime type of the object.