新手上路,请多指教。

关于面向对象编程,我的理解

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语句就可以简单明了的实现所需要的功能。面向对象编程是为了方便我们,使得我们的开发更加的高效率。可是,上面的面向对象反而使得有点画蛇添足。应该合理的运用面向对象编程,而不是滥用。