自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如此理解面向?qū)ο缶幊?/h1>

開發(fā) 后端
我以前給一些公司講一些設(shè)計(jì)模式的培訓(xùn)課,我一再提到,那23個經(jīng)典的設(shè)計(jì)模式和OO半毛錢關(guān)系沒有,只不過人家用OO來實(shí)現(xiàn)罷了。

從Rob Pike 的 Google+上的一個推看到了一篇叫《Understanding Object Oriented Programming》的文章,我先把這篇文章簡述一下,然后再說說老牌黑客Rob Pike的評論。

先看這篇教程是怎么來講述OOP的。它先給了下面這個問題,這個問題需要輸出一段關(guān)于操作系統(tǒng)的文字:假設(shè)Unix很不錯,Windows很差。

這個把下面這段代碼描述成是Hacker Solution。(這幫人覺得下面這叫黑客?我估計(jì)這幫人真是沒看過C語言的代碼)

  1. public class PrintOS   
  2. {   
  3.     public static void main(final String[] args)   
  4.     {   
  5.         String osName = System.getProperty("os.name") ;   
  6.         if (osName.equals("SunOS") || osName.equals("Linux"))   
  7.         {   
  8.             System.out.println("This is a UNIX box and therefore good.") ;   
  9.         }   
  10.         else if (osName.equals("Windows NT") || osName.equals("Windows 95"))   
  11.         {   
  12.             System.out.println("This is a Windows box and therefore bad.") ;   
  13.         }   
  14.         else 
  15.         {   
  16.             System.out.println("This is not a box.") ;   
  17.         }   
  18.     }   

然后開始用面向?qū)ο蟮木幊谭绞揭徊揭徊降剡M(jìn)化這個代碼。

先是以過程化的思路來重構(gòu)之。

過程化的方案

  1. public class PrintOS   
  2. {   
  3.     private static String unixBox()   
  4.     {   
  5.         return "This is a UNIX box and therefore good." ;   
  6.     }   
  7.     private static String windowsBox()   
  8.     {   
  9.         return "This is a Windows box and therefore bad." ;   
  10.     }   
  11.     private static String defaultBox()   
  12.     {   
  13.         return "This is not a box." ;   
  14.     }   
  15.     private static String getTheString(final String osName)   
  16.     {   
  17.         if (osName.equals("SunOS") || osName.equals("Linux"))   
  18.         {   
  19.             return unixBox() ;   
  20.         }   
  21.         else if (osName.equals("Windows NT") ||osName.equals("Windows 95"))   
  22.         {   
  23.             return windowsBox() ;   
  24.         }   
  25.         else 
  26.         {   
  27.             return defaultBox() ;   
  28.         }   
  29.     }   
  30.     public static void main(final String[] args)   
  31.     {   
  32.         System.out.println(getTheString(System.getProperty("os.name"))) ;   
  33.     }   

然后是一個幼稚的面向?qū)ο蟮乃悸贰?/p>

幼稚的面向?qū)ο缶幊?nbsp;

