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

    简介

    思想应该是 中介 思想, 就是把一个任务抽离出来, 用另一个对象以组合的方式实现. 在Spring 中以 AOP(Aspect Oriented Programming, 面向切面编程)的方式出现, 可以理解为横向扩展

    code

    public class Client {
        public static void main(String[] args) {
    //        Host host = new Host();
    //        host.rent();
            Host host = new Host();
            Proxy proxy = new Proxy(host);
            proxy.rent();
        }
    }
    
    
    public class Host implements Rent{
        @Override
        public void rent() {
            System.out.println("房东要出租");
        }
    }
    
    
    public class Proxy  {
        private Host host;
    
        public Proxy() {
        }
    
        public  Proxy(Host h){
            this.host = h;
        }
    
        public void rent() {
            seeHoust();
            host.rent();
            hetong();
            fare();
        }
    
        public void seeHoust() {
            System.out.println("中介带你看房");
        }
    
        public void fare() {
            System.out.println("收中介费");
        }
    
        public  void hetong() {
            System.out.println("签租赁合同");
        }
    }
    
    
    public interface Rent {
        void rent();
    }
    
    

    UML

    优点

    1. 可以使真实角色的操作(出租)更加纯粹! 不用去关注一些公共的业务(看房子)
    2. 公共也就交给代理角色, 实现了业务的分工
    3. 公共业务发生扩展的时候, 方便集中管理.

    确定

    1. 一个真实角色就会产生一个代理角色; 代码量会翻倍开发效率会变低.

    角色分析

    1. 抽象角色 : 一般会使用接口或者抽象类来解决
    2. 真实角色 : 被代理的角色
    3. 代理角色 : 代理真实角色 , 代理真实角色后 , 我们一般会做一些附属操作
    4. 客户 : 访问代理对象的人!
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    浅谈flume
    浅谈storm
    浅谈zookeeper
    IntelliJ IDEA 使用教程
    浅谈spark
    添加本地jar包到maven仓库
    eclipse通过maven进行打编译
    pom.xml中添加远程仓库
    maven编译错误maven-assembly-plugin:2.2-beta-5:assembly (default-cli) on project
    最长上升子序列
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/14820941.html
Copyright © 2011-2022 走看看