Amazon Ads

2015年10月16日 星期五

【筆記】使用Java練習觀察者模式(Observer pattern)

Subject.java
package idv.jk.study.designpattern.observer;

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

public abstract class Subject
{
    private String mMessage;
    List mObserverList = new ArrayList();

    public void add(Observer observer)
    {
        mObserverList.add(observer);
    }

    public void remove(Observer observer)
    {
        mObserverList.remove(observer);
    }

    public void broadcast()
    {
        for(Observer o : mObserverList)
        {
            o.update();
        }
    }

    public String getMessage()
    {
        return mMessage;
    }

    public void setMessage(String message)
    {
        this.mMessage = message;
    }
}
Observer.java
package idv.jk.study.designpattern.observer;

public abstract class Observer
{
    protected String mName;
    protected Subject mSubject;

    public abstract void update();
}
Sheep.java
package idv.jk.study.designpattern.observer;

public class Sheep extends Observer
{
    public Sheep(String name, Subject subject)
    {
        this.mName = name;
        this.mSubject = subject;
        subject.add(this);
    }

    @Override
    public void update()
    {
        System.out.printf("通知 %s:%s%n", mName, mSubject.getMessage());
    }
}
SheepDog.java
package idv.jk.study.designpattern.observer;

public class SheepDog extends Subject
{
}
Main.java
package idv.jk.study.designpattern.observer;

public class Main
{
    public static void main(String[] args)
    {
        //Shaun the sheep中的那隻牧羊犬
        SheepDog bitzer = new SheepDog();

        //chief actor
        Sheep shaun = new Sheep("Shaun", bitzer);

        //Shaun's cousin, is the flock's only lamb,
        Sheep timmy = new Sheep("Timmy", bitzer);

        //Shirley is the largest member of the flock
        Sheep shirley = new Sheep("Shirley", bitzer);

        //Nuts, is quite an eccentric, but useful sheep and usually like the rest of the flock, accompanies and helps Shaun.
        Sheep nuts = new Sheep("Nuts", bitzer);

        bitzer.setMessage("主人回來了,快點恢復正常羊的樣子");
        bitzer.broadcast();

        System.out.println("晚上了...");
        //晚上了,Timmy比較早睡,Bitzer就不通知他
        bitzer.remove(timmy);
        bitzer.setMessage("星星出來了,大家快來看啊");
        bitzer.broadcast();
    }
}

2015年10月13日 星期二

【筆記】使用Java來做快速排序(quick sort)的簡單範例

上次真正去接觸、實作快速排序,想想是當年上職訓課,老實講,當時也是一知半解,XD
package idv.jk.study.algorithm.sort;

/**
 * Created by bioyang on 2015/10/10.
 */
public class QuickSort
{
    public static void main(String[] args)
    {
        int[] numberArray = new int[]{6, 1, 2, 7, 9, 3, 4, 5, 10, 8};

        new QuickSort().quickSort(0, numberArray.length - 1, numberArray);

        for(int n : numberArray)
        {
            System.out.print(n + "\t");
        }
    }

     /**
     *
     * @param left 最左邊那個數字的index
     * @param right 最右邊那個數字的index
     * @param numberArray 要排序的整數陣列
     */
    public void quickSort(int left, int right, int[] numberArray)
    {
        if(left > right)
        {
            //代表排序已結束
            return;
        }
        int startIndex = left; //代表最左邊那個數字的起始index
        int endIndex = right;   //代表最右邊那個數字的起始index
        int baseValue = numberArray[left]; //用來儲存要排序的數字陣列最左邊的數字
        int temp;   //用來暫存交換時的值

        while (startIndex != endIndex)
        {
            //要先從右往左找
            while (numberArray[endIndex] >= baseValue && startIndex < endIndex)
            {
                endIndex--;
            }

            while (numberArray[startIndex] <= baseValue && startIndex < endIndex)
            {
                startIndex++;
            }

            if (startIndex < endIndex)
            {
                temp = numberArray[startIndex];
                numberArray[startIndex] = numberArray[endIndex];
                numberArray[endIndex] = temp;
            }
        }

        numberArray[left] = numberArray[startIndex];
        numberArray[startIndex] = baseValue;

        //這裡會叫用遞迴,想起有次聽到良葛格說;「遞迴只應天上有,人間只能用迴圈」,XD
        //將原本最左邊的數字歸位後,開始排序以比這個數小的那群數字
        quickSort(left, startIndex - 1, numberArray);
        //將原本最左邊的數字歸位後,開始排序以比這個數大的那群數字
        quickSort(startIndex + 1, right, numberArray);

    }
}

