This tutorial aims to guide you on how to effectively use Structural Patterns in Java. By the end of this guide, you'll understand how to organize different classes and objects to form larger structures and provide new functionality using these patterns.
Structural Patterns provide a manner to define relationships between classes or objects so that they can work together to achieve a common goal. They simplify the structure by identifying relationships.
The Adapter Pattern works as a bridge between two incompatible interfaces. It involves a single class which is responsible to join functionalities of independent or incompatible interfaces.
// Existing way requests are implemented
class OldSystem {
public void oldRequest() {
System.out.println("Old System");
}
}
// New interface
interface NewSystem {
void newRequest();
}
// Adapter Wrapper class
class Adapter implements NewSystem {
private OldSystem oldSystem;
public Adapter(OldSystem oldSystem) {
this.oldSystem = oldSystem;
}
@Override
public void newRequest() {
oldSystem.oldRequest();
}
}
// Using the new system (with adapter)
public class Main {
public static void main(String[] args) {
NewSystem newSystem = new Adapter(new OldSystem());
newSystem.newRequest(); // Outputs: "Old System"
}
}
The Composite Pattern is used where we need to treat a group of objects in a similar way as a single object. It composes objects in term of a tree structure to represent part as well as whole hierarchies.
import java.util.ArrayList;
import java.util.List;
// Component
interface Employee {
void showEmployeeDetails();
}
// Leaf
class Developer implements Employee {
private String name;
public Developer(String name) {
this.name = name;
}
@Override
public void showEmployeeDetails() {
System.out.println("Developer: " + name);
}
}
// Composite
class Team implements Employee {
private List<Employee> employeeList = new ArrayList<Employee>();
@Override
public void showEmployeeDetails() {
for(Employee emp:employeeList) {
emp.showEmployeeDetails();
}
}
public void addEmployee(Employee emp) {
employeeList.add(emp);
}
}
// Using composite pattern
public class Main {
public static void main(String[] args) {
Employee dev1 = new Developer("John Doe");
Employee dev2 = new Developer("Jane Doe");
Team team = new Team();
team.addEmployee(dev1);
team.addEmployee(dev2);
team.showEmployeeDetails();
// Outputs: "Developer: John Doe", "Developer: Jane Doe"
}
}
We have discussed two Structural Patterns - Adapter and Composite. Now let's dive into more examples.
The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.
interface Image {
void display();
}
class RealImage implements Image {
private String filename;
public RealImage(String filename) {
this.filename = filename;
loadFromDisk();
}
private void loadFromDisk() {
System.out.println("Loading " + filename);
}
@Override
public void display() {
System.out.println("Displaying " + filename);
}
}
class ProxyImage implements Image {
private RealImage realImage;
private String filename;
public ProxyImage(String filename) {
this.filename = filename;
}
@Override
public void display() {
if (realImage == null) {
realImage = new RealImage(filename);
}
realImage.display();
}
}
// Using Proxy Pattern
public class Main {
public static void main(String[] args) {
Image image = new ProxyImage("test.jpg");
// Image will be loaded from disk
image.display();
// Image will not be loaded from disk
image.display();
}
}
Remember, the key to mastering these patterns is through continuous practice. Try to use these patterns in your regular coding tasks where necessary. Take part in coding challenges and try to implement these patterns. Happy coding!