文章中整理了部分設計模式,有些設計模式尚未吃透,因此暫且不提。算法
public class Singleton1 {
public static final Singleton1 INSTANCE = new Singleton1();
private Singleton1(){}
}
複製代碼
public class Singleton2_1 {
private Singleton2_1() {}
private static Singleton2_1 instance = null;
public static Singleton2_1 getInstance() {
if (null == instance) {
instance = new Singleton2_1();
}
return instance;
}
}
複製代碼
public class Singleton2_2 {
private Singleton2_2() {}
private static Singleton2_2 instance = null;
public static Singleton2_2 getInstance() {
if (null == instance) {
synchronized(Singleton2_2.class) {
if (null == instance) {
instance = new Singleton2_2();
}
}
}
return instance;
}
}
複製代碼
public class Singleton2_3 {
private Singleton2_3() {}
// 防止指令重排
private static volatile Singleton2_3 instance = null;
public static Singleton2_3 getInstance() {
if (null == instance) {
synchronized(Singleton2_3.class) {
if (null == instance) {
instance = new Singleton2_3();
}
}
}
return instance;
}
}
複製代碼
public class Singleton3 {
private Singleton3() {}
public static Singleton3 getInstance() {
return SingletonHolder.instance;
}
private static class SingletonHolder {
private static final Singleton3 instance = new Singleton3();
}
}
複製代碼
public enum Singleton4 {
INSTANCE;
}
複製代碼
public final class ReadTimeoutException extends TimeoutException {
private static final long serialVersionUID = 169287984113283421L;
public static final ReadTimeoutException INSTANCE = new ReadTimeoutException();
private ReadTimeoutException() { }
}
複製代碼
@ChannelHandler.Sharable
public final class MqttEncoder extends MessageToMessageEncoder<MqttMessage> {
public static final MqttEncoder INSTANCE = new MqttEncoder();
private MqttEncoder() { }
//...省略代碼
}
複製代碼
public interface PubStrategy {
void pub(String key);
}
複製代碼
public class RolePubStrategy implements PubStrategy {
@Override
public void pub(String key) {
// get users by role key
System.out.println("1.Get users by role key...");
// foreach pub msg to users
System.out.println("2.Foreach pub msg to users...");
}
}
複製代碼
public class UserPubStrategy implements PubStrategy {
@Override
public void pub(String key) {
// find user by key
System.out.println("1.Get user by key...");
// pub msg to user
System.out.println("2.Pub msg to user...");
}
}
複製代碼
public class StrategyContext {
private PubStrategy strategy;
public StrategyContext(PubStrategy strategy) {
this.strategy = strategy;
}
public void pub(String key) {
strategy.pub(key);
}
}
複製代碼
@UnstableApi
interface EventExecutorChooser {
/**
* Returns the new {@link EventExecutor} to use.
*/
EventExecutor next();
}
複製代碼
public final class DefaultEventExecutorChooserFactory implements EventExecutorChooserFactory {
public static final DefaultEventExecutorChooserFactory INSTANCE = new DefaultEventExecutorChooserFactory();
private DefaultEventExecutorChooserFactory() { }
@SuppressWarnings("unchecked")
@Override
public EventExecutorChooser newChooser(EventExecutor[] executors) {
if (isPowerOfTwo(executors.length)) {
return new PowerOfTwoEventExecutorChooser(executors);
} else {
return new GenericEventExecutorChooser(executors);
}
}
private static boolean isPowerOfTwo(int val) {
return (val & -val) == val;
}
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 {
private final AtomicInteger idx = new AtomicInteger();
private final EventExecutor[] executors;
GenericEventExecutorChooser(EventExecutor[] executors) {
this.executors = executors;
}
@Override
public EventExecutor next() {
return executors[Math.abs(idx.getAndIncrement() % executors.length)];
}
}
}
複製代碼
上面策略模式的StrategyContext修改爲StrategyContextDecorator。設計模式
public class StrategyContextDecorator implements PubStrategy {
private PubStrategy pubStrategy;
public StrategyContextDecorator(PubStrategy pubStrategy) {
this.pubStrategy = pubStrategy;
}
@Override
public void pub(String key) {
pubStrategy.pub(key);
}
}
複製代碼
class WrappedByteBuf extends ByteBuf {
protected final ByteBuf buf;
protected WrappedByteBuf(ByteBuf buf) {
if (buf == null) {
throw new NullPointerException("buf");
}
this.buf = buf;
}
@Override
public final boolean hasMemoryAddress() {
return buf.hasMemoryAddress();
}
// ...省略代碼
}
複製代碼
public abstract class Observerable {
protected List<Observer> observers;
protected String message;
/**
* 添加一個觀察者
* @param observer 觀察者對象
*/
abstract public void registerObserver(Observer observer);
/**
* 移除一個觀察者
* @param observer 觀察者對象
*/
abstract public void removeObserver(Observer observer);
/**
* 通知全部觀察者
*/
abstract public void notifyObserver(String message);
}
複製代碼
public class ConcreteObserverable extends Observerable {
public ConcreteObserverable() {
observers = Lists.newArrayList();
}
@Override
public void registerObserver(Observer observer) {
observers.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyObserver(String message) {
this.message = message;
for (Observer observer: observers) {
observer.update(message);
}
}
}
複製代碼
public interface Observer {
public void update(String message);
}
複製代碼
public class ConcreteObserver implements Observer {
@Override
public void update(String message) {
System.out.println("message up: " + message);
}
}
複製代碼
// 被觀察對象
public interface ChannelFuture extends Future<Void> {
Channel channel();
// ...省略代碼
@Override
ChannelFuture addListener(GenericFutureListener<? extends Future<? super Void>> listener);
@Override
ChannelFuture removeListener(GenericFutureListener<? extends Future<? super Void>> listener);
}
// 具體被觀察對象
public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V> {
private Object listeners;
@Override
public Promise<V> setSuccess(V result) {
if (setSuccess0(result)) {
notifyListeners();
return this;
}
throw new IllegalStateException("complete already: " + this);
}
}
// notifyListeners 實現
private void notifyListeners0(DefaultFutureListeners listeners) {
GenericFutureListener<?>[] a = listeners.listeners();
int size = listeners.size();
for (int i = 0; i < size; i ++) {
notifyListener0(this, a[i]);
}
}
// 觀察者
public interface ChannelFutureListener extends GenericFutureListener<ChannelFuture> {
ChannelFutureListener FIRE_EXCEPTION_ON_FAILURE = new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (!future.isSuccess()) {
future.channel().pipeline().fireExceptionCaught(future.cause());
}
}
};
}
複製代碼
避免請求發送者與接收者耦合在一塊兒,讓多個對象都有可能接收請求,將這些對象鏈接成一條鏈,而且沿着這條鏈傳遞請求,直到有對象處理它爲止。bash
public abstract class AbstractHandler {
private AbstractHandler next;
abstract protected void eventSpread();
public void setNext(AbstractHandler next) {
this.next = next;
}
public void spread() {
eventSpread();
// 向下傳播
if (null != next) {
next.eventSpread();
}
}
}
public class Handle1 extends AbstractHandler {
@Override
public void eventSpread() {
System.out.println("Handle1 event spread...");
}
}
public class Handle2 extends AbstractHandler {
@Override
public void eventSpread() {
System.out.println("Handle2 event spread...");
}
}
public class TestChain {
public static void main(String[] args) {
AbstractHandler h1 = new Handle1();
AbstractHandler h2 = new Handle2();
h1.setNext(h2);
h1.spread();
}
}
複製代碼
ChannelPipeline是責任鏈模式的變種,詳見ChannelPipeline原理解讀。app