1. 程式人生 > >Write the CODE,change the World!

Write the CODE,change the World!

    觀察者模式:又稱釋出-訂閱模式、模型-檢視模式。一般在需要其他物件告知自己需要發生改變時使用。例如A表發生改變後,需要告知B更新資料。可以動態的實現新增多個觀察者,也可以隨時刪除觀察者。

    觀察者模式可以使用在需要根據一個物件的改變動態的修改其他物件,或者激發其他物件相應的事件,而同時又不清楚會有多少個物件的時候。將資料與具體的表現充分分離,符合開閉原則。但同時需要注意,如果多個觀察者之間如果存在者交叉依賴的話,可能會引起死迴圈。如果觀察者過多那麼依次通知每個觀察者將會非常耗時。需要注意到,觀察者模式不應該將物件具體的變化以及為什麼會發生變化告知給觀察者,應該充分將資料與具體的操作也就是表現分離開來。

    本例實現一個動態的簡易通訊錄,實現當一個使用者修改了自己的通訊方式時,其他使用者可以動態獲取到更新的內容。

程式碼如下

import java.util.ArrayList;

import java.util.List;

public class test

{

    public static void main(String[] args)

    {

        player a=new player("A");

        player b=new player("B");

        player c=new player("C");

        player d=new player("D");

        subject s=new subject();

        s.addObServer(a);

        s.addObServer(b);

        s.addObServer(c);

        s.addObServer(d);

        a.setSubject(s);

        b.setSubject(s);

        c.setSubject(s);

        d.setSubject(s);

        s.show();

        a.change();

    }

}

abstract class obServer

{

    public abstract void show();

}

class player extends obServer

{

    String name;

    subject s;

    public player(String name)

    {

        this.name=name;

    }

    public void show()

    {

        System.out.println(name);

    }

    public void change()

    {

        name+=name;

        System.out.println("Somebody's name changed.Scaning all mumbers...");

        s.show();

    }

    public void setSubject(subject s)

    {

        this .s=s;

    }

}

class subject

{

    List<obServer> sub;

    public subject()

    {

        sub=new ArrayList<obServer>();

    }

    public void addObServer(obServer o)

    {

        sub.add(o);

    }

    public void show()

    {

        for(obServer i : sub)

        {

            i.show();

        }

    }

    public void changed()

    {

        show();

    }

}

結果如下

A

B

C

D

somebody's name changed.scaning all mumbers...

AA

B

C

D

2016-8-24

12:01

相關推薦

樓上小宇___home(Write the code,change the world!)

考研資料結構程式碼實現 本人以清華大學嚴蔚敏版《資料結構》為主要參考書,將書中重要資料結構與演算法的虛擬碼進行了重寫,供大家學習參考,其中所有程式都經過本人在vs2015上除錯通過,大部分程式都附上了參考圖例和輸入輸出樣例供大家參

Write the CODE,change the World!

    觀察者模式:又稱釋出-訂閱模式、模型-檢視模式。一般在需要其他物件告知自己需要發生改變時使用。例如A表發生改變後,需要告知B更新資料。可以動態的實現新增多個觀察者,也可以隨時刪除觀察者。     觀察者模式可以使用在需要根據一個物件的改變動態的修改其他物件,或者激發其

Write the Code . Change the World

多執行緒有兩種實現方法,分別是繼承Thread類與實現Runnable介面 同步的實現方面有兩種,分別是synchronized,wait與notify wait():使一個執行緒處於等待狀態,並且釋放所持有的物件的lock。 sleep():使一個正在執行的執行緒處於睡眠狀態,是一個靜態方法,呼叫此方法要

【小工匠】show me the code ,change the world

---------------------->>GitHub<<----------------- https://github.com/yangshangwei --------------------->>公眾號<<---------------

小工匠(show me the code ,change the world

Andoroid新特性解讀 主要介紹Design Support Library中新推出的控制元件,介紹詳細的說明和使用,以及Android中新的特性

Code change the World.

題目: (1)問題描述:   給定x 軸上n 個閉區間。去掉儘可能少的閉區間,使剩下的閉區間都不相交。 (2)程式設計任務:   給定n 個閉區間,程式設計計算去掉的最少閉區間數。 (3)資料輸入:   第一行是正整數n,表示閉區間數。接下來的n行中,每行有2 個整數,分

coding the code,changing the world

本文中將介紹一個具體的linux標準檔案系統ext2的磁碟上檔案組織方式和資料塊定址(邏輯地址到實體地址對映) 兩個問題: 1.一個檔案如何組織,採用何種結構; 2.檔案的讀寫實現,如何從邏輯空間找到

AOSP Part 1: Get the code using the Manifest and Repo tool

6 months ago, I moved to New York, the first city I lived in outside of Israel. With a new job at a new place, I decided to also try a new laptop runn

You say you want a revolution well you know we all want to change the world.——《喬布斯傳》書摘

      五天時間,每晚都看這本被稱為2011年最好的書。真的很感動,我會讀到凌晨兩點,酣然睡去,就想像自己看見了喬布斯。       喬布斯說,我願意用自己全部的品位換取和蘇格拉底共處一個下午。  

【Shangri-La】---Because the people who are crazy enough to think they can change the world,are the ones who do...

---Because the people who are crazy enough to think they can change the world,are the ones who do...

Everything to know about digital celebrities and how they could change the world

Part II: How do digital celebrities progress?MonetizationWhile a large portion of these celebrities are still in the experimental phase, there is a subset

IoT + Machine Learning is Going to Change the World

IoT + Machine Learning is Going to Change the WorldAnd it’s going to be awesomeThe Internet of Things is slowly permeating every aspect of our lives. If yo

IoT Machine Learning is Going to Change the World

The Internet of Things is slowly permeating every aspect of our lives. If you don't already own an IoT device, you've surely heard of them. From smart ther

Computer Interfaces and how will they change the world?

The sensors of brain computer interfaces are the most critical part of the entire system. A sensor is some sort of device (wire, detector, camera) that pic

Change the world by program.

莫比烏斯函式定義 μ(n)=⎧⎩⎨⎪⎪1(−1)k0(n=0)(n=p1p2...pk,∀pi!=pj)(others)μ(n)={1(n=0)(−1)k(n=p1p2...pk,∀pi!=pj)0(others) 莫比烏斯函式計算 直接計算,只需要對

IT,Change Myself;Change My Life;Change The World

ASN.1是什麼? 具體是什麼去自己搜尋吧 ASN.1是一種編碼格式,只要遵循固定格式標準,都可以解析ASN編碼。先舉個簡單例子,聯絡資訊資料 假設01代表名字,02代表電話下面分別分別張三和王二麻子的 電話號碼分別為12345678和123456

Product change the world

  (via:無網不剩) 做人要有態度,做產品自然也要有態度。李宗盛在「致匠心」裡提到:人不能孤獨地活著,之所以有作品是為了溝通,透過作品去告訴人家心裡的想法,眼中看世界的樣子,所在意的,所珍惜的,所以,作品就是自己。 那該用怎樣的態度去做產品呢?以下是我想到的

The code world of freedom

最佳原則 堅持制定好的程式碼規範。 無論團隊人數多少,程式碼應該同出一門。 1.專案命名 全部採用小寫方式, 以下劃線分隔。 例:my_project_name 2.目錄命名 參照專案命名規則; 有複數結構時,要採用複數命名法。 例:scripts, styles,

GANs will change the world

GANs will change the worldIt’s New Year’s 2017, so time to make predictions. Portfolio diversification has never been me, so I’ll make just one.Generative