偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

從Rob Pike 的 Google+上的一個(gè)推看到了一篇叫《Understanding Object Oriented Programming》的文章,我先把這篇文章簡(jiǎn)述一下,然后再說(shuō)說(shuō)老牌黑客Rob Pike的評(píng)論。

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

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

  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è)代碼。

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

過(guò)程化的方案

  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.     }   

然后是一個(gè)幼稚的面向?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語(yǔ)句,他們說(shuō)這叫代碼的“logic bottleneck”(邏輯瓶頸),因?yàn)槿绻阋黾右粋€(gè)操作系統(tǒng)的判斷的話,你不但要加個(gè)類,還要改那段if-else的語(yǔ)句。

所以,他們整出一個(gè)叫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.     }   

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

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

Rob Pike的評(píng)論

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

Rob Pike在他的Google+的這貼里評(píng)論到這篇文章——

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

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

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

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

Sometimes data is just data and functions are just functions.

我的理解

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

我以前給一些公司講一些設(shè)計(jì)模式的培訓(xùn)課,我一再提到,那23個(gè)經(jīng)典的設(shè)計(jì)模式和OO半毛錢關(guān)系沒有,只不過(guò)人家用OO來(lái)實(shí)現(xiàn)罷了。設(shè)計(jì)模式就三個(gè)準(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é)任編輯:張偉 來(lái)源: 酷殼
相關(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對(duì)象編程

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

面向過(guò)程面向?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-16 17:23:57

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

2010-07-13 17:18:29

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

2019-03-26 10:50:22

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

2011-05-25 10:21:44

Javascript

2011-06-28 11:06:16

Scala

2023-12-11 15:32:30

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

2023-01-10 09:06:17

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ù)棧公眾號(hào)