关于面向对象编程

新手上路,请多指教。


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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值