Netty中使用的設計模式

曹自標發表於2021-01-19

建立型

簡單工廠
public class DefaultThreadFactory implements ThreadFactory {
    @Override
    public Thread newThread(Runnable r) {
        Thread t = newThread(FastThreadLocalRunnable.wrap(r), prefix + nextId.incrementAndGet());
        try {
            if (t.isDaemon() != daemon) {
                t.setDaemon(daemon);
            }

            if (t.getPriority() != priority) {
                t.setPriority(priority);
            }
        } catch (Exception ignored) {
            // Doesn't matter even if failed to set.
        }
        return t;
    }

    protected Thread newThread(Runnable r, String name) {
        return new FastThreadLocalThread(threadGroup, r, name);
    }
}

建造者模式
public final class ClientCookieDecoder extends CookieDecoder {

    ...
    if (cookieBuilder == null) {
        cookieBuilder = new CookieBuilder(cookie, header);
    } else {
        cookieBuilder.appendAttribute(nameBegin, nameEnd, valueBegin, valueEnd);
    }
    
    private static class CookieBuilder {
        void appendAttribute(int keyStart, int keyEnd, int valueStart, int valueEnd) {}
        
        private void parse4(int nameStart, int valueStart, int valueEnd) {
            ...
        }

        private void parse6(int nameStart, int valueStart, int valueEnd) {
            ...
        }
    } 
}
單例模式

建構函式private修飾

public final class ReadTimeoutException extends TimeoutException {

    private static final long serialVersionUID = 169287984113283421L;

    public static final ReadTimeoutException INSTANCE = PlatformDependent.javaVersion() >= 7 ?
            new ReadTimeoutException(true) : new ReadTimeoutException();

    ReadTimeoutException() { }

    private ReadTimeoutException(boolean shared) {
        super(shared);
    }
}
public final class MqttEncoder extends MessageToMessageEncoder<MqttMessage> {

    public static final MqttEncoder INSTANCE = new MqttEncoder();

    private MqttEncoder() { }
}

結構型

策略模式

在新連線接入的時候,EventExcutorChooser會根據EventExcutor的長度來進行選擇。因為預設建構函式是建立CPU兩倍的執行緒,也可以使用者自定義執行緒個數。那麼這裡將接入的新連線交給哪個執行緒,就用到了這種策略模式。

public final class DefaultEventExecutorChooserFactory implements EventExecutorChooserFactory {

    @Override
    public EventExecutorChooser newChooser(EventExecutor[] executors) {
        if (isPowerOfTwo(executors.length)) {
            return new PowerOfTwoEventExecutorChooser(executors);
        } else {
            return new GenericEventExecutorChooser(executors);
        }
    }
    
    private static final class PowerOfTwoEventExecutorChooser implements EventExecutorChooser {
        private final AtomicInteger idx = new AtomicInteger();
        private final EventExecutor[] executors;

        PowerOfTwoEventExecutorChooser(EventExecutor[] executors) {
            this.executors = executors;
        }

        @Override
        public EventExecutor next() {
            return executors[idx.getAndIncrement() & executors.length - 1];
        }
    }

    private static final class GenericEventExecutorChooser implements EventExecutorChooser {
        // Use a 'long' counter to avoid non-round-robin behaviour at the 32-bit overflow boundary.
        // The 64-bit long solves this by placing the overflow so far into the future, that no system
        // will encounter this in practice.
        private final AtomicLong idx = new AtomicLong();
        private final EventExecutor[] executors;

        GenericEventExecutorChooser(EventExecutor[] executors) {
            this.executors = executors;
        }

        @Override
        public EventExecutor next() {
            return executors[(int) Math.abs(idx.getAndIncrement() % executors.length)];
        }
    }
}
裝飾模式

WrappedByteBuf、UnreleasableByteBuf、SimpleLeakAwareByteBuf。第一個類就相當於裝飾者父類,後兩個就是裝飾類,而ByteBuf就是原型。

class WrappedByteBuf extends ByteBuf {
    @Override
    public boolean release(int decrement) {
        return buf.release(decrement);
    }
}


class SimpleLeakAwareByteBuf extends WrappedByteBuf {
@Override
public boolean release(int decrement) {
if (super.release(decrement)) {
closeLeak();
return true;
}
return false;
}

private void closeLeak() {
    // Close the ResourceLeakTracker with the tracked ByteBuf as argument. This must be the same that was used when
    // calling DefaultResourceLeak.track(...).
    boolean closed = leak.close(trackedByteBuf);
    assert closed;
}

}


行為型

介面卡模式
public class ChannelInboundHandlerAdapter extends ChannelHandlerAdapter implements ChannelInboundHandler {}

public abstract class ChannelHandlerAdapter implements ChannelHandler {}

public interface ChannelHandler {
    void handlerAdded(ChannelHandlerContext ctx) throws Exception;
}
代理模式
public abstract class ProxyHandler extends ChannelDuplexHandler {
    private final SocketAddress proxyAddress;
    
    protected ProxyHandler(SocketAddress proxyAddress) {
        this.proxyAddress = ObjectUtil.checkNotNull(proxyAddress, "proxyAddress");
    }
    
    @Override
    public final void connect(
            ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
            ChannelPromise promise) throws Exception {

        if (destinationAddress != null) {
            promise.setFailure(new ConnectionPendingException());
            return;
        }

        destinationAddress = remoteAddress;
        ctx.connect(proxyAddress, localAddress, promise);
    }
}
觀察者模式

ChannelFuture中有個重要的addListener方法,會新增特定的監聽器到future中,這些監聽器會在future isDone返回true的時候立刻被通知

public interface ChannelFuture extends Future<Void> {

    @Override
    ChannelFuture addListener(GenericFutureListener<? extends Future<? super Void>> listener);
}
迭代器模式
public abstract class AbstractByteBuf extends ByteBuf {
    @Override
    public int forEachByte(ByteProcessor processor) {
        ensureAccessible();
        try {
            return forEachByteAsc0(readerIndex, writerIndex, processor);
        } catch (Exception e) {
            PlatformDependent.throwException(e);
            return -1;
        }
    }
    
    int forEachByteAsc0(int start, int end, ByteProcessor processor) throws Exception {
        for (; start < end; ++start) {
            if (!processor.process(_getByte(start))) {
                return start;
            }
        }

        return -1;
    }
}
責任鏈模式

責任鏈模式(Chain of Responsibility Pattern)為請求建立了一個接收者物件的鏈

public interface ChannelPipeline
        extends ChannelInboundInvoker, ChannelOutboundInvoker, Iterable<Entry<String, ChannelHandler>> {
        
    ChannelPipeline addFirst(String name, ChannelHandler handler);
    ChannelPipeline addLast(String name, ChannelHandler handler);
    ChannelPipeline addBefore(String baseName, String name, ChannelHandler handler);
    ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler);
}

相關文章