廣告

2015年3月30日 星期一

[design pattern] [java] Template Pattern

template pttern:
use abstract class to define template(behavior)
to execute its methods.
And use some class to extend it.
to override the method of superclass.



Programmer

package com.test.templatepattern;

public abstract class Programmer {

 abstract boolean getUP();
 abstract boolean atCompany();
 abstract boolean Home();
 
 public void dailyRoutine(){
  getUP();
  atCompany();
  Home();
 }
}



Seven
package com.test.templatepattern;

public class Seven extends Programmer {

 @Override
 boolean getUP() {
  System.out.println("sleeping");
  return false;
 }

 @Override
 boolean atCompany() {
  System.out.println("coding");
  return false;
 }

 @Override
 boolean Home() {
  System.out.println("sleeping");
  return false;
 }

}




Leo
package com.test.templatepattern;

public class Leo extends Programmer {

 @Override
 boolean getUP() {
  System.out.println("meeting");
  return false;
 }

 @Override
 boolean atCompany() {
  System.out.println("meeting");
  return false;
 }

 @Override
 boolean Home() {
  System.out.println("meeting");
  return false;
 }

}




Lewis
package com.test.templatepattern;

public class Lewis extends Programmer {

 @Override
 boolean getUP() {
  System.out.println("coding");
  return false;
 }

 @Override
 boolean atCompany() {
  System.out.println("coding");
  return false;
 }

 @Override
 boolean Home() {
  System.out.println("coding");
  return false;
 }

}




testTemplatePattern

package com.test.templatepattern;

public class testTemplatePattern {

 public static void main(String[] args) {
  /*
  template pttern:
  use abstract class to define template(behavior) 
  to execute its methods.
  And use some class to extend it.
  to override the method of superclass.
  */
  
    Programmer lewis = new Lewis();
    lewis.dailyRoutine();
       System.out.println();
      
       Programmer leo = new Leo();
       leo.dailyRoutine();
       System.out.println();
       
       Programmer seven = new Seven();
       seven.dailyRoutine();
       System.out.println();
 }

}



console
coding
coding
coding

meeting
meeting
meeting

sleeping
coding
sleeping



2015年3月28日 星期六

[git] 基本git使用 tutirial 1 (add, commit, push, pull)

由於最近我們team的版本控管要從SVN轉為git,
所以想說寫個基本的git的筆記,
之前都是用github的gui所以對一些commamd還不太熟,
雖然eclipse也支援git的功能,
不過還是練習一下command吧!!

但礙於公司規定所以每晚還是要將git
同步到svn,真是辛苦fai大了!

我們這邊使用的server為gitlab。

首先我們先在gitLab上創一個空的project,
(當然你也可以在本機創好在push上去,會這樣
做是因為gitlab有提供語法XD)



































git 與 svn最大不同在於
svn是直接commit到server端,
並且本機不會有其他版本的資訊,

git則是每次commit都是先到自己的本機,
再用push送到server,所以本機會有自己完整
的版本資訊,

其實我還是喜歡用svn的,
自己以前還直接在研究室的電腦架svn server XD,

進入正題:

設定個人資訊:
可以用以下指令查看,
git config --list

user.name=lewisli
user.email=lewisli.acer@gmail.com
又或者是到 .gitconfig中察看,

設定個人資訊
git config --global user.name "lewisli"
git config --global user.email "lewisli.acer@gmail.com"

當然也可以針對此目錄設定local的個人資訊。
http://lewisli1.blogspot.tw/2015/03/git-specify-multiple-users-user-local.html
接著輸入以下指令
用來建立此目錄的git設定檔與gitlab的連結,
mkdir gitTutorial
cd gitTutorial
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin http://XXX.xxx.xxx.xxx:7070/lewisli/gitTutorial.git
git push -u origin master

-m的意思是說 要加入message(版本控管 log很重要阿)
其他可用 git commit -h 自己看


































如此成功上傳了
可用
git log --stat
git log -p 
來查看commit資訊


clone git server上面的專案
git clone http://XXX.xxx.xxx.xxx:7070/lewisli/gitTutorial.git

