分類
Design Pattern

Observer Pattern(觀察者模式)

一對多關係(Subject & Observer),實際資料位於主題(Subject)中,觀察者依附於主題,只要主題資料有更動,觀察者也會一併被通知
主題類別(要發送訊息的類別實作它)

  public interface Subject {//主題
      public void registerObserver(Observer ob);// 註冊觀察者
      public void removeObserver(Observer ob);// 移除觀察者
      public void notifyObservers();// 通知所有觀察者資料變動
  }

 
觀察者類別(要接收訊息的類別實作它)

  import java.util.ArrayList;
  interface Observer {// 觀察者
      public void updateNum(ArrayList<Integer> al);// 所有觀察者更新資料
  }

 

樂透開獎機(這是我的主題)

  import java.util.ArrayList;
  import java.util.Random;
  public class LottoSystem implements Subject, Runnable {// Runnable只用於方便開獎,沒有其他用途
      ArrayList<Observer> obsal;// 存放所有觀察者
      ArrayList<Integer> numberal;// 存放樂透數字
      public LottoSystem() {
          obsal = new ArrayList<Observer>();
          numberal = new ArrayList<Integer>();
          for (int i = 0; i < 6; i++) {// 初始 6 個號碼為 0
              numberal.add(0);
          }
      }
      public void registerObserver(Observer ob) {
          obsal.add(ob);
      }
      public void removeObserver(Observer ob) {
          int i = obsal.indexOf(ob);
          if (i >= 0) {
              obsal.remove(i);
          }
      }
      // 主題更新資料之後,必須呼叫此方法以通知所有觀察者
      public void notifyObservers() {
          for (int i = 0; i < obsal.size(); i++) {
              Observer ob = (Observer) obsal.get(i);
              ob.updateNum(numberal);
          }
      }
      public void lottery() {
          Random r = new Random();
          for (int i = 0; i < numberal.size(); i++) {
              int tempnum = r.nextInt(48);
              for (int j = 0; j < numberal.size(); j++) {
                  if (tempnum == numberal.get(j) && i != j) {
                      tempnum = r.nextInt(48);
                      j = 0;
                      i = 0;
                  }
              }
              numberal.set(i, tempnum);
          }
          notifyObservers();
      }
      // 模擬開獎
      public void run() {
          while (true) {
              lottery();
              try {
                  Thread.currentThread().sleep(1500);
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  }

 

這是購買樂透的人(也是觀察者)

  import java.util.ArrayList;
  public class ShowLottoNum implements Observer {
      String name;
      ArrayList<Integer> numlist;// 存放樂透數字
      Subject s;// 主題,用來註冊該觀察者
      // 當觀察者建構自己必須傳入已存在的主題物件以註冊自己本身
      public ShowLottoNum(Subject s, String name) {
          this.s = s;
          s.registerObserver(this);
          this.name = name;
      }
      @Override
      public void updateNum(ArrayList<Integer> al) {
          // TODO Auto-generated method stub
          numlist = al;
          showNum();
      }
      public void showNum() {
          System.out.print(name + " get number :");
          for (int i = 0; i < numlist.size(); i++) {
              System.out.print(numlist.get(i) + ",");
          }
          System.out.println();
      }
  }

測試程式

  import java.util.ArrayList;
  import java.util.List;
  public class Simulator {
      public static void main(String[] args) {
          LottoSystem ls = new LottoSystem();// 主題
          ShowLottoNum sln = new ShowLottoNum(ls, "John");// 觀察者1(若需要更多觀察者,只要照著
                                                          // ShowLottoNum 建構即可 )
          ShowLottoNum sln2 = new ShowLottoNum(ls, "Peter");// 觀察者2
          ShowLottoNum sln3 = new ShowLottoNum(ls, "Little flower");// 觀察3
          // 開始開獎
          new Thread(ls).start();
      }
  }
分類
Android

Can't bind to local 8X00 for debugger

Description:

Debugger connected fail。
 

Solution:

開啟 eclipse 後 -> Window -> Preferences  -> Android -> DDMS
勾選 Use ADBHOST -> 127.0.0.1

分類
Jenkins

在 ubuntu 上安裝 jenkins

1.安裝 Jenkins
開啟終端機輸入

wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo aptitude update 
sudo adduser jenkins
 
(unix密碼輸入自選 , 全名輸入jenkins , 其他使用預設(enter))
 
sudo aptitude install jenkins

 

查看安装是否成功:
開啟瀏覽器输入 http://localhost:8080 或 http://127.0.0.1:8080
使用指令啟動jenkins :
sudo /etc/init.d/jenkins start
使用指令停止
jenkins : sudo /etc/init.d/jenkins stop
此時在區網內其他電腦也可開啟瀏覽器輸入 http://遠端ip:8080 (遠端ip視內外網而定)
 
2.安裝其他套件
從 http://updates.jenkins-ci.org/download/plugins/ 選擇需要的套件
下載後通常是 .hpi 格式,把這些放到 /var/lib/jenkins/plugins ,再重新啟動Jenkins

(先移動到放置 .hpi資料夾中 使用 cp -r ./ /var/lib/jenkins/plugins)
分類
Ubuntu tools

putty psftp使用記錄

Putty
1.到官網下載 putty

2.執行 putty 依序輸入 遠端主機id , 選擇 ssh , 點擊連線

3. login as : 遠端主機帳號
    password : 遠端主機帳號密碼

psftp
1.到官網下載 psftp

2.執行 psftp 輸入 open 遠端主機id 按enter

3.輸入遠端主機帳號

4.再輸入密碼

ps.使用指令類似 sftp

分類
Ubuntu tools

ubuntu 使用 ssh sftp 記錄

ubuntu 10.04 預設沒有安裝 ssh-server, 若想遠端使用ssh登入主機就必須安裝ssh server
 
SSH
1.  安裝 ssh server
終端機輸入

sudo apt-get install openssh-server openssh-client

2. 啟動 ssh server
終端機輸入

sudo /etc/init.d/ssh start

 
3. 遠端連線
終端機輸入

ssh 遠端主機帳號@遠端主機id
輸入密碼

 
SFTP
1. 遠端連線
終端機輸入

sftp 遠端主機帳號@遠端主機id
輸入密碼

 
2. 可使用在遠端主機指令

pwd , cd , ls , dir , mkdir , rmdir , rm , rename , chmod , chgrp , chown

 
3. 可使用在本機端指令

lcd , lls , lmkdir , lpwd , lrmdir

 
4. 本機上傳檔案到遠端

put 本機檔案 遠端路徑

 
5. 從遠端下載檔案到本機

get 遠端檔案 本機路徑
分類
git 錯誤紀錄

Git error: Permission denied (publickey). fatal: The remote end hung up unexpectedly

Permission denied (publickey).
fatal: The remote end hung up unexpectedly

 
Root Caused:
在本機端 git clone 專案時,使用指令時權限錯誤
Solution:
在輸入git clone 指令時切換到適合的權限,再輸入
或在放置專案的地方輸入 sshp-add

分類
Ubuntu tools

ubuntu gcin 輸入法

開啟終端機輸入

apt-get install gcin

安裝完成後重開機即可使用
調整設定
系統 -> 偏好設定  -> gcin 輸入法設定 -> 內定輸入法 開啟&關閉

分類
Android

eclipse android 實機装置顯示 ????????? unknow

Description:
接上實機後,Eclipse DDMS Devices 顯示 ??????????  ?? unknow
 
Solution:
開啟终端機輸入

sudo -s

輸入密碼取得  root 權限
移動到 SDK 中的 platfarm-tools 資料夾,其內要有adb檔案。
以下步驟可能要重複好幾次,直到顯示名稱OK
輸入

sudo ./adb kill-server

會開始Connection attempts
再輸入

sudo ./adb devices

如果装置有顯示名稱就OK了
 

分類
Ubuntu tools

ubuntu10.04 調整解析度

安裝好 ubuntu 之後解析度只有 800×600 和 640×480,如何調整更大的解析度呢
參考此篇
解法:
1.
終端機輸入

gtf 1280 1024 60 -x

2.
再輸入

sudo gedit /etc/X11/xorg.conf


把以下內容全部貼到 xorg.conf裡面

Section "InputDevice"
Identifier "Generic Keyboard"
Driver "kbd"
Option "XkbRules" "xorg"
Option "XkbModel" "pc105"
Option "XkbLayout" "us"
EndSection
Section "InputDevice"
Identifier "Configured Mouse"
Driver "mouse"
Option "CorePointer"
EndSection
Section "Device"
Identifier "Configured Video Device"
EndSection
Section "Monitor"
Identifier "Configured Monitor"
Vendorname "Generic LCD Display"
Modelname "LCD Panel 1280x1024"
Horizsync 31.5-64.0
Vertrefresh 56.0 - 65.0
modeline "640x480@60" 25.2 640 656 752 800 480 490 492 525 -vsync -hsync
modeline "800x600@56" 36.0 800 824 896 1024 600 601 603 625 +hsync +vsync
modeline "800x600@60" 40.0 800 840 968 1056 600 601 605 628 +hsync +vsync
modeline "1024x768@60" 65.0 1024 1048 1184 1344 768 771 777 806 -vsync -hsync
modeline "1280x960@60" 102.1 1280 1360 1496 1712 960 961 964 994 -hsync +vsync
modeline "1280x1024@60" 108.0 1280 1328 1440 1688 1024 1025 1028 1066 +hsync +vsync
Gamma 1.0
EndSection
Section "Screen"
Identifier "Default Screen"
Monitor "Configured Monitor"
Device "Configured Video Device"
Defaultdepth 24
SubSection "Display"
Depth 24
Virtual 1280 1024
Modes "1280x1024@60" "1280x960@60" "1024x768@60" "800x600@60" "800x600@56" "640x480@60"
EndSubSection
EndSection
Section "ServerLayout"
Identifier "Default Layout"
screen 0 "Default Screen" 0 0
EndSection
Section "Module"
Load "dri"
Load "v4l"
EndSection
Section "ServerFlags"
EndSection

 

分類
git 錯誤紀錄

Git error: Permission denied (publickey) fatal: Could not read from remote repository

Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

 
Root Caused:
通常是 SSH Key 沒有加入的問題
Solution:
1.重新產生 SSH Key
終端機輸入

ssh-keygen –t rsa –b 2048


2.顯示 SSH Key
終端機輸入

cat ~/.ssh/id_rsa.pub


3.貼到 Gerrit 中
Settings -> SSH Public Keys -> add key 
貼上第2步中所有的內容