1.
Constructor Pattern
In Java, constructors are a fundamental part of class definition, initializing instance variables with parameters.
public class Person {
private String name;
private int age;
private boolean isDeveloper;
public Person(String name, int age, boolean isDeveloper) {
[Link] = name;
[Link] = age;
[Link] = isDeveloper;
}
public void writesCode() {
[Link](isDeveloper ? "this person writes code" : "this person does not write code");
}
public static void main(String[] args) {
Person person1 = new Person("Ana", 32, true);
Person person2 = new Person("Bob", 36, false);
[Link](); // prints: this person writes code
[Link](); // prints: this person does not write code
}
}
2. Module Pattern
Java lacks closures, so encapsulation is achieved using private fields and public methods, often with a singleton-like approach.
public class FruitCollection {
private List<String> objects = new ArrayList<>();
private FruitCollection() {} // Private constructor for encapsulation
public static FruitCollection getInstance() {
return [Link];
}
private static class SingletonHolder {
private static final FruitCollection INSTANCE = new FruitCollection();
}
public void addObject(String object) {
[Link](object);
}
public void removeObject(String object) {
[Link](object);
}
public List<String> getObjects() {
return new ArrayList<>(objects); // Return a copy to prevent modification
}
1
public static void main(String[] args) {
FruitCollection collection = [Link]();
[Link]("apple");
[Link]("orange");
[Link]([Link]()); // prints: [apple, orange]
}
}
3. Revealing Module Pattern
In Java, this can be simulated with a class exposing specific methods while hiding implementation details.
public class FruitCollection {
private List<String> objects = new ArrayList<>();
private void addObject(String object) {
[Link](object);
}
private void removeObject(String object) {
[Link](object);
}
private List<String> getObjects() {
return new ArrayList<>(objects);
}
public static class PublicInterface {
public static void addName(FruitCollection collection, String object) {
[Link](object);
}
public static void removeName(FruitCollection collection, String object) {
[Link](object);
}
public static List<String> getNames(FruitCollection collection) {
return [Link]();
}
}
public static void main(String[] args) {
FruitCollection collection = new FruitCollection();
[Link](collection, "Bob");
[Link](collection, "Alice");
[Link]([Link](collection)); // prints: [Bob, Alice]
}
}
4. Singleton Pattern
Java’s standard singleton pattern uses a static inner class for lazy initialization.
public class ConfigurationSingleton {
private int number = 5;
private int size = 10;
2
private double randomNumber = [Link]();
private ConfigurationSingleton() {}
public static ConfigurationSingleton getInstance() {
return [Link];
}
private static class SingletonHolder {
private static final ConfigurationSingleton INSTANCE = new ConfigurationSingleton();
}
public void initialize(int number, int size) {
[Link] = number;
[Link] = size;
}
@Override
public String toString() {
return "ConfigurationSingleton{number=" + number + ", size=" + size + ", randomNumber=" + randomNumber + "}";
}
public static void main(String[] args) {
ConfigurationSingleton config1 = [Link]();
[Link](8, 10);
[Link](config1); // prints: ConfigurationSingleton{number=8, size=10, randomNumber=someValue}
ConfigurationSingleton config2 = [Link]();
[Link](config2); // prints same instance
}
}
5. Observer Pattern (Publisher/Subscriber)
Java provides the Observer and Observable classes, or we can implement a custom version.
import [Link];
import [Link];
public class PublisherSubscriber {
static class Subscriber {
private String id;
private Consumer<Object> callback;
public Subscriber(String id, Consumer<Object> callback) {
[Link] = id;
[Link] = callback;
}
public void notify(Object data) {
[Link](data);
}
}
private List<Subscriber> subscribers = new ArrayList<>();
3
public void subscribe(String id, Consumer<Object> callback) {
[Link](new Subscriber(id, callback));
}
public void unsubscribe(String id) {
[Link](sub -> [Link](id));
}
public void publish(Object data) {
for (Subscriber sub : new ArrayList<>(subscribers)) {
[Link](data);
}
}
public static void main(String[] args) {
PublisherSubscriber pubSub = new PublisherSubscriber();
[Link]("sub1", data -> [Link]("Event: " + data));
[Link]("Hello"); // prints: Event: Hello
[Link]("sub1");
}
}
6. Mediator Pattern
A mediator class coordinates interactions between components.
import [Link];
import [Link];
public class Mediator {
private Map<String, Step> steps = new HashMap<>();
public void register(String name, Step step) {
[Link](name, step);
}
public void next(String currentStep) {
Step step = [Link](currentStep);
if (step != null && [Link]() != null) {
[Link]().run();
}
}
interface Step {
Runnable getNext();
}
public static void main(String[] args) {
Mediator mediator = new Mediator();
Step step1 = () -> [Link]("Step 2");
[Link]("step1", () -> [Link]());
[Link]("step1"); // prints: Step 2
}
}
4
7. Prototype Pattern
Java uses cloning or prototype instantiation for this pattern.
public class Person implements Cloneable {
private String name;
private int age;
public Person(String name, int age) {
[Link] = name;
[Link] = age;
}
public void sayHi() {
[Link]("Hi, I am " + name + " and I have " + age);
}
@Override
protected Object clone() throws CloneNotSupportedException {
return [Link]();
}
public static void main(String[] args) throws CloneNotSupportedException {
Person person1 = new Person("John Doe", 26);
Person person2 = (Person) [Link]();
[Link](); // prints: Hi, I am John Doe and I have 26
}
}
8. Command Pattern
Encapsulates requests as objects.
interface Command {
void execute();
}
class Calculator {
public int add(int x, int y) { return x + y; }
public int subtract(int x, int y) { return x - y; }
}
class CommandManager {
private Calculator calculator = new Calculator();
public void execute(String command, int... args) {
switch (command) {
case "add" -> [Link]([Link](args[0], args[1]));
case "subtract" -> [Link]([Link](args[0], args[1]));
default -> [Link]("Command not found");
}
}
}
public class Main {
public static void main(String[] args) {
5
CommandManager manager = new CommandManager();
[Link]("add", 3, 5); // prints: 8
[Link]("subtract", 5, 3); // prints: 2
}
}
9. Facade Pattern
Simplifies a complex subsystem.
import [Link];
public class Facade {
private DOMSelector selector = new DOMSelector();
public List<Element> select(String query) {
return [Link](query);
}
}
class DOMSelector {
public List<Element> query(String query) {
// Simulated complex logic
return [Link](new Element());
}
}
class Element {}
public class Main {
public static void main(String[] args) {
Facade facade = new Facade();
List<Element> elements = [Link](".parent .child");
[Link]([Link]()); // prints: 1 (simulated)
}
}
These Java implementations adapt the JavaScript patterns to Java’s paradigm, ensuring functionality while respecting language
constraints.