Proxy Pattern

2023. 11. 7. 21:57디자인패턴

728x90

proxy pattern

: 어떤 객체에 대한 접근을 제어하기 위한 용도로

대리인에 해당하는 객체를 제공하는 패턴.

 

 

proxy 종류

  • remote proxy: 원격 객체에 대한 접근 제어
  • virtual proxy: 생성하는 비용이 많이 드는 자원에 대한 접근 제어
  • protection proxy: 접근 권한에 필요한 자원에 대한 접근 제어

 

remote proxy

다른 네트워크상에 있는 객체에 접근하려고 하는 경우

proxy를 통해 접근할 수 있다.

proxy가 진짜 객체를 대신하는 역할을 한다.

 

 

 

Java RMI

Remote Method Invocation: 원격 메서드 호출

client는 stub에게 요청 -> stub는 skeleton에게 요청 -> skeleton은 service에게 요청

 

 

Making the Remote service

1. Remote Interface 만들기

: 어떤 서비스 메서드를 원격으로 제공할 것인지 인터페이스 정의

import java.rmi.*;

public interface MyRemote extends Remote{
	public String sayHello() throws RemoteException; //원격접속 실패 시 예외처리
}

원격으로 하기 위해 자바의 Remote 인터페이스 상속
-> Remote 인터페이스 안에는 아무 메서드도 없음

단지 원격으로 한다고 표시하는 역할(marker interface)

 

2. Remote Inteface 메서드 구현

import java.rmi.*;
import java.rmi.server.*;

public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
    public String sayHello() {
    	return “Server says, ‘Hey’”;
    }
    public MyRemoteImpl() throws RemoteException { } //생성자
    
    public static void main (String[] args) {
        try {
            MyRemote service = new MyRemoteImpl();
            Naming.rebind(“RemoteHello”, service); //stub을 registry에 등록
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

3. stubs와 skeletons 만들기

: dos에서 rmic 명령어를 사용하면 자동으로 만들어진다.

-> rmic MyRemoteImpl

4. RMI registry 프로그램 시작

-> rmiregistry

5. remote 서비스 시작하기

-> java MyRemoteImpl

 

 

Client

 

1. RMI registry에서 stub을 찾아 다운로드

-> Naming.lookup("rmi://127.0.0.1/RemoteHello");

2. RMI registry에서 stub 객체를 client 네트워크 환경으로 반환한다.

3. 이제 stub 객체를 Service 객체인 것처럼 사용한다.

import java.rmi.*;

public class MyRemoteClient {
    public static void main (String[] args) {
    new MyRemoteClient().go();
    }
    public void go() {
        try {
        MyRemote service = (MyRemote) Naming.lookup(“rmi://127.0.0.1/RemoteHello”);
        String s = service.sayHello();
        System.out.println(s);
        } catch(Exception ex) {
            ex.printStackTrace();
    }
}

 

 

GumballMachine 원격 서비스로 사용하기

1.

RMI registry에서 stub 찾아 다운로드, getState() 요청

2. 

client->stub->skeleton->service 객체 순으로 요청 전달

 

3.

service->skeleton->stub->client 객체 순으로 요청 반

import java.rmi.*;
    public interface GumballMachineRemote extends Remote {
    public int getCount() throws RemoteException;
    public String getLocation() throws RemoteException;
    public State getState() throws RemoteException;
}
import java.io.*;

public interface State extends Serializable {//객체를 넘기기 위해 Serializable(직렬화) 상속
    public void insertQuarter();
    public void ejectQuarter();
    public void turnCrank();
    public void dispense();
}
import java.rmi.*;
import java.rmi.server.*;

public class GumballMachine
	extends UnicastRemoteObject implements GumballMachineRemote
{
    // instance variables here
    public GumballMachine(String location, int numberGumballs) throws RemoteException {
    // code here
    }
    public int getCount() {
    	return count;
    }
    public State getState() {
    	return state;
    }
    public String getLocation() {
    	return location;
    }
    // other methods here
}
/*서비스 객체*/
public class GumballMachineTestDrive {
    public static void main(String[] args) {
        GumballMachineRemote gumballMachine = null;
        int count;
        if (args.length < 2) {
            System.out.println(“GumballMachine <name> <inventory>”);
            System.exit(1);
        }
        try {
            count = Integer.parseInt(args[1]);
            gumballMachine =
            new GumballMachine(args[0], count);
            Naming.rebind(“//” + args[0] + “/gumballmachine”, gumballMachine);
            } catch (Exception e) {
            	e.printStackTrace();
        }
    }
}
/*클라이언트 객체*/
import java.rmi.*;

public class GumballMonitorTestDrive {
    public static void main(String[] args) {
        String[] location = {“rmi://santafe.mightygumball.com/gumballmachine”,
        	“rmi://boulder.mightygumball.com/gumballmachine”,
        	“rmi://seattle.mightygumball.com/gumballmachine”};
        GumballMonitor[] monitor = new GumballMonitor[location.length];
        for (int i=0;i < location.length; i++) {
            try {
                GumballMachineRemote machine =
                	(GumballMachineRemote) Naming.lookup(location[i]);
                monitor[i] = new GumballMonitor(machine);
                System.out.println(monitor[i]);
                } catch (Exception e) {
                	e.printStackTrace();
            }
        }
        for(int i=0; i < monitor.length; i++) {
        	monitor[i].report();
        }
    }
}
728x90

'디자인패턴' 카테고리의 다른 글

Bridge Design Pattern  (2) 2023.12.05
Compound Patterns  (2) 2023.11.14
State pattern  (0) 2023.10.31
composite pattern  (0) 2023.10.30
The iterator pattern  (0) 2023.10.22