我有一個大工廠,我試圖弄清楚如何使它看起來很漂亮。
有大約40個可能的對象/構造組合:C++和STL:構造函數工廠
if(algorithm == "SHA-1")
return new HashImpl<...>(algorithm, seed, size);
if(algorithm == "SHA-224")
return new HashImpl<...>(algorithm, seed, size);
if(algorithm == "SHA-256")
return new HashImpl<...>(algorithm, seed, size);
...
if(algorithm == "AES" || algorithm == "AES128")
return new BlockCipherImpl<...>(algorithm, seed, size);
...
if(algorithm == "HmacSHA1")
return new HmacImpl<...>(algorithm, seed, size);
...
有沒有把這個成一張地圖,這樣我至少可以廢除順序查找的方法嗎?我在解決如何使構造函數成爲函子時遇到問題。
編輯:該代碼可以在這裏找到:
http://code.google.com/p/owasp-esapi-cplusplus/source/browse/trunk/src/crypto/SecureRandomImpl.cpp,各地開始線路130
您能否更具體地瞭解HashImpl的<...>? –
您的代碼在返回的內容中沒有顯示出不同。不同的算法字符串有什麼不同?模板參數?構造函數的參數?哪個 ? –
羅伯特和桑德 - 我的不好。我發佈了一個鏈接到實際的代碼。全部返回'BaseImplementation *'。 – jww