设计模式

装饰者与被装饰者继承同一个接口

修改被装饰者行为

package com.xiaofeiyang;

/**
 * @author: yangchun
 * @description:
 * @date: Created in 2020-04-23 14:48
 */
public class Decorate {
    public interface OnSalePlan{
        float getPrice(float oldPrice);
    }
    public static class NonePlan implements OnSalePlan{
        static final NonePlan instance = new NonePlan();
        private NonePlan(){

        }
        @Override
        public float getPrice(float oldPrice) {
            return oldPrice;
        }
    }
    public static class KnockPlan implements OnSalePlan{
        private float amount;
        private KnockPlan(float amount){
            this.amount = amount;
        }
        @Override
        public float getPrice(float oldPrice) {
            return oldPrice -amount;
        }
    }
    public static class DiscountPlan implements OnSalePlan{
        private int discount;
        private OnSalePlan onSalePlan;
        private DiscountPlan(int discount,OnSalePlan onSalePlan){
            this.discount = discount;
            this.onSalePlan = onSalePlan;
        }
        private DiscountPlan(int discount){
           this(discount,NonePlan.instance);
        }
        @Override
        public float getPrice(float oldPrice) {
            return onSalePlan.getPrice(oldPrice) * (discount /100);
        }
    }
}

 Netty中WrappedByteBuf,以及它的子类

观察者模式

  观察者和被观察者

  观察者订阅消息,被观察者发布消息

package com.xiaofeiyang;

import java.util.ArrayList;
import java.util.List;
import java.util.Observer;

/**
 * @author: yangchun
 * @description:
 * @date: Created in 2020-04-23 18:36
 */
public class ObserverTest {
    public interface Observerable{
        void registerObserver(Observer o);
        void removeObserver(Observer o);
        void notifyObserver();
    }
    public interface Observer{
        void notify(String message);
    }
    public static class Girl implements Observerable{
        private String message;
        private List<Observer> observerList;
        public Girl(){
            observerList = new ArrayList<>();
        }
        @Override
        public void registerObserver(Observer o) {
            observerList.add(o);
        }

        @Override
        public void removeObserver(Observer o) {
            observerList.remove(o);
        }

        @Override
        public void notifyObserver() {
            for(Observer observer:observerList){
                observer.notify(message);
            }
        }
        public void hasBoyFriend(){
            message = "有男朋友了";
            notifyObserver();
        }
        public void getMarried(){
            message = "结婚了";
            notifyObserver();
        }
        public void getSingled(){
            message = "单身了";
            notifyObserver();
        }
    }
    public static class man implements Observer{
        @Override
        public void notify(String message) {
            System.out.println("man "+message);
        }
    }
    public static class oldMan implements Observer{
        @Override
        public void notify(String message) {
            System.out.println("oldMan "+message);
        }
    }

    public static void main(String[] args) {
        man man = new man();
        oldMan oldMan = new oldMan();
        Girl girl =new Girl();
        girl.registerObserver(man);
        girl.registerObserver(oldMan);
        girl.getMarried();
        girl.removeObserver(man);
        girl.getSingled();
    }
}

Netty里面writeAndFlush

迭代器模式

  迭代器接口

  对容器里面的对象进行访问

  ByteBuf

package com.xiaofeiyang;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledDirectByteBuf;

/**
 * @author: yangchun
 * @description:
 * @date: Created in 2020-04-23 22:03
 */
public class IteratorTest {
    public static void main(String[] args) {
        ByteBuf header = Unpooled.wrappedBuffer(new byte[]{1,2,3});
        header.forEachByte(value -> {
            System.out.println(value);
            return true;
        });
    }
}

责任链模式

  责任处理器接口

  创建和添加删除责任处理器接口

  上下文

  责任终止机制

  ChannelHandler,责任处理器接口

  ChannelPipeline,责任链条

  ChannelHandlerContext,上下文可以拿到channel和executor

  fireChannelRead,显示传播

  

posted on 2020-04-23 16:13  清浊  阅读(179)  评论(0)    收藏  举报