zoukankan      html  css  js  c++  java
  • Java 设计模式之代理模式

    直接调用代理类,用代理类访问目标类。

    package Pak;
    
    public interface Sourceable {
        public void method();
    }
     1 package Pak;
     2 
     3 public class Source implements     Sourceable {
     4     
     5     @Override
     6     public void method(){
     7         System.out.println("the original method!");
     8         
     9     }
    10 }
     1 package Pak;
     2 
     3 public class Proxy implements Sourceable {
     4     private Source source;
     5 
     6     public Proxy() {
     7         super();
     8         this.source = new Source();
     9     }
    10 
    11     @Override
    12     public void method() {
    13         // TODO Auto-generated method stub
    14         before();
    15         source.method();
    16         after();
    17     }
    18 
    19     private void after() {
    20         System.out.println("after proxy!");
    21     }
    22 
    23     private void before() {
    24         System.out.println("Before proxy!");
    25     }
    26 }
     1 package Pak;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 public class Main {
     7 
     8     public static void main(String[] args) throws Exception {
     9         Sourceable source = new Proxy();
    10         source.method();
    11 
    12     }
    13 }
  • 相关阅读:
    使用eclipse新建一个SWT工程
    C++类的构造函数
    D3D编程的常见报错及解决
    D3D窗口的初始化
    C++联合体的内存使用
    QT程序如何编译
    Restart
    HTML
    信号、槽位及布局
    QT对话框程序
  • 原文地址:https://www.cnblogs.com/netact/p/3839750.html
Copyright © 2011-2022 走看看