Abstract Factory Pattern-Abstract Factory (Java Implementation)

Abstract Factory Pattern-Abstract Factory

In the abstract factory pattern, an interface is a factory responsible for creating a related object without explicitly specifying their class. Each generated factory can provide objects according to the factory pattern.

This article takes the example of iluwatar on github as an example.

A country has armies, castles, and kings. If you want to create a kingdom (nation), then you need to repeat these definitions every time you create a country.

Therefore, the abstract factory pattern is used to abstract the factory that creates the kingdom. The components of the country are all these, and only need to be defined in the abstract factory, only the details are different between countries, so the specific kingdom is used to realize the concrete Implementation of the Army/Castle/Kingdom Factory. 

Definition of an interface

Army interface

/**
 * army
 */
public interface Army {
    String getDescription();
}

Castle interface

/**
 * Castle
 */
public interface Castle {
    String getDescription();
}

King interface

/**
 * King
 */
public interface King {
    String getDescription();
}

KingdomFactory interface

An abstract definition of a factory that can create states.

/**
 * nation
 */
public interface KingdomFactory {
    Castle createCastle();

    King createKing();

    Army createArmy();
}

Implementation of a specific factory

So far, the abstract definition is complete, let's see what needs to continue to be implemented if we want to create a country.

It's not just creating the first country, but every time you add a country, you don't need to change the underlying abstract definition.

Let's start creating a kingdom of Ox

OrcArmy class

public class OrcArmy implements Army {
    private static final String DESCRIPTION = "This is the army of Ox Kingdom!";

    @Override
    public String getDescription() {
        return DESCRIPTION;
    }
}

OrcCastle class

public class OrcCastle implements Castle {
    private static final String DESCRIPTION = "This is the castle of Ox Kingdom!";

    @Override
    public String getDescription() {
        return DESCRIPTION;
    }
}

OrcKing class

public class OrcKing implements King {
    private static final String DESCRIPTION = "This is the king of Ox!";

    @Override
    public String getDescription() {
        return DESCRIPTION;
    }
}

OrcKingdomFactory class

public class OrcKingdomFactory implements KingdomFactory {
    @Override
    public Castle createCastle() {
        return new OrcCastle();
    }

    @Override
    public King createKing() {
        return new OrcKing();
    }

    @Override
    public Army createArmy() {
        return new OrcArmy();
    }
}

Main

for test run

public class Main {
    public static void main(String[] args) {
        KingdomFactory factory = new OrcKingdomFactory();

        Army orcArmy = factory.createArmy();
        King orcKing = factory.createKing();
        Castle orcCastle = factory.createCastle();

        System.out.println(orcArmy.getDescription());
        System.out.println(orcKing.getDescription());
        System.out.println(orcCastle.getDescription());
    }
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325958503&siteId=291194637