动态代理的5模式使用示例和Mixin模式
重量级的orm和ioc产品离不开动态代理,作为开发人员,多数情况不用关注动态代理的内部实现机制,但是了解其一般的规律和模式还是有必要的,比如:虽然你开发期间采用了poco,因为开启了动态代理,运行期间则不是poco。本文简单描述了5种代理生成模式和1种mixin模式,最后给出一个示例。
public interface iplayable
{
void play();
}
public class animal : iplayable
{
public virtual void play()
{
console.writeline("animal.play");
}
}
public class dog : animal
{
public override void play()
{
console.writeline("dog.play");
}
}
public interface irunable
{
void run();
}
public class runability : irunable
{
public void run()
{
console.writeline("runability.run");
}
}
public class animalinterceptor : iinterceptor
{
public void intercept(iinvocation invocation)
{
console.writeline("before animalinterceptor.intercept");
if (invocation.invocationtarget != null)
{
invocation.proceed();
}
console.writeline("after animalinterceptor.intercept");
}
}
第一种:classproxy
{
console.writeline("\n*************classproxy*************\n");
var generator = new proxygenerator();
var animal = generator.createclassproxy<animal>(new animalinterceptor());
animal.play();
console.writeline(animal.gettype());
console.writeline(animal.gettype().basetype);
var compositefield = animal.gettype().getfield("__target");
console.writeline(compositefield);
foreach (var interfacetype in animal.gettype().getinterfaces())
{
console.writeline(interfacetype);
}
}
第二种:classproxywithtarget
{
console.writeline("\n*************classproxywithtarget*************\n");
var generator = new proxygenerator();
var animal = generator.createclassproxywithtarget<animal>(new dog(), new animalinterceptor());
animal.play();
console.writeline(animal.gettype());
console.writeline(animal.gettype().basetype);
var compositefield = animal.gettype().getfield("__target");
console.writeline(compositefield);
foreach (var interfacetype in animal.gettype().getinterfaces())
{
console.writeline(interfacetype);
}
}
第三种:interfaceproxywithouttarget
{
console.writeline("\n*************interfaceproxywithouttarget*************\n");
var generator = new proxygenerator();
var animal = generator.createinterfaceproxywithouttarget<iplayable>(new animalinterceptor());
animal.play();
console.writeline(animal.gettype());
console.writeline(animal.gettype().basetype);
var compositefield = animal.gettype().getfield("__target");
console.writeline(compositefield);
foreach (var interfacetype in animal.gettype().getinterfaces())
{
console.writeline(interfacetype);
}
}
第四种:interfaceproxywithtarget
{
console.writeline("\n*************interfaceproxywithtarget*************\n");
var generator = new proxygenerator();
var animal = generator.createinterfaceproxywithtarget<iplayable>(new dog(), new animalinterceptor());
animal.play();
console.writeline(animal.gettype());
console.writeline(animal.gettype().basetype);
var compositefield = animal.gettype().getfield("__target");
console.writeline(compositefield);
foreach (var interfacetype in animal.gettype().getinterfaces())
{
console.writeline(interfacetype);
}
}
第五种:interfaceproxywithtargetinterface
{
console.writeline("\n*************interfaceproxywithtargetinterface*************\n");
var generator = new proxygenerator();
var animal = generator.createinterfaceproxywithtargetinterface<iplayable>(new dog(), new animalinterceptor());
animal.play();
console.writeline(animal.gettype());
console.writeline(animal.gettype().basetype);
var compositefield = animal.gettype().getfield("__target");
console.writeline(compositefield);
foreach (var interfacetype in animal.gettype().getinterfaces())
{
console.writeline(interfacetype);
}
}
mixin模式
{
console.writeline("\n*************mixin*************\n");
var generator = new proxygenerator();
var options = new proxygenerationoptions();
options.addmixininstance(new runability());
var animal = generator.createclassproxy<animal>(options, new animalinterceptor());
animal.play();
(animal as irunable).run();
console.writeline(animal.gettype());
console.writeline(animal.gettype().basetype);
var compositefield = animal.gettype().getfield("__target");
console.writeline(compositefield);
foreach (var field in animal.gettype().getfields())
{
if (field.name.startswith("__mixin"))
{
console.writeline(field);
}
}
foreach (var interfacetype in animal.gettype().getinterfaces())
{
console.writeline(interfacetype);
}
}
上一篇: log4net创建系统日志的详细步骤
推荐阅读
-
动态代理的5模式使用示例和Mixin模式
-
Java中的代理模式--静态代理和动态代理
-
Java代理模式中的静态代理和动态代理
-
Mybaits 源码解析 (十一)----- 设计模式精妙使用:静态代理和动态代理结合使用:@MapperScan将Mapper接口生成代理注入到Spring
-
Mybaits 源码解析 (十一)----- 设计模式精妙使用:静态代理和动态代理结合使用:@MapperScan将Mapper接口生成代理注入到Spring
-
Java代理设计模式(Proxy)的四种具体实现:静态代理和动态代理 Java设计模式DesignPattern代理模式proxy模式
-
JAVA动态代理和CGLIB模式的实现方法介绍(代码示例)
-
Java中的代理模式--静态代理和动态代理
-
JAVA动态代理和CGLIB模式的实现方法介绍(代码示例)