zoukankan      html  css  js  c++  java
  • java 静态代理模式

    1 package proxy.staticproxy;
    2 
    3 public interface IStar {
    4 
    5     public void sing();
    6 }
    1 package proxy.staticproxy;
    2 
    3 public class RealStar implements IStar{
    4     @Override
    5     public void sing() {
    6         System.out.println("我是大明星:我要大声唱:巴拉巴拉。。。");
    7     }
    8 }
     1 package proxy.staticproxy;
     2 
     3 public class ProxyStar implements IStar {
     4 
     5     private IStar realStar;
     6 
     7     public IStar proxy(IStar realStar){
     8         this.realStar = realStar;
     9         return this;
    10     }
    11 
    12 
    13     @Override
    14     public void sing() {
    15 
    16         System.out.println("我是代理人:大明星唱歌开始前,我先宣传一下:巴拉巴拉。。。");
    17 
    18         this.realStar.sing();
    19 
    20         System.out.println("我是代理人:大明星唱歌完毕了,我来总结一下:巴拉巴拉。。。");
    21     }
    22 }
     1 package proxy.staticproxy;
     2 
     3 public class Test {
     4 
     5     /**
     6      *
     7      * 静态代理可以分为两类,以“代理人”和“大明星”为例。
     8      * 下面测试的为基于相同接口的代理,代理人和大明星都是实现的相同接口
     9      * 代理人实例内部维护(组合)大明星实例
    10      * 对外暴露代理人的行为
    11      * 另一类是基于继承的代理,假如大明星本身就没有实现任何接口
    12      * 那么,代理人可以通过继承大明星,覆写大明星的方法(除final修饰的方法,wait方法,notify方法)、
    13      * 达到代理大明星的逻辑
    14      * @param args
    15      */
    16     public static void main(String[] args) {
    17         IStar star = new ProxyStar().proxy(new RealStar());
    18         star.sing();
    19     }
    20 }
  • 相关阅读:
    js--DOM基本使用
    前端--js基础2
    前端--js基础1
    tcp/udp编程
    关于网络
    异常处理
    装饰器
    1.__new__ 魔术方法 单态(例)模式 __del__ 魔术方法(析构方法) __call__ 魔术方法
    面向对象程序设计及面向对象封装 目录
    script 标签里的 async 和 defer
  • 原文地址:https://www.cnblogs.com/cnblogszs/p/10366684.html
Copyright © 2011-2022 走看看