Lesson 5
Applying Object-Oriented Design Patterns in Java
Lesson Introduction

Hello! Today, we'll venture into the realm of design patterns. Specifically, we'll tackle exercises that apply a single design pattern to problem-solving. Mastering these patterns is a surefire way to extend your coding skills.

Our goal today is to fortify your understanding of when and how to apply specific Object-Oriented Programming (OOP) design patterns. These patterns include Encapsulation, Abstraction, Polymorphism, and Composition.

We'll dissect four real-life scenarios and distinguish which pattern is applicable and why.

Let's get underway!

Real-life Example 1: Database Management System (Encapsulation)

The Encapsulation pattern proves beneficial for the development of a Database Management System (DBMS). Each DBMS table represents a class, the fields represent private data members, and the functions operating on this data serve as methods.

Encapsulation ensures that data members are accessed through methods that promote data integrity and prevent inadvertent anomalies. Here's a mini-code snippet to support this concept:

Java
1class Employees { 2 private Map<Integer, String> employees = new HashMap<>(); // private data member 3 4 public void addEmployee(int eid, String name) { // method to operate on private data 5 employees.put(eid, name); 6 } 7 8 public void updateEmployee(int eid, String newName) { // method to operate on private data 9 if (employees.containsKey(eid)) { 10 employees.put(eid, newName); 11 } 12 } 13 14 public String getEmployee(int eid) { // getter method for private data 15 return employees.get(eid); 16 } 17} 18 19public class Main { 20 public static void main(String[] args) { 21 Employees employees = new Employees(); 22 employees.addEmployee(1, "John"); 23 employees.addEmployee(2, "Mark"); 24 25 employees.updateEmployee(2, "Jake"); 26 27 System.out.println(employees.getEmployee(1)); // Outputs: John 28 System.out.println(employees.getEmployee(2)); // Outputs: Jake 29 } 30}

In this context, Encapsulation restricts direct access to employee data, presenting a protective layer via designated methods.

Real-life Example 2: Graphic User Interface (GUI) Development (Polymorphism)

When transitioning to GUI development, consider the creation of controls like buttons or checkboxes. Despite belonging to the same class, each responds differently when clicked. This situation illustrates Polymorphism, which allows us to handle different objects uniformly via a common interface.

Check out this illustrative example:

Java
1class Control { 2 public void click() { 3 // method that can be overridden 4 } 5} 6 7class Button extends Control { 8 @Override 9 public void click() { 10 System.out.println("Button Clicked!"); // overridden method 11 } 12} 13 14class CheckBox extends Control { 15 @Override 16 public void click() { 17 System.out.println("CheckBox Clicked!"); // overridden method 18 } 19} 20 21public class Main { 22 public static void main(String[] args) { 23 Control b = new Button(); 24 Control c = new CheckBox(); 25 26 // Click Controls 27 b.click(); // Outputs: Button Clicked! 28 c.click(); // Outputs: CheckBox Clicked! 29 } 30}

Despite sharing the common click interface, different controls display unique responses. This characteristic demonstrates Polymorphism.

Real-life Example 3: Creating a Web Page Structure (Composition)

Let's explore the Composition design pattern through a Java approach to create a simple web page structure. Here, we'll build a fundamental structure representing a webpage composed of various elements like headers, paragraphs, and lists. This abstraction allows us to understand how composite objects work together to form a larger system.

Java
1import java.util.ArrayList; 2import java.util.List; 3 4interface WebPageElement { 5 String render(); 6} 7 8class Header implements WebPageElement { 9 private String text; 10 11 public Header(String text) { 12 this.text = text; 13 } 14 15 @Override 16 public String render() { 17 return "<h1>" + text + "</h1>"; 18 } 19} 20 21class Paragraph implements WebPageElement { 22 private String text; 23 24 public Paragraph(String text) { 25 this.text = text; 26 } 27 28 @Override 29 public String render() { 30 return "<p>" + text + "</p>"; 31 } 32} 33 34class ListElement implements WebPageElement { 35 private List<String> items; 36 37 public ListElement(List<String> items) { 38 this.items = items; 39 } 40 41 @Override 42 public String render() { 43 StringBuilder itemsStr = new StringBuilder(); 44 for (String item : items) { 45 itemsStr.append("<li>").append(item).append("</li>"); 46 } 47 return "<ul>" + itemsStr.toString() + "</ul>"; 48 } 49} 50 51class WebPage { 52 private String title; 53 private List<WebPageElement> elements; 54 55 public WebPage(String title) { 56 this.title = title; 57 this.elements = new ArrayList<>(); 58 } 59 60 public void addElement(WebPageElement element) { 61 elements.add(element); 62 } 63 64 public String display() { 65 StringBuilder elementsStr = new StringBuilder(); 66 for (WebPageElement element : elements) { 67 elementsStr.append(element.render()).append("\n"); 68 } 69 return "<html>\n<head>\n <title>" + title + "\n</title>\n</head>\n<body>\n " + elementsStr.toString() + "\n</body>\n</html>"; 70 } 71} 72 73public class Main { 74 public static void main(String[] args) { 75 WebPage page = new WebPage("My Web Page"); 76 page.addElement(new Header("Welcome to My Web Page")); 77 page.addElement(new Paragraph("This is a paragraph of text on the web page.")); 78 List<String> items = new ArrayList<>(); 79 items.add("Item 1"); 80 items.add("Item 2"); 81 items.add("Item 3"); 82 page.addElement(new ListElement(items)); 83 84 System.out.println(page.display()); 85 /* 86 Outputs: 87 <html> 88 <head> 89 <title>My Web Page 90 </title> 91 </head> 92 <body> 93 <h1>Welcome to My Web Page</h1> 94 <p>This is a paragraph of text on the web page.</p> 95 <ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul> 96 </body> 97 </html> 98 */ 99 } 100}

In this code, we've designed a web page structure using the Composition design pattern. Each web page element (Header, Paragraph, and ListElement) is a WebPageElement, allowing for unified handling while maintaining their specific behaviors (rendering as HTML elements).

The WebPage class acts as a composite object that can contain an arbitrary number of WebPageElement instances, each representing different parts of a web page. By adding these elements to the WebPage and invoking the display method, we dynamically compose a complete web page structure in HTML format.

Real-life Example 4: Creating a Vehicle (Abstraction)

Consider creating a Vehicle class in Java. Here, Abstraction comes into play. You expose only the necessary functionality and abstract away the internal workings of the Vehicle.

Let's see this in code:

Java
1abstract class Vehicle { 2 protected String color; 3 protected String engineType; 4 protected boolean engineRunning; 5 6 public Vehicle(String color, String engineType) { 7 this.color = color; 8 this.engineType = engineType; 9 this.engineRunning = false; 10 } 11 12 public abstract void startEngine(); 13 public abstract void stopEngine(); 14 public abstract void drive(); 15} 16 17class Car extends Vehicle { 18 public Car(String color, String engineType) { 19 super(color, engineType); 20 } 21 22 @Override 23 public void startEngine() { 24 engineRunning = true; 25 System.out.println("Car engine started!"); 26 } 27 28 @Override 29 public void stopEngine() { 30 engineRunning = false; 31 System.out.println("Car engine stopped!"); 32 } 33 34 @Override 35 public void drive() { 36 if (engineRunning) { 37 System.out.println(this.color + " car is driving on the " + this.engineType + " engine type!"); 38 } else { 39 System.out.println("Start the engine first!"); 40 } 41 } 42} 43 44public class Main { 45 public static void main(String[] args) { 46 Car car = new Car("red", "gasoline"); 47 car.startEngine(); 48 car.drive(); 49 /* 50 Output: 51 Car engine started! 52 Car is driving! 53 */ 54 } 55}

Here, the Vehicle abstract class exposes relevant and necessary functions such as startEngine(), stopEngine(), and drive(), while the Car class implements this abstract class and provides concrete implementations. However, it hides or abstracts away internal state management (engineRunning). This is a basic instance of Abstraction, which simplifies the interaction with the class and hides underlying complexity.

Design Pattern Identification

Let's recap the major OOP patterns:

  • Encapsulation: This pattern confines data and related methods into one unit, veiling direct data access.
  • Abstraction: This pattern offers a simplified interface, cloaking complexity.
  • Polymorphism: This pattern facilitates treating different objects as related objects of a common superclass.
  • Composition: This pattern builds elaborate systems by composing closely related objects.

Reflect on these principles and practice applying them to a variety of scenarios to better recognize suitable patterns.

Lesson Summary

Great job! You've poked and prodded at the practical applications of OOP design patterns. We've explored the use of Encapsulation in Database Management Systems, the pivotal role of Polymorphism in GUI development, the importance of Composition when designing a web page builder, and how Abstraction helps to build a vehicle structure.

Next up are hands-on exercises to reinforce these concepts. Remember, practice is the master key to understanding these concepts. So keep coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.