设计模式

观察者模式

观察者定义了一种一对多的依赖关系,多个观察者监听一个主题对象,当主题对象的状态发生改变时,这些观察者会被通知从而自我跟新。

结构图

clipboard.png

具体代码如下

abstract class Subject{

    private ArrayList<BaseObserver> list=new ArrayList<>();

    public void attach(BaseObserver observer){
        list.add(observer);
    }

    public void Deattach(Observer observer){
        list.remove(observer);
    }

    public void notifyall(){
        for(BaseObserver observer:list){
            observer.update();
        }
    }

}

class ConcreteSubject extends Subject{

    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }
}

abstract class BaseObserver {
    protected Subject subject;
    public abstract void update();
}

class BinaryObserver extends BaseObserver{

    public BinaryObserver(Subject subject){
        this.subject=subject;
    }


    @Override
    public void update() {
        System.out.println("BinaryObserver update");
    }
}

class HexaObserver extends BaseObserver{

    public HexaObserver(Subject subject){
        this.subject=subject;
    }


    @Override
    public void update() {
        System.out.println("HexaObserver update");
    }
}


public class Observer {
    
    public static void main(String[]args){

        
        ConcreteSubject subject=new ConcreteSubject();
        BaseObserver hexaobserver=new HexaObserver(subject);
        BaseObserver binaryObserver=new BinaryObserver(subject);

        //添加观察者
        subject.attach(hexaobserver);
        subject.attach(binaryObserver);

        subject.setState(1);
        subject.notifyall();

    }
}

输出:

HexaObserver update
BinaryObserver update

桥接模式

将抽象部分与具体实现部分分离

结构图

clipboard.png

代码

interface DrawAPI{
    //接口并不知道会被那个类调用,因此默认为public
    void draw(String color);
}

class DrawRed implements DrawAPI{

    @Override
    public void draw(String color) {
        System.out.println("RedDraw"+" "+color);
    }
}

class DrawGreen implements DrawAPI{

    @Override
    public void draw(String color) {
        System.out.println("GreenDraw"+" "+color);
    }
}


abstract class CircleShape{
    protected DrawAPI dramAPI;

    abstract void draw();

}

class GreenCircle extends CircleShape{

    public GreenCircle(DrawAPI drawapi){
        dramAPI=drawapi;
    }

    @Override
    void draw() {
        dramAPI.draw("Green");
    }
}

class RedCircle extends CircleShape{

    public RedCircle(DrawAPI drawapi){
        dramAPI=drawapi;
    }

    @Override
    void draw() {
        dramAPI.draw("Red");
    }
}



public class Bridge {

    public static void main(String []args){
        DrawAPI greendraw=new DrawGreen();
        DrawAPI reddraw2=new DrawRed();

        CircleShape greencircle=new GreenCircle(greendraw);
        CircleShape redcircle=new RedCircle(reddraw2);

        greencircle.draw();
        redcircle.draw();

    }

}
输出:

GreenDraw Green
RedDraw Red

中介者模式

用一个类来封装一系列对象间的交互,使对象间耦合松散

代码

class BaseMediator{

    private China china;
    private America america;

    public China getChina() {
        return china;
    }

    public void setChina(China china) {
        this.china = china;
    }

    public America getAmerica() {
        return america;
    }

    public void setAmerica(America america) {
        this.america = america;
    }

    public void declare(Country country, String message){
        if(country==china){
            System.out.println("China declare "+message+" America");
        }else {
            System.out.println("Amercia declare "+message+" China");
        }

    }

}

abstract class Country{
    protected BaseMediator mediator;

    abstract void declare(String message);

}


class China extends Country{

    public China(BaseMediator media){
        mediator=media;
    }




    @Override
    void declare( String message) {
        mediator.declare(this,message);
    }
}

class America extends Country{
    public America(BaseMediator media){
        mediator=media;
    }

    @Override
    void declare(String message) {
        mediator.declare(this,message);
    }
}




public class Mediator {
    public static void  main(String []args){
        BaseMediator mediator=new BaseMediator();
        China china=new China(mediator);
        America america=new America(mediator);

        mediator.setAmerica(america);
        mediator.setChina(china);
        china.declare("to");
        america.declare("to");

    }

}

命令模式

命令模式将一个请求封装成一个对象,将请求与如何执行请求分开。

结构图

clipboard.png

代码

class Receiver{

    public void receiver(String s){
        System.out.println("I receiver ..."+s);
    }

}

interface Order{

    void executed();

}


class SellStock implements  Order{

    private Receiver receiver;

    @Override
    public void executed() {
        receiver.receiver("Sell");

    }

    public void setReceiver(Receiver receiver){
        this.receiver=receiver;
    }
}

class Invoker{
    private Order order;


    public void setOrder (Order order){
        this.order=order;
    }

    public void execute() {
        order.executed();
    }
}
public class Command {
    public static void main(String []args){
        Receiver receiver=new Receiver();
        SellStock sellStock=new SellStock();
        sellStock.setReceiver(receiver);

        Invoker invoker=new Invoker();
        invoker.setOrder(sellStock);
        invoker.execute();
        
    }
    
}

输出:

I receiver ...Sell

外观模式

为系统的一组接口提供一个一致的界面(依赖反转)

代码

class BaseFacade{
    private SubOne one;
    private SubTwo two;
    private SubThree three;
    private SubFour four;


    public BaseFacade(){
        one=new SubOne();
        two=new SubTwo();
        three=new SubThree();
        four=new SubFour();

    }

    public void methodA(){
        one.MethodA();;
        two.MethodB();;
        
    }
}

class SubOne{
    public void MethodA(){
        System.out.println("MethodA");
    }

}

class SubTwo{
    public void MethodB(){
        System.out.println("MethodB");
    }

}

class SubThree{
    public void MethodC(){
        System.out.println("MethodC");
    }

}

class SubFour{
    public void MethodD(){
        System.out.println("MethodD");
    }

}

public class Facade {

    public static void main(String []args){
           BaseFacade baseFacade=new BaseFacade();
           baseFacade.methodA();
        
    }
}

输出:

MethodA
MethodB

诗和远方丶
24 声望4 粉丝

每篇都是原创,拒绝复制粘贴


« 上一篇
设计模式

引用和评论

0 条评论