Sobes.tech
Junior

Which design pattern in Java can be used to implement object creation?

sobes.tech AI

Answer from AI

Design Pattern "Factory Method".

It defines an interface for creating an object, but leaves the decision of which class to instantiate to subclasses. It allows a class to delegate the creation of objects to subclasses.

// Product interface
interface Product {
    void use();
}

// Concrete Product A
class ConcreteProductA implements Product {
    @Override
    public void use() {
        System.out.println("Using ConcreteProductA");
    }
}

// Concrete Product B
class ConcreteProductB implements Product {
    @Override
    public void use() {
        System.out.println("Using ConcreteProductB");
    }
}

// Creator
abstract class Creator {
    // Factory method
    abstract Product factoryMethod();

    // Business logic using the product
    void doSomething() {
        Product product = factoryMethod();
        product.use();
    }
}

// Concrete Creator A
class ConcreteCreatorA extends Creator {
    @Override
    Product factoryMethod() {
        return new ConcreteProductA();
    }
}

// Concrete Creator B
class ConcreteCreatorB extends Creator {
    @Override
    Product factoryMethod() {
        return new ConcreteProductB();
    }
}

Pattern "Abstract Factory".

It provides an interface for creating families of related or dependent objects without specifying their concrete classes.

// Abstract Factory
interface AbstractFactory {
    ProductA createProductA();
    ProductB createProductB();
}

// Abstract Products
interface ProductA {
    void useA();
}

interface ProductB {
    void useB();
}

// Concrete Factory 1
class ConcreteFactory1 implements AbstractFactory {
    @Override
    public ProductA createProductA() {
        return new ConcreteProductA1();
    }

    @Override
    public ProductB createProductB() {
        return new ConcreteProductB1();
    }
}

// Concrete Factory 2
class ConcreteFactory2 implements AbstractFactory {
    @Override
    public ProductA createProductA() {
        return new ConcreteProductA2();
    }

    @Override
    public ProductB createProductB() {
        return new ConcreteProductB2();
    }
}

// Concrete products (omitted for brevity, e.g., ConcreteProductA1, ConcreteProductA2, etc.)
// class ConcreteProductA1 implements ProductA { ... }
// class ConcreteProductA2 implements ProductA { ... }
// class ConcreteProductB1 implements ProductB { ... }
// class ConcreteProductB2 implements ProductB { ... }

Pattern "Builder".

It separates the construction of a complex object from its representation so that the same construction process can create different representations.

// Class to be built
class ComplexObject {
    private String partA;
    private int partB;
    private boolean partC;

    // Private constructor
    private ComplexObject(Builder builder) {
        this.partA = builder.partA;
        this.partB = builder.partB;
        this.partC = builder.partC;
    }

    // Static inner Builder class
    static class Builder {
        private String partA;
        private int partB;
        private boolean partC;

        public Builder setPartA(String partA) {
            this.partA = partA;
            return this; // For chaining
        }

        public Builder setPartB(int partB) {
            this.partB = partB;
            return this;
        }

        public Builder setPartC(boolean partC) {
            this.partC = partC;
            return this;
        }

        // Method to build the final object
        public ComplexObject build() {
            // Validity checks can be added here
            return new ComplexObject(this);
        }
    }

    // Methods of ComplexObject...
}

Pattern "Prototype".

It specifies the kinds of objects to create using a prototypical instance and creates new objects by copying this prototype.

// Prototype interface
interface Prototype extends Cloneable {
    Prototype clone();
}

// Concrete Prototype
class ConcretePrototype implements Prototype {
    private String property;

    public ConcretePrototype(String property) {
        this.property = property;
    }

    public String getProperty() {
        return property;
    }

    @Override
    public Prototype clone() {
        try {
            // Shallow copy
            return (Prototype) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null; // Should be unreachable
        }
    }
}

Pattern "Singleton".

It guarantees that a class has only one instance and provides a global point of access to it.

class Singleton {
    // Static field to hold the instance
    private static Singleton instance;

    // Private constructor to prevent direct instantiation
    private Singleton() {
        // Initialization
    }

    // Static method to get the single instance
    public static Singleton getInstance() {
        if (instance == null) {
            // Lazy initialization (create on first access)
            // Add synchronization for thread safety in multithreaded environments
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }

    // Other methods of Singleton...
}
Which design pattern in Java can be used to implement… - sobes.tech