標籤

ASP.NET MVC (29) Visual C# (15) JQuery (10) Plugins (8) JQuery Plugins (6) JavaScript (6) MySQL (5) CSS (4) LinQ (4) Mac OS (4) CentOS (3) Design Pattern (3) Entity Framework (3) IIS (3) Python (3) Windows (3) php (3) Docker (2) LAMP (2) SQL Server (2) WCF (2) .NET (1) .NET Core (1) AWS (1) Browser (1) GIS (1) IE (1) Internet Security (1) Linux (1) Platform (1) React (1) SEO (1) Testing (1) VMware (1) Windows 7 (1) cookie (1) curl (1) laravel (1) phpBB (1) session (1) 中古屋 (1) 透天 (1) 閒言閒語 (1) 面試 (1) 鳥松 (1)
顯示具有 Design Pattern 標籤的文章。 顯示所有文章
顯示具有 Design Pattern 標籤的文章。 顯示所有文章

2016年7月14日 星期四

[Design Pattern] 如何在Hello World中濫用(套用) Design Pattern

前幾天看到一篇「The Abuse of Design Patterns in writing a Hello World Program」文章中,示範了如何在最簡單的Hello World程式上,一步步套用不需要的Design Pattern,雖然看著有趣,但我認為對於想了解Design Patterns的人來說,還是有參考的價值的.

原文中的故事很有趣如下:
One comment I saw in a news group just after patterns started to become more popular was someone claiming that in a particular program they tried to use all 23 GoF patterns. They said they had failed, because they were only able to use 20. They hoped the client would call them again to come back again so maybe they could squeeze in the other 3.

Trying to use all the patterns is a bad thing, because you will end up with synthetic designs—speculative designs that have flexibility that no one needs. These days software is too complex. We can’t afford to speculate what else it should do. We need to really focus on what it needs.” – Erich Gamma

上述大意是說有個團隊想要在程式中套用GoF四人幫所有的23種Design Patterns,但是最後失敗了,因為只套用了其中20種,他們希望客戶最後還會請他們將最後3種Design Patterns也套用上去.
使用所有的Pattern不是個好主意,最終會使軟體架構變得太複雜,而且設計了永遠用不到的彈性,其實只要專注在當下產品所需要才是正道.



由於原文是使用Java當作範例,這邊我除了將它改寫為C#外,也加了一些改變:


1. 首先最基本的Hello World:



2. 定義 觀察者模式(Observer Pattern) 的主題與訂閱者



3. 實作主題與訂閱者的類別











4. 命令模式(Command Pattern) 的介面與實作



5. 抽象工廠模式(Abstract Factory) 的介面與實作



6. 單例模式 (Singleton),這邊使用Double checked locking,濫用得徹底一點 XD


7. 讓這複雜的Hello World動起來吧:



8.執行結果,這麼精美的一行...


原文有做了一些結論:


  1. 對於單純了Hello World程式來說太複雜了
  2. 我們加了一些根本就用不到的程式彈性
  3. 整個系統的分析與設計浪費太多時間
  4. 造成了類別爆炸(class explosion)
  5. 違反KISS原則(Keep it simple, stupid!)
  6. 這不是原始程式的目的,原始程式只是在命令列印出一行Hello World,現在這個程式碼離這個目.











Reference:
The Abuse of Design Patterns in writing a Hello World Program






2014年12月26日 星期五

Anti Pattern

一個講解Anti-Pattern 還不錯的網站

http://www.antipatterns.com/briefing/sld001.htm





2013年10月18日 星期五

[Visual C#][Design Pattern] C#實作執行緒安全(thread-safe)的Singleton類別


Singleton是撰寫程式時常用到的一個設計模式,例如需要實作某個單一控管的容器(連接池是一個例子),同時也需要考慮Singleton類別的執行緒安全,我們必須保證不會同時有兩個以上的物件被實體化(new instance)。

以下是一個Singleton類別的範例,此範例使用雙重判斷(double-check)的手法來避免兩個以上的物件被實體化:

public sealed class Singleton

{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Singleton() {}
   public static Singleton Instance
   {
      get
      {
         if (instance == null)
         {
            lock (syncRoot)
            {
               if (instance == null)
                  instance = new Singleton();
            }
         }
         return instance;
      }
   }
}



[Reference]:

Implementing Singleton in C#

Thread Safe C# Singleton Pattern