新手上路,請多指教。
關於面向對象編程,我的理解
1)封裝具有共性的事物屬性和行為
2)接口和實現的分離
如果有一句話來說的話,那就是“用一個類去實例化各種各樣的對象”。
下面是我在酷殼所看到的一篇文章《如何理解面向對象編程》。
關於裡面的三段代碼。
這是最初的版本。
public class PrintOS { public static void main(final String[] args) { String osName = System.getProperty("os.name") ; if (osName.equals("SunOS") || osName.equals("Linux")) { System.out.println("This is a UNIX box and therefore good.") ; } else if (osName.equals("Windows NT") || osName.equals("Windows 95")) { System.out.println("This is a Windows box and therefore bad.") ; } else { System.out.println("This is not a box.") ; } } }
接著就是過程化的版本。
public class PrintOS { private static String unixBox() { return "This is a UNIX box and therefore good." ; } private static String windowsBox() { return "This is a Windows box and therefore bad." ; } private static String defaultBox() { return "This is not a box." ; } private static String getTheString(final String osName) { if (osName.equals("SunOS") || osName.equals("Linux")) { return unixBox() ; } else if (osName.equals("Windows NT") ||osName.equals("Windows 95")) { return windowsBox() ; } else { return defaultBox() ; } } public static void main(final String[] args) { System.out.println(getTheString(System.getProperty("os.name"))) ; } }
接下來是幼稚的面向對象編程
PrintOS.java
public class PrintOS { public static void main(final String[] args) { System.out.println(OSDiscriminator.getBoxSpecifier().getStatement()) ; } }
OSDiscriminator.java
public class OSDiscriminator // Factory Pattern { private static BoxSpecifier theBoxSpecifier = null ; public static BoxSpecifier getBoxSpecifier() { if (theBoxSpecifier == null) { String osName = System.getProperty("os.name") ; if (osName.equals("SunOS") || osName.equals("Linux")) { theBoxSpecifier = new UNIXBox() ; } else if (osName.equals("Windows NT") || osName.equals("Windows 95")) { theBoxSpecifier = new WindowsBox() ; } else { theBoxSpecifier = new DefaultBox () ; } } return theBoxSpecifier ; } }
BoxSpecifier.java
public interface BoxSpecifier { String getStatement() ; }
DefaultBox.java
public class DefaultBox implements BoxSpecifier { public String getStatement() { return "This is not a box." ; } }
UNIXBox.java
public class UNIXBox implements BoxSpecifier { public String getStatement() { return "This is a UNIX box and therefore good." ; } }
WindowsBox.java
public class WindowsBox implements BoxSpecifier { public String getStatement() { return "This is a Windows box and therefore bad." ; } }
OO大師的方案
PrintOS.java
public class PrintOS { public static void main(final String[] args) { System.out.println(OSDiscriminator.getBoxSpecifier().getStatement()) ; } }
OSDiscriminator.java
public class OSDiscriminator // Factory Pattern { private static java.util.HashMap storage = new java.util.HashMap() ; public static BoxSpecifier getBoxSpecifier() { BoxSpecifier value = (BoxSpecifier)storage.get(System.getProperty("os.name")) ; if (value == null) return DefaultBox.value ; return value ; } public static void register(final String key, final BoxSpecifier value) { storage.put(key, value) ; // Should guard against null keys, actually. } static { WindowsBox.register() ; UNIXBox.register() ; MacBox.register() ; } }
BoxSpecifier.java
public interface BoxSpecifier { String getStatement() ; }
DefaultBox.java
public class DefaultBox implements BoxSpecifier // Singleton Pattern { public static final DefaultBox value = new DefaultBox () ; private DefaultBox() { } public String getStatement() { return "This is not a box." ; } }
UNIXBox.java
public class UNIXBox implements BoxSpecifier // Singleton Pattern { public static final UNIXBox value = new UNIXBox() ; private UNIXBox() { } public String getStatement() { return "This is a UNIX box and therefore good." ; } public static final void register() { OSDiscriminator.register("SunOS", value) ; OSDiscriminator.register("Linux", value) ; } }
WindowsBox.java
public class WindowsBox implements BoxSpecifier // Singleton Pattern { public static final WindowsBox value = new WindowsBox() ; private WindowsBox() { } public String getStatement() { return "This is a Windows box and therefore bad." ; } public static final void register() { OSDiscriminator.register("Windows NT", value) ; OSDiscriminator.register("Windows 95", value) ; } }
MacBox.java
public class MacBox implements BoxSpecifier // Singleton Pattern { public static final MacBox value = new MacBox() ; private MacBox() { } public String getStatement() { return "This is a Macintosh box and therefore far superior." ; } public static final void register() { OSDiscriminator.register("Mac OS", value) ; } }
上面幾個版本的代碼,給我的感覺是越來越來難看懂,本來用if-else語句或者switch語句就可以簡單明了的實現所需要的功能。面向對象編程是為了方便我們,使得我們的開發更加的高效率。可是,上面的面向對象反而使得有點畫蛇添足。應該合理的運用面向對象編程,而不是濫用。
本文出自 “淡定的dreamer” 博客,請務必保留此出處http://idiotxl1020.blog.51cto.com/6419277/1288523