代理模式那些事

直接上代码

public class MarkCompany implements InvocationHandler {

private Objectfactory;

public Object getFactory() {

return factory;

}

public void setFactory(Object factory){

this.factory = factory;

}

//通过Proxy获得动态代理

    public Object getProxyIn(){

return Proxy.newProxyInstance(factory.getClass().getClassLoader(),

factory.getClass().getInterfaces(),this);

}

//通过动态代理对象进行增强

    @Override

    public Object invoke(Object o, Method method, Object[] objects)throws Throwable {

doSthAfter();

Object invoke = method.invoke(factory, objects);

doSthBefore();

return invoke;

}

private void doSthAfter(){

Log.i("TAG","doSthAfter: MarkCompany精美包装,快递一条龙服务");

}

private void doSthBefore(){

Log.i("TAG","doSthBefore: MarkCompany根据需求,进行市场调研和产品分析");

}

}


public class AaFactoryimplements ManToolsFactory{

@Override

    public void saleManTools(String size) {

Log.i("TAG","AaFactory saleManTools: "+"按需定制一个size为"+size+"的女model");

}

}


public class Alivenimplements WomanToolsFactory {

@Override

    public void saleWomanTools(float length) {

doSthAfter();

womanToolsFactory.saleWomanTools(length);

doSthAfter();

}

private void doSthAfter(){

Log.i("TAG"," Aliven doSthAfter: 精美包装,快递一条龙服务");

}

private void doSthBefore(){

Log.i("TAG","Aliven doSthBefore: 根据需求,进行市场调研和产品分析");

}

private WomanToolsFactorywomanToolsFactory;

public Aliven(WomanToolsFactory womanToolsFactory){

this.womanToolsFactory = womanToolsFactory;

}

}



public class BbFactory implements WomanToolsFactory{

@Override

    public void saleWomanTools(float length) {

Log.i("TAG","BbFactory saleManTools: "+"按需定制一个size为"+length+"的男model");

}

}



public interface ManToolsFactory {

void saleManTools(String size);

}


//代理对象

public class Mark implements ManToolsFactory ,WomanToolsFactory{

public ManToolsFactoryfactory;

public Mark(ManToolsFactory manToolsFactory){

factory = manToolsFactory;

}

@Override

    public void saleManTools(String size) {

doSthAfter();

factory.saleManTools(size);

doSthBefore();

}

private void doSthAfter(){

Log.i("TAG","Mark doSthAfter: 精美包装,快递一条龙服务");

}

private void doSthBefore(){

Log.i("TAG","Mark doSthBefore: 根据需求,进行市场调研和产品分析");

}

@Override

    public void saleWomanTools(float length) {

}

private WomanToolsFactorywomanToolsFactory;

public Mark(WomanToolsFactory womanToolsFactory){

this.womanToolsFactory = womanToolsFactory;

}

}


public interface WomanToolsFactory {

void saleWomanTools(float length);

}


在需要的地方使用对应的方法

private void factory(){

//静态代理

        ManToolsFactory manToolsFactory =new AaFactory();

Mark mark =new Mark(manToolsFactory);

//静态代理

        mark.saleManTools("D");

//静态代理

        WomanToolsFactory womanToolsFactory =new BbFactory();

Aliven aliven =new Aliven(womanToolsFactory);

aliven.saleWomanTools(10f);

//动态代理

//举例1

        WomanToolsFactory womanToolsFactory1 =new BbFactory();

MarkCompany markCompany =new MarkCompany();

markCompany.setFactory(womanToolsFactory1);

WomanToolsFactory employ1 = (WomanToolsFactory)markCompany.getProxyIn();

employ1.saleWomanTools(12f);

//举例2

        ManToolsFactory manToolsFactory1 =new AaFactory();

markCompany.setFactory(manToolsFactory1);

ManToolsFactory employ2 = (ManToolsFactory)markCompany.getProxyIn();

employ2.saleManTools("111");

}

你可能感兴趣的:(代理模式那些事)