oop vs cop

42
OOP vs COP Objects against Classes

Upload: gpadovani

Post on 25-May-2015

2.617 views

Category:

Education


0 download

DESCRIPTION

Are we using correctly OOP? Are we focus on the right "thing" when we are desiging and programming in OOP?

TRANSCRIPT

Page 1: OOP vs COP

OOP vs COPObjects against Classes

Page 2: OOP vs COP

The beginning ...

About a year ago, I said to a friend: "Why is it called 'OOP'? It should be called 'COP' as the classes are more important!" ...my friend stopped saying "ciao" to me for a month...he works in Python and Javascript!

Page 3: OOP vs COP

The beginning ...

Some time later I saw this post of Uncle Bob "The Last programming Language" where he talks about Clojure and I read a phrase that left a strong impression on me: Inheritance is not the only way to do polymorphism

Page 4: OOP vs COP

The beginning ...

Then I discovered this book. At the moment this is the best book that I have ever read about TDD and design ...

Page 5: OOP vs COP

The beginning ...

Then I began to study Ruby and work in Javascript. In Javascript the concept of class does not exist! In Ruby the objects are softer than in C# or Java.

Page 6: OOP vs COP

OOP - Rubyirb(main):001:0> a = [1, 2]=> [1, 2]irb(main):002:0> a.somma()NoMethodError: undefined method `somma' for [1, 2]:Array from (irb):2 from C:/Ruby192/bin/irb:12:in `<main>'irb(main):003:0> def a.somma()irb(main):004:1> self.inject(:+)irb(main):005:1> end=> nilirb(main):006:0> a.somma=> 3irb(main):007:0>

Page 7: OOP vs COP

OOP - Javascript

var obj = { type: "xxx", color: "red", getInfo: function () { return this.type + ' is ' + this.color; }}; obj.color = "black";alert(obj.getInfo());

Page 8: OOP vs COP

Language matters!?

Is it just a language problem or is there something else?

Let's start from the beginning ...

Page 9: OOP vs COP

Object Oriented Programming

What is it ?

Page 10: OOP vs COP

OOP - Is it this?

Page 11: OOP vs COP

OOP - or this?

Page 12: OOP vs COP

OOP - OR THIS ????

Page 13: OOP vs COP

Let's ask "Dad"...http://en.wikiquote.org/wiki/Alan_Kay

"OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them."

Page 14: OOP vs COP

● Messaging● Local retention & protection● Hiding of State-process● Extreme late binding

So OOP is:

Page 15: OOP vs COP

Where are the classes????

Page 16: OOP vs COP

Where are the objects ?????

Page 17: OOP vs COP

Let's ask Alan again...

"It was probably in 1967 when someone asked me what I was doing, and I said: 'It's object-oriented programming'."[...]"I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages (so messaging came at the very beginning -- it took a while to see how to do messaging in a programming language efficiently enough to be useful)."

http://userpage.fu-berlin.de/~ram/pub/pub_jf47ht81Ht/doc_kay_oop_en

Page 18: OOP vs COP

Let's ask Alan again...http://lists.squeakfoundation.org/pipermail/squeak-dev/1998-October/017019.html "Just a gentle reminder that I took some pains at the last OOPSLA to try to remind everyone that Smalltalk is not only NOT its syntax or the class library, it is not even about classes. I'm sorry that I long ago coined the term "objects" for this topic because it gets many people to focus on the lesser idea."

The big idea is "messaging" [ ... ]

Page 19: OOP vs COP

Why OOP ...

OOP creates a modular design that is easily modified without having to restructure the entire system.

Page 20: OOP vs COP

Why OOP ...

Keeps large software projects manageable for human programmers

Page 21: OOP vs COP

Why OOP ...

Keeps large software projects manageable for human programmers via: Modularization - Decompose problem into smaller subproblems that can be solved independently.

Page 22: OOP vs COP

Why OOP ...

