15.8.20笔记

今天继续学习。接着之前的代理模式继续。

先是想到了代理模式的作用,就搜素了一下。代理模式的作用:

  • 安全考虑:这点很容易理解,比如我只想暴露A类的方法f()给外界,但是还有其他的方法g()、h(),那么用代理模式就能达到目的了。
  • 性能增前:延迟重量级对象创建
  • 方法增强:这点貌似是装饰器模式的特长,但是代理模式也可以做到

以上是静态代理代理模式,与之对应的还有动态代理模式,动态代理模式比静态代理模式更加灵活,因为不再需要为每个接口写一个形式上完全一样的封装类,将来接口变得也基本不需要做改动。

下面来看一下动态代理机制:

贴出动态代理里面核心的代码:

// 数据库查询接口类
public interface IDBQuery {
    public void request(String requestParamStr);
}

//数据库查询实现类
public DBQuery implements IDBQuery {
    public DBQuery() {
    // 连接数据库,初始化等等耗时操作,略
    }

    @Override
    public void request(String requestParamStr) {
    // 略
    }
}

// 代理类
public class DBQueryProxy implements IDBQuery {
private DBQuery real= null;   // 被代理对象

    @Override
    public void request(String requestParamStr) {
        if(real == null)
        real = new DBQuery();   // 延迟到这里创建DBQuery对象
        real.request();
    }
}
// 使用
IDBQuery query = new DBQueryProxy();
query.requst();   // 真正使用时才会创建真实的DBQuery对象

public Object invoke(Object proxy, Method method, Object[] args)throws Throwable
处理代理实例上一个方法的调用并返回处理结果。
当一个代理实例上的一个方法被调用时,则其关联的调用处理程序这个方法将被调用。

proxy:被调用方法的代理实例

method:与代理实例上调用的接口方法对应的方法对象,该方法对象将是接口中声明的方法,也可以是代理类通过代理接口的父接口继承的方法

args:包含代理实例上方法调用被传递的参数值对象数组,如接口方法没有参数则为null,原始类型的参数用适当的原始包装类的实例 如java.lang.Integer 等

return:从代理实例上方法调用返回的值。如果接口方法声明的返回类型是原始类型,则返回与之相应的原始包装类的实例,否则 返回其声明指定的类型。

  • 若返回值为null并且接口的方法是返回类似是原始类型,则抛出NullPointerException。

  • 若返回值与接口方法声明的返回类型不兼容,则抛出ClassCastException

    throws:从代理实例上方法调用抛出的异常。异常类型是接口方法可抛出的任何异常类型or未受检查异常or错误
    若抛出一个受检查异常而该受检查不是接口方法中声明的异常类型,则一个UndeclaredThrowableException抛出

此时我们再从两者的区别来比较一下:

与静态代理类对照的是动态代理类,动态代理类的字节码在程序运行的时候有java反射机制动态生成,无需程序员手工编写它的源代码。动态代理不仅简化了编程工作,而且提高了软件系统的可扩展性,因为java反射机制可以生成任意类型的动态代理。

function SystemStatus({}) { const networkTopologySVG = ` <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 400" class="w-full h-full"> <style> /* 移除地球旋转动画 */ .earth-group { transform-origin: center; } </style> <!-- 左侧:静态地球图标(互联网) --> <g transform="translate(100, 150)" class="earth-group"> <circle cx="0" cy="0" r="40" fill="#3B82F6" stroke="#1E40AF" stroke-width="2"/> <path d="M-40,0 C-20,-15 20,-15 40,0" stroke="#1E40AF" stroke-width="1" fill="none"/> <path d="M-40,0 C-20,15 20,15 40,0" stroke="#1E40AF" stroke-width="1" fill="none"/> <circle cx="0" cy="0" r="35" fill="none" stroke="#1E40AF" stroke-width="1" stroke-dasharray="5,5"/> <text x="0" y="60" text-anchor="middle" font-size="12" fill="#1E40AF">互联网</text> </g> <!-- 互联网到路由器的连接线 --> <line x1="140" y1="150" x2="300" y2="150" stroke="#9CA3AF" stroke-width="2" stroke-dasharray="5,5"/> <!-- 中间:路由器图标(无文字) --> <g transform="translate(300, 120)"> <rect x="0" y="0" width="120" height="60" rx="5" fill="#3B82F6" stroke="#1E40AF" stroke-width="2"/> <circle cx="20" y="15" r="4" fill="#10B981"/> <circle cx="40" y="15" r="4" fill="#10B981"/> <circle cx="60" y="15" r="4" fill="#10B981"/> <circle cx="80" y="15" r="4" fill="#10B981"/> <circle cx="100" y="15" r="4" fill="#10B981"/> <rect x="15" y="30" width="90" height="20" rx="3" fill="#1E40AF"/> </g> <!-- 路由器到设备的连接线 --> <line x1="420" y1="150" x2="480" y2="150" stroke="#6EE7B7" stroke-width="2"/> <line x1="420" y1="150" x2="480" y2="90" stroke="#60A5FA" stroke-width="2"/> <line x1="420" y1="150" x2="480" y2="210" stroke="#F87171" stroke-width="2"/> <!-- 右侧:路由器下挂设备(增加间距) --> <!-- 笔记本电脑 --> <g transform="translate(480, 60)"> <rect x="0" y="0" width="80" height="50" rx="5" fill="#E5E7EB"/> <rect x="15" y="5" width="50" height="30" rx="3" fill="#D1D5DB"/> <rect x="20" y="10" width="40" height="20" fill="#F9FAFB"/> <rect x="30" y="35" width="20" height="10" fill="#9CA3AF"/> <text x="40" y="80" text-anchor="middle" font-size="10" fill="#4B5563">笔记本电脑</text> </g> <!-- 智能手机 --> <g transform="translate(480, 150)"> <rect x="0" y="0" width="40" height="60" rx="5" fill="#F9FAFB" stroke="#9CA3AF" stroke-width="2"/> <rect x="5" y="5" width="30" height="40" fill="#D1D5DB"/> <circle cx="20" y="55" r="3" fill="#9CA3AF"/> <text x="20" y="90" text-anchor="middle" font-size="10" fill="#4B5563">智能手机</text> </g> <!-- 台式电脑 --> <g transform="translate(480, 250)"> <rect x="0" y="0" width="50" height="40" rx="3" fill="#E5E7EB"/> <rect x="5" y="5" width="40" height="25" fill="#F9FAFB"/> <rect x="15" y="40" width="20" height="25" fill="#9CA3AF"/> <rect x="55" y="10" width="5" height="20" fill="#9CA3AF"/> <rect x="50" y="0" width="25" height="30" rx="2" fill="#D1D5DB"/> <text x="40" y="80" text-anchor="middle" font-size="10" fill="#4B5563">台式电脑</text> </g> </svg> `; // 模拟初始系统状态数据 const initialStatusData = { uploadRate: "1.2 Mbps", downloadRate: "15.8 Mbps", wanStatus: { status: "已联网", ipv4: "122.77.241.10", subnetMask: "255.255.255.0", gateway: "122.77.241.1", dns1: "8.8.8.8", dns2: "8.8.4.4" }, systemStatus: { model: "FAST FW300R", hostname: "Home-Router", cpuUsage: "24%", memoryUsage: "512MB / 1GB (51%)", uptime: "3天12小时45分钟", version: "V1.2.0" }, connectionStatus: { onlineDevices: 8, networkConnections: 24 } }; const [status, setStatus] = useState(null); const [refreshTime, setRefreshTime] = useState(new Date()); // 模拟获取系统状态 const refresh = () => { return new Promise(resolve => { setTimeout(() => { setStatus({ ...initialStatusData, connectionStatus: { onlineDevices: Math.floor(Math.random() * 5) + 6, networkConnections: Math.floor(Math.random() * 20) + 20 } }); setRefreshTime(new Date()); resolve(); }, 300); }); }; useEffect(() => { refresh(); const interval = setInterval(refresh, 60000); return () => clearInterval(interval); }, []); if (!status) return html`<div class="flex justify-center items-center h-32">加载中...</div>`; return html` <div class="m-4 grid grid-cols-1 gap-4"> <div class="py-1 divide-y border rounded bg-white flex flex-col"> <div class="flex items-center justify-between px-4 py-2"> <div class="font-light uppercase text-gray-600">网络拓扑状态</div> <div class="text-xs text-gray-500"> 最后更新: ${refreshTime.toLocaleTimeString()} <button class="ml-2 text-blue-600 hover:text-blue-800" onClick=${refresh} > 刷新 </button> </div> </div> <div class="py-2 px-5 flex-1 flex flex-col"> <div class="grid grid-cols-1 md:grid-cols-3 gap-4"> <!-- 网络拓扑视图 --> <div class="border rounded p-3 flex flex-col items-center col-span-1 md:col-span-3"> <h3 class="text-sm font-medium text-gray-700 mb-2">网络拓扑视图</h3> <div class="w-full h-64 object-contain" dangerouslySetInnerHTML=${{ __html: networkTopologySVG }} ></div> </div> <!-- WAN口状态 --> <div class="border rounded p-3"> <h3 class="text-sm font-medium text-gray-700 mb-2">WAN口状态</h3> <div class="space-y-2"> <div class="flex justify-between"> <span class="text-sm text-gray-600">状态</span> <span class="text-sm font-medium text-green-600">${status.wanStatus.status}</span> </div> <div class="flex justify-between"> <span class="text-sm text-gray-600">IPv4地址</span> <span class="text-sm font-medium">${status.wanStatus.ipv4}</span> </div> <div class="flex justify-between"> <span class="text-sm text-gray-600">子网掩码</span> <span class="text-sm font-medium">${status.wanStatus.subnetMask}</span> </div> <div class="flex justify-between"> <span class="text-sm text-gray-600">网关</span> <span class="text-sm font-medium">${status.wanStatus.gateway}</span> </div> <div class="flex justify-between"> <span class="text-sm text-gray-600">DNS1</span> <span class="text-sm font-medium">${status.wanStatus.dns1}</span> </div> <div class="flex justify-between"> <span class="text-sm text-gray-600">DNS2</span> <span class="text-sm font-medium">${status.wanStatus.dns2}</span> </div> </div> </div> <!-- 系统状态 --> <div class="border rounded p-3"> <h3 class="text-sm font-medium text-gray-700 mb-2">系统状态</h3> <div class="space-y-2"> <div class="flex justify-between"> <span class="text-sm text-gray-600">型号</span> <span class="text-sm font-medium">${status.systemStatus.model}</span> </div> <div class="flex justify-between"> <span class="text-sm text-gray-600">主机名</span> <span class="text-sm font-medium">${status.systemStatus.hostname}</span> </div> <div class="flex justify-between"> <span class="text-sm text-gray-600">固件版本</span> <span class="text-sm font-medium">${status.systemStatus.version}</span> </div> <div class="flex justify-between"> <span class="text-sm text-gray-600">CPU使用率</span> <span class="text-sm font-medium">${status.systemStatus.cpuUsage}</span> </div> <div class="flex justify-between"> <span class="text-sm text-gray-600">内存占用</span> <span class="text-sm font-medium">${status.systemStatus.memoryUsage}</span> </div> <div class="flex justify-between"> <span class="text-sm text-gray-600">运行时间</span> <span class="text-sm font-medium">${status.systemStatus.uptime}</span> </div> </div> </div> <!-- 连接状态 --> <div class="border rounded p-3"> <h3 class="text-sm font-medium text-gray-700 mb-2">连接状态</h3> <div class="space-y-3"> <div class="flex justify-between items-center"> <span class="text-sm text-gray-600">上传速率</span> <span class="text-sm font-medium">${status.uploadRate}</span> </div> <div class="w-full bg-gray-200 rounded-full h-2"> <div class="bg-blue-500 h-2 rounded-full" style="width: 30%"></div> </div> <div class="flex justify-between items-center mt-2"> <span class="text-sm text-gray-600">下载速率</span> <span class="text-sm font-medium">${status.downloadRate}</span> </div> <div class="w-full bg-gray-200 rounded-full h-2"> <div class="bg-green-500 h-2 rounded-full" style="width: 70%"></div> </div> <div class="grid grid-cols-2 gap-4 mt-4"> <div class="bg-blue-50 rounded p-3 text-center"> <div class="text-2xl font-bold">${status.connectionStatus.onlineDevices}</div> <div class="text-sm text-gray-600">在线终端</div> </div> <div class="bg-green-50 rounded p-3 text-center"> <div class="text-2xl font-bold">${status.connectionStatus.networkConnections}</div> <div class="text-sm text-gray-600">网络连接数</div> </div> </div> </div> </div> </div> </div> </div> </div>`; } 手机,电脑到路由器直接的距离远一点,给出修改后的完整代码
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值