通常一個project會有一些不需commit的檔案,
所以可以在.gitignore做設定,
git會去讀這個檔案來知道哪些檔案不需要上傳,
參考
http://lewisli1.blogspot.tw/2015/03/git-gitingore-white-list.html


===================================================

注意1:clone與github上的fork是不同的
詳見:
http://luckyyang.github.io/blog/2013/03/23/fork-you-github/
http://site.douban.com/196781/widget/notes/12161495/note/269163206/

注意2:clone,pull , fecth 是不同的
詳見:
http://blog.mikepearce.net/2010/05/18/the-difference-between-git-pull-git-fetch-and-git-clone-and-git-rebase/

2015年3月26日 星期四

[eclipse] Access restriction on class due to restriction on required library rt.jar


  . 問題 : [eclipse]換workspace時發生
            =>Access restriction on class due to 
              restriction on required library rt.jar 
  
  . 解法 : 方法1
            1. Go to the Build Path settings in the project properties. 
            2 Remove the JRE System Library 
            3 Add it back; Select "Add Library" and select the JRE System Library. 

           方法2 
            window - > java -> compiler -> error/warning -> deprecated and restricted api 把 forbidden reference 將 error change to warning
     

[design pattern] [java] Facade Pattern

我認為wiki寫的蠻清楚的,這邊直接貼上說明
A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can:
  • make a software library easier to use, understand and test, since the facade has convenient methods for common tasks;
  • make the library more readable, for the same reason;
  • reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;
  • wrap a poorly designed collection of APIs with a single well-designed API (as per task needs).
The Facade design pattern is often used when a system is very complex or difficult to understand because the system has a large number of interdependent classes or its source code is unavailable. This pattern hides the complexities of the larger system and provides a simpler interface to the client. It typically involves a single wrapper class which contains a set of members required by client. These members access the system on behalf of the facade client and hide the implementation details.



Animal.java
package com.test.facadepattern;

public interface Animal {

 void printMsg();
 void getFeet();
 void life();
}


Bird.java
package com.test.facadepattern;

public class Bird implements Animal{

 @Override
 public void printMsg() {
  // TODO Auto-generated method stub
  System.out.println("bird class");
 }

 @Override
 public void getFeet() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void life() {
  // TODO Auto-generated method stub
  
 }
 

}


Cat.java
package com.test.facadepattern;

public class Cat implements Animal {

 @Override
 public void printMsg() {
  // TODO Auto-generated method stub
  System.out.println("cat class");
 }

 @Override
 public void getFeet() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void life() {
  // TODO Auto-generated method stub
  
 }


 
}


Dog.java
package com.test.facadepattern;

public class Dog implements Animal{

 @Override
 public void printMsg() {
  // TODO Auto-generated method stub
  System.out.println("dog class");
 }

 @Override
 public void getFeet() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void life() {
  // TODO Auto-generated method stub
  
 }
 


}


Zoo.java
package com.test.facadepattern;

public class Zoo {

 private Animal bird ;
 private Animal cat ;
 private Animal dog ;
 
 public Zoo(){
  bird  = new Bird();
  dog   = new Dog();
  cat   = new Cat();
 }
 
 public void imDog(){
  dog.printMsg();
 }
 
 public void imBird(){
  bird.printMsg();
 }
 
 public void imCat(){
  cat.printMsg();
 }
 
}


testFacadePattern .java
package com.test.facadepattern;

public class testFacadePattern {

 public static void main(String[] args) {
  
  Zoo zoo = new Zoo();
  
  zoo.imBird();
  zoo.imCat();
  zoo.imDog();
  
 }

}


console
bird class
cat class
dog class

[git] .gitingore , white list | ignore every except XXX & XXX



*
#ignore all

!src/
#file under the src/ won't be ignored

!.build.properties
#build.properties won't be ignored

2015年3月25日 星期三

[git] specify multiple users | 指定不同的user (local & global)

因為小弟之前只有再用githib,
當初安裝時他就幫我設定好user的相關資訊,
而且是global的


git config --global user.name "Your Name Here"
git config --global user.email your@email.com

但現在團隊要用gitlab來控管專案
我總不能用我github的身分來做動作吧.

所以隊要開發的專案設一個user

git config user.name "Your Name Here"
git config user.email your@email.com