본문 바로가기

JAVA/GOF Design Pattern

GOF Abstract Factory Pattern

SMALL

추상 팩토리 패턴(Abstract Factory Pattern)은 객체를 생성하기 위한 패턴 중 하나입니다. 이 패턴은 관련된 객체들의 집합을 생성하기 위한 인터페이스를 제공하며, 이를 이용하여 구체적인 객체의 생성을 추상화합니다.

 

추상 팩토리 패턴은 객체 생성을 위한 인터페이스와 구현을 분리하여, 객체 생성 과정의 유연성과 확장성을 높이고, 코드의 의존성을 낮춥니다. 이 패턴은 특히 서로 관련된 객체들을 생성해야 하는 경우에 유용합니다.

 

추상 팩토리 패턴은 일반적으로 추상 팩토리(Abstract Factory)라는 인터페이스를 정의하며, 이 인터페이스는 관련된 객체들의 집합을 생성하는 메서드들을 제공합니다. 구체적인 팩토리 클래스들은 추상 팩토리 인터페이스를 구현하며, 이 클래스들은 실제로 객체를 생성합니다. 객체 생성 과정은 일반적으로 팩토리 메서드 패턴을 이용하여 추상화됩니다.

 

추상 팩토리 패턴의 장점은 객체 생성 과정의 변경이나 확장에 대해 유연하게 대처할 수 있다는 것입니다. 만약 새로운 객체가 추가되어야 하는 경우, 해당 객체를 생성하는 새로운 팩토리 클래스를 구현하면 됩니다. 이렇게 하면 새로운 객체를 생성하는 코드를 기존 코드와 분리시켜, 코드의 유지 보수와 확장이 더 쉬워집니다.

 

Computer 추상클래스를 만듭니다. getRam, getHDD, getCPU 추상 메서드를 선언합니다.

 

package gof.abstractFactory;

public abstract class Computer {
     
    public abstract String getRAM();
    public abstract String getHDD();
    public abstract String getCPU();
     
    @Override
    public String toString(){
        return "RAM= "+this.getRAM()+", HDD="+this.getHDD()+", CPU="+this.getCPU();
    }
}
 

 

Computer 추상클래스를 상속받은 PC 클래스입니니다.

세개의 파라미터를 받는 생성자가 있고, getRam, getHDD, getCPU 메서드를 재정의합니다.

 

package gof.abstractFactory;

public class PC extends Computer{
    private String ram;
    private String hdd;
    private String cpu;

    public PC(String ram, String hdd, String cpu) {
        this.ram = ram;
        this.hdd = hdd;
        this.cpu = cpu;
    }

    @Override
    public String getRAM() {
       
        return this.ram;
    }

    @Override
    public String getHDD() {
        return this.hdd;
    }

    @Override
    public String getCPU() {
        return this.cpu;
    }
   
}

 

Server 클래스입니다. Computer 추상클래스를 상속받고, getRAM, getHDD, getCPU를 재정의합니다.

 

package gof.abstractFactory;

public class Server extends Computer {
    private String ram;
    private String hdd;
    private String cpu;

    public Server(String ram, String hdd, String cpu) {
        this.ram = ram;
        this.hdd = hdd;
        this.cpu = cpu;
    }

    @Override
    public String getRAM() {
        return this.ram;
    }

    @Override
    public String getHDD() {
       return this.hdd;
    }

    @Override
    public String getCPU() {
      return this.cpu;
    }
   
}

 

ComputerAbstractFactory 인터페이스입니다. Computer 타입 creaComputer 추상메서드를 선언합니다.

 

package gof.abstractFactory;

public interface ComputerAbstractFactory {
    public Computer creaComputer();
}

 

PCFactory 클래스입니다. ComputerAbstractFactory클래스를 구현하고 creaComputer 메서드를 재정의하면서 PC객체를 생성합니다.

 

package gof.abstractFactory;

public class PCFactory implements ComputerAbstractFactory {

    private String ram;
    private String hdd;
    private String cpu;
   
    public PCFactory(String ram, String hdd, String cpu){
        this.ram=ram;
        this.hdd=hdd;
        this.cpu=cpu;
    }

    @Override
    public Computer creaComputer() {
       return new PC(ram, hdd, cpu);
    }
   
}

 

ServerFactory 클래스입니다. ComputerAbstractFactory클래스를 구현하고 creaComputer 메서드를 재정의하면서 Server객체를 생성합니다.

 

package gof.abstractFactory;

public class ServerFactory implements ComputerAbstractFactory {
    private String ram;
    private String hdd;
    private String cpu;
   
    public ServerFactory(String ram, String hdd, String cpu){
        this.ram=ram;
        this.hdd=hdd;
        this.cpu=cpu;
    }

    @Override
    public Computer creaComputer() {
        return new Server(ram,hdd,cpu);
    }

}

 

ComputerFactory 클래스입니다. Computer type static 메서드인 getComputer 메서드를 정의합니다.

 

package gof.abstractFactory;

public class ComputerFactory {
    public static Computer getComputer(ComputerAbstractFactory factory){
        return factory.creaComputer();
    }
}

 

package gof.abstractFactory;

public class TestDesignPatterns {
    public static void main(String[] args) {
        testAbstractFactory();
    }

    private static void testAbstractFactory() {
        Computer pc = ComputerFactory.getComputer(new PCFactory("2 GB","500 GB","2.4 GHz"));
        Computer server = ComputerFactory.getComputer(new ServerFactory("16 GB","1 TB","2.9 GHz"));
        System.out.println("AbstractFactory PC Config::"+pc);
        System.out.println("AbstractFactory Server Config::"+server);
    }
}

* Reference :https://www.digitalocean.com/community/tutorials/abstract-factory-design-pattern-in-java

LIST

'JAVA > GOF Design Pattern' 카테고리의 다른 글

GOF 스트레티지 패턴(Strategy Pattern)  (0) 2023.05.09
GOF Observer Pattern  (0) 2023.05.09
GOF Iterator 패턴  (0) 2023.04.28
Gof Template method 패턴  (0) 2023.04.28
GOF Factory method 패턴  (0) 2023.04.26