zoukankan      html  css  js  c++  java
  • 设计模式观察者模式

    类图

     

     

    源码

    Observer.java
    package com.demo;
    
    public interface Observer {
    
        public void moneyUp();
        public void moneyDown();
    }
    
    
    Subject.java
    package com.demo;
    
    import java.util.ArrayList;
    
    public abstract class Subject {
        protected ArrayList<Observer> olist=new ArrayList<>();
        public Subject() {
            // TODO Auto-generated constructor stub
        }
        public void add(Observer o) {
            olist.add(o);
        }
        public void remove(Observer o) {
            olist.remove(o);
        }
        public abstract void up(double a);
        public abstract void down(double a);
    
    }
    
    GuMin.java
    package com.demo;
    
    public class GuMin implements Observer{
         
        private String name;
        public GuMin(String n) {
            // TODO Auto-generated constructor stub
            name=n;
        }
    
        @Override
        public void moneyUp() {
            // TODO Auto-generated method stub
              System.out.println(name+"买了一些股票(股票升了)");
        }
    
        @Override
        public void moneyDown() {
            // TODO Auto-generated method stub
            System.out.println(name+"股票降价了,哭了");
        }
    
    }
    
    GuPiao.java
    package com.demo;
    
    public class GuPiao extends Subject{
    
        public GuPiao() {
            // TODO Auto-generated constructor stub
        }
    
        /*
         * public void add(Observer o) { super.add(o); }
         */
        public void up(double a) {
            // TODO Auto-generated method stub
            if(a>0.05)
            for(Observer gum : olist){
                    gum.moneyUp();;
            }
        }
    
        public void down(double a) {
            // TODO Auto-generated method stub
            if(a>0.05)
                for(Observer gum:olist){
                        gum.moneyDown();;
                }
        }
    
    }
    
    Client.java
    package com.demo;
    
    public class Client {
    
        public static void main(String[]args) {
            // TODO Auto-generated constructor stub
                Subject f=new GuPiao();
                GuMin g1=new GuMin("股民A");
                GuMin g2=new GuMin("股民B");
                f.add(g1);
                f.add(g2);
                System.out.println("股票升了");
                f.up(0.1);
                System.out.println("比率小于0.05");
                System.out.println();
                f.down(0.001);
                
                System.out.println("股票降了");
                f.down(0.1);
                System.out.println("比率小于0.05");
                f.down(0.001);
        }
    
    }

     

    测试截图

     

     

  • 相关阅读:
    php八种常用函数
    已知二叉树的前序中序遍历,如何得到它的后序遍历?
    PTA_Have fun with numbers(C++)
    PTA_输入符号及符号个数打印沙漏(C++)
    Web安全之SQL注入
    南京邮电大学//bugkuCTF部分writeup
    修改或添加HTTP请求头
    第二次作业
    博客作业1
    linux python 串口
  • 原文地址:https://www.cnblogs.com/lx06/p/15688580.html
Copyright © 2011-2022 走看看