COMP2511 Week 8

Adgenda

  • Admin Stuff
  • Generics
  • Singleton Pattern

Admin Stuff

  • Ass 2 is due Week 9 Friday (28th July)
  • Ass1 interviews will continue today

Generics

Generics

What is it?

Style of programming that allow types to be used as a parameter to methods, classes and interfaces.

Generics

What is it?

Style of programming that allow types to be used as a parameter to methods, classes and interfaces.

Why would you use it?

  • Allow one implementation to work for a collection of different types
  • Offer strong type checking at compile time

Generics

public class Box<T> {
    
    private T value; 
    
    public Box(T value) {
        this.value = value;
    }
    
    public void setValue(T value) {
        this.value = value;
    }
    
    public T getValue() {
        return value;
    }
}
public class Box<T, S> {
    
    private T value1; 
    private S value2; 
    
    public Box(T value1, S value2) {
        this.value1 = value1;
        this.value2 = value2;
    }
    
    public void setValue(T value1, S value2) {
        this.value1 = value1;
        this.value2 = value2;
    }
    
    public T getValue1() {
        return value1;
    }
    
    public S getValue2() {
        return value2;
    }
}

Generics

Inside src/stack, there are a series of stubs for a Stack class which takes in a generic type. There are a series of tests inside StackTest.java which currently fail.

 

Implement the methods so that the tests pass, using an ArrayList to store the internal data structure.

Generics

What methods does it force us to implement?

  • Forces us to implement the .iterator() method

Why does the Iterable interfacehas an E as well?

  • .next() returns elements of type E

What is the Iterable interface​​​​​​​

  • Defines something that can be looped over

Generics

public static Integer sumStack(Stack<? extends Integer> stack);
  • What does the <? extends Type> and
    <? super Type> mean?
  • What is the difference between ? and E?
  • extends: the parameterized type must be the type or subclass of the given type
  • super: the parameterized type must be the type or superclass of the given type
  • ? can't be referred to as a type (Type erasure)

Singleton Pattern

Singleton Pattern

What is this?

Creational design pattern that specifies that a class can only have one instance. Accessing the instance must be via a global access point.

 

Why use it?

  • Control access to some shared resource. i.e. a file.
  • Need global variables that would not be overwritten 

 

Singleton Pattern

The instance is only created when the getInstance method is first called.

Singleton Pattern

Singleton Pattern

Currently, the threads can all access the bank account at the same time. This can lead to undesirable behaviour like

 

Denver is accessing the bank.
Tokyo is accessing the bank.
The final balance is: $100
Rio is accessing the bank.
Rio successfully withdrew $20
Tokyo successfully withdrew $6
Denver successfully withdrew $49
Tokyo successfully withdrew $6
Denver failed to withdraw $49.
Rio successfully withdrew $20
Rio failed to withdraw $20.
Denver is leaving the bank, the balance is -1
Tokyo failed to withdraw $6.
Rio failed to withdraw $20.
Tokyo failed to withdraw $6.
Rio failed to withdraw $20.
Tokyo failed to withdraw $6.
Tokyo failed to withdraw $6.
Rio is leaving the bank, the balance is -1
Tokyo is leaving the bank, the balance is -1

Bank account only had $100 but $101 was taken out!

Singleton Pattern

The undesired behaviour is caused race conditions

 

Refactor using the Singleton Pattern to ensure only one person can access the bank account at a time!

 

The End

Made with Slides.com