java ???????????2??
???????????? ???????[ 2013/8/6 10:02:38 ] ????????
??????????
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class Client
{
static public void main(String[] args) throws Throwable
{
RealSubject rs = new RealSubject(); //?????????????????
InvocationHandler ds = new DynamicSubject(rs); //???????????
Subject subject = (Subject) Proxy.newProxyInstance(rs.getClass().getClassLoader()??rs.getClass
().getInterfaces()??ds );
subject.request();
}
????5)???????
package dynamicProxy;
public interface Work {
public void startWork();
}
package dynamicProxy;
public class JasonWork implements Work {
public void startWork() {
System.out.println("jason start to work...");
}
}
public interface Play {
public void startPlay();
}
public class JasonPlay implements Play {
public void startPlay() {
System.out.println("jason start to play...");
}
}
public class Test {
public static void main(String[] args)
{
JasonWork work=new JasonWork();
InvocationHandler dynamicProxy=new DynamicProxy(work);
Work jasonproxy=(Work)Proxy.newProxyInstance(work.getClass().getClassLoader()??
work.getClass().getInterfaces()?? dynamicProxy);
jasonproxy.startWork();
JasonPlay play=new JasonPlay();
InvocationHandler dynamicProxy=new DynamicProxy(play);
Play jasonproxy=(Play)Proxy.newProxyInstance(play.getClass().getClassLoader()??
play.getClass().getInterfaces()?? dynamicProxy);
jasonproxy.startPlay();
}
}
????===>????????????????κ???????????(work/play)?????н??????ж???????.
????------------------------------ ????(state) -------------------------------
????(1)State?????壺?????????????????; ????????????????????????.
???????ó????State???????????б??????"?????л?".???????????????If elseif else ???????л??? ???????????????ж??л???????????????????????????State????.
????-->????????????????????仯??.
????(2)???state????????????: ???? + ?????????????(??????+????????)
??????????????????????????setter??getter.?????ú???????+?????????????????(????????????????????????).
????????????????????????е????.
????????????????????????????????????У?????????????????????????-->?????????′ε??÷???????????????仯.
??????????????????????
???????????????????????????????????????????????“?”????????е?“??”???????
??????????????????“??”??“?”?????????“?”????????????в?????????
??????ν????????????
????“??”????????????????????????“?”?????У?????????“??”???????“??”?????????“?”??????????????????????“?”???ɡ?
????(3)????:
public interface Color {
public void show();
}
package state;
class Light
{
Color color;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Light()
{
color=new RedColor(this);
}
public void showColor()
{
color.show();
}
}
class RedColor implements Color
{
Light light;
public RedColor(Light light)
{
this.light=light;
}
public void show()
{
System.out.println("the color is red??the car must stop !");
System.out.println("write down all logic shoud do this in this state.....");
light.setColor(new GreenColor(light));
}
}
class GreenColor implements Color
{
Light light;
public GreenColor(Light light)
{
this.light=light;
}
public void show()
{
System.out.println("the color is green??the car can run !");
System.out.println("write down all logic shoud do this in this state.....");
light.setColor(new YellowColor(light));
}
}
class YellowColor implements Color
{
Light light;
public YellowColor(Light light)
{
this.light=light;
}
public void show()
{
System.out.println("the color is yellow??the car shoud stop !");
System.out.println("write down all logic shoud do this in this state.....");
light.setColor(new RedColor(light));
}
}
public class CarLight {
public static void main(String[] args) {
Light light=new Light();
//???????????
light.showColor();
//?????????
light.showColor();
//?????????
light.showColor();
//???????????????.
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11