상세 컨텐츠

본문 제목

[Design Pattern] Proxy Pattern : Protection Pattern

JAVA

by jeonghojin 2023. 4. 1. 11:10

본문

프록시 패턴은 어떠한 객체에 대한 접근을 제어하는 용도로 객체의 대리인 역할을 하는 객체를 제공하는 패턴이다.

- 실제 객체를 수행하기 이전에 전처리를 하거나, 기존 객체를 캐싱할 수 있다. 기존 객체의 리소스가 무거울 경우, 이러한 캐싱 과정을 통해 부하를 줄일 수 있는 장점이 있다.

- 기존 객체를 수정하지 않고 일련의 로직을 프록시 패턴을 통해 추가할 수 있다.

- 프록시는 기존 객체와 클라이언트의 요청 사이에 위치해 있기 때문에 하나의 방패(보안) 역할을 하기도 한다.

 
 


 

Protection Pattern

 
보호 프록시는 프록시 객체가 사용자의 실제 객체에 대한 접근을 제어한다.

- 실제 개체에 대한 액세스를 제어한다.
- 프록시에서 권한을 먼저 확인하고 실제 객체를 생성(접근)할지를 판단한다.
- 보호 프록시는 개체의 액세스 권한이 서로 달라야 할 떄 유용하다.


프록시 패턴 | 데코레이터 패턴

 
프록시 패턴 (Proxy Pattern) : 어떠한 클래스에 대한 접근을 제어 혹은 접근에 초점을 맞춘 용도로 사용
데코레이터 패턴(Decorator Pattern) : 클래스에 새로운 행동 or 기능을 추가하기 위한 용도로 사용

 

프록시 패턴 | 어댑터 패턴

 
- 프록시 패턴어댑터 패턴 모두 클라이언트와 객체 사이에 위치하여 클라이언트의 요청을 받아서 다른 객체에게 전달해주는 역할을 한다.
- 하지만, 어댑터 패턴은 다른 객체의 인터페이스를 바꿔주지만, 프록시 패턴에서는 똑같은 인터페이스를 사용한다.


인사팀에서 인사정보에 대한 데이터 접근을 직책 단위로 세분화하려고 한다. 기존에는 오로지 인사팀에서만 사용했던 부분이었으나 최근 인사정보를 직책별로 공개해줘야 하는 경우가 생겼다. 따라서 전산팀에 근무중인 나는 직책에 따라서 조직원의 인사정보 접근을 제어하는 업무를 수행해야 한다.

 
Enum GRADE

public enum GRADE {
    Staff, Manager, VicePresident
}


 
Interface Employee

public interface Employee {
    String getName();// 이름
    GRADE getGrade();// 직책
    String getInformation(Employee viewer);// 인사정보 (매개변수 조회자)
}

NormalEmployeee

public class NormalEmployee implements Employee {

    private String name;
    private GRADE grade;

    public NormalEmployee(String name, GRADE grade) {
        this.name = name;
        this.grade = grade;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public GRADE getGrade() {
        return grade;
    }

    @Override
    public String getInformation(Employee viewer) {
        return "Display "+getGrade().name() + " ' "+getName() + " ' personnal information.";
    }
}

ProtectedEmployeee

public class ProtectedEmployee implements Employee {

    private Employee employee;

    public ProtectedEmployee(Employee employee) {
        this.employee = employee;
    }

    @Override
    public String getName() {
        return employee.getName();
    }

    @Override
    public GRADE getGrade() {
        return employee.getGrade();
    }

    @Override
    public String getInformation(Employee viewer) {
        // 본인 인사정보 조회
        if (this.employee.getGrade() == viewer.getGrade() && this.employee.getName().equals(viewer.getName())) {
            return this.employee.getInformation(viewer);
        }

        switch (viewer.getGrade()) {
            case VicePresident:// 부사장은 조직장, 조직원들을 볼 수 있다.
                if (this.employee.getGrade() == GRADE.Manager || this.employee.getGrade() == GRADE.Staff) {
                    return this.employee.getInformation(viewer);
                }
            case Manager:
                if (this.employee.getGrade() == GRADE.Staff) { // 조직장들은 조지원들을 볼 수 있다.
                    return this.employee.getInformation(viewer);
                }

            case Staff:
            default:
                throw new NotAuthorizedException(); //  조직원들은 다른사람의 인사정보를 볼 수 없다.
        }
    }
}

 
NotAuthorizedException

public class NotAuthorizedException extends RuntimeException {
    private static final long SerialVersionUID = -1714144282967712658L;
}

 

 



참고 :

더보기

https://gngsn.tistory.com/126

Design Pattern, Proxy

Structural Object Pattern Proxy Pattern ----------------- INDEX ----------------- Proxy Pattern ? Structure Sample Code: Java Applicability 원격 프록시 Remote Proxy 가상 프록시 Virtual Proxy 보호 프록시 Protection Proxy 스마트 참조자

gngsn.tistory.com

https://velog.io/@pjh612/%EB%94%94%EC%9E%90%EC%9D%B8-%ED%8C%A8%ED%84%B4-%ED%94%84%EB%A1%9D%EC%8B%9C-%ED%8C%A8%ED%84%B4Proxy-Pattern

디자인 패턴 - 프록시 패턴(Proxy Pattern)

프록시 패턴의 프록시(Proxy)는 '대리' 라는 뜻을 가졌다. 이름 그대로 무엇인가를 대신 처리한다는 의미다. 프록시 서버, 리버스 프록시, 프록시 패턴 등에서의 프록시는 그런 의미에서 사용된다.

velog.io

https://jdm.kr/blog/235

프록시 패턴(Proxy Pattern) :: JDM's Blog

프록시 패턴 정의 실제 기능을 수행하는 객체Real Object 대신 가상의 객체Proxy Object를 사용해 로직의 흐름을 제어하는 디자인 패턴입니다. 프록시 패턴 특징 원래 하려던 기능을 수행하며 그외의

jdm.kr

https://today-retrospect.tistory.com/102

Java 디자인 패턴 두번째 이야기 - 프록시 패턴(Proxy Pattern)

[목차] 1. 프록시 패턴이란? 2. 프록시 패턴은 왜 사용하는 것일까? 3. 프록시 패턴의 종류 3-1. 가상 프록시 3-2. 원격 프록시 3-3. 보호 프록시 4. 구현을 보기 전에 알아두면 좋은 부분 5. 프록시 패턴

today-retrospect.tistory.com

https://gre-eny.tistory.com/259

Head First: Design Patterns - 프록시 패턴(Proxy Pattern): 보호 프록시(protection proxy)

디자인 패턴: 프록시 패턴(Proxy Pattern) 이 포스팅은 Head First: Design Patterns 책을 보고, 개인적으로 정리한 포스팅입니다. Proxy Pattern 이란? 프록시 패턴(Proxy Pattern) 은 어떤 객체에 대한 접근을 제어

gre-eny.tistory.com

 

'JAVA' 카테고리의 다른 글

[Design Pattern] Facade Pattern  (0) 2023.04.06
[Design Pattern] Proxy Pattern : Remote Proxy  (0) 2023.04.02
[Design Pattern] Proxy Pattern : Virtual Proxy  (0) 2023.03.30
[Design Pattern] Proxy Pattern  (0) 2023.03.29
[Design Pattern] Adapter Pattern  (0) 2023.03.28

관련글 더보기