星期五, 10月 05, 2007

MVC Model 亂談

Strut 首推 Service to worker pattern (http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceToWorker.html) 來取代傳統的 dispatcher view pattern。仔細想想後,發現還可介於兩者之間,如 asp 2 master pages 同時使用兩者的功能。

State Pattern 小畫家設計圖

State Pattern 的運作情況,即右圖,內容是一個小畫家的程式利用 State Pattern 構造。客戶首先必須選取一個按鈕,並於畫紙上拖拉出相對的圖形

Controller,也就是小畫家的 Business Logic 或稱 Model,在此我並沒有計劃用 Modular 的模式,單純地使用 Mediate Pattern 的設計方案來增加效能 (在我的假設裡,VTable Lookup 較 Hash Table 來的有效率,好管理 OO 在設計是 Big Plus)

其它還可以在 2 流程內加入Commander Pattern来實現 Undo 和 Redo

星期三, 5月 30, 2007

printf / sprintf in PHP

Most programmers are familiar with printf/sprinf in languages such as C/C++.
They have the following benefits
1. Ease of use and clarity - the flexablility to define a format string such as "%d, %s"
2.
Maximum efficiency (ability to directly use existing buffers)
and drawbacks
3.
Length safety
4.
Type safety
5.
Templatability

#3.,#4. #5. doesn't apply to PHP language in general, therefore its a good idea to use printf/sprintf exclusively. Question is how? How to enhance printf without going through much work?

First we look at what kind of enhancement could be done? We can obviously enhance #1. by creating a new string symbol, for example %dbs, %dbs would work like %s, except it would
perform mysql_escape_string implicitly.

The perl regular expression and callback comes to rescue.
The following is a snap from drupal source.



/**
* Helper function for db_query().
*/
function _db_query_callback($match, $init = FALSE) {
static $args = NULL;
if ($init) {
$args = $match;
return;
}

switch ($match[1]) {
case '%d': // We must use type casting to int to convert FALSE/NULL/(TRUE?)
return (int) array_shift($args); // We don't need db_escape_string as numbers are db-safe
case '%s':
return db_escape_string(array_shift($args));
case '%%':
return '%';
case '%f':
return (float) array_shift($args);
case '%b': // binary data
return db_encode_blob(array_shift($args));
}
}

/**
* Indicates the place holders that should be replaced in _db_query_callback().
*/
define('DB_QUERY_REGEXP', '/(%d|%s|%%|%f|%b)/');

/**
* Runs a basic query in the active database.
*
* User-supplied arguments to the query should be passed in as separate
* parameters so that they can be properly escaped to avoid SQL injection
* attacks.
*
* @param $query
* A string containing an SQL query.
* @param ...
* A variable number of arguments which are substituted into the query
* using printf() syntax. Instead of a variable number of query arguments,
* you may also pass a single array containing the query arguments.

* Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
* in '') and %%.
*
* NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
* and TRUE values to decimal 1.
*
* @return
* A database query result resource, or FALSE if the query was not
* executed correctly.
*/
function db_query($query) {
$args = func_get_args();
array_shift($args);
$query = db_prefix_tables($query);
if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
$args = $args[0];
}
_db_query_callback($args, TRUE);
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
return _db_query($query);
}


Happy Coding!

Banner 圖片


打開 Photoshop 把數張圖貼上去,用 Ctrl+T 將它們改變大小,記得每張圖分別放在一個層,之後用遮罩把它們弄的朦朧

卡通人物手繪

幻想一下角度,直接畫就出來了