Practical uses of OOP

2019-04-22 18:40发布

I recently had a debate with a colleague who is not a fan of OOP. What took my attention was what he said:

"What's the point of doing my coding in objects? If it's reuse then I can just create a library and call whatever functions I need for whatever task is at hand. Do I need these concepts of polymorphism, inheritance, interfaces, patterns or whatever?"

We are in a small company developing small projects for e-commerce sites and real estate.

How can I take advantage of OOP in an "everyday, real-world" setup? Or was OOP really meant to solve complex problems and not intended for "everyday" development?

标签: oop paradigms
13条回答
Bombasti
2楼-- · 2019-04-22 19:26

My personally view: context

When you program in OOP you have a greater awareness of the context. It helps you to organize the code in such a way that it is easier to understand because the real world is also object oriented.

查看更多
趁早两清
3楼-- · 2019-04-22 19:27

Not only does it make

  • programming easier / more maintainable in the current situation for other people (and yourself)
  • It is already allowing easier database CRUD (Create, Update, Delete) operations.

You can find more info about it looking up: - Java : Hibernate - Dot Net : Entity Framework

See even how LINQ (Visual Studio) can make your programming life MUCH easier.

  • Also, you can start using design patterns for solving real life problems (design patterns are all about OO)

Perhaps it is even fun to demonstrate with a little demo:

  • Let's say you need to store employees, accounts, members, books in a text file in a similar way.

.PS. I tried writing it in a PSEUDO way :)

the OO way

Code you call: io.file.save(objectsCollection.ourFunctionForSaving())

class objectsCollection

function ourFunctionForSaving() As String

String _Objects

   for each _Object in objectsCollection
         Objects &= _Object & "-"
   end for

return _Objects end method

NON-OO Way

I don't think i'll write down non-oo code. But think of it :)

NOW LET'S SAY

In the OO way. The above class is the parent class of all methods for saving the books, employees, members, accounts, ... What happens if we want to change the way of saving to a textfile? For example, to make it compactible with a current standard (.CVS).

And let's say we would like to add a load function, how much code do you need to write? In the OO- way you only need the add a New Sub method which can split all the data into parameters (This happens once).

Let your collegue think about that :)

查看更多
beautiful°
4楼-- · 2019-04-22 19:31

More than getting something to just work - your friend's point, a well designed OO design is easier to understand, to follow, to expand, to extend and to implement. It is so much easier for example to delegate work that categorically are similar or to hold data that should stay together (yes even a C struct is an object).

查看更多
Lonely孤独者°
5楼-- · 2019-04-22 19:32

The good things about OOP come from tying a set of data to a set of behaviors.

So, if you need to do many related operations on a related set of data, you can write many functions that operate on a struct, or you can use an object.

Objects give you some code reuse help in the form of inheritance.

IME, it is easier to work with an object with a known set of attributes and methods that it is to keep a set of complex structs and the functions that operate on them.

Some people will go on about inheritance and polymorphism. These are valuable, but the real value in OOP (in my opinion) comes from the nice way it encapsulates and associates data with behaviors.

Should you use OOP on your projects? That depends on how well your language supports OOP. That depends on the types of problems you need to solve.

But, if you are doing small websites, you are still talking about enough complexity that I would use OOP design given proper support in the development language.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-04-22 19:35

In domains where state and behavior are poorly aligned, Object-Orientation reduces the overall dependency density (i.e. complexity) within these domains, which makes the resulting systems less brittle.

This is because the essence of Object-Orientation is based on the fact that, organizationally, it doesn't dustinguish between state and behavior at all, treating both uniformly as "features". Objects are just sets of features clumpled to minimize overall dependency.

In other domains, Object-Orientation is not the best approach. There are different language paradigms for different problems. Experienced developers know this, and are willing to use whatever language is closest to the domain.

查看更多
SAY GOODBYE
7楼-- · 2019-04-22 19:36

Ask your friend to visualize any object in his very Room, House or City... and if he can tell a single such object which a system in itself and is capable of doing some meaningful work.

Things like a button isnt doing something alone - it takes lots of objects to make a phone call.
Similarly a car engine is made of the crank shaft, pistons, spark plugs. OOPS concepts have evolved from our perception in natural processes or things in our lives.

The "Inside COM" book tells the purpose of COM by taking analogy from a childhood game of identifying animals by asking questions.

查看更多
登录 后发表回答