從Rob Pike 的 Google+上的一個推看到了一篇叫《Understanding Object Oriented Programming》的文章
先看這篇教程是怎麼來講述OOP的
這個把下面這段代碼描述成是Hacker Solution
public class PrintOS
{
public static void main(final String[] args)
{
String osName = System
if (osName
{
System
}
else if (osName
{
System
}
else
{
System
}
}
}
然後開始用面向對象的編程方式一步一步地進化這個代碼
先是以過程化的思路來重構之
過程化的方案
public class PrintOS
{
private static String unixBox()
{
return
}
private static String windowsBox()
{
return
}
private static String defaultBox()
{
return
}
private static String getTheString(final String osName)
{
if (osName
{
return unixBox() ;
}
else if (osName
{
return windowsBox() ;
}
else
{
return defaultBox() ;
}
}
public static void main(final String[] args)
{
System
}
}
然後是一個幼稚的面向對象的思路
幼稚的面向對象編程
PrintOS
public class PrintOS
{
public static void main(final String[] args)
{
System
}
}
OSDiscriminator
public class OSDiscriminator // Factory Pattern
{
private static BoxSpecifier theBoxSpecifier = null ;
public static BoxSpecifier getBoxSpecifier()
{
if (theBoxSpecifier == null)
{
String osName = System
if (osName
{
theBoxSpecifier = new UNIXBox() ;
}
else if (osName
{
theBoxSpecifier = new WindowsBox() ;
}
else
{
theBoxSpecifier = new DefaultBox () ;
}
}
return theBoxSpecifier ;
}
}
BoxSpecifier
public interface BoxSpecifier
{
String getStatement() ;
}
DefaultBox
public class DefaultBox implements BoxSpecifier
{
public String getStatement()
{
return
}
}
UNIXBox
public class UNIXBox implements BoxSpecifier
{
public String getStatement()
{
return
}
}
WindowsBox
public class WindowsBox implements BoxSpecifier
{
public String getStatement()
{
return
}
}
他們覺得上面這段代碼沒有消除if語句
所以
OO大師的方案
注意其中的Design Pattern
PrintOS
public class PrintOS
{
public static void main(final String[] args)
{
System
}
}
OSDiscriminator
public class OSDiscriminator // Factory Pattern
{
private static java
public static BoxSpecifier getBoxSpecifier()
{
BoxSpecifier value = (BoxSpecifier)storage
if (value == null)
return DefaultBox
return value ;
}
public static void register(final String key
{
storage
}
static
{
WindowsBox
UNIXBox
MacBox
}
}
BoxSpecifier
public interface BoxSpecifier
{
String getStatement() ;
}
DefaultBox
public class DefaultBox implements BoxSpecifier // Singleton Pattern
{
public static final DefaultBox value = new DefaultBox () ;
private DefaultBox() { }
public String getStatement()
{
return
}
}
UNIXBox
public class UNIXBox implements BoxSpecifier // Singleton Pattern
{
public static final UNIXBox value = new UNIXBox() ;
private UNIXBox() { }
public String getStatement()
{
return
}
public static final void register()
{
OSDiscriminator
OSDiscriminator
}
}
WindowsBox
public class WindowsBox implements BoxSpecifier // Singleton Pattern
{
public static final WindowsBox value = new WindowsBox() ;
private WindowsBox() { }
public String getStatement()
{
return
}
public static final void register()
{
OSDiscriminator
OSDiscriminator
}
}
MacBox
public class MacBox implements BoxSpecifier // Singleton Pattern
{
public static final MacBox value = new MacBox() ;
private MacBox() { }
public String getStatement()
{
return
}
public static final void register()
{
OSDiscriminator
}
}
作者還非常的意地說
於是我去看了一下第一作者Joseph Bergin的主頁
Rob Pike的評論
(Rob Pike是當年在Bell lab裡和Ken一起搞Unix的主兒
Rob Pike在他的Google+的這貼裡評論到這篇文章——
他並不確認這篇文章是不是搞笑?但是他覺得這些個寫這篇文章是很認真的
他說
然後
他還給了一個鏈接
Sometimes data is just data and functions are just functions
我的理解
我覺得
我以前給一些公司講一些設計模式的培訓課
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26893.html