观察者模式---拉数据方式---顾客对xx感兴趣(多个观察者对一个主题的不同部分感兴趣)

观察者模式---拉数据方式---顾客对xx感兴趣(多个观察者对一个主题的不同部分感兴趣),第1张

主题接口:

package seeTest;

public interface ISubject {
    void addObserver(IObserve observe);
    void delObserver(IObserve observe);
    void notifyObservers();  //所有可见
}

观察者接口:

package seeTest;

public interface IObserve {
    void update();
}

具体主题:BookStore

package seeTest;

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

public class BookStore implements ISubject{
    private String bookName;
    private String bookAuthor;
    private String press;
    private double bookPrice;
    private List objlist = new ArrayList();
    
    @Override
    public void addObserver(IObserve observe) {
        if(!objlist.contains(observe)){
            objlist.add(observe);
        }
    }

    @Override
    public void delObserver(IObserve observe) {
        if(objlist.contains(observe)){
            objlist.remove(observe);
        } 
    }

    @Override
    public void notifyObservers() {
        for(IObserve obs:objlist){
           obs.update();
        }
    }
    public void setBookInfo(String bookName,String bookAuthor,String press,double bookPrice){
        this.bookName = bookName;
        this.bookAuthor = bookAuthor;
        this.press = press;
        this.bookPrice = bookPrice;
        this.notifyObservers();

    }
    //get 和 set
    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getBookAuthor() {
        return bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public String getPress() {
        return press;
    }

    public void setPress(String press) {
        this.press = press;
    }

    public double getBookPrice() {
        return bookPrice;
    }

    public void setBookPrice(double bookPrice) {
        this.bookPrice = bookPrice;
    }
    
    
}

具体观察者1 CustomerOne

package seeTest;

public class CustomerOne implements IObserve{
    ISubject subject;
    String bookName;
    double bookPrice;

    public ISubject getSubject() {
        return subject;
    }
    public void setSubject(ISubject subject) {
        this.subject = subject;
        this.subject.addObserver(this);
    }
    @Override
    public void update() {
        if(subject instanceof BookStore){
            this.bookName = ((BookStore)subject).getBookName();
            this.bookPrice = ((BookStore)subject).getBookPrice();
            System.out.println("张三"+ "关心"+ bookName+"和价格:"+bookPrice);      
        }
        
    }
    
}

具体观察者2 CustomerTwo

package seeTest;

public class CustomerTwo implements IObserve{
    ISubject subject;
    String bookName;
    String bookAuthor;
    String press;
    public ISubject getSubject() {
        return subject;
    }
    public void setSubject(ISubject subject) {
        this.subject = subject;
        this.subject.addObserver(this);
    }
    @Override
    public void update() {
        if(subject instanceof BookStore){
            this.bookName = ((BookStore)subject).getBookName();
            this.bookAuthor = ((BookStore)subject).getBookAuthor();
            this.press = ((BookStore)subject).getPress();
            System.out.println("李四"+ "关心:"+ "书名:"+bookName + "书作者:"+ bookAuthor + "书出版社:"+press) ;       
        }
        
    }
}

测试类:

package seeTest;

public class Test {
    public static void main(String[] args) {
        BookStore bookstore = new BookStore();
        CustomerOne c1 = new CustomerOne();
        CustomerTwo c2 = new CustomerTwo();
        bookstore.setBookInfo("c++","wu","beijing",10.0);//创建图书
        c1.setSubject(bookstore);
        c1.update();
        System.out.println("*************");
        c2.setSubject(bookstore);
        c2.update();
    }
}

运行结果:

 参考:设计模式(二)——观察者模式 - JavaShuo

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/740614.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存