⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userfactory.java

📁 动态代理实例源代码
💻 JAVA
字号:
package com.lakeviewtech;

import java.lang.reflect.Proxy;

/**
 * Simple factory class for creating instances of the User interface.
 * In this way, we can shield client code from the implementation of this
 * interface, and provide any implementation we desire without changing
 * the client code.
 */
public class UserFactory {

    private UserFactory() {
    }

    /**
     * Creates a new User instance.
     */
    public static User create() {
        // create a new implementation:
        return new UserImpl();
    }

    /**
     * Demo application.
     */
    public static void main(String[] args) {
        try {
            User user = UserFactory.create();
            user.setUsername("fred");
            user.setPassword("pw");
            System.out.println(user);

            user = UserFactory.create();
            user.setUsername("barney");
            //user.setPassword("pw");

            /* set the password to null, this should trigger a validation
               failure.
            */
            user.setPassword(null);
            System.out.println(user);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -