An in-depth explanation of java design patterns - static proxy
1. Define general interface
/** * 定义通用接口 * @author Administrator * @date 2018/7/22 */ public interface IBusiness { /** * 接口执行方法 */ void execute(); }
2. Define real operation implementation class
/** * 真实操作实现类 * @author Administrator * @date 2018/7/22 */ public class BusinessImpl implements IBusiness{ @Override public void execute() { System.out.println("真正执行操作......"); } }
3. Static proxy operation
/** * 静态代理 * @author Administrator * @date 2018/7/22 */ public class ProxyBusiness implements IBusiness{ /** * 定义需要执行的接口 */ private IBusiness business; /** * 通过set方法传参,真实需要执行的对象 */ public void setBusiness(IBusiness business) { this.business = business; } /** * 代理对象执行操作 */ @Override public void execute() { if(business == null){ System.out.println("实际操作对象为null"); return; } before(); business.execute(); after(); } /** * 执行操作前 */ public void before(){ System.out.println("执行操作前......"); } /** * 执行操作后 */ public void after(){ System.out.println("执行操作后......"); } /** * 测试 */ public static void main(String[] args) { ProxyBusiness proxyBusiness = new ProxyBusiness(); BusinessImpl business = new BusinessImpl(); proxyBusiness.setBusiness(business); proxyBusiness.execute(); } }
Summary: Static proxy can pass Different implementation classes that implement common interfaces are used to complete related agents, but if they are not the same general interface, it is invalid; many interfaces and agent classes need to be implemented, resulting in large agent interfaces and implementation classes that are inconvenient to maintain. It is recommended to use dynamic proxy.
The above is the detailed content of An in-depth explanation of java design patterns - static proxy. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use