C++--oop Object-Oriented understanding

Source: Internet
Author: User

A tweet from Rob Pike's Google + saw an article called "Understanding Object Oriented Programming," and I'll start by outlining this article and then talking about the old hack Rob Pike's comments.
Let's see how this tutorial is going to tell you about OOP. It first gives the following question, which requires the output of a text about the operating system: Assuming UNIX is good, Windows is poor.
This describes the following code as hacker solution. (Do these people think this is called a hacker?) I guess the guys really haven't read the C code.
public class PrintOS
{
public static void Main (finalstring[] 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.");
}
ElseIf (Osname.equals ("Windows NT") | | osname.equals ("Windows 95"))
{
System.out.println ("This was a Windows box and therefore bad.");
}
Else
{
System.out.println ("This was not a box.");
}
}
}
It then begins to evolve this code step-by-stage with object-oriented programming.
First, the process of thinking to reconstruct it.

The process of the solution
public class PrintOS
{
private static String Unixbox ()
{
Return "This is a UNIX box and therefore good.";
}
private static String Windowsbox ()
{
Return "This was a Windows box and therefore bad.";
}
private static String Defaultbox ()
{
Return "This was not a box.";
}
private static String getthestring (finalstring osname)
{
if (Osname.equals ("SunOS") | | | osname.equals ("Linux"))
{
Returnunixbox ();
}
ElseIf (Osname.equals ("Windows NT") | | Osname.equals ("Windows 95"))
{
Returnwindowsbox ();
}
Else
{
Returndefaultbox ();
}
}
public static void Main (finalstring[] args)
{
System.out.println (Getthestring (System.getproperty ("Os.name"));
}
}
Then there is a naïve object-oriented approach.


Naïve object-oriented programming
Printos.java
Publicclassprintos
{
Publicstaticvoidmain (finalstring[] args)
{
System.out.println (Osdiscriminator.getboxspecifier (). Getstatement ());
}
}

Osdiscriminator.java
publicclassosdiscriminator//Factory Pattern
{
Privatestaticboxspecifier Theboxspecifier =null;
Publicstaticboxspecifier Getboxspecifier ()
{
if (Theboxspecifier ==null)
{
String osname = System.getproperty ("Os.name");
if (Osname.equals ("SunOS") | | | osname.equals ("Linux"))
{
Theboxspecifier =newunixbox ();
}
ElseIf (Osname.equals ("Windows NT") | | osname.equals ("Windows 95"))
{
Theboxspecifier =newwindowsbox ();
}
Else
{
Theboxspecifier =newdefaultbox ();
}
}
Returntheboxspecifier;
}
}

Boxspecifier.java
Publicinterfaceboxspecifier
{
String getstatement ();
}

Defaultbox.java
Publicclassdefaultboximplementsboxspecifier
{
Publicstring getstatement ()
{
Return "This was not a box.";
}
}

Unixbox.java
Publicclassunixboximplementsboxspecifier
{
Publicstring getstatement ()
{
Return "This is a UNIX box and therefore good.";
}
}

Windowsbox.java
Publicclasswindowsboximplementsboxspecifier
{
Publicstring getstatement ()
{
Return "This was a Windows box and therefore bad.";
}
}
They feel that the above code does not eliminate the IF statement, they say this is called the Code "logic bottleneck" (logical bottleneck), because if you want to add an operating system to judge, you not only have to add a class, but also to change the paragraph if-else statement.
So, they complete an object-oriented solution called sophisticated.


Master Oo's solution
Note the design Pattern in which

Printos.java
Publicclassprintos
{
Publicstaticvoidmain (finalstring[] args)
{
System.out.println (Osdiscriminator.getboxspecifier (). Getstatement ());
}
}

Osdiscriminator.java
publicclassosdiscriminator//Factory Pattern
{
Privatestaticjava.util.HashMap storage =newjava.util.hashmap ();

Publicstaticboxspecifier Getboxspecifier ()
{
Boxspecifier value = (boxspecifier) storage.get (System.getproperty ("Os.name"));
if (value ==null)
Returndefaultbox.value;
returnvalue;
}
Publicstaticvoidregister (finalstring key,finalboxspecifier value)
{
Storage.put (key, value);//should guard against null keys, actually.
}
Static
{
Windowsbox.register ();
Unixbox.register ();
Macbox.register ();
}
}

Boxspecifier.java
Publicinterfaceboxspecifier
{
String getstatement ();
}

Defaultbox.java
publicclassdefaultboximplementsboxspecifier//Singleton Pattern
{
Publicstaticfinaldefaultbox value =newdefaultbox ();
Privatedefaultbox () {}
Publicstring getstatement ()
{
Return "This was not a box.";
}
}

Unixbox.java
publicclassunixboximplementsboxspecifier//Singleton Pattern
{
Publicstaticfinalunixbox value =newunixbox ();
Privateunixbox () {}
Public String getstatement ()
{
Return "This is a UNIX box and therefore good.";
}
Publicstaticfinalvoidregister ()
{
Osdiscriminator.register ("SunOS", value);
Osdiscriminator.register ("Linux", value);
}
}

Windowsbox.java
Publicclasswindowsboximplementsboxspecifier//Singleton Pattern
{
public static final Windowsbox value =new windowsbox ();
Privatewindowsbox () {}
Publicstring getstatement ()
{
Return "This was a Windows box and therefore bad.";
}
Publicstaticfinalvoidregister ()
{
Osdiscriminator.register ("Windows NT", value);
Osdiscriminator.register ("Windows", value);
}
}

Macbox.java
publicclassmacboximplementsboxspecifier//Singleton Pattern
{
Publicstaticfinalmacbox value =newmacbox ();
Privatemacbox () {}
Public String getstatement ()
{
Return "This was a Macintosh box and therefore far superior.";
}
Publicstaticfinalvoidregister ()
{
Osdiscriminator.register ("Mac OS", value);
}
}
The author also very much said that he added a "Mac OS" thing. To be honest, when I saw the last piece of code that OO got out, I was about to vomit. I thought of two things in a flash: the "object-oriented is a scam" and "popular programming" in the previous cool shell, "design pattern driven programming", and the other I think of those who have been agile brainwashed programmers and consultants, also this virtue.
So I went to see the homepage of Joseph Bergin, the first author, and this Ph.D is a book about agility and patterns that has just been completed.
Rob Pike's comments
(Rob Pike was a unix Zhu er with Ken in Bell Lab, and later UTF-8 with Ken, and now he's going to go language with Ken.) Note: Do not think Ken and Dennis are friends, in fact they are the real old friends! )
Rob Pike, in his Google + post, commented on this article
He is not sure this article is not funny? But he thinks these are serious about writing this article. He said he was going to comment on the article because they were a hacker, at least in terms of the term in this article.
He said that the program does not need any object at all, only a small configuration table, which is configured with the corresponding operating system and the text you want to output. It's not over. Such a simple design, very easy to expand, their so-called hack solution is completely clumsy code. Behind those so-called code evolution is quite insane and foolish, and this completely misled the cognition of programming.
He then added that he felt the OO fanatics were terrified of data, and that they liked to use multi-layered relationships to do a job that would have only needed to retrieve three rows of data tables. He said he had heard of someone working on his work to replace the while loop with a variety of oo things. (I've heard that Chinese thoughtworks are really fond of using object to replace all if-else statements, and they even like to limit the number of lines in a function to 10 lines)
He also gave a link to http://prog21.dadgum.com/156.html, you can read it. In the end, he says, the essence of OOP is to program the data and the behavior associated with it. Even this is not entirely true, because:
Sometimes data is just data and functions are just functions.
I understand.
I think, the example of this article is too poor, poor feel like oo high-level black. Object-Oriented programming focuses on: 1) data and its behavior of packaging, 2) program interface and implementation of decoupling. You are afraid, to give an example of multiple switches and multiple appliances, or, as in STL, a sorting algorithm is much better than this example for a number of different container examples. To be honest, there are too many such things in the Java SDK.
I used to tell some companies about the design patterns of training courses, I have repeatedly mentioned that the 23 classic design patterns and oo half-dime relationship is not, but people use OO to achieve it. Design pattern on three criteria: 1) in combination rather than inheritance, 2) depends on the interface rather than the implementation, 3) high cohesion, low coupling. You see, this is all about UNIX design guidelines.
(End of full text)

C++--oop Object-Oriented understanding

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.