Skip to content

Instantly share code, notes, and snippets.

@aleccool213
Created December 8, 2014 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aleccool213/c477aec55a2b5120e451 to your computer and use it in GitHub Desktop.
Save aleccool213/c477aec55a2b5120e451 to your computer and use it in GitHub Desktop.
CSC301 Exam notes
# CSC301 Exam notes
-------------------------------
## Java
When creating a class, we can check if arguments are null by calling setAttribute(*classCreationArgument*) on all of the arguments.
#### Checking for null arguments:
Our class creation function,
```
public DirectRoute(TrainCompany trainCompany, String fromStation, String toStation, double price) {
setTrainCompany(trainCompany);
setFromStation(fromStation);
setToStation(toStation);
setPrice(price);
}
```
Inside all of our setAttribute functions,
```
if (trainCompany == null) {
throw new IllegalArgumentException("trainCompany is null");
}
```
#### Static class varibles
Want to have a collection of objects that are only associated with a specific class?
We can do exactly that by putting in a class variable with the static keyword as such,
```
public static ArrayList<String> names = new ArrayList<String>();
```
We can now reference this variable throughout our class by just useing `names` and in other classes by simply calling
```
ClassName.names(args)
```
### DAO example
Write a data accessor interface that will let us support different ways of saving and loading data.
Example of DAO interface which will allow us to create and get instances without thinking about underlying data storage,
```
public interface TrainCompanyDAO {
public TrainCompany createInstance(String name);
public TrainCompany getInstance(String name);
}
```
Once we have written our specific DAO classes, we can do something useful like this,
```
public static void doSomethingUseful(TrainCompanyDAO dao){
TrainCompany c1 = dao.createInstance("FastTrain");
System.out.println(c1);
System.out.println(dao.getInstance("Via"));
}
```
### Iterator example
If we want to iterate through a collection in Java and don't want to have access to the underlying collection itself we use an iterator object. An example is below,
```
private Map<String, Collection<Post>> username2Posts = new HashMap<String, Collection<Post>>();
if(username2Posts.containsKey(username)){
return username2Posts.get(username).iterator();
}
```
## Builder design pattern
Telescoping design pattern might happen when we are adding more and more arguments to a constructor method for a class. Solution:
```
public class Pizza {
private int size;
private boolean cheese;
private boolean pepperoni;
private boolean bacon;
public static class Builder {
//required
private final int size;
//optional
private boolean cheese = false;
private boolean pepperoni = false;
private boolean bacon = false;
public Builder(int size) {
this.size = size;
}
public Builder cheese(boolean value) {
cheese = value;
return this;
}
public Builder pepperoni(boolean value) {
pepperoni = value;
return this;
}
public Builder bacon(boolean value) {
bacon = value;
return this;
}
public Pizza build() {
return new Pizza(this);
}
}
```
Then we would construct a Pizza object as such,
```
Pizza pizza = new Pizza.Builder(12)
.cheese(true)
.pepperoni(true)
.bacon(true)
.build();
```
###Factory Design Pattern
Summary: We need to create instances of class X but currently can't. Class x is an interface and might have other dependacies. We do not want to make significant changes to our software design to make way for this new requirement. I like this definition:
>Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
####Four main components
Interface that every object must implement
```
public interface TripAdvisor {
public double getCheapestPrice(String fromStation, String toStation);
}
```
The actual implementation for an object
```
public class TripAdvisor1 implements TripAdvisor {
@Override
public double getCheapestPrice(String fromStation, String toStation){
return 0;
}
}
```
Interface that every factory must implement
```
public interface TripAdvisorFactory {
public TripAdvisor getInstance();
}
```
Actual factory implementation
```
public class TripAdvisorFactoryImpl1 implements TripAdvisorFactory{
public TripAdvisor getInstance(){
return new TripAdvisor1();
}
}
```
Example of main:
```
public class Application {
// Notice that this class depends only on interfaces, and not on
// any specific implementation.
private TripAdvisorFactory factory;
public Application(TripAdvisorFactory factory){
this.factory = factory;
}
public void run(){
TripAdvisor advisor = factory.getInstance();
System.out.println("Now I can test the trip advisor ...");
double price = advisor.getCheapestPrice("Toronto", "Montreal");
System.out.println("A cheapest trip from Toronto to Montreal costs " + price + "$.");
}
}
```
Then we can simply create a new Application, pass it a specific factory implementation and tell it to run.
###Observer Design Pattern
Summary: If a different application needs to do something over take notice of stock price changes for instance, we should use this pattern. Just two components really, the class that responds to changes and a component to put in objects that will send off notifications.
Observer objects `extends Observable`. They call `this.notifyObservers();` to notify their observers. Objects wanting to watch this class use `implements Observer` and make an update method as such,
```
@Override
public void update(Observable observable, Object additionalArgument) {
Stock stock = (Stock) observable;
System.out.println("Stock price updated: " + stock);
System.out.println("Based on the price I can decide if I should buy/sell this stock.");
}
```
A full example of the main method,
```
public static void main(String[] args) {
Application application = new Application();
Stock s = new Stock("AMZN", new BigDecimal("296.52"));
s.addObserver(application);
s.setPrice(s.getPrice().multiply(new BigDecimal("1.02")));
s.setPrice(s.getPrice().multiply(new BigDecimal("0.97")));
}
```
So now when .setPrice() gets called, the update method in application will be called.
###Logging example
## Software Development
-------------------------------------
### The red flags of feature implementation
* Mutual Dependacies
* Keep coupling at a minimum.
* This means do not make modular components that are actual very reliant on each other.
* Classes with too many responsibilities
* Do not put functions in a class that make sense in a different class essentially.
### Scrum
Summary: A development process which focus's on small 'sprints' and prioritized items that are assigned 'time points'. Review and adjust at the end of every sprint and see what went right and what went wrong. Velocity is a big part to how a team can measure if its development is going well or not using this model.
* Split the work between small teams
* Split the work up into nice user stories
* Sprints
* Short periods of time where a team will focus on a specific set of user stories. Should define a single feature or a small subset of features. At the end of the sprint we should have something to show stock holders or the "boss"
* Daily Meetings
* Meetings that are done everyday to update team members on what everyone is working on and
* Sprint backlog
* Items to be done during the sprint
* Product Backlog
* Items to be done during the entire product development cycle
### Kaban!
Summary: A development process which uses a visual board to split up what everyone is working on and only a certain amount of tickets can be assigned to different phases.
* The board
* Different phases like implementing, testing, deploying
* More freedom
* Allows a team to use their own process types
* More lineant to things like feature ticketing
*
### MVP stuff
* Description
* What are users gonna do with this product
* Don't try to convince
* Tries to answer questions that are already prevalent, ones that exist in the world today.
### Things to create yourself before the exam
* A simple DAO example
* Factory design pattern
* Observer design pattern
* Logging design pattern
*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment