经典著作《设计模式》中将策略模式定义为:定义一系列的算法,把它们一个个的封装起来,并且使它们可以相互转换。这个定义还是比较抽象,下面我将通过一个例子来具体的讲解策略模式。感觉这样更容易帮助新手理解模式,这也是《Head First Design Patterns》中讲解模式的方法。先来描述一下用到的例子的背景资料:
SHOW FULL COLUMNS FROM `jrk_downrecords` [ RunTime:0.000974s ]
SELECT `a`.`aid`,`a`.`title`,`a`.`create_time`,`m`.`username` FROM `jrk_downrecords` `a` INNER JOIN `jrk_member` `m` ON `a`.`uid`=`m`.`id` WHERE `a`.`status` = 1 GROUP BY `a`.`aid` ORDER BY `a`.`create_time` DESC LIMIT 10 [ RunTime:0.085461s ]
SHOW FULL COLUMNS FROM `jrk_tagrecords` [ RunTime:0.000962s ]
SELECT * FROM `jrk_tagrecords` WHERE `status` = 1 ORDER BY `num` DESC LIMIT 20 [ RunTime:0.001187s ]
SHOW FULL COLUMNS FROM `jrk_member` [ RunTime:0.000910s ]
SELECT `id`,`username`,`userhead`,`usertime` FROM `jrk_member` WHERE `status` = 1 ORDER BY `usertime` DESC LIMIT 10 [ RunTime:0.003160s ]
SHOW FULL COLUMNS FROM `jrk_searchrecords` [ RunTime:0.000834s ]
SELECT * FROM `jrk_searchrecords` WHERE `status` = 1 ORDER BY `num` DESC LIMIT 5 [ RunTime:0.003773s ]
SELECT aid,title,count(aid) as c FROM `jrk_downrecords` GROUP BY `aid` ORDER BY `c` DESC LIMIT 10 [ RunTime:0.014824s ]
SHOW FULL COLUMNS FROM `jrk_articles` [ RunTime:0.011486s ]
UPDATE `jrk_articles` SET `hits` = 2 WHERE `id` = 424121 [ RunTime:0.013584s ]