2015年10月12日 星期一

【筆記】用Java實作備忘錄(memoto)模式的簡單範例

嘗試用說故事的方弍來寫看看,XD Main.java
package idv.jk.study.designpattern.memoto;

/**
 * Created by bioyang on 2015/10/12.
 */
public class Main
{
    public static void main(String[] args)
    {
        //有一個叫小明的工程師
        Programmer smallMing = Programmer.beforeWork();
        //他在上班前的身心狀態
        System.out.printf("開始工作前的HP為 %d, 憤怒條為 %d\r\n", 
                            smallMing.getHitPoint(), smallMing.getAngryPoint());

        //為了確認明天是全新的一天,他的大腦要把上班前的狀態給記下來
        ProgrammerBrain brainOfSmallMing = new ProgrammerBrain();
        brainOfSmallMing.setBodyStateMemoto(smallMing.getBodyStateMemoto());

        //小明修了一個Bug
        smallMing.fixBug();
        //這對他的身心狀態沒有太大的影響
        System.out.printf("修了一個Bug的HP為 %d, 憤怒條為 %d\r\n", 
                            smallMing.getHitPoint(), smallMing.getAngryPoint());

        //客戶改了需求
        smallMing.requirementChanged();
        //要殺死一個工程師,只要改一個需求
        System.out.printf("知道客戶改了需求後的HP為 %d, 憤怒條為 %d\r\n", 
                            smallMing.getHitPoint(), smallMing.getAngryPoint());

        //但一天過去了,又是美好、新的一天
        smallMing.recoverBodyStateMemoto(brainOfSmallMing.getBodyStateMemoto());
        System.out.printf("隔天起床工作前的HP為 %d, 憤怒條為 %d\r\n", 
                            smallMing.getHitPoint(), smallMing.getAngryPoint());
    }
}
Programmer.java
package idv.jk.study.designpattern.memoto;

/**
 * Created by bioyang on 2015/10/12.
 */
public class Programmer
{
    /**
     * 生命值
     */
    private int hitPoint;
    /**
     * 憤怒條
     */
    private int angryPoint;

    private BodyStateMemoto mBodyStateMemoto;

    public BodyStateMemoto saveState()
    {
        return new BodyStateMemoto(this.hitPoint, this.angryPoint);
    }

    public static Programmer beforeWork()
    {
        Programmer programmer = new Programmer();
        programmer.setHitPoint(100);
        programmer.setAngryPoint(0);
        return programmer;
    }

    public void fixBug()
    {
        this.hitPoint -= 5;
        this.angryPoint += 10;
    }

    public void requirementChanged()
    {
        this.hitPoint = 0;
        this.angryPoint = 200;
    }

    public BodyStateMemoto getBodyStateMemoto()
    {
        return new BodyStateMemoto(getHitPoint(), getAngryPoint());
    }

    public void recoverBodyStateMemoto(BodyStateMemoto bodyStateMemoto)
    {
        setHitPoint(bodyStateMemoto.getHitPoint());
        setAngryPoint(bodyStateMemoto.getAngryLevel());
    }

    public int getHitPoint()
    {
        return hitPoint;
    }

    public void setHitPoint(int hitPoint)
    {
        this.hitPoint = hitPoint;
    }

    public int getAngryPoint()
    {
        return angryPoint;
    }

    public void setAngryPoint(int angryPoint)
    {
        this.angryPoint = angryPoint;
    }
}
BodyStateMemoto.java
package idv.jk.study.designpattern.memoto;

/**
 * Created by bioyang on 2015/10/12.
 */
public class BodyStateMemoto
{
    /**
     * 生命力
     */
    private int hitPoint;
    /**
     * 憤怒值
     */
    private int angryLevel;

    public BodyStateMemoto(int hitPoint, int angryLevel)
    {
        this.hitPoint = hitPoint;
        this.angryLevel = angryLevel;
    }

    public int getHitPoint()
    {
        return hitPoint;
    }

    public void setHitPoint(int hitPoint)
    {
        this.hitPoint = hitPoint;
    }

    public int getAngryLevel()
    {
        return angryLevel;
    }

