OOP2

View.java

1
import java.util.Observer; // The interface that will be implemented.
2
import java.util.Observable; // Required to have a private member of type Observable.
3
//import java.lang.NullPointerException; // Required to throw said exception.
4
5
//class UpdateNotImplementedException extends Exception {}
6
7
// NOTE: This is a medium between the java core stuff and the implementation of the view of this program. It handles the boilerplate that is the same in all other view implementations.
8
9
public abstract class View implements Observer { // This is an abstract class. See note above for further information.
10
		// m_model - Member: The model that is being observed. This class must inherit from Observable.
11
		private Observable m_model;
12
		public Observable model() {
13
				if(m_model == null) // The model must be specified. If not, initiate core meltdown:
14
						throw new NullPointerException("No model was specified.");
15
				else
16
						return m_model;
17
		}
18
		public void setModel(Observable model) {
19
				m_model = model;
20
		}
21
22
		// m_controller - Member: Represents the controlling class. This class must inherit from Controller.
23
		private Controller m_controller;
24
		public Controller controller() {
25
				if(m_controller == null) // The programmer has refused to use a controller. Thee shall be punished with ye exception.
26
						throw new NullPointerException("No controller was specified.");
27
				else
28
						return m_controller;
29
		}
30
		public void setController(Controller controller) {
31
				m_controller = controller;
32
		}
33
34
		// INFO: The given empty constructor is there because one sometimes can't give all required values up front. Abuse of this function will not be tolerated (See getters in this interface). Use with caution!
35
		/*public viewTemplate() {
36
			System.out.println("Empty declaration of viewTemplate."); // Printing a notification to the console. It makes debugging easier.
37
		}*/
38
		// Commented out the empty constructor, since it can be easily integrated in the default constructor without any hassle.
39
		public View(Observable model, Controller controller) { // The default constructor.
40
				// Generating notifications if no model and/or controller are given yet:
41
				if(model == null)
42
						System.out.println("In viewTemplate default constructor: model was a null pointer.");
43
				if(controller == null)
44
						System.out.println("In viewTemplate default constructor: controller was a null pointer.");
45
46
				setModel(model);
47
				setController(controller);
48
		}
49
50
		// NOTE: Implementing Observer requires one to implement update(). The reason I do that here, is because my compiler will otherwise commit suicide. But, this will only print a notification that the given update has had no effect whatsoever on the class (This is an abstract class after all).
51
		public void update(Observable observable, Object args) {
52
				System.out.println("update() was called, but no implementation was found.");
53
		}
54
}
55