Keeps large software projects manageable for human programmers via: Abstraction -- Understandability - Terminology of the problem domain is reflected in the software solution. Individual modules are understandable by human readers.

Page 23: OOP vs COP

Why OOP ...

Keeps large software projects manageable for human programmers via: Encapsulation -- Information Hiding - Hide complexity from the user of a software or SDK. Protect low-level functionality

Page 24: OOP vs COP

Why OOP ...

Keeps large software projects manageable for human programmers via: Composability -- Structured Design - Interfaces allow modules to combine freely in order to produce new systems.

Page 25: OOP vs COP

Why OOP ...

Keeps large software projects manageable for human programmers via: Hierarchy - Incremental development from small and simple to more complex modules.

Page 26: OOP vs COP

Why OOP ...

Keeps large software projects manageable for human programmers via:

Continuity - Changes and maintenance in only a few modules does not affect the overall architecture.

Page 27: OOP vs COP

Why OOP ...

Modularization Abstraction Encapsulation Composability Hierarchy Continuityetc ...

Page 28: OOP vs COP

OOP could be this?

Page 29: OOP vs COP

OOP

The strongest concept of OOP is "messaging", so while we are developing we should concentrate on how the objects communicate between themselves

Page 30: OOP vs COP

OOP - System Level

When we develop a system we should focus on: ● Separation of concern● High level of abstraction

Page 31: OOP vs COP

OOP - System Level

We obtain a Hexagonal Architecture

Page 32: OOP vs COP

OOP - Object Level

How can we discover ports and adapters? ● Encapsulation● Information Hiding● Low Coupling● High Cohesion● Composite Simpler Than The Sum● Context Independence● Hide the correct information

Page 33: OOP vs COP

OOP - Object Level

The objects should have: ● Low Coupling● High Cohesion● Composite Simpler Than The Sum● Context Independence● Hide the correct information

Page 34: OOP vs COP

OOP - Object Level

We should compose the objects to describe the system, so we have: ● Declarative Layer - Where the objects are

created and composed● Application Layer - Where the object

communicates

Page 35: OOP vs COP

And the classes???

The classes are "the declarative layer of the declarative layer". The classes are "factories" for the objects. So you could have other factories for the objects...

Page 36: OOP vs COP

OOP - Rubyirb(main):001:0> a = [1, 2]=> [1, 2]irb(main):002:0> a.somma()NoMethodError: undefined method `somma' for [1, 2]:Array from (irb):2 from C:/Ruby192/bin/irb:12:in `<main>'irb(main):003:0> def a.somma()irb(main):004:1> self.inject(:+)irb(main):005:1> end=> nilirb(main):006:0> a.somma=> 3irb(main):007:0>

Page 37: OOP vs COP

OOP - Javascript

var obj = { type: "xxx", color: "red", getInfo: function () { return this.type + ' is ' + this.color; }}; obj.color = "black";alert(obj.getInfo());

Page 38: OOP vs COP

OOP - Scheme

(define (make-from-real-imag x y) (define (dispatch op) (cond ((eq? op 'real-part) x) ((eq? op 'imag-part) y) ((eq? op 'magnitude) (sqrt (+ (square x) (square y)))) ((eq? op 'angle) (atan y x)) (else (error "Unknown op -- MAKE-FROM-REAL-IMAG" op)))) dispatch)

Page 39: OOP vs COP

OOP - Scheme

es:> (define x (make-from-real-imag 1 2))> x#<procedure:dispatch>> (x 'real-part)1> (x 'imag-part)2>

Page 40: OOP vs COP

Example

There is a web server that exposes some APIs to manage playlists of images. We create a desktop program to manage these playlists ... server: https://github.com/gpad/rmsclient: https://github.com/gpad/PlayListManager

Page 41: OOP vs COP
Page 42: OOP vs COP

OOP vs COP

Thank YOU !!!

twitter: https://twitter.com/#!/GPad619github: https://github.com/gpade-mail: [email protected]