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

    多线程里的Runnable接口创建线程正是用了静态代理模式
    package Tread; public class StaticProxy { /** * 静态代理: 真实角色
    代理角色 两者要实现相同接口 * * @param args */ public static void main(String[] args) { You you = new You(); WeddingCompany wc = new WeddingCompany(you); wc.marry(); } } interface Marry { public abstract void marry(); } class You implements Marry { @Override public void marry() { System.out.println("你和嫦娥结婚"); } } class WeddingCompany implements Marry { private Marry you; public WeddingCompany() { super(); } public WeddingCompany(You you) { this.you = you; } public void before() { System.out.println("布置猪窝"); } public void after() { System.out.println("闹玉兔"); } @Override public void marry() { before(); you.marry(); after(); } }

      示例如下

    package Tread;
    
    public class Thread03 implements Runnable {
    
    	@Override
    	public void run() {
    		for (int i = 0; i < 1000; i++) {
    			System.out.println("一边敲代码");
    		}
    
    	}
    
    }
    package Tread;
    
    public class Thread03App {
    	public static void main(String[] args) {
    		Thread03 t = new Thread03();(真实角色)
    		Thread proxy = new Thread(t);
    		proxy.start();
    		for (int i = 0; i < 1000; i++) {
    			System.out.println("一边聊qq");
    		}
    	}
    }
    

      

  • 相关阅读:
    STL容器[26]
    SHELL[01]
    SHELL[04]
    SHELL[02]I/O重定向
    STL容器[39]
    stl.set用法总结
    STL容器[33]
    STL容器[29]
    hdu acm1071
    hdu acm 2673
  • 原文地址:https://www.cnblogs.com/yjxs/p/9848655.html
Copyright © 2011-2022 走看看