Great ideas in Object Oriented Programming
Contents
Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. – Wikipedia
OOP is just a programming paradigm and it helps programmer to build more robust and implement more abstract concept which are needed in their project.OOP isn’t the treasure which only belongs to a special programming language. It offer a way to make the problem more easy to be understood and to be solved.
There are three gread ideas in OOP.
- Encapsulation
- Composition, inheritance, and delegation
- Polymophsim
Encapsulation
The most basic idea in OOP is that each object encapsulates some data and code. The object takes requests from other client object. The object alone is responsible for its own state, exposing public message for clients, and declaring private the variables and the methods. The client depends on the simple public interface, and does not known about or depend on the details of the implementation.
Encapsulation remind us to put details and complexity into a black box. The outside people(user) don’t need to care about how this box was built but just use it with the instructions which the box suppled.
Q: What if we want to implement a abstract concept in mathematic – Point.
Point, the most basic presentation of point is two coordinates in Descartes coordinate system(x, y). How to implement it with program ?
Consider the following implementation in java.
|
|
Here all information is public, this is the most convenient for end users, but limits our ability to change things later in development. What would happend if you build a software and you find something should be changed with the implementation of MyPoint1
. You may want to add more attributes into this class. You may also want to rewrite your code and change it into another coordinate system but not descartes coordinate system.
If the others module depends on your implementation of MyPoints1
and use the data member of that class directly, you may have to rewrite your whole project and it will cost your a lot of time. It will be nightmare for programmer to be told “Hey, buddy. There is something changed with requirement, you have to rewrite your whole project. Just put the code into garbage collector. It can’t be modified to satisfy the new requirement.”
Here is a bettern one solution. In MyPoint2
, the detail of this class are setted as private which means that the client(user) can’t use the information about the implementation directly. The only way that the user can use the information which they need is to use the interface that the class supply with. In this example, the interface which is public can access getX() and getY() .
|
|
The advantage of accessing control and encapsulation is that we can replace the current implementation into another better one and make sure the client don’t need to be modified for this changes in the original class(MyPoint2).
Here, we implement Point with polar coordinate system. All the interface that set as public in MyPoint2
are unchanged. This make sure that the client which use getX()
and getY()
don’t need to be modified for changes with the implementation of Point.
|
|
Composition, inheritance, and delegation
In object-oriented programming, inheritance is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior). In this ariticle, we only talk about the class-based inheritance.
What’s the adavantages of inheritance ?
- Overriding
- Code Reuse
Many OOP programming language permit to replace the implementation of one or more method that it inherited from the base class. We call this feature as Overriding. There are two way to help us to reuse code. The one way is composition and the other way is inheritance.
You reuse code by creating new classes, but instead of creating them from scratch, you use existing classes that someone has already built and debugged.
In Java and Python (I don’t know the implementation of C++), you always doing inheritance when you create an object.
Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class. Since: JDK1.0 – Java Doc notes
You could also read my article about Moemory Model of Object in C++ and In Object In Python. If you are interesting in the implementation of inheritance, just read all about it.
Warning of using inheritance
In heritance is a clever and appealing techonology, it is best applied in somewhat rare circumstances where you have several deeply similar classes. It’s a common error for beginning OOP programmers to try to use inheritance for everything. In constrat, application of modularity and encapsulation and API design may be less flashy, but they are incredibly common and powerful.
Polymophsim
OOP Design
作者: Jason Leaster
来源: http://jasonleaster.github.io
链接: http://jasonleaster.github.io/2016/06/14/great-ideas-in-object-oriented-programming/
本文采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可