Mastering Java Design Patterns - Day 3: Abstract Factory Pattern

Mastering Java Design Patterns - Day 3: Abstract Factory Pattern

Hello everyone! Welcome back to our journey through Java Design Patterns. So far, we've covered the Singleton and Factory Method Patterns. Today, we’ll explore the Abstract Factory Pattern.

What is the Abstract Factory Pattern?

The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is particularly useful when a system needs to be independent of how its objects are created.

Why Use the Abstract Factory Pattern?

  • Consistency: It ensures that products created by the factory are compatible with each other.
  • Decoupling: It decouples the client code from the implementation details of the product families.
  • Scalability: It makes it easy to introduce new families of products without altering existing code.

How to Implement the Abstract Factory Pattern in Java

Here's a simple implementation of the Abstract Factory Pattern:

// Abstract Product

interface Chair {
    void sitOn();
}

interface Table {
    void eatOn();
}        
// Concrete Products

class VictorianChair implements Chair {
    public void sitOn() {
        System.out.println("Sitting on a Victorian Chair");
    }
}

class VictorianTable implements Table {
    public void eatOn() {
        System.out.println("Eating on a Victorian Table");
    }
}

class ModernChair implements Chair {
    public void sitOn() {
        System.out.println("Sitting on a Modern Chair");
    }
}

class ModernTable implements Table {
    public void eatOn() {
        System.out.println("Eating on a Modern Table");
    }
}        
// Abstract Factory

interface FurnitureFactory {

    Chair createChair();

    Table createTable();

}        
// Concrete Factories

class VictorianFurnitureFactory implements FurnitureFactory {

    public Chair createChair() {
        return new VictorianChair();
    }

    public Table createTable() {
        return new VictorianTable();
    }
}

class ModernFurnitureFactory implements FurnitureFactory {

    public Chair createChair() {
        return new ModernChair();
    }

    public Table createTable() {
        return new ModernTable();
    }
}        
// Client code

public class AbstractFactoryPatternDemo {

    public static void main(String[] args) {

        FurnitureFactory victorianFactory = new VictorianFurnitureFactory();

        Chair victorianChair = victorianFactory.createChair();

        Table victorianTable = victorianFactory.createTable();

        victorianChair.sitOn();

        victorianTable.eatOn();

        FurnitureFactory modernFactory = new ModernFurnitureFactory();

        Chair modernChair = modernFactory.createChair();

        Table modernTable = modernFactory.createTable();

        modernChair.sitOn();

        modernTable.eatOn();

    }
}        

Discussion:

Have you found the Abstract Factory Pattern useful in your projects? How did it help in managing object creation and ensuring consistency? Share your thoughts and examples in the comments below!

Engage with these posts to foster a collaborative learning environment for everyone. Looking forward to your contributions!

Stay tuned for tomorrow's topic: Builder Pattern.

#ehadjistratis #Java #DesignPatterns #AbstractFactoryPattern #Programming #Coding #SoftwareDevelopment #LearningJourney #JuniorDevelopers #TechCommunity


Let's Connect!

As we strive to improve and refine our coding skills, sharing knowledge and experiences becomes invaluable. Let's build a strong community of Java developers who support each other's growth. If you enjoyed these articles or have insights to share, please comment, like, and repost!

Don't forget to follow for more tips and discussions on Java development. Together, we can elevate our skills and contribute to a more robust developer ecosystem. #ehadjistratis #JavaCommunity #TechGrowth #KnowledgeSharing #DeveloperEcosystem


要查看或添加评论,请登录

Emmanuel Hadjistratis (he/him)的更多文章

社区洞察