首页  

netty中的设计模式     所属分类 netty 浏览量 31
观察者模式    事件的监听和通知机制  
工厂模式    io.netty.channel.ReflectiveChannelFactory   根据 channelClass 创建 channel
策略模式     xxxStrategy 
装饰者模式   扩展 ByteBuf 功能     WrappedByteBuf
责任链模式   ChannelPipeline  xxxFilter  
单例模式
构建者模式   Bootstrap  通过构建者模式来设置各种参数   
模板方法模式    服务端和客户端代码模板  , ByteToMessageDecoder 抽象方法 decode  , 子类继承父类,覆写父类的抽象方法
迭代器模式     ChannelPipeline 接口继承 Iterable  , CompositeByteBuf 
适配器模式        ChannelInboundHandlerAdapter     
Reactor 模式   使用 Reactor 模式来管理多个连接和事件,通过一个主线程监听事件并分发给相应的处理线程


通过装饰者模式,在不改变 ByteBuf 原有接口的情况下,为其添加额外的功能,如内存泄漏检测、释放等 final class DefaultSelectStrategy implements SelectStrategy { static final SelectStrategy INSTANCE = new DefaultSelectStrategy(); private DefaultSelectStrategy() { } @Override public int calculateStrategy(IntSupplier selectSupplier, boolean hasTasks) throws Exception { // 是否有任务 ,有就不阻塞 ,否则就阻塞 return hasTasks ? selectSupplier.get() : SelectStrategy.SELECT; } } EventLoopGroup bossGrpup = new NioEventLoopGroup(1); EventLoopGroup wrokerGrpup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGrpup,wrokerGrpup) // ReflectiveChannelFactory 根据 channelClass 创建 channel .channel(NioServerSocketChannel.class) // 模板 实现不同的 handler .childHandler(new ChannelInitializer< SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LoggingHandler()); } }); ChannelFuture f = bootstrap.bind(8888).sync(); f.channel().closeFuture().sync(); io.netty.channel.DefaultChannelPipeline#DefaultChannelPipeline protected DefaultChannelPipeline(Channel channel) { this.channel = ObjectUtil.checkNotNull(channel, "channel"); succeededFuture = new SucceededChannelFuture(channel, null); voidPromise = new VoidChannelPromise(channel, true); tail = new TailContext(this); head = new HeadContext(this); head.next = tail; tail.prev = head; } io.netty.util.concurrent.DefaultEventExecutorChooserFactory public final class DefaultEventExecutorChooserFactory implements EventExecutorChooserFactory { // 单例模式 public static final DefaultEventExecutorChooserFactory INSTANCE = new DefaultEventExecutorChooserFactory(); private DefaultEventExecutorChooserFactory() { } // 策略模式 public EventExecutorChooser newChooser(EventExecutor[] executors) { if (isPowerOfTwo(executors.length)) { return new PowerOfTowEventExecutorChooser(executors); } else { return new GenericEventExecutorChooser(executors); } } io.netty.handler.codec.ByteToMessageDecoder#decode // 抽象方法 子类实现 protected abstract void decode(ChannelHandlerContext ctx, ByteBuf in, List< Object> out) throws Exception; io.netty.handler.codec.ByteToMessageDecoder#callDecode 调用 decode 方法 CompositeByteBuf 是一个实现了 ByteBuf 接口的类,它使用组合的方式来管理多个 ByteBuf 对象。 CompositeByteBuf 本身并不是迭代器模式的一部分,但它在内部使用了一个迭代器来遍历和管理组成它的多个 ByteBuf 实例 CompositeByteBuf 的特点: 组合多个 ByteBuf:CompositeByteBuf 可以包含多个 ByteBuf 对象,这些对象可以是任何实现了 ByteBuf 接口的实例。 视图整合:对于外部使用者来说,CompositeByteBuf 表现得就像一个单一的 ByteBuf,尽管它内部是由多个部分组成的。 动态调整:可以动态地向 CompositeByteBuf 中添加或删除 ByteBuf,就像在一个大的缓冲区中操作一样。

上一篇     下一篇
netty VS mina

热锅冷油,炒菜不粘锅的科学原理

netty Event Handler 和 Pipeline

Java类命名建议

中证A500指数

JDK future 与 netty future promise 实例