4/14/2009

第二命名規則:Pascal Case

從之前的Camel Case介紹可以得知,Pascal Case就是Upper Camel Case..

從wiki摘出來的說法是:
Upper Camel Case
每一個單字的首字母都採用大寫字母,例如:FirstName、LastName、CamelCase,也被稱為 Pascal 命名法

他不是很難,因為他跟Lower Camel Case的差異僅是Upper Camel Case的開頭第一個單字為大寫而已..!!

軟體設計層次(金字塔)


左圖是典型的軟體設計層次的金字塔模型,各位看看就好囉!!
以後再解釋幾個重要的成份..

4/13/2009

第一命名規則:Camel Case

中文就是駱駝命名法啦...!!
根據微軟的部分說法是這樣的:
識別項的第一個字母是小寫的,而每個隨後串聯文字的第一個字母是大寫的。例如:
backColor


根據Wiki的說法是:
Camel Case的詞語來自Perl語言普遍使用的大小寫混合格式...
而 Larry Wall 等人所著的暢銷書《Programming Perl》(O'Reilly 出版)的封面圖片正是一匹駱駝。」

Camel Case的限制條件,不得使用下列表式:

  1. 單字之間不得空格,ex:back Color
  2. 不可使用底線區隔,ex:back_Color
  3. 不能使用連接符號連接單字之間,ex: back-Color
Camel Case分成兩種格式:
Lower Camel Case
第一個單字以小寫字母開始;第二個單字的首字母大寫,例如:firstName、lastName
Upper Camel Case
每一個單字的首字母都採用大寫字母,例如:FirstName、LastName、CamelCase,也被稱為 Pascal 命名法

我個人對命名的看法:
  1. 盡量不要使用縮寫,最好能在兩個單字內搞定
  2. 非得需要縮寫的話,請表達語意清楚,或著附加註解..!
一些範例:(Pascal Case會另外拆另一篇文章講..)
Low Camel Case
==一個字以內:==
test、color、execute、observer、singleton、factory、measurements、time、connection、stop、start、run
==兩個字情況:==
getName、setProperty、executeReader、backColor、startRunning、nationalLanguage、dataBase、threadConnection、windowFrame、actionListener、windowAdapter
==三個字情況:==
getId、getSn、setNo、lastIndexOf、trimToSize、createSystemObject、removeArrayList
==四個字以上:==
全寫出來或著
setHRDepartment、addNewWorkProject、newProcessId、destroySid、rebuildNewThread

Gof的基本Designed Pattern意見..

Design Pattern總共分為三大類:

>>Creational Mode

>>Structural Mode

>>Behavioral Mode

按照Gof的Designed Pattern書籍..
對於新手應該從以下Pattern切入較為適當..

  1. Abstract Factory
  2. Composite
  3. Factory Method
  4. Strategy
  5. Adapter
  6. Decorator
  7. Observer
  8. Template Method
上述這幾個大多可以說是蹲馬步..
學習難度都不是很高..
當然如果覺得Gof的書本身就枯燥乏味、不是很好看(他確實是不好看..!)..
那麼O大師有趣的Designed Pattern書籍是不錯的選擇之一...

我對委由(delegate)的基本看法..!!

基本上就是透過delegate...
可以達到彈性化的行為呼叫....
而不必被綁得死死的...
我們可以擁一種簡單的部分代碼表示..
public abstract class Cpu{
protected CpuInstruction cpuInstruction;
protected String type;
//skip...
public void executeInstruction(){
cpuInstruction.execute();
}//end executeInstruction
//skip...
}//end Cpu
//-------------------------------------
public class AmdCpu extends Cpu{
public AmdCpu(string type){
cpuInstrcution=new X86Instruction();
this.type=type;
}//end constructor

public void printType(){
System.out.println("CPU Type: "+this.type);
}//end printType

}//end AmdCpu
由上可以看到executeInstruction()是一個委由(delegate)的方法..
而他的行為決定則是由次類別(AmdCpu)來決定..
因此並不會一開始就被綁死..
這樣做是極具彈性了..
不過上述程式碼的缺點就是"行為實踐"不夠彈性罷了!!

Adapter Pattern的轉接結構

Adapter Pattern對於不同介面之間可以提供相容的轉換..
這是一種非常有用的設計..
例如:
舊版的列舉器轉換成新的迭代裝置(Iterator)...
生活化一點..
例如美規接口透過一個轉接器(Adapter)轉換成歐規接口..
這樣的一種概念其實不難懂...
相對的,他的實作也是很容易...!!

來談談範例:
我們以建立一個匯流排結構PCI介面..
假設並沒有實際設計PCI的具象實踐(Instance)...
僅有一個PCIe的具象實踐..
客乎要求我們必須以PCIe架構建立在早期的PCI架構上..

1.首先我們建立一個介面以作為超型別(Super-Type)之應用..
public interface IPciInterface {
public void link();
public void read();
public void write();
}//end IPciInterface
2.我們得知有一個Pcie具象實踐可以使用,並且他是實作於IPcieInterface介面..
public class Pcie implements IPcieInterface{
public void link(){
System.out.println("Linking!");
}//end link
public void read(){
System.out.println("Reading!");
}//end read

public void write(){
System.out.println("Writing!");
}//end write

}//end Pcie
3.為了符合客戶的需求,我們建立一個PcieToPci的轉接器(Adapter)..
public class PcieToPci implements IPciInterface{

private Pcie pcie;
public PcieToPci(Pcie pcie){
this.pcie=pcie;
}//end constructor

public void link(){
pcie.link();
}//end link

public void read(){
pcie.read();
}//end read

public void write(){
pcie.write();
}//end write

}//end PcieToPci
4.建立好PciToPci轉接器後,再將剩下的Client部分處理..
public class Main{
public static void main(String args[]){
Pci pcie=new Pcie();//Pcie的具象實踐
PcieToPci pcieToPci=new PcieToPci(pcie);

//operations
pcieToPci.link();
pcieToPci.read();
pcieToPci.write();

}//end main

}//end Main

以上是一個非常簡單的Adapter Pattern應用..
他並不難..
學習起來也很快!!...
大概就這樣..

4/10/2009

Singleton Pattern概要..!!

Singleton Pattern目的用來創造獨一無二的資源...
這是非常有用的結構..
例如:
針對撰寫系統..
假設我們透過Factory Pattern來撰寫獨一無二的DataBaseFactory..
來進行對DataBase的統一存取...
這時透過Singleton Pattern便可以建立獨一無二的Factory..
可以看看簡單的代碼範例:
class Singleton{
private Singleton(){

}//end constructor

public static Singleton getInstance(){
return new Singleton();
}//end getInstance

}//end Singleton
由上可以看出幾個要點:
1.constructor必須private防止外部建立多個instance
2.一定要搭配static關鍵字
3.本身是可以返回自有的constructor

但是上述依然不夠完美..
我們把它改寫成如下:
class Singleton{
private static Singleton uniqueInstance;
private Singleton(){

}//end constructor

public static Singleton getInstance(){
if(uniqueInstance==null)
uniqueInstance=new Singleton();
return uniqueInstance;

}//end getInstance

}//end Singleton
可以看出這樣的方式可以做到對Singleton物件的唯一性..
但是如果想到Multi-Thread情況..
他依然還是有問題:
1.容易造成資源取得先後順序問題
2.容易造成資訊錯誤的災難

我們可以透過synchrnized關鍵字來建立critical section..
如下這樣:
class Singleton{
private static Singleton uniqueInstance;
private Singleton(){

}//end constructor

public static synchronized Singleton getInstance(){
if(uniqueInstance==null)
uniqueInstance=new Singleton();
return uniqueInstance;
}//end getInstance
}//end Singleton
但是要注意一點:
synchronized對於Mutli-Thread效能是有衝擊的..
因此必須要考量..!!

我們可以採用雙重鎖來盡量減少多次的同步化
如下做法:
class Singleton{
private volatile static Singleton uniqueInstance;//volatile關鍵字可以表是變數的同一存取
private Singleton(){

}//end constructor

public static Singleton getInstance(){
if(uniqueInstance==null){

synchronized(your_class_name.class){
if(uniqueInstance==null) uniqueInstance=new Singleton();
}//end synchronized

}//end if
return uniqueInstance;
}//end getInstance

}//end Singleton

以上便是很簡單的Singleton應用..!!