Development

Java의 공장 디자인 패턴 :이 기사는 공장 디자인 패턴을 다룰 수 있으며,이 기사는 슈퍼 클래스에서 객체를 생성하기위한 인터페이스를 제공하지만 서브 클래스가 생성 될 객체의 유형을 변경..

sonpro 2023. 3. 13. 02:10
반응형

Factory design pattern

Factory Design Pattern in Java

In software development, the Factory design pattern is a creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. This pattern is widely used in Java programming to create objects without exposing the creation logic to the client and to refer to newly created objects using a common interface.

Why use the Factory design pattern?

The Factory design pattern is used when we need to create objects of a specific class, but we don't know which subclass we should use until runtime. This pattern is useful when we want to decouple the client code from the creation logic of objects. By using the Factory pattern, we can create objects without exposing the creation logic to the client.

How does the Factory design pattern work?

The Factory design pattern works by creating a factory class that has a method for creating objects. This method takes a parameter that specifies the type of object to be created. The factory class then creates an object of the specified type and returns it to the client.

The following is an example of how the Factory design pattern can be implemented in Java:

public interface Shape {
    void draw();
}

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a circle");
    }
}

public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a rectangle");
    }
}

public class ShapeFactory {
    public Shape createShape(String shapeType) {
        if (shapeType.equalsIgnoreCase("circle")) {
            return new Circle();
        } else if (shapeType.equalsIgnoreCase("rectangle")) {
            return new Rectangle();
        } else {
            return null;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        ShapeFactory shapeFactory = new ShapeFactory();
        Shape circle = shapeFactory.createShape("circle");
        Shape rectangle = shapeFactory.createShape("rectangle");
        circle.draw();
        rectangle.draw();
    }
}

In this example, we have an interface called Shape that defines a method called draw. We also have two classes that implement the Shape interface: Circle and Rectangle. We then have a ShapeFactory class that has a method called createShape that takes a parameter called shapeType. This method creates an object of the specified type and returns it to the client.

In the main method, we create an instance of the ShapeFactory class and use it to create two objects: a circle and a rectangle. We then call the draw method on each object to draw the shapes.

Benefits of using the Factory design pattern

The Factory design pattern has several benefits, including:

  • Decoupling the client code from the creation logic of objects
  • Providing a common interface for creating objects
  • Allowing subclasses to alter the type of objects that will be created
  • Encapsulating the object creation code in one place

Conclusion

The Factory design pattern is a powerful tool in Java programming that allows us to create objects without exposing the creation logic to the client. By using this pattern, we can decouple the client code from the creation logic of objects and provide a common interface for creating objects. The Factory pattern is widely used in Java programming and is a valuable tool for any software developer's toolkit.

반응형