Young87

SmartCat's Blog

So happy to code my life!

游戏开发交流QQ群号60398951

当前位置:首页 >跨站数据测试

netty中的设计模式

设计模式在 Netty 中的应用

单例模式源码举例

单例模式要点回顾:

1、一个类在任何情况下只有一个对象,并提供一个全局访问点。

2、可延迟创建。

3、避免线程安全问题

案例分析

@Sharable 
public final class MqttEncoder extends MessageToMessageEncoder<MqttMessage> { 
    public static final MqttEncoder INSTANCE = new MqttEncoder(); 
    private MqttEncoder() { }
    protected void encode(ChannelHandlerContext ctx, MqttMessage msg, List<Object> out) throws Exception { 
    out.add(doEncode(ctx.alloc(), msg)); 
    }... 
}

策略模式源码举例

策略模式要点回顾:

1、封装一系列可相互替换的算法家族。

2、动态选择某一个策略。

案例分析:

public final class DefaultEventExecutorChooserFactory implements EventExecutorChooserFactory { 
    public static final DefaultEventExecutorChooserFactory INSTANCE = new        DefaultEventExecutorChooserFactory(); 
    private DefaultEventExecutorChooserFactory() { 
    }
    public EventExecutorChooser newChooser(EventExecutor[] executors) { 
        return (EventExecutorChooser)(isPowerOfTwo(executors.length)?new 
        DefaultEventExecutorChooserFactory.PowerOfTowEventExecutorChooser(executors):new 
        DefaultEventExecutorChooserFactory.GenericEventExecutorChooser(executors)); 
    }
​
    private static boolean isPowerOfTwo(int val) { 
        return (val & -val) == val; 
    }
... 
}

装饰者模式源码举例

装饰者模式要点回顾:

1、装饰者和被装饰者实现同一个接口。

2、装饰者通常继承被装饰者,同宗同源。

3、动态修改、重载被装饰者的方法

WrappedByteBuf :

class WrappedByteBuf extends ByteBuf { 
    protected final ByteBuf buf; 
    protected WrappedByteBuf(ByteBuf buf) { 
        if(buf == null) { 
            throw new NullPointerException("buf"); 
        } else { 
            this.buf = buf; 
        } 
    }
... 
}

UnreleasableByteBuf :

final class UnreleasableByteBuf extends WrappedByteBuf { 
    private SwappedByteBuf swappedBuf; 
    UnreleasableByteBuf(ByteBuf buf) { super(buf); }
    ...
    public boolean release() { return false; }
    public boolean release(int decrement) { return false;}
}

SimpleakAwareByteBuf:

final class SimpleLeakAwareByteBuf extends WrappedByteBuf { 
    private final ResourceLeak leak; 
    SimpleLeakAwareByteBuf(ByteBuf buf, ResourceLeak leak) { 
        super(buf); 
        this.leak = leak; 
    }
    ... 
    public boolean release() { 
        boolean deallocated = super.release(); 
        if(deallocated) { 
            this.leak.close(); 
        }
        return deallocated; 
    }
    public boolean release(int decrement) { 
        boolean deallocated = super.release(decrement); 
        if(deallocated) { 
            this.leak.close(); 
        }
        return deallocated; 
    } 
}

观察者模式源码举例

观察者模式要点回顾:

1、两个角色:观察者和被观察者。

2、观察者订阅消息,被观察者发布消息。

3、订阅则能收到消息,取消订阅则收不到。

channel.writeAndFlush()方法:

AbstractChannel:

public abstract class AbstractChannel extends DefaultAttributeMap implements Channel {      ... 
    public ChannelFuture writeAndFlush(Object msg) { 
        return  this.pipeline.writeAndFlush(msg); 
    }
                                                                                            public ChannelFuture writeAndFlush(Object msg, ChannelPromise promise) {                     return this.pipeline.writeAndFlush(msg, promise);                                     }

迭代器模式源码举例

迭代器模式要点回顾:

\1. 实现迭代器接口

\2. 实现对容器中的各个对象逐个访问的方法。

public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements Iterable<ByteBuf> { 
    protected byte _getByte(int index) { 
        CompositeByteBuf.Component c = this.findComponent(index); 
        return c.buf.getByte(index - c.offset); 
    }
    ... 
}

责任链模式源码举例

责任链:是指多个对象都有机会处理同一个请求,从而避免请求的发送者和接收者之间的耦合关系。然后,将这些对 象连成一条链,并且沿着这条链往下传递请求,直到有一个对象可以处理它为止。在每个对象处理过程中,每个对象 只处理它自己关心的那一部分,不相关的可以继续往下传递,直到链中的某个对象不想处理,可以将请求终止或丢弃。

责任链模式要点回顾:

1、需要有一个顶层责任处理接口(ChannelHandler)。

2、需要有动态创建链、添加和删除责任处理器的接口(ChannelPipeline)。

3、需要有上下文机制(ChannelHandlerContext)。

4、需要有责任终止机制(不调用 ctx.fireXXX()方法,则终止传播)

AbstractChannelHandlerContext:

abstract class AbstractChannelHandlerContext extends DefaultAttributeMap implements ChannelHandlerContext, ResourceLeakHint {
    private AbstractChannelHandlerContext findContextInbound() {                                AbstractChannelHandlerContext ctx = this;
        do {
            ctx = ctx.next;
           } 
          while(!ctx.inbound); 
           return ctx;
    }
}

工厂模式源码举例

工厂模式要点回顾:

1、将创建对象的逻辑封装起来。

ReflectiveChannelFactory:

public class ReflectiveChannelFactory<T extends Channel> implements ChannelFactory<T> { 	private final Class<? extends T> clazz; 
	public ReflectiveChannelFactory(Class<? extends T> clazz) { 
    	if(clazz == null) { 
            throw new NullPointerException("clazz"); 
        } else { 
            this.clazz = clazz; 
        } 
    }
    public T newChannel() {                                                                     try {
       return (Channel)this.clazz.newInstance(); 
    } catch (Throwable var2) { 
    	throw new ChannelException("Unable to create Channel from class " + this.clazz, var2); 
  } 
 }
}

除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog

上一篇: 高通Adreno Vulkan 开发(3)

下一篇: 现在不能打开“id=***”,因为它正用于其他用途,例如移动项目、拷贝项目或清倒废纸篓。请在当前任务完成后再试一次。

精华推荐