2015年4月9日 星期四

C#學習筆記-@與string的運用

當@符號出現於字串前面的時候,他後面的字串會被直譯。
ex: "C:\\users\\administrator\\downloads" 等同於 @"C:\users\administrator\downloads"

其中唯一的例外是雙引號 " ,當雙引號出現在@後面的字串中的時候,要使用雙雙引號 "" 才能夠代表一個雙引號。
ex: "<a site=\"abc.com\">" 等同於 @"<a site=""abc.com"">"

2014年5月13日 星期二

C++學習筆記(4)-單冒號的使用

: 單冒號的使用方法

1. 定義變數佔用的(可以使用的)大小,以bit為單位
ex:
class a{
int ex : 4; //指這個int佔用4個bit,也只能用這4個bit
};

2. 初始化值的其中一種寫法
ex:
class b{
int ex1;
int ex2;
b(int one, int two): ex1(one), ex2(two){
//constructor
}
};

其他就是繼承符號跟public:、private:之類的用法。

2013年12月16日 星期一

C++學習筆記(3)-static_cast和dynamic_cast

static_cast:強轉型態,使用不安全轉型的時候會失敗造成程式錯誤,但是比起dynamic_cast稍快一些
ex:
double a = 1.0;
int b =  static_cast<int>(a);
這是安全的

class A{}
class B:public A{
     int a = 1;
     int getA();
}

void func(){
    B* b = new B;
    A* a = static_cast<A*>(b);
    a->getA();
}
這是不安全的,會失敗。

dynamic_cast:基本上適用於各種轉型,但其base class內必須要有virtual函式。此cast在執行時執行(相對於static_cast是在編譯時),所以效能較差
可用於交叉轉換,任何轉換下錯誤則回傳null,不會造成crash

2013年11月13日 星期三

POSD學習筆記(8)-Template Method

先在base class裡面定義要/不要做的一連串動作,然後把動作設成abstract,由derived class決定每個abstract method動作的內容

Ex:
class base{
         final void template(){
                   A();
                   B();
                   C();
         }
         abstract void A();
         abstract void B();
         abstract void C();
}

class derived : base{
         public void A();
         public void B();
         public void C();
}

2013年11月12日 星期二

POSD學習筆記(7)-Strategy Pattern

沒有時間,先用學校投影片的圖擋一下,之後再自己畫

例:
Client new Context工具,然後呼叫Context.ContextInterface(Algorithm Type)
接著Context會讓Strategy去依照給的Algorithm Type去呼叫對應的ConcreteStrategy然後回傳給Context,Context再給Client。

好處:
  1. 同類型的演算法可歸為同一類
  2. 可以不用switch/if去選擇演算法











以下為轉載,轉自:http://www.cnblogs.com/oomusou/archive/2007/03/26/687727.html

Remark:
strategy和template method目的相同,皆對『新需求』的不同演算法提供『擴充』的機制,但手法卻不同,strategy採用object的方式,利用delegation改變演算法,而template method則採用class的繼承方式來改變演算法,也因為strategy採用object方式,所以有run-time改變的可能,但template method採class手法,所以無法run-time改變。

GoF的原文如下:
Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.




POSD學習筆記(6)-Composite Pattern & Decorator Pattern

Composite Pattern的作用:
  1. 階層式架構
  2. 使用者使用容易,不必在乎何者是leaf物件、何者是composite物件
  3. 容易新增物件,直接加掛即可
  4. 容易閱讀

老師教的Composite Pattern和Decorator Pattern的差別:
  • Decorator:adds additional responsibilities.
    • avoid explosion of subclassing
  • Composite:object aggregation.
    • treats one and multiple objects uniformly

網路上找到的Composite Pattern和Decorator Pattern的差別:
  • Composite Pattern:So the essence is that all elements in your composite structure have the same interface even though some are leaf nodes and others are entire structures. User interfaces often use this approach to allow easy composability.
    • 新增架構方便,使用者無感 。
    • (例如原本的圖只有圓跟方,現在又新增了三角形,而使用者可以直接去使用三角形,並不會有不便)

  • Decorator Pattern:The decorator pattern allows an entity to completely contain another entity so that using the decorator looks identical to the contained entity. This allows the decorator to modify the behaviour and/or content of whatever it is encapsulating without changing the outward appearance of the entity. For example, you might use a decorator to add logging output on the usage of the contained element without changing any behaviour of the contained element.
    • 新增動作方便,使用者無感。
    • (例如原本可以畫直的捲軸,現在也可以畫橫的捲軸,而使用者可以直接去畫橫捲軸物件出來,不會感覺有什麼問題)

 

POSD學習筆記(5)-Bridge Pattern

目的:
  1. 將抽象類別與實作分離,降低耦合度,使兩者能分別變更(例如更改其中一個實作的method,前端並不會知道你有更改)。
  2. 隱藏實作,不讓使用者知道。
  3. 當抽象類別與實作都有龐大的分支時,減少新增分支所需的工。