userfactory.java
来自「动态代理实例源代码」· Java 代码 · 共 48 行
JAVA
48 行
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 + =
减小字号Ctrl + -
显示快捷键?