Development

스프링 프레임 워크와 함께 Java의 디자인 패턴

sonpro 2023. 3. 11. 01:20
반응형

Design Pattern

Design Pattern in Java with Spring Framework

Design patterns are reusable solutions to common software design problems. They provide a way to solve problems that developers face every day while designing software. In this article, we will discuss some of the most commonly used design patterns in Java with the Spring Framework.

Singleton Pattern

The Singleton pattern is a creational pattern that ensures that a class has only one instance and provides a global point of access to it. In Spring, the Singleton pattern is used by default for all beans. This means that by default, all beans are singletons, and only one instance of a bean is created and shared across the application.

public class Singleton {
    private static Singleton instance;
    private Singleton() {}
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

Factory Pattern

The Factory 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. In Spring, the Factory pattern is used to create beans. The Spring container uses a factory method to create beans, which allows for flexibility in creating different types of beans.

public interface Shape {
    void draw();
}

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

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

public class ShapeFactory {
    public Shape getShape(String shapeType) {
        if (shapeType == null) {
            return null;
        }
        if (shapeType.equalsIgnoreCase("RECTANGLE")) {
            return new Rectangle();
        } else if (shapeType.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        }
        return null;
    }
}

Observer Pattern

The Observer pattern is a behavioral pattern that defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. In Spring, the Observer pattern is used extensively for event handling. The Spring container provides an event mechanism that allows beans to publish events and other beans to subscribe to those events.

public class EventPublisher {
    private ApplicationEventPublisher publisher;
    public void publishEvent(String message) {
        publisher.publishEvent(new CustomEvent(this, message));
    }
}

public class CustomEvent extends ApplicationEvent {
    private String message;
    public CustomEvent(Object source, String message) {
        super(source);
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}

@Component
public class EventSubscriber implements ApplicationListener<CustomEvent> {
    @Override
    public void onApplicationEvent(CustomEvent event) {
        System.out.println("Received message: " + event.getMessage());
    }
}

Decorator Pattern

The Decorator pattern is a structural pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. In Spring, the Decorator pattern is used to add functionality to beans at runtime. This is done using AOP (Aspect-Oriented Programming) and Spring AOP.

public interface Shape {
    void draw();
}

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

public abstract class ShapeDecorator implements Shape {
    protected Shape decoratedShape;
    public ShapeDecorator(Shape decoratedShape) {
        this.decoratedShape = decoratedShape;
    }
    public void draw() {
        decoratedShape.draw();
    }
}

public class RedShapeDecorator extends ShapeDecorator {
    public RedShapeDecorator(Shape decoratedShape) {
        super(decoratedShape);
    }
    public void draw() {
        decoratedShape.draw();
        setRedBorder(decoratedShape);
    }
    private void setRedBorder(Shape decoratedShape) {
        System.out.println("Border Color: Red");
    }
}

Conclusion

Design patterns are an essential part of software design and development. They provide a way to solve common problems that developers face every day. In this article, we discussed some of the most commonly used design patterns in Java with the Spring Framework. These patterns are Singleton, Factory, Observer, and Decorator. By understanding these patterns, developers can write better code and create more maintainable and scalable applications.

반응형