Textual User Interface
Graphical User Interface
Event in GUIS
a user-initiated action, such as a mouse click or keyboard input, which triggers a specific response or function within the graphical user interface
Main Event Loop
a continuous program structure that waits for and dispatches user input events, updating the graphical interface and executing corresponding functions to provide a responsive and interactive user experience
Event Handlers
functions or routines that are programmed to respond to specific user-initiated events, such as mouse clicks or keyboard inputs, by executing predetermined actions or processes within the graphical user interface
Listeners
an object or function that is programmed to detect and respond to specific events triggered by user interactions with graphical components, such as buttons or text fields
to handle events, a listener must implement the relevant listener interface(s) and must be registered with the component
Different Components of GUIs
frames - the top-level containers in a GUI, serving as the main application window that holds other components
panels - containers used to organize and group other components within a GUI, helping to structure the layout
labels - used to display text or images, providing descriptive information in the GUI
buttons - interactive elements that users can click to trigger predefined actions or functions in the application
- check boxes - allow users to make binary choices (selected or unselected) by toggling a checkbox
text fields - areas where users can input and edit single lines of text, such as entering names or numbers
text areas - larger input areas that allow users to enter and edit multiple lines of text, suitable for longer pieces of information or messages
Handling Button Events
Replacing functionality at runtime through GUI
Can make use of checkboxes, using a switch statement based on the values of the these checkboxes to decide which of the methods will be called.
Suppose you have a generator that takes all of the outputs of a method and does something with them. If we would like to have the possibility of choosing what to do them in real-time based on GUI user-inputs, what could be some solutions?
Observer Design Pattern
behavioural design pattern where an object, known as the subject, maintains a list of dependents, called observers, that are notified of any changes in the subject’s state
Components of Observer Design Pattern
Code Example for Observer Design Pattern
// Subject interfaceinterface Subject { void registerObserver(Observer observer); void removeObserver(Observer observer); void notifyObservers();}// Observer interfaceinterface Observer { void update(String message);}// Concrete subjectclass ConcreteSubject implements Subject { private List observers = new ArrayList<>(); private String state; public void setState(String newState) { this.state = newState; notifyObservers(); } @Override public void registerObserver(Observer observer) { observers.add(observer); } @Override public void removeObserver(Observer observer) { observers.remove(observer); } @Override public void notifyObservers() { for (Observer observer : observers) { observer.update(state); } }}// Concrete observerclass ConcreteObserver implements Observer { private String name; public ConcreteObserver(String name) { this.name = name; } @Override public void update(String message) { System.out.println(name + “ received an update: “ + message); }}
Client Code Example for Observer Design Pattern
// Client codepublic class Client { public static void main(String[] args) { // Creating a concrete subject ConcreteSubject subject = new ConcreteSubject(); // Creating concrete observers Observer observer1 = new ConcreteObserver(“Observer 1”); Observer observer2 = new ConcreteObserver(“Observer 2”); // Registering observers with the subject subject.registerObserver(observer1); subject.registerObserver(observer2); // Updating the state of the subject, which triggers notification to observers subject.setState(“New State”); }}
Facade Design Pattern
a design pattern looking to provide a simplified and unified interface to a set of interfaces or complex subsystem by creating a single, straightforward entry point for clients
Components of Facade Design Pattern
Code Example for Facade Design Pattern
// Subsystem componentsclass CPU { void start() { System.out.println(“CPU is starting”); } void execute() { System.out.println(“CPU is executing”); }}class Memory { void load() { System.out.println(“Memory is loading”); }}class HardDrive { void read() { System.out.println(“Hard Drive is reading”); }}// Facade classclass ComputerFacade { private CPU cpu; private Memory memory; private HardDrive hardDrive; public ComputerFacade() { this.cpu = new CPU(); this.memory = new Memory(); this.hardDrive = new HardDrive(); } // Facade method to start the computer public void startComputer() { cpu.start(); memory.load(); hardDrive.read(); cpu.execute(); System.out.println(“Computer started successfully”); }}
Client Code for Facade Design Pattern
// Client codepublic class Client { public static void main(String[] args) { // Using the ComputerFacade to start the computer ComputerFacade computerFacade = new ComputerFacade(); computerFacade.startComputer(); }}
Adapter Design Pattern
structural design pattern that allows the interface of an existing class to be used as another interfacei.e., enables classes with incompatible interfaces to work together by providing a wrapper/adapter that converts one interface into another, making the object compatible
Components of Adapter Design Pattern
Code Example for Adapter Design Pattern
// Target interfaceinterface Target { void request();}// Adaptee (existing class)class Adaptee { void specificRequest() { System.out.println(“Adaptee’s specificRequest called”); }}// Adapter class implementing the Target interfaceclass Adapter implements Target { private Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; } @Override public void request() { adaptee.specificRequest(); }}
Client Code for Adapter Design Pattern
Adaptee adaptee = new Adaptee();Target adapter = new Adapter(adaptee);adapter.request(); // calls a method of adaptee
Decorator Design Pattern
structural design pattern that allows behaviour to be added to an individual object, either statically or dynamically, without affecting the behaviour of other objects from the same classused to extend or augment the behaviour of objects in a flexible and reusable way, providing an alternative to subclassing for extending functionality
Components of Decorator Design Pattern