Monday, November 11, 2019

Object-oriented programming and design



When creating a software system for a particular problem domain, the problem is carefully analyzed and a design or working model is worked upon [1]. If this process involves an object-oriented point of view, then it is called object-oriented analysis and design (OOAD) process [1]. The design or model is created in the OOAD process using class diagrams [4]. To implement such model, object-oriented programming (OOP) is needed [1]. OOP is currently the world’s most widely practiced programming paradigm [1].

History
Alan Kay is considered to be the inventor of what we call today “object-oriented development” [8]. Before the notion of having real world objects to solve problems, structured programming was in place [2]. The major drawback of this approach was that data was not secure [2]. Additionally, there was problem with manageability of software system for large systems [2]. OOP was introduced to tackle these problems [2]. The OOP introduced ways to limit access to data and many design patterns and architectures were introduced around OOP to manage complex systems. There were many different works by many different inventors around the world for OOP starting from the 1960s [8]. Among them, the main two notable contributions were created by Bjarne Stroustrup in 1979 with the invention of C++ and by James Gosling in 1995 with the invention of Java [3, 6]. The essence of OOP has remained the same throughout these years however various OOP languages, frameworks and design patterns have come out and become popular [9]. The frameworks provide boilerplate code for rapid application development and the design patterns plus architecture provide best practice guidelines for maintainable code [9, 10]. Some popular frameworks are Spring (Java), Express (JavaScript), Laravel (PHP), etc. SOLID and GRASP are examples of prevalent OOP architectures and Singleton, Factory, etc. are popular design patterns nowadays [9, 10].


Major Concepts
OOP involves the following major three concepts: Encapsulation, Inheritance and Polymorphism [2]. Encapsulation is the representation of a real-world entity by combining the characteristics and the associated behaviors of that entity [2]. It is implemented by the use of a class that encapsulates data and methods that act on the data [2]. It provides data hiding by utilizing access modifiers thereby providing data security on need basis [2]. Inheritance provides hierarchical categorization of real-world entities, so as to conveniently model the system [2]. It is implemented by the use of superclass-subclass relationship where common characteristics and behaviors are implemented in superclass and specialized characteristics and behaviors are implemented in subclass [2]. It provides code reusability where once written and tested class can be reused later in the inheritance hierarchy [2]. Polymorphism means having multiple behavioral forms depending on the entity or context [2]. It is implemented by the use of method overloading which provides context at compile time and method overriding which provides context at runtime [2]. It increases code readability, reduces code redundancy and supports lose coupling thereby making code more maintainable for developers [2].

The OOP languages are basically divided into following three types according to their inclination towards object-orientation [8]:
  • Pure OO languages: In these languages, everything is object including primitives [8]. Most popular example is Ruby [8].
  • OO with procedural components: In these languages, there are procedural components like primitives but they support all features of pure OO languages [8]. Most popular example is Java [8].
  • Object based languages / Prototype-based languages: In these languages, they do not include all the features of object orientation or have support in a different way [7]. Most popular example is JavaScript which is used to provide prototype-based full object orientation but currently can provide object-based also [8].


Implementation
The most pervasive programming language in the world currently is Java [6]. Java supports all features of OOP plus contains some procedural features like primitives and lambdas [6]. It provides encapsulation by the use of a class utilizing access modifiers like private, package-private (default), protected and public as:




Similarly, Java provides inheritance with superclass-subclass relationship as:




Further, Java provides polymorphism in many ways out of which one example is:
 




In this implementation of polymorphism, the appropriate version of myMethod() is called depending on the object that is in context. This is called dynamic polymorphism or late binding in Java [6]. We also have static polymorphism or early binding in Java via method overloading [6].

JavaScript (JS) is another popular language in the world. But it is used for more of functional programming rather than object-oriented [7]. In current version of JS, functions are objects [5,7]. Earlier JS used to be object based only but with the introduction of class in ECMAScript 6, it can now support object orientation [5]. Also, with the introduction of asynchronous programming in JS, it is currently one of the most powerful programming languages [5].



For OO, we can write encapsulation code in JS as:



The same code with class can be written nowadays as:





Inheritance in JS can be achieved similar to Java as:
 


Polymorphism in JS can be realized as:



Thus, we can conclude that JavaScript and Java are both powerful languages as both can be used to implement OOP and solve real-world problems. However, Java is more preferred in electronic devices and large financial applications such as banks whereas JavaScript is more chosen for web development for both client-side and server-side programming [6, 7].

Further Reading
Due to the limitation of time and other constraints, this work cannot get into more detail explaining every aspect of OOP. As an enthusiast on the art of OOP, it might be a good idea to take a deep dive on Gang of Four, SOLID, and GRASP techniques [10]. Further newer technologies like asynchronous programming may be a good topic for another study.


References
  1. Deitel, P., & Deitel, Harvey M. (2012). Java : How to program (9th ed., How to program series). Upper Saddle River, N.J.: Prentice Hall.
  2. Ganesh, S.G., Kiran H., & Sharma T. (2016). Oracle Certified Professional Java SE 8 Programmer Exam 1Z0-809: A Comprehensive OCPJP 8 Certification Guide. Apress.
  3. https://en.wikipedia.org/wiki/C++
  4. https://en.wikipedia.org/wiki/Class_diagram
  5. https://en.wikipedia.org/wiki/ECMAScript
  6. https://en.wikipedia.org/wiki/Java_(programming_language)
  7. https://en.wikipedia.org/wiki/JavaScript
  8. https://en.wikipedia.org/wiki/Object-oriented_programming
  9. https://en.wikipedia.org/wiki/Software_design_pattern
  10. https://en.wikipedia.org/wiki/SOLID

No comments:

Post a Comment