PrintOS.java

  1. public class PrintOS   
  2. {   
  3.     public static void main(final String[] args)   
  4.     {   
  5.         System.out.println(OSDiscriminator.getBoxSpecifier().getStatement()) ;   
  6.     }   

OSDiscriminator.java

  1. public class OSDiscriminator // Factory Pattern   
  2. {   
  3.     private static BoxSpecifier theBoxSpecifier = null ;   
  4.     public static BoxSpecifier getBoxSpecifier()   
  5.     {   
  6.         if (theBoxSpecifier == null)   
  7.         {   
  8.             String osName = System.getProperty("os.name") ;   
  9.             if (osName.equals("SunOS") || osName.equals("Linux"))   
  10.             {   
  11.                 theBoxSpecifier = new UNIXBox() ;   
  12.             }   
  13.             else if (osName.equals("Windows NT") || osName.equals("Windows 95"))   
  14.             {   
  15.                 theBoxSpecifier = new WindowsBox() ;   
  16.             }   
  17.             else 
  18.             {   
  19.                 theBoxSpecifier = new DefaultBox () ;   
  20.             }   
  21.         }   
  22.         return theBoxSpecifier ;   
  23.     }   

BoxSpecifier.java

  1. public interface BoxSpecifier   
  2. {   
  3.     String getStatement() ;   

DefaultBox.java

  1. public class DefaultBox implements BoxSpecifier   
  2. {   
  3.     public String getStatement()   
  4.     {   
  5.         return "This is not a box." ;   
  6.     }   

UNIXBox.java

  1. public class UNIXBox implements BoxSpecifier   
  2. {   
  3.     public String getStatement()   
  4.     {   
  5.         return "This is a UNIX box and therefore good." ;   
  6.     }   

WindowsBox.java

  1. public class WindowsBox implements BoxSpecifier   
  2. {   
  3.     public String getStatement()   
  4.     {   
  5.         return "This is a Windows box and therefore bad." ;   
  6.     }   

他們覺得上面這段代碼沒有消除if語句,他們說這叫代碼的“logic bottleneck”(邏輯瓶頸),因?yàn)槿绻阋黾右粋€操作系統(tǒng)的判斷的話,你不但要加個類,還要改那段if-else的語句。

所以,他們整出一個叫Sophisticated的面向?qū)ο蟮慕鉀Q方案。

OO大師的方案

注意其中的Design Pattern

PrintOS.java

  1. public class PrintOS   
  2. {   
  3.     public static void main(final String[] args)   
  4.     {   
  5.         System.out.println(OSDiscriminator.getBoxSpecifier().getStatement()) ;   
  6.     }   

OSDiscriminator.java

  1. public class OSDiscriminator // Factory Pattern   
  2. {   
  3.     private static java.util.HashMap storage = new java.util.HashMap() ;   
  4.     
  5.     public static BoxSpecifier getBoxSpecifier()   
  6.     {   
  7.         BoxSpecifier value = (BoxSpecifier)storage.get(System.getProperty("os.name")) ;   
  8.         if (value == null)   
  9.             return DefaultBox.value ;   
  10.         return value ;   
  11.     }   
  12.     public static void register(final String key, final BoxSpecifier value)   
  13.     {   
  14.         storage.put(key, value) ; // Should guard against null keys, actually.   
  15.     }   
  16.     static 
  17.     {   
  18.         WindowsBox.register() ;   
  19.         UNIXBox.register() ;   
  20.         MacBox.register() ;   
  21.     }   

BoxSpecifier.java

  1. public interface BoxSpecifier   
  2. {   
  3.     String getStatement() ;   

DefaultBox.java

  1. public class DefaultBox implements BoxSpecifier // Singleton Pattern   
  2. {   
  3.     public static final DefaultBox value = new DefaultBox () ;   
  4.     private DefaultBox() { }   
  5.     public String getStatement()   
  6.     {   
  7.         return "This is not a box." ;   
  8.     }   

UNIXBox.java

  1. public class UNIXBox implements BoxSpecifier // Singleton Pattern   
  2. {   
  3.     public static final UNIXBox value = new UNIXBox() ;   
  4.     private UNIXBox() { }   
  5.     public  String getStatement()   
  6.     {   
  7.         return "This is a UNIX box and therefore good." ;   
  8.     }   
  9.     public static final void register()   
  10.     {   
  11.         OSDiscriminator.register("SunOS", value) ;   
  12.         OSDiscriminator.register("Linux", value) ;   
  13.     }   

WindowsBox.java

  1. public class WindowsBox implements BoxSpecifier  // Singleton Pattern   
  2. {   
  3.     public  static final WindowsBox value = new WindowsBox() ;   
  4.     private WindowsBox() { }   
  5.     public String getStatement()   
  6.     {   
  7.         return "This is a Windows box and therefore bad." ;   
  8.     }   
  9.     public static final void register()   
  10.     {   
  11.         OSDiscriminator.register("Windows NT", value) ;   
  12.         OSDiscriminator.register("Windows 95", value) ;   
  13.     }   

MacBox.java

  1. public class MacBox implements BoxSpecifier // Singleton Pattern   
  2. {   
  3.     public static final MacBox value = new MacBox() ;   
  4.     private MacBox() { }   
  5.     public  String getStatement()   
  6.     {   
  7.         return "This is a Macintosh box and therefore far superior." ;   
  8.     }   
  9.     public static final void register()   
  10.     {   
  11.         OSDiscriminator.register("Mac OS", value) ;   
  12.     }   

作者還非常的意地說,他加了一個“Mac OS”的東西。老實(shí)說,當(dāng)我看到最后這段OO大師搞出來的代碼,我快要吐了。我瞬間想到了兩件事:一個是以前酷殼上的《面向?qū)ο笫莻€騙局》和 《各種流行的編程方式》中說的“設(shè)計(jì)模式驅(qū)動編程”,另一個我想到了那些被敏捷洗過腦的程序員和咨詢師,也是這種德行。

于是我去看了一下第一作者Joseph Bergin的主頁,這個Ph.D是果然剛剛完成了一本關(guān)于敏捷和模式的書。

Rob Pike的評論

(Rob Pike是當(dāng)年在Bell lab里和Ken一起搞Unix的主兒,后來和Ken開發(fā)了UTF-8,現(xiàn)在還和Ken一起搞Go語言。注:不要以為Ken和Dennis是基友,其實(shí)他們才是真正的老基友?。?/p>

Rob Pike在他的Google+的這貼里評論到這篇文章——

他并不確認(rèn)這篇文章是不是搞笑?但是他覺得這些個寫這篇文章是很認(rèn)真的。他說他要評論這篇文章是因?yàn)樗麄兪且幻鸋acker,至少這個詞出現(xiàn)在這篇文章的術(shù)語中。

他說,這個程序根本就不需要什么Object,只需要一張小小的配置表格,里面配置了對應(yīng)的操作系統(tǒng)和你想輸出的文本。這不就完了。這么簡單的設(shè)計(jì),非常容易地?cái)U(kuò)展,他們那個所謂的Hack Solution完全就是笨拙的代碼。后面那些所謂的代碼進(jìn)化相當(dāng)瘋狂和愚蠢的,這個完全誤導(dǎo)了對編程的認(rèn)知。

然后,他還說,他覺得這些OO的狂熱份子非常害怕數(shù)據(jù),他們喜歡用多層的類的關(guān)系來完成一個本來只需要檢索三行數(shù)據(jù)表的工作。他說他曾經(jīng)聽說有人在他的工作種用各種OO的東西來替換While循環(huán)。(我聽說中國Thoughtworks那幫搞敏捷的人的確喜歡用Object來替換所有的if-else語句,他們甚至還喜歡把函數(shù)的行數(shù)限制在10行以內(nèi))

他還給了一個鏈接http://prog21.dadgum.com/156.html,你可以讀一讀。最后他說,OOP的本質(zhì)就是——對數(shù)據(jù)和與之關(guān)聯(lián)的行為進(jìn)行編程。便就算是這樣也不完全對,因?yàn)椋?/p>

Sometimes data is just data and functions are just functions.

我的理解

我覺得,這篇文章的例子舉得太差了,差得感覺就像是OO的高級黑。面向?qū)ο缶幊套⒅氐氖牵?)數(shù)據(jù)和其行為的打包封裝,2)程序的接口和實(shí)現(xiàn)的解耦。你那怕,舉一個多個開關(guān)和多個電器的例子,不然就像STL中,一個排序算法對多個不同容器的例子,都比這個例子要好得多得多。老實(shí)說,Java SDK里太多這樣的東西了。

我以前給一些公司講一些設(shè)計(jì)模式的培訓(xùn)課,我一再提到,那23個經(jīng)典的設(shè)計(jì)模式和OO半毛錢關(guān)系沒有,只不過人家用OO來實(shí)現(xiàn)罷了。設(shè)計(jì)模式就三個準(zhǔn)則:1)中意于組合而不是繼承,2)依賴于接口而不是實(shí)現(xiàn),3)高內(nèi)聚,低耦合。你看,這完全就是Unix的設(shè)計(jì)準(zhǔn)則。

原文鏈接:http://coolshell.cn/articles/8745.html/comment-page-1

責(zé)任編輯:張偉 來源: 酷殼
相關(guān)推薦

2013-06-07 10:55:10

2024-01-03 13:38:00

C++面向?qū)ο缶幊?/a>OOP

2017-04-21 09:07:39

JavaScript對象編程

2012-01-17 09:34:52

JavaScript

2012-06-07 10:11:01

面向?qū)ο?/a>設(shè)計(jì)原則Java

2010-11-17 11:31:22

Scala基礎(chǔ)面向?qū)ο?/a>Scala

2022-07-30 23:41:53

面向過程面向?qū)ο?/a>面向協(xié)議編程

2012-12-13 11:01:42

IBMdW

2023-10-25 13:42:19

Java面向?qū)ο?/a>

2012-02-27 09:30:22

JavaScript

2010-07-13 17:18:29

Perl面向?qū)ο缶幊?/a>

2019-03-26 10:50:22

Python面向?qū)ο?/a>編程語言

2011-05-25 10:21:44

Javascript

2023-01-10 09:06:17

2023-12-11 15:32:30

面向?qū)ο缶幊?/a>OOPpython

2010-07-16 17:23:57

Perl面向?qū)ο缶幊?/a>

2011-06-28 11:06:16

Scala

2010-07-13 13:06:41

Perl面向?qū)ο?/a>

2011-05-25 10:59:26

Javascript繼承

2023-11-30 08:00:54

面向?qū)ο?/a>面向切面
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號