lumiproxy
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    The static proxy pattern is one of the commonly used design patterns in software development, playing a significant role in various application domains. This article delves into the principles, applications, and advantages of the static proxy pattern, providing an in-depth analysis of its fundamental structure and roles. Introducing the Concept and Purpose of Static Proxy The static proxy pattern allows the creation of a proxy object to control access to other objects. Through the proxy object, additional operations such as access control, logging, etc., can be executed before or after accessing the actual object. ![1](https://hackmd.io/_uploads/ryJpJ5Mx0.png) Brief Overview of Application Areas and Advantages of Static Proxy The static proxy pattern finds wide application in software development, especially in areas like network programming, logging, transaction management, etc. Its advantages include clear code structure, strong extensibility, and logical separation. The static proxy pattern is particularly useful in: Network Programming: In network programming, static proxy can be used to implement Remote Procedure Call (RPC), forwarding local method calls to remote servers, and handling network communication details as needed. Logging: [Static proxy](https://www.lumiproxy.com/proxies/static/) can be utilized for logging method invocations, facilitating tracking of program execution flow during debugging and analysis. With the proxy object, logging logic can be added before and after method execution without modifying the original method implementation. Transaction Management: In scenarios requiring transaction management like database operations, static proxy can ensure transaction control. The proxy object can begin a transaction before method execution and commit or rollback the transaction based on the execution result, ensuring data consistency and integrity. Basic Principles of the Static Proxy Pattern Definition and Characteristics of Static Proxy Static proxy refers to establishing the relationship between proxy objects and delegate objects during compilation. It is characterized by compile-time determination and stable performance compared to dynamic proxy. Basic Structure and Working Principle of Static Proxy The static proxy pattern consists of proxy classes and delegate classes. The proxy class controls access to the delegate class and executes additional operations when necessary. The delegate class is the actual object being proxied, and the proxy class interacts with it through its instance. Role Analysis in the Static Proxy Pattern: Proxy Class, Delegate Class, Interface Proxy Class: Responsible for controlling access to the delegate class and executing additional operations. Proxy classes usually implement the same interface as the delegate class for interaction. Delegate Class: Executes the actual task. It is the instance of the delegate object and contains the real business logic. Interface: Defines the interaction specification between the proxy class and the delegate class, enabling proxy classes to interact with different delegate classes. In the static proxy pattern, proxy classes implement the same interface as the delegate classes, enabling them to replace delegate classes to accomplish tasks and execute additional operations when necessary, without modifying client code. ![2](https://hackmd.io/_uploads/ryWAyczxR.png) Application Case Analysis Logging Static proxy can be employed to log method invocations, aiding in debugging and analysis by tracing program execution flow. public interface UserService { void addUser(String username, String password); } public class UserServiceImpl implements UserService { @Override public void addUser(String username, String password) { // Actual business logic } } public class UserServiceProxy implements UserService { private UserService target; public UserServiceProxy(UserService target) { this.target = target; } @Override public void addUser(String username, String password) { System.out.println("Add user: " + username); target.addUser(username, password); System.out.println("User added successfully"); } } Access Control Static proxy can be utilized to implement access control, restricting user access to certain methods. public interface UserService { void addUser(String username, String password); } public class UserServiceImpl implements UserService { @Override public void addUser(String username, String password) { // Actual business logic } } public class UserServiceProxy implements UserService { private UserService target; public UserServiceProxy(UserService target) { this.target = target; } @Override public void addUser(String username, String password) { if (checkPermission()) { target.addUser(username, password); } else { throw new SecurityException("No permission to add user"); } } private boolean checkPermission() { // Permission checking logic return true; } } Practical Applications of the Static Proxy Pattern Static Proxy in Java Programming In Java programming, [static proxy](https://www.lumiproxy.com/proxies/static/) is commonly used in various scenarios. Below, we introduce examples of static proxy based on interface and inheritance. Example: Interface-based Static Proxy // Interfacepublic interface UserService { void addUser(String username, String password); } // Delegate classpublic class UserServiceImpl implements UserService { @Override public void addUser(String username, String password) { // Actual business logic } } // Proxy classpublic class UserServiceProxy implements UserService { private UserService target; public UserServiceProxy(UserService target) { this.target = target; } @Override public void addUser(String username, String password) { System.out.println("Before adding user"); target.addUser(username, password); System.out.println("After adding user"); } } Example: Inheritance-based Static Proxy // Base classpublic class BaseService { public void operation() { // Actual business logic } } // Proxy classpublic class ProxyService extends BaseService { @Override public void operation() { System.out.println("Before operation"); super.operation(); System.out.println("After operation"); } } Common Application Scenarios of Static Proxy in Software Development Static proxy finds wide application in software development, including but not limited to: Logging: Recording method invocations to trace program execution flow during debugging and analysis. Security Control: Restricting user access to certain methods to implement access control and permission management. Performance Monitoring: Collecting performance metrics such as execution time before and after method execution for performance monitoring and optimization. Access Control: Restricting method access based on various conditions such as user roles and permissions. ![4](https://hackmd.io/_uploads/SyOGlqfg0.png) Analysis of Advantages and Disadvantages of the Static Proxy Pattern Advantages Reduced Coupling: Separation of proxy classes and delegate classes reduces coupling between different parts of the system, making it easier to maintain and extend. Functionality Extension: Additional functionality or control logic can be added to the target object without modifying its implementation by using proxy classes. Access Control: Implementing access control for target objects by performing permission checks or parameter validation in proxy classes. Disadvantages Increased Code Complexity and Maintenance Cost: Creating proxy classes for each class increases code complexity and maintenance cost. Lack of Flexibility: The [static proxy](https://www.lumiproxy.com/proxies/static/) pattern lacks flexibility and cannot adapt to dynamically changing requirements, such as dynamically adding or modifying proxy objects. Comparison between Static Proxy and Dynamic Proxy Basic Principles and Characteristics of Dynamic Proxy Dynamic proxy is a mechanism to dynamically generate proxy classes at runtime, without the need to determine the specific type of proxy class at compile time. It is implemented based on Java's reflection mechanism, allowing the creation of proxy objects at runtime and execution of additional logic during method invocation. The main characteristics of dynamic proxy include: Runtime Generation: Proxy classes are dynamically generated at runtime, without the need for compile-time determination. No Manual Proxy Class Writing: Dynamic proxy can automatically generate proxy classes based on the interface of the target object, eliminating the need for manual proxy class code writing. Based on Reflection Mechanism: Dynamic proxy is implemented based on Java's reflection mechanism, enabling the invocation of methods on the target object through reflection at runtime. Differences and Application Scenarios between Static Proxy and Dynamic Proxy Static proxy and dynamic proxy differ in implementation principles and characteristics, and they are suitable for different scenarios. Static Proxy: The relationship between proxy class and delegate class is determined at compile time, and the code of the proxy class is static and cannot be dynamically modified. It is suitable for scenarios where the target object is known at compile time and the proxy object does not need to change dynamically. Dynamic Proxy: Proxy classes are dynamically generated at runtime, allowing dynamic addition, modification, or removal of proxy class functionalities. It is suitable for scenarios where the target object cannot be determined at compile time or where the functionality of the proxy object needs to change dynamically at runtime. ![3](https://hackmd.io/_uploads/SJ6ZeczeA.png) Example: Performance Comparison between Dynamic Proxy and Static Proxy To compare the performance difference between dynamic proxy and static proxy, we can conduct a simple benchmark test to evaluate their performance. // Delegate class interfacepublic interface UserService { void addUser(String username, String password); } // Delegate classpublic class UserServiceImpl implements UserService { @Override public void addUser(String username, String password) { // Actual business logic } } // Static Proxypublic class UserServiceProxy implements UserService { private UserService target; public UserServiceProxy(UserService target) { this.target = target; } @Override public void addUser(String username, String password) { // Static proxy logic } } // Dynamic Proxypublic class DynamicProxyHandler implements InvocationHandler { private Object target; public DynamicProxyHandler(Object target) { this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // Dynamic proxy logic return method.invoke(target, args); } } Conclusion [Static proxy and dynamic proxy](https://www.lumiproxy.com/proxies/static/) are both commonly used design patterns, each suitable for different scenarios. This article compares the implementation principles, characteristics, and application scenarios of static proxy and dynamic proxy, and demonstrates their differences through performance comparison. Static proxy is suitable for scenarios where the target object is known at compile time and the proxy object does not need to change dynamically, while dynamic proxy is suitable for scenarios where the target object cannot be determined at compile time or where the functionality of the proxy object needs to change dynamically at runtime. In the future, with technological advancements, the static proxy pattern may increasingly combine with the dynamic proxy pattern to leverage more advantages.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully