Understanding Object Oriented Programming

Source: Internet
Author: User
understanding Object Oriented Programming Joseph Bergin
Pace University
jbergin@pace.edu
Russel Winder
King ' s College London
russel@dcs.kcl.ac.uk

The code on this page grew out of a discussion on the Object Technology in Computer Science education list server. The discussion had been going on to about hours in late March and early April (on the centered of "question I s OO really; Is it a real paradigm, different from procedural programming or are it just a packaging mechanism for procedural G? " Both of the authors believe that it's a real paradigm shift, requiring a change in mental model in the practitioners. Winder produced the "three" following code fragments to show the difference in styles between hackers Al programmers, and (naive) object oriented programmers. Bergin then added the more sophisticated OO version this is appears last. Theproblem

The problem to being solved is to output a value judgment about operating systems. The assumptions being (of course) is good and Windows are bad. Hacker Solution

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)") 
		{
			System.out.println ("This is a Windows box and therefore bad. ");
		} 
		else 
		{
			System.out.println ("This isn't a box.");}
		}

Their claim:it works doesn ' t It what more does you want? Also I got mine implemented and working faster than any of the others, so there. Evaluation

While this solves the problem, it would is easy to modify in the future if the problem. In particular, if we need to add new operating systems, we need to extend the  if  structure. If we want to add additional functionality for each operating system, we would likely the this expand to Nested  if  statements. This would get unwieldy over time. Thus, the hacker has solved the immediate problem, but made little progress on future to the program. 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)") {return Windowsbox ();
		else {return defaultbox ();  } public static void Main (final string[] args) {System.out.println (getthestring (System.getproperty ("Os.name"))
	; }
}

Their claim:java is a wonderful procedural programming language; it naturally supports top-down decomposition Clearly the way of analyzing and designing quality solutions to problems – as exemplified by this example.
Evaluation

The procedural programmer has made some progress on the larger problem. If a operating system needs to is added, we extend the IF statement in the Getthestring function and add a new function f or that OS. However, if the functionality of each OS needs to being extended, what we are likely to the if the IF statement Would most likely be replicated elsewhere at the program each time we need to make the distinction between operating Syste Ms. Once that happens, whenever we add a new OS or change or add functionality we'll need to find all of these if statem Ents and update them compatibly*. This is very the error prone and results in entropy setting into such.

In effect the programmer is using Ad-hoc polymorphism. We want different things to happen, but the programmer must the specifically the choice of ' the ' to what in each happen Ance. Naive Object oriented Solution

This solution requires several classes in several files.

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 BOXSPECIF
  	IER 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)") {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 isn't 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 was a Windows box and therefore bad. '
 }
}

Their Claim:well I managed to get both Singleton and Factory is into the implementation so according to all the hype About object-oriented programming and patterns it must be good.

(Note:the factory is a kind of naive singleton.) Evaluation

This programmer has made quite a lot more progress toward the goal of writing a maintainable. In particular, if we need to add a OS, we extend the  if  statement as before, and write a new C Lass for that OS. This is similar to what the procedural programmer had to do. However, if we need to add functionality for each OS, we are need to change the classes this deal with that OS. the  if  statement in Osdiscriminator be still a "logic bottleneck" but it is the Program. This means so the location of change are easy to find (the classes that implement the functionality). Also, if we add functionality by changing the interface Boxspecifier, then the compiler'll tell us if some class fails t o Implement the new required functionality. We won ' t have to search the "locus of" for the "the" for "the" and "no help" from the tools.

However, this is solution still does ad-hoc polymorphism in the IF statement. Object oriented programming attempts to remove all such ad-hoc decision. Every if and Everyswitch should to viewed as a lost for dynamic opportunity. If we can replace this and dynamic polymorphism then the program would be much easier to maintain. sophisticated Object oriented + Patterns Solution

In the following, Printos.java and Boxspecifier.java are unchanged from the above.

Printos.java------------public class Printos {public static void main (final string[] args) {System.out.print
  	ln (Osdiscriminator.getboxspecifier (). Getstatement ()); } Osdiscriminator.java--------------------public class Osdiscriminator//Factory pattern {private static Java.ut Il.
  
 	HASHMAP storage = new Java.util.HashMap (); public static Boxspecifier Getboxspecifier () {Boxspecifier value = (boxspecifier) storage.get (System.getproperty ("OS.N
		Ame "));
		if (value = = null) return defaultbox.value;
 	return value; public static void register (final String key, final boxspecifier value) {storage.put (key, value);//Should gu 
  	ard 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 U
	Nixbox 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 stat
	IC Final Windowsbox value = new Windowsbox ();
  	Private Windowsbox () {} public String getstatement () {return ' This was 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 MACBO
	X 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); }
}

Their claim:aaaaahhhhh.  And besides, I added important functionality-Mac OS. Evaluation

Here we are have turned the OS objects into singletons and so there can is only one one such object in all of these classes. This may is desirable or not. If It isn't, then the factory wouldn ' t return the objects in the hash table, but would return clones of them instead.

Here we have maintainable code. To add a new OS, like the Mac OS, we just add a new class and add it registration to the factory. To change the functionality we change the OS classes. To add new functionality, we either modify the OS classes, or extend them. Note This there is NO Ad-hoc polymorphism here except the single test for NULL in the factory. Deconstruction

Whether It is clear or not, the mental processes of the programmers who wrote this different versions was quite Ent. The hacker wanted to get the immediate job has been done in all cost. The procedural programmer views the nature of computation as a decomposition of a function into sub-functions (helper func tions) that solve sub-problems. The object-oriented programmers to the nature of computation as a swarm of interacting agents this provide services for O Ther objects. Further, the sophisticated OO programmer lets the system take care the all polymorphic tasks possible. This programmer sees the essence of object oriented programming as the naive object-oriented. Notes

Singleton and Factory are discussed in design Patterns by Gamma, Helm, Johnson, and Vlissides (Addison-wesley, 1995). This is the ' famous ' Gang of Four ' or GOF book. The Defaultbox is a kind of Null Object. This are by Bobby Wolfe and can being found in Languages the program Design 3, edited by Martin, Riehle, and Bu Schmann (Addison-wesley, 1998)

While object oriented programmers try to avoid ad-hoc polymorphism it isn ' t always possible. The hard-to-impossible cases are when dealing with primitive (non-object) data in hybrid languages like Java, parsing Inpu T, and when creating new objects. Here, however, we have solved the creational problem and a simple factory containing. The creational problem can is solved in general through the use of reflection, such as the Java reflection API. The other situations are less tractable.

For more on Ad-hoc polymorphism, and the Bergin ' s Selection Patterns.
For more on dynamic polymorphism, Bergin ' s polymorphism Patterns.
For more on how the object oriented programmer thinks, and the Bergin ' s object Patterns. More

This is another perspective in the same ideas from out friend and colleague Dung X. Nguyen of Rice University. By the way, Dung has do a lot with using patterns to enhance object-oriented code seen by students. He often works with Stephen Wong of Oberlin College. They have some nice papers on this in the last few SIGCSE conference.

Note:for a discussion on why replicated code, such as this in the if structure of the procedural solution, are bad to Ken T Beck ' s discussion on the wiki wiki Web. onceandonlyonce

Last Updated:july 30, 2000

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.