About Object-Oriented Programming

Source: Internet
Author: User

If you are a newbie, please advise.


My understanding about Object-Oriented Programming

1)Encapsulate attributes and behaviors of common things

2)Separation of interfaces and Implementations

If there is one sentence, it is "instantiating various objects with a class ".


The following is an article I have seen in cool shell: How to understand object-oriented programming.


For the three-Section code.

This is the initial version.

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.") ;        }    }}



The next step is the procedural version.

Procedural Solution
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"))) ;    }}



Next is the naive object-oriented programming.

Naive Object-Oriented Programming

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 master's Solution

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) ;    }}


The code of the above versions makes me feel more and more difficult to understand. Originally, if-else statements or switch statements can be used to implement the required functions in a simple and clear way. Object-Oriented Programming is designed to make development more efficient and convenient for us. However, the above object-oriented method makes it a little superfluous. We should make rational use of object-oriented programming instead of abuse.

This article is from the "Calm dreamer" blog, please be sure to keep this source http://idiotxl1020.blog.51cto.com/6419277/1288523

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.