1. 程式人生 > 其它 >字串列表元組之間的轉換

字串列表元組之間的轉換

技術標籤:設計模式

靜態代理模式
ITeach.java

package com.godv.proxy;

public interface ITeach {
	public void teach();
}

TeacherManager.java

package com.godv.proxy;

public class TeacherManager implements ITeach{
	@Override
	public void teach() {
		// TODO Auto-generated method stub
		System.out.println("授課"
); } }

TeacherManagerProxy.java

package com.godv.proxy;

public class TeacherManagerProxy implements ITeach {

	private ITeach teach;

	public TeacherManagerProxy(ITeach teach) {
		this.teach = teach;
	}

	@Override
	public void teach() {
		// TODO Auto-generated method stub
		prepared();
		teach.teach
(); end(); } private void prepared() { System.out.println("準備"); } private void end() { System.out.println("結束"); } }

Client.java

package com.godv.proxy;

public class Client {
	public static void main(String[] args) {
		TeacherManager tm = new TeacherManager();
		TeacherManagerProxy tmp =
new TeacherManagerProxy(tm); tmp.teach(); } }

靜態代理模式JDK
ITeach.java

package com.godv.proxy.dynamic;

public interface ITeach {
	public void teach();

	public String getName(String name);
}

TeacherManager.java

package com.godv.proxy.dynamic;

public class TeacherManager implements ITeach {
	@Override
	public void teach() {
		// TODO Auto-generated method stub
		System.out.println("授課");
	}

	@Override
	public String getName(String name) {
		// TODO Auto-generated method stub
		return "godv : " + name;
	}
}

TeacherManagerProxyFactory.java

package com.godv.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class TeacherManagerProxyFactory {

	private Object obj;

	public TeacherManagerProxyFactory(Object obj) {
		this.obj = obj;
	}

	private void prepared() {
		System.out.println("準備");
	}

	private void end() {
		System.out.println("結束");
	}

	public Object getProxyInstance() {
		return Proxy.newProxyInstance(
				obj.getClass().getClassLoader(), 
				obj.getClass().getInterfaces(),
				new InvocationHandler() {

					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						// TODO Auto-generated method stub
						prepared();
						Object result = method.invoke(obj, args);
						end();
						return result;
					}
				});
	}
}

Client.java

package com.godv.proxy.dynamic;

public class Client {
	public static void main(String[] args) {
		TeacherManager tm = new TeacherManager();
		TeacherManagerProxyFactory tmp = new TeacherManagerProxyFactory(tm);
		ITeach proxyInstance = (ITeach) tmp.getProxyInstance();
		proxyInstance.teach();
		String name = proxyInstance.getName("nice");
		System.out.println(name);
	}
}

Cglib
需要導包
在這裡插入圖片描述
提供一個連結
連結:https://pan.baidu.com/s/1ZAgeMi329_ohpxI_GKAw3A
提取碼:sfhx

TeacherManager.java

package com.godv.proxy.cglibpro;

public class TeacherManager {
	public void teach() {
		// TODO Auto-generated method stub
		System.out.println("授課");
	}

	public String getName(String name) {
		// TODO Auto-generated method stub
		return "godv : " + name;
	}
}

ProxyFactory.java

package com.godv.proxy.cglibpro;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class ProxyFactory implements MethodInterceptor {

	private Object obj;

	public ProxyFactory(Object obj) {
		this.obj = obj;
	}

	private void prepared() {
		System.out.println("準備");
	}

	private void end() {
		System.out.println("結束");
	}

	// 返回一個代理物件: 是 target 物件的代理物件
	public Object getProxyInstance() {
		// 1. 建立一個工具類
		Enhancer enhancer = new Enhancer();
		// 2. 設定父類
		enhancer.setSuperclass(obj.getClass());
		// 3. 設定回撥函式
		enhancer.setCallback(this);
		// 4. 建立子類物件,即代理物件
		return enhancer.create();
	}

	@Override
	public Object intercept(Object arg0, Method method, Object[] args, MethodProxy arg3) throws Throwable {
		// TODO Auto-generated method stub
		prepared();
		Object result = method.invoke(obj, args);
		end();
		return result;
	}

}

Client.java

package com.godv.proxy.cglibpro;

public class Client {
	public static void main(String[] args) {
		TeacherManager tm = new TeacherManager();
		ProxyFactory pf = new ProxyFactory(tm);
		TeacherManager pftm = (TeacherManager) pf.getProxyInstance();
		pftm.teach();
		String name = pftm.getName("nice");
		System.out.println(name);
	}
}