Professional Documents
Culture Documents
Patterns
Patterns
12. Patterns
3/31/2018
John Roberts
2
Overview
• Singleton
• Observer/Observable
3
Singleton
• Clients may only use the access function to manipulate the Singleton
• Github
5 https://sourcemaking.com/design_patterns/singleton/java/1
Singleton
4 public class Singleton {
5 private int counter = 0;
6
8 private Singleton() {
9 System.out.println( "Singleton created" );
10 }
11
12 public int getCounter() {
13 return this.counter;
14 }
15
16 public void incrementCounter() {
17 this.counter++;
18 }
19
20 /**
21 * SingletonHolder is loaded on the first execution of Singleton.getInstance()
22 * or the first access to SingletonHolder.INSTANCE, not before.
23 */
24 private static class SingletonHolder {
25 private static final Singleton INSTANCE = new Singleton();
26 }
27
28 public static Singleton getInstance() {
29 return SingletonHolder.INSTANCE;
30 }
31 }
6
Test
1 public class Main {
2 public static void main( String[] args ) {
3 // This is illegal
4 // Singleton illegal = new Singleton();
5 // Main.java:4: error: Singleton() has private access in Singleton
6
7 Singleton first = Singleton.getInstance();
8 System.out.println( "First count is " + first.getCounter() );
9
10 for( int i = 0; i < 5; i ++ ) {
11 first.incrementCounter();
12 }
13 System.out.println( "First count is " + first.getCounter() );
14
15 Singleton second = Singleton.getInstance();
16 System.out.println( "Second count is " + second.getCounter() );
17 }
18 }
7 https://github.com/sfsu-csc-413-roberts/lecture-12/tree/master/singleton
Output
ᐅ make
javac Main.java; java Main
Singleton created
First count is 0
First count is 5
Second count is 5
8
Benefits
9
Singleton Pattern
• Global state
11
Overview
• Singleton
• Observer/Observable
12
Observer Pattern
• When the professor modifies the course website, they send a mail to
the students on the mailing list.
• The notification tells the student which professor sent the message,
so that they know which website to look at.
• The student can then access the page to read the information.
14
Observer
15
Subject (Observable)
1 import java.util.ArrayList;
2
3 public class Observable {
4 private ArrayList<Observer> observers = new ArrayList<>();
5
6 public void addObserver( Observer observer ) {
7 observers.add( observer );
8 }
9
10 public void removeObserver( Observer observer ) {
11 observers.remove( observer );
12 }
13
14 public void notifyObservers() {
15 for( Observer observer : observers ) {
16 observer.update( this );
17 }
18 }
19 }
17
IncrementObservable Concrete Class
18
Observer Interface,
IncrementObserver Concrete Class
1 public interface Observer {
2 public void update( Observable observable );
3 }
20
Output
ᐅ make
javac Main.java; java Main
Blarg 0 observer sees 1
Blarg 1 observer sees 1
Blarg 2 observer sees 1
Blarg 3 observer sees 1
Blarg 4 observer sees 1
Blarg 0 observer sees 2
Blarg 1 observer sees 2
Blarg 2 observer sees 2
Blarg 3 observer sees 2
Blarg 4 observer sees 2
** I just incremented twice.
Blarg 0 observer sees 3
Blarg 1 observer sees 3
Blarg 2 observer sees 3
Blarg 3 observer sees 3
Blarg 4 observer sees 3
21
Java Implementations
• java.util.Observable (class)
• java.util.Observer (interface)
23
Overview
• Singleton
• Observer/Observable
24
MVC Pattern
26
View
27
Controller
Controller
29
MVC Pattern
30
Benefits
• Loosely Coupled