    public void setAngryLevel(int angryLevel)
    {
        this.angryLevel = angryLevel;
    }
}
ProgrammerBrain.java
package idv.jk.study.designpattern.memoto;

/**
 * Created by bioyang on 2015/10/12.
 */
public class ProgrammerBrain
{
    private BodyStateMemoto mBodyStateMemoto;

    public void setBodyStateMemoto(BodyStateMemoto bodyStateMemoto)
    {
        this.mBodyStateMemoto = bodyStateMemoto;
    }

    public BodyStateMemoto getBodyStateMemoto()
    {
        return mBodyStateMemoto;
    }
}

2015年10月10日 星期六

【筆記】ReactJS呼叫外部API練習-顯示YouBike站點列表

最近有機會碰到React除了做完它的教學增加點自信外,再來嘗試做一些套用API的練習,這裡使用台北市政府提供的YouBike臺北市公共自行車即時資訊來取得YouBike站點資訊。

這裡拜歐先用 Node JS開一個網頁伺服器,主要是因為若用JavaScript去對上述的API送要求的話,會有Cross Domain呼叫的問題,會比較麻煩,所以這裡就用Node在伺服器送出要求,取回所需要的資料。

先寫一支名為server.js的程式:
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var request = require('request');

app.set('port', (process.env.PORT || 9999));

app.use('/', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.get('/api/comments', function(req, res) {
 res.end('abc');
});

    app.get('/youbike/:pageSize/:offset', function(req, res){
    var pageSize = req.params.pageSize,
      offset = req.params.offset;
    request('http://data.taipei/opendata/datalist/apiAccess?scope=resourceAquire&rid=ddb80380-f1b3-4f8e-8016-7ed9cba571d5&limit=' + pageSize + '&offset=' + offset, 
        function(err, response, body){
          if(err)
          {
            console.log(err);
          }
          else if(response.statusCode == 200)
          {
            res.end(body);
          }
    })
    });

app.listen(app.get('port'), function() {
  console.log('Server started: http://localhost:' + app.get('port') + '/');
});
上列程式中,會用到Express這個Framwork,以及request這個package,它主要的功能就是在受到用戶端的要求時,再組成查詢的URLAPI送出要求,取回JSON格式的資料。

再來會有一支名為index.htmlHTML程式來顯示資料:
<!DOCTYPE html>
<html>
<head>
        
 
</head>
<body>
    
</body> </html>
那我們主要的 React是寫在youbiks.jsx中,其中主要定義了YouBikeSiteYouBikeSiteRowYouBikeTable等三個元件:
var YouBikeSite = React.createClass({
 getInitialState : function(){
  return {
   site : [],
  };
 },
   render : function(){
  var site = this.props.site;
    return (
     
{site.sno}
{site.sarea}
{site.sna}
{site.sbi}
{site.mday}
); } }); var YouBikeSiteRow = React.createClass({ render : function(){ console.log('YouBikeSiteList'); return (
{this.props.siteList.map(function(site) { //console.log(site); return (<YouBikeSite site={site} />); })}
); } }); var YouBikeTable = React.createClass({ getInitialState : function(){ return { siteList : [], page : 1 }; }, componentDidMount : function(){ this.loadYouBikeSiteInfo(); }, loadYouBikeSiteInfo : function(){ console.log('this.state.page: ' + this.state.page); var pageSize = 10, offset = (this.state.page >= 1 ? this.state.page - 1 : 0) * pageSize; console.log(offset); var youBikeSiteUrl = '/youbike/' + pageSize + '/' + offset; $.ajax({ url : youBikeSiteUrl, dataType : 'json', cache : false, success : function(data){ if(data.result && data.result.results) { this.setState({ siteList : data.result.results }); } //console.log(this.state.siteList); }.bind(this), error : function(xhr, status, err){ console.error(youBikeSiteUrl, status, err.toString()); }.bind(this) }); }, nextPage : function() { this.state.page += 1; this.loadYouBikeSiteInfo(); }, previousPage : function() { this.state.page -= 1; if(this.state.page < 0) { this.state.page = 0; } this.loadYouBikeSiteInfo(); }, render : function(){ return (

YouBike Site Information

場站代號
場站區域
場站名稱
目前車輛數
資料更新時間
<YouBikeSiteRow siteList={this.state.siteList}/>
) } }); ReactDOM.render( <YouBikeTable />, document.getElementById('container') );
完整的範例可以到 GitHub下載。