프록시 모드(정적 및 동적)
3424 단어 프록시 모드
package ProxyPattern;
public interface Italk {
public void talk(String msg);
}
RealSubject:
package ProxyPattern;
public class People implements Italk {
public String username;
public String age;
public String getName() {
return username;
}
public void setName(String name) {
this.username = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public People(String name1, String age1) {
this.username = name1;
this.age = age1;
}
public void talk(String msg) {
System.out.println(msg + "! , " + username + ", " + age);
}
}
프록시 클래스:
package ProxyPattern;
public class TalkProxy implements Italk {
Italk people;
public TalkProxy(Italk people) {
this.people = people;
}
public void talkProxy(Italk people) {
this.people = people;
}
public void talk(String msg) {
people.talk(msg);
}
public void talk(String msg, String singname) {
people.talk(msg);
sing(singname);
}
private void sing(String singname) {
System.out.println(" :" + singname);
}
}
테스트:
package ProxyPattern;
public class MyProxyTest {
public static void main(String[] args) {
People people1 = new People(" ", "18");
people1.talk("No ProXY Test");
System.out.println("-----------------------------");
TalkProxy talker = new TalkProxy(people1);
talker.talk("ProXY Test", " ");
}
}
다른 프록시 모드: 동적 프록시:
package Dybamic;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class Handler implements InvocationHandler {
public Object targetObj;
public Handler(Object targetObj) {
this.targetObj = targetObj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("before the function \"" + method.getName() + "\"");
Object ret = method.invoke(targetObj, args);
System.out.println(ret);
System.out.println("after the function \"" + method.getName() + "\"");
return ret;
}
}
동적 프록시 테스트:
package ProxyPattern;
package Dybamic;
import java.lang.reflect.Proxy;
public class testMain {
public static void main(String[] args) {
IUser realUser = new UserImp("sun");
Handler hand = new Handler(realUser);
IUser proxy = (IUser) Proxy.newProxyInstance(realUser.getClass()
.getClassLoader(), realUser.getClass().getInterfaces(), hand);
proxy.getName();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
간편한 Java 프록시 모드여러분과 함께 자바 프록시 모드에 대해 얘기를 나눠보도록 하겠습니다. 1. 정적 에이전트 1.1 정적 에이전트의 에이전트 클래스와 피에이전트의 클래스는 모두 공통된 인터페이스를 유지해야 한다. 1.2 프록시 클래스,...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.