創(chuàng)建對象
1. 構(gòu)造器創(chuàng)建對象
Student stu1 = new Student("kygo", 100);
System.out.println(stu1);
2. 通過克隆創(chuàng)建對象(內(nèi)存復(fù)制)
實(shí)現(xiàn)Cloneable接口满败,重寫clone()方法
@Override
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException();
}
}
測試
Student stu2 = (Student) stu1.clone();
System.out.println(stu2);
3. 通過反序列化創(chuàng)建對象(從流中讀取對象)
實(shí)現(xiàn)序列化接口Serializable
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(stu2);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream ois = new ObjectInputStream(in);
Student stu3 = (Student) ois.readObject();
System.out.println(stu3);
4. 通過反射(reflection)創(chuàng)建對象
拿到一個類的原始數(shù)據(jù)類對象
1.根據(jù)路徑
Class<?> clazz1 = Class.forName("com.kygo.Student");
2.用類.class
Class<?> clazz2 = Student.class;
3.用類對象實(shí)例的getClass()方法
Class<?> clazz3 = stu3.getClass();
根據(jù)類的類對象可以創(chuàng)建這個類的對象實(shí)例
Student stu4 = (Student) clazz1.newInstance();
根據(jù)類的類對象使用getConstructor()方法的到這個類的構(gòu)造器赏陵,使用構(gòu)造器可以創(chuàng)建對象
Constructor<?> constructor = clazz2.getConstructor(String.class, int.class);
Student stu5 = (Student) constructor.newInstance("wang Dachui", 18);
測試:
Class<?> clazz1 = Class.forName("com.kygo.Student");
Class<?> clazz2 = Student.class;
Class<?> clazz3 = stu3.getClass();
// System.out.println(clazz1 == clazz2); // true
// System.out.println(clazz2 == clazz3); // true
Student stu4 = (Student) clazz1.newInstance();
System.out.println(stu4);
// System.out.println(stu4.getName());
Constructor<?> constructor = clazz2.getConstructor(String.class, int.class);
Student stu5 = (Student) constructor.newInstance("wang Dachui", 18);
System.out.println(stu5);
// System.out.println(stu5.getName());
// System.out.println(stu5.getAge());
Constructor<?>[] constructors = clazz3.getConstructors();
for (Constructor<?> temp : constructors) {
Student stu6 = (Student) temp.newInstance();
System.out.println(stu6);
}
設(shè)計(jì)模式
單例
單例 - 讓一個類只能創(chuàng)建出一個對象(服務(wù)器上常用)
- 將構(gòu)造器私有(不允許直接使用構(gòu)造器創(chuàng)建對象)
- 通過公開的靜態(tài)的方法向外界返回該類的唯一實(shí)例
餓漢式單例
public class StudentManager {
private static StudentManager instance = new StudentManager();
// ...
private StudentManager() {
}
public static StudentManager getInstance() {
return instance;
}
// ...
}
懶漢式單例(懶加載單例)
public class StudentManager {
private static StudentManager instance = null;
// ...
private StudentManager() {
}
public static synchronized StudentManager getInstance() {
if (instance == null) {
instance = new StudentManager();
}
return instance;
}
// ...
}
登記式單例
public class ServiceFactory {
private static Map<Class<?>, Object> map = new HashMap<>();
// 登記式單例
static {
map.put(DeptService.class,
ServiceProxy.getProxyInstance(new DeptServiceImpl()));
map.put(EmpService.class,
ServiceProxy.getProxyInstance(new EmpServiceImpl()));
}
private ServiceFactory() {
throw new AssertionError();
}
public static Object factory(Class<?> serviceType) {
return map.get(serviceType);
}
}
一般類里面沒有狀態(tài)屬性都可以改造成單例模式類
簡單工廠模式
簡單工廠模式(靜態(tài)工廠模式)
將對象的實(shí)例化過通過工廠方法隱藏起來
使得對象的使用者可以和具體的對象以及創(chuàng)建對象的過程解耦合
例子1:在工廠用構(gòu)造器創(chuàng)建對象
工廠類:
public class FruitFactory {
public static Fruit factory(String type) {
Fruit fruit = null;
switch (type.toLowerCase()) {
case "apple":
fruit = new Apple();
break;
case "banana":
fruit = new Banana();
break;
case "durian":
fruit = new Durian();
break;
}
return fruit;
}
}
測試:
Fruit apple = FruitFactory.factory("apple");
Fruit durian = FruitFactory.factory("durian");
Fruit grape = FruitFactory.factory("grape");
System.out.println(apple);
System.out.println(durian);
System.out.println(grape);
例子2:使用反射
工廠類:
public class FruitFactory {
public static Fruit factory(String className) {
try {
Class<?> clazz = Class.forName(className);
return (Fruit) clazz.newInstance();
} catch (Exception e) {
// throw new RuntimeException();
return null;
}
}
public static Fruit factory(Class<?> fruitType) {
try {
return (Fruit) fruitType.newInstance();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
測試:
Fruit apple = FruitFactory.factory(Apple.class);
Fruit durian = FruitFactory.factory(Durian.class);
Fruit grape = FruitFactory.factory("com.kygo.Grape");
System.out.println(apple);
System.out.println(durian);
System.out.println(grape);
動態(tài)代理
JDK 1.3引入了動態(tài)代理機(jī)制 - 可以通過反射的方式動態(tài)生成代理對象
InvocationHandler - invoke(Object, Method, Object[])
Proxy類 - newProxyInstance(ClassLoader, Class<?>[], InvocationHandler)
舊做法:創(chuàng)建代理類并實(shí)現(xiàn)要代理的類相同的接口喷舀,重寫接口方法艺演,在要代理的方法亲澡,添加自己的東西(日志等)
例子1:考生找槍手代考
考生接口:
public interface Candidate {
public void joinExam();
}
人類:
public class Person {
protected String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
懶學(xué)生:
public class LazyStudent extends Person implements Candidate {
public LazyStudent(String name) {
super(name);
}
@Override
public void joinExam() {
System.out.println("姓名: " + name);
}
}
槍手:
public class ExamAgent extends Person implements Candidate {
private Candidate target;
public ExamAgent(String name) {
super(name);
}
public void setTarget(Candidate target) {
this.target = target;
}
@Override
public void joinExam() {
target.joinExam();
System.out.println(name + "奮筆疾書答案.");
System.out.println(name + "答題完畢交卷.");
}
}
測試:
LazyStudent student = new LazyStudent("王大錘");
ExamAgent agent = new ExamAgent("駱昊");
agent.setTarget(student);
agent.joinExam();
現(xiàn)在:
- 第一步 創(chuàng)建代理類并實(shí)現(xiàn)InvocationHandler的接口
class ListProxy<E> implements InvocationHandler
- 第二步 添加被代理對象的屬性姥芥,和代理對象的構(gòu)造器 - 單例
private Object target; // 被代理的對象(目標(biāo)對象)
private ListProxy(Object target) {
this.target = target;
}
- 第二步 實(shí)現(xiàn)靜態(tài)方法getProxyInstance()拿到代理類對象
通過Proxy工具類的newProxyInstance方法生成動態(tài)代理
public static <E> Object getProxyInstance(Object target) {
Class<?> clazz = target.getClass();
// 通過Proxy工具類的newProxyInstance方法生成動態(tài)代理
// 第一個參數(shù)是被代理對象的類加載器
// 第二個參數(shù)是被代理對象實(shí)現(xiàn)的接口(被代理對象和代理對象要實(shí)現(xiàn)相同的接口)
// 第三個參數(shù)是實(shí)現(xiàn)了InvocationHandler接口的對象
return Proxy.newProxyInstance(clazz.getClassLoader(),
clazz.getInterfaces(), new ListProxy<E>(target));
}
- 第三步 重寫回調(diào)方法invoke()
InvocationHandler接口(單方法接口/函數(shù)式接口)中的invoke方法是一個回調(diào)方法
當(dāng)執(zhí)行被代理對象target的任何一個方法時不是直接執(zhí)行而是回調(diào)代理對象的invoke方法
該方法的第二個參數(shù)代表了需要執(zhí)行的方法 第三個參數(shù)是執(zhí)行方法時傳入的參數(shù)
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
// InvocationHandler接口(單方法接口/函數(shù)式接口)中的invoke方法是一個回調(diào)方法
// 當(dāng)執(zhí)行被代理對象target的任何一個方法時不是直接執(zhí)行而是回調(diào)代理對象的invoke方法
// 該方法的第二個參數(shù)代表了需要執(zhí)行的方法 第三個參數(shù)是執(zhí)行方法時傳入的參數(shù)
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 被代理對象的所有方法都要通過代理對象來幫它調(diào)用執(zhí)行
// 調(diào)用被代理對象的方法 第一個參數(shù)是被代理的對象 第二個參數(shù)是方法參數(shù)
// 如果被代理對象當(dāng)前要執(zhí)行的方法沒有參數(shù)那么args的值就是null
// 如果被代理對象的方法有返回值那么retValue就保存該返回值
// 如果被代理對象的方法沒有返回值那么retValue的值就是null
Object retValue = method.invoke(target, args);
// 如果執(zhí)行的方法名以add据悔、remove幼驶、clear艾杏、retain打頭
// 那么通過下面代碼給這些方法增加在控制臺打印日志的功能
String methodName = method.getName();
// System.out.println("正在執(zhí)行" + methodName + "方法");
if (methodName.startsWith("add") || methodName.startsWith("remove")
|| methodName.startsWith("clear") || methodName.startsWith("retain")) {
if (target instanceof List) {
List<E> list = (List<E>) target;
// 以下代碼就是代理對象附加的行為(被代理對象沒有的行為)
System.out.println("Size = " + list.size());
System.out.println(Arrays.toString(list.toArray()));
}
}
// 將之前執(zhí)行被代理對象的方法得到的返回值返回
return retValue;
}
}