Objectives

Students will be able to:

  • define a class file storing properties
  • define a constructor method
  • create an instance of an object(s) in a computer program
  • Design a UML Diagram

public class AdminController{

public AdminController() {

System.out.println("Admin system initialized.");

}

public void checkFuelLevel(double level) {

System.out.println("Fuel level: " + level + "%");

if (level < 15) {

sendAlert("Warning: Low fuel level!");

}

}

public void checkCabinPressure(double pressure) {

System.out.println("Cabin pressure reading: " + pressure);

if (pressure < 70) {

sendAlert("Cabin pressure too low! Deploy oxygen masks.");

}

}

private void sendAlert(String message) {

System.out.println("ADMIN ALERT: " + message);

}

}

Object Oriented Programming

Singletons

 

Scenario

An airplane’s onboard software system includes an AdminController object responsible for monitoring:

  • fuel levels
  • navigation status
  • cabin pressure
  • emergency alerts

Multiple subsystems (navigation, engines, cabin sensors, cockpit display) must access this controller to report data or request decisions.

The system must guarantee that only one AdminController instance exists at any time.

What problems might occur if two AdminController objects exist?

Which object would the sensors talk to? Both?

Could conflicting decisions be made?

Click the button to see the AdminController class.

Singletons

In software design, when we need to guarantee exactly one instance of a class and provide global access to it, we use a pattern called the singleton.

Task 1

Look again at the AdminController class above. There is no way to stop other classes creating AdminController instances.

Hance, more than 1 instance can exist, which is undesireable in this scenario.

The following video will introduce the ingenious way that a single instance of a class can be created and have global access to all other parts of the program/system.

Complete the worksheet as you follow the video.

Glossary

singleton

static

class variable

private

data hiding

encapsulation/p>

instance