SlideShare a Scribd company logo
1 of 70
Download to read offline
by Hüseyin Ergin
Programming Concepts
Focused on Object-oriented
Programming
Programming
• Just writing some text in a specific format
• There are thousands of programming languages!
2
So, what is the trick here?
• A compiler/interpreter does the actual job!
• Read text file, interpret the contents, output
another file more understandable by machines,
less understandable by us 
3
Modern IDEs
• Previous slide is not a modern day practice.
– At least only computer engineers should do it.
• Now we have IDEs (Integrated Development
Environment)
– Providing
• Text editors
• Compilers
• And many other useful features
4
IDE Samples
5
What is a program?
• Just some sequential lines to instruct the
computer what to do!
6
What is a programming language?
• Provides the developer useful constructs
– Defining variables
– Looping
– Conditionals
– Many more…
• And advanced constructs
– Data management
– Memory management
– Networking
– Scheduling
7
Of course…
• The programs are not small any more!
– Chrome browser: 17 millions LOC (lines of code)
– Office 2013: 45 millions LOC
– Facebook: 60 millions LOC
• You can’t just put all the lines sequentially and
expect someone to understand!
• Of course we needed extra structures to
handle the complexity
8
Down to actual examples
• Lets see some code in action
– DogV1.py
• Sequential
– DogV2.py
• Functional
• Exact level of functional details
– AreaV1.py
– AreaV2.py
– AreaV3.py
– AreaV4.py
9
Which language to choose?
• According to Google*:
– The fastest language is C++
– BUT
• If you don’t optimize your code, you may end up slower than other
languages
– See C++ Dbg vs other languages
10
*Loop Recognition in C++/Java/Go/Scala by Robert Hundt - Google
Why did I choose Python?
• Fast to develop a program
– Really fast, no chit-chat
• Lightweight IDE
• Dynamic typing
– I didn’t need to write the type of my variables
• Either it is integer, string or any other things
• Flexible
11
Parameters, parameters…
• DogV3.py
– More parameters mean more generalization of
that function
• AreaV5.py
12
How about storing some information?
• AreaV6.py
– Hiding some information inside of the function
• AreaV7.py
– More information hidden inside.
13
Next?
• If I can store some information inside a function,
– Why not there is a special construct for these kind
of scenarios?
• There is one already!
– Classes
14
Object-oriented Programming
• A.k.a.
– Object-oriented paradigm
– Object-oriented design
– Object-oriented methodology
15
Wouldn’t it be nice…
• If there are specialized Rectangle element that
– Knows how many sides are there.
– Knows how to calculate its own area.
• Whenever I need a Rectangle, I just use this
element only.
• Whenever I ask for its information, it answers itself.
16
Side = 4
Area = side*side
Side
Height
Base
Area =
(height*base)/2
height
base
Objects
• So everything we infer to have its own
properties, methods etc. should be an object
17
Side = 4
Area = side*side
Side
Height
Base
Area =
(height*base)/2
height
base
Objects
• Car, glass, bike, bus
– All objects
• Dog, cat, person, laptop
– Again objects
• Running, walking etc… ?
– Nop, these are not objects!
– These are actions (methods) that can be done by an object
• Like a Rectangle can compute its own area
• A people can run.
• Side, height, numberOfLegs etc… ?
– Nop
– These are properties (attributes) that can be owned by an object
• Like side of a Rectangle.
• Numberoflegs of a Person.
18
Everything can be an object!
• It just depends on the context!
• Distance between two cities can be an object
– If it is the main focus of the problem.
• Relationship between two people can be an
object
– If we are solving a problem about this.
19
Let’s identify objects!
• Company XYZ is a manufacturing company that
produces cartoon action figures for big
entertainment companies.
• This company is using an inventory and tracking
system.
• The inventory system keeps track of how many of
each figurine is stored in each warehouse.
• Figures are stored in cases.
• Clients order the figurines and the cases are
eventually shipped to clients.
20
Objects Identified
• Company XYZ is a manufacturing company that
produces cartoon action figurines for big
entertainment companies.
• This company is using an inventory and tracking
system.
• The inventory system keeps track of how many of
each figurine is stored in each warehouse.
• Figures are stored in cases.
• Clients order the figurines and the cases are
eventually shipped to clients.
21
Any more?
• Company XYZ is a manufacturing company that
produces cartoon action figurines for big
entertainment companies.
• This company is using an inventory and tracking
system.
• The inventory system keeps track of how many of
each figurine is stored in each warehouse.
• Figures are stored in cases.
• Clients order the figurines and the cases are
eventually shipped to clients.
22
Identify on another example!
• An ATM needs to allow a customer to identify themselves
– Each customer has a debit card and PIN
• Customers should be presented with some kind of menu to
help direct them.
• Customers can perform two actions:
– They should be able to deposit funds
– They should be able to withdraw funds upto $200
• These funds must be withdrawn in units of $20
• The ATM should tell some banking software to update the
customers’ account at the end of transaction
• The ATM should also give the customer some record of the
transaction.
23
Objects identified
• An ATM needs to allow a customer to identify themselves
– Each customer has a debit card and PIN
• Customers should be presented with some kind of menu to
help direct them.
• Customers can perform two transactions:
– They should be able to deposit funds
– They should be able to withdraw funds upto $200
• These funds must be withdrawn in units of $20
• The ATM should tell some banking software to update the
customers’ account at the end of transaction
• The ATM should also give the customer some record of the
transaction.
24
Something unnecessary here
• PIN can be a property of either the customer
of the debit card!
25
Object-oriented Languages
• Most modern language now supports object-
orientation
– Java
– C++ (C with Classes)
– C#
– Python
– And many more
26
UML Class Diagrams
• Now we know objects, we need a way to
design an object-oriented system
• This system should be independent from any
programming language.
• UML is here.
– Unified Modeling Language
27
Basic UML concepts
• Classes
– Objects
• Associations
– Relations between objects
• Attributes
– Properties of an object
28
Shape System
• Abstract class
– Abstract method
• Generalization
• Attributes
– Specific to some classes or general for all
• Methods
29
Shape System (Code)
• ShapeV1.py
– Implementation of Shape and Square
– Constructors
• ShapeV2.py
– Addition of Triangle to the system
30
Let’s draw our animal system
• Specific methods to some classes
– According to their abilities
31
Animal System (Code)
• AnimalV1.py
– Implementation with Animal, Dog and Cat
32
Let’s add some spice
• An animal has an owner
• Association
– And multiplicities
– Exact read is in arrow direction
• 0 or more animals can have 1 owner
– But vice-versa is also ok
• 1 person can have 0 or more dogs
• See the new method in Animal class
33
Animal System with Owner (Code)
• AnimalV2.py
– Implementation with Animal, Dog, Cat and Person
34
Class vs Object
• What is the relation between this Person and
Huseyin?
– Person is a class
– Huseyin is an object (conforming to Person class)
• Meaning Huseyin is a runtime element
• In UML there is a separate diagram for this
– Object diagram
35
Object Diagram
• Class Diagram
• Code
• Object Diagram
36
More spice for class diagram
• Compositions
• A room is composed of many chairs and many tables etc.
etc.
• Code is same as “association”
37
A little more spice
• Interfaces
– A blueprint of the implementing classes.
– So Dogg should implement these methods
• Eat and Travel
• Code is same as “abstract” classes.
• Animal.java
– Implementation of Animal, Dog and Cat
38
Fundamental Principles of
Object-oriented Design
• Encapsulation
• Inheritance
• Abstraction
• Polymorphism
• Cohesion
• Coupling
39
Encapsulation
• Information hiding.
– Hide unnecessary information from the public.
• EncapsulationV1.java
• Public and private methods!
40
Inheritance
• We already see in Animal or Shape system.
• Dog inherits methods/attributes of Animal.
41
Abstraction
• Working with something we know how to use but we don’t
know how it works internally.
• We know animals make sounds and we know how to call it,
but we don’t know internals.
– Internals defined later on Dog, Cat or Bird classes.
42
Polymorphism
• Having more than one form.
• We don’t know how to compute the areas of
each shape, but we know it will have different
forms in each of the subclasses.
43
Cohesion
• To what degree, a program’s various tasks and responsibilities are
related to one another.
• Strong cohesion means responsibilities are related to one another.
– For example Math class in Java:
• It does sin(), cos(), asin(), sqrt(), power(), exp()
• It has constants like PI, E etc.
– So this class performs only one task: math-related computation
• If a class has:
– Methods to print something,
– Sending an email,
– Working with trigonometric functions
– How should we name it? Complicated, right?
• Strong cohesion helps to build quality code.
44
Coupling
• The extent to which components/classes
depend on one another.
• CouplingV1.java
– Loose coupling
• CouplingV2.java
– Tight coupling
• Loose coupling helps to build quality code!
45
Advanced Object-oriented
Design Principles
• S.O.L.I.D.
– Single responsibility principle
– Open-closed principle
– Liskov substitution principle
– Interface segregation principle
– Dependency inversion principle
• Principles by Robert Martin
– A known software engineer and pioneer
46
Single responsibility principle
• A class should have one and only one reason to change.
– A class should have one job!
• SRPv1.java
– Problems:
• Area calculator is doing so many jobs
– Computing areas, printing in some format
– It should only sum the areas of the shapes, nothing more.
• SRPv2.java
– Solution to the printing problem
• The logic to print in some different style in handled in a new class
47
Open-closed principle
• Objects should be open to extension, but close
to modification
– Basically, the classes should be easily extendable
without modifying the inner structure.
• OCPv1.java
– Violation of the principle
• OCPv2.java
– Now we don’t need to modify sum method each
time we add a new shape.
48
Liskov substitution principle
• Every subclass should be substitutable for their
parent class.
– We should use Dog class instead of Animal class
whenever needed.
• LSPv1.java
– Added a new calculator by extending old one.
– We can input this one wherever old one is asked.
49
Interface segregation principle
• A client should never be forced to implement an
unnecessary method.
• ISPv1.java
– Forcing Square to implement volume method,
which it doesn’t have.
• ISPv2.java
– Segregating Shape interface into two to satisfy
principle.
50
Dependency inversion principle
• Entities must depend on abstractions/interfaces rather than actual
classes.
– So that they can be decoupled.
• Other definitions:
– High-level modules shouldn’t depend on low-level modules. Both should
depend on abstractions.
– Abstractions should not depend on details, details should depend on
abstractions.
• DIPv1.java
– Manager depends on Worker, and SuperWorker is just sitting there.
• DIPv2.java
– Now Manager can manage any guy that CanWork.
51
Principles are done
• Now let’s see how we can proceed.
52
Now the real deal
• How will learning object-orientation help me
write better codes?
• First of all, your code will be more readable
– Therefore, more maintainable
– As a result, more understandable by “you” later on!
– Especially, if you design your UML diagrams with
your code too.
• Second, is the design patterns.
53
Design Patterns
• People solved same problems for years
• And collected good solutions as a collection of
something called Design Patterns
• First and most famous software related design
patterns are in this book:
– Design Patterns: Elements of Reusable Object-
oriented Software
• Erich Gamma, Richard Helm, Ralph Johnson, John
Vlissides
54
A couple of them
• Let’s apply some of the design patterns in real
problems and see how it improves our
performance or solve our problems.
• We will see the structure in UML class diagram.
– And see how it works in code.
55
CHAIN OF RESPONSIBILITY
Design Patterns
56
Chain of Responsibility Design Pattern
• Scenario:
– Any people sends help emails to help.desk@ua.edu
– Any questions are welcome and it will be redirected to
correct department
– Intuitive solution:
• A central help manager that has associations to all
departments.
57
Any principles violated?
• What if I want to add more departments?
– Open-closed principle
• Other problems?
– Workload of the help manager?
58
Chain of Responsibility Structure
• Handlers connected with successor links.
• The request will be sent to first handler, then
follows successors.
59
New solution after design pattern
• Order the departments with respect to the most
workload to the least.
• A new department can be added easily to the
end.
60
Code example for
Chain of Responsibility
• CheckAuthority.java
– Purchase requests to be approved by:
• Manager (upto 5000)
• Director (upto 10000)
• Vice-president (upto 20000)
• President (upto 30000)
• Rest needs a board meeting
– It starts from manager and to upper level officials.
61
FLYWEIGHT
Design Patterns
62
Flyweight Design Pattern
• Scenario:
– Creating a forest of trees
– Lots and lots of them
– Intuitive solution
63
Any problems?
• What happens if I create millions?
– Lots of objects, probably some of the attributes are
same.
• For example color: Green will probably be 95% of the
object pool.
64
Solution: Flyweight Pattern
• Structure
• Extrinsic states are properties that can be store in client and
used as parameter
– Like position.
• Intrinsic states are stored in flyweight itself.
– Like type, color.
65
New solution after Flyweight
• Now we have less TreeModel.
– And the position is passed as parameter
66
New class diagram
• FlyweightV1.java
• FlyweightV2.java
• These are other circle drawing examples, but
see the time difference between two
implementations.
67
Conclusion
• This was the important parts of object-oriented
programming.
• Of course, there is a lot more when you dive
into different problems.
• Just take advantage of these principles and
structures.
• DON’T FORGET: If you don’t optimize, the
fastest language may be slowest.
68
Questions?
69
References:
• http://www.introprogramming.info/english-
intro-csharp-book/read-online/chapter-20-
object-oriented-programming-principles/
• https://scotch.io/bar-talk/s-o-l-i-d-the-first-
five-principles-of-object-oriented-design
• http://gameprogrammingpatterns.com/flyweig
ht.html
70

More Related Content

What's hot

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Concept of basic illumination model
Concept of basic illumination modelConcept of basic illumination model
Concept of basic illumination model
Ankit Garg
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 

What's hot (20)

Cohen sutherland line clipping
Cohen sutherland line clippingCohen sutherland line clipping
Cohen sutherland line clipping
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Array ppt
Array pptArray ppt
Array ppt
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Concept of basic illumination model
Concept of basic illumination modelConcept of basic illumination model
Concept of basic illumination model
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
class and objects
class and objectsclass and objects
class and objects
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Functions in C
Functions in CFunctions in C
Functions in C
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 

Viewers also liked

Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
daxesh chauhan
 
What Is A Paradigm Ppt
What Is A Paradigm PptWhat Is A Paradigm Ppt
What Is A Paradigm Ppt
Lee Howell
 

Viewers also liked (20)

Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Are You a SOLID Coder?
Are You a SOLID Coder?Are You a SOLID Coder?
Are You a SOLID Coder?
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
OOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOPOOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOP
 
Flyweight Pattern
Flyweight PatternFlyweight Pattern
Flyweight Pattern
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
OOP paradigm, principles of good design and architecture of Java applications
OOP paradigm, principles of good design and architecture of Java applicationsOOP paradigm, principles of good design and architecture of Java applications
OOP paradigm, principles of good design and architecture of Java applications
 
3-oop java-inheritance
3-oop java-inheritance3-oop java-inheritance
3-oop java-inheritance
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
 
OO Development 1 - Introduction to Object-Oriented Development
OO Development 1 - Introduction to Object-Oriented DevelopmentOO Development 1 - Introduction to Object-Oriented Development
OO Development 1 - Introduction to Object-Oriented Development
 
Object oriented programming concepts
Object oriented programming conceptsObject oriented programming concepts
Object oriented programming concepts
 
C++ classes
C++ classesC++ classes
C++ classes
 
SEMINAR
SEMINARSEMINAR
SEMINAR
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming Paradigms
 
What Is A Paradigm Ppt
What Is A Paradigm PptWhat Is A Paradigm Ppt
What Is A Paradigm Ppt
 
Object Oriented Design
Object Oriented DesignObject Oriented Design
Object Oriented Design
 
Object-Oriented Programming Concepts
Object-Oriented Programming ConceptsObject-Oriented Programming Concepts
Object-Oriented Programming Concepts
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 

Similar to Object Oriented Paradigm

Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014
Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014
Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014
The Hive
 

Similar to Object Oriented Paradigm (20)

Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Algorithms 1
Algorithms 1Algorithms 1
Algorithms 1
 
lecture_for programming and computing basics
lecture_for programming and computing basicslecture_for programming and computing basics
lecture_for programming and computing basics
 
Problem solving
Problem solvingProblem solving
Problem solving
 
CPP12 - Algorithms
CPP12 - AlgorithmsCPP12 - Algorithms
CPP12 - Algorithms
 
CS101- Introduction to Computing- Lecture 34
CS101- Introduction to Computing- Lecture 34CS101- Introduction to Computing- Lecture 34
CS101- Introduction to Computing- Lecture 34
 
Lecture 1 (bce-7)
Lecture   1 (bce-7)Lecture   1 (bce-7)
Lecture 1 (bce-7)
 
Unit no_1.pptx
Unit no_1.pptxUnit no_1.pptx
Unit no_1.pptx
 
CPP02 - The Structure of a Program
CPP02 - The Structure of a ProgramCPP02 - The Structure of a Program
CPP02 - The Structure of a Program
 
Knowledge base system appl. p 1,2-ver1
Knowledge base system appl.  p 1,2-ver1Knowledge base system appl.  p 1,2-ver1
Knowledge base system appl. p 1,2-ver1
 
Domain-Driven Design: The "What" and the "Why"
Domain-Driven Design: The "What" and the "Why"Domain-Driven Design: The "What" and the "Why"
Domain-Driven Design: The "What" and the "Why"
 
Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptx
 
Artificial Intelligence by B. Ravikumar
Artificial Intelligence by B. RavikumarArtificial Intelligence by B. Ravikumar
Artificial Intelligence by B. Ravikumar
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithms
 
Machine Learning Essentials Demystified part1 | Big Data Demystified
Machine Learning Essentials Demystified part1 | Big Data DemystifiedMachine Learning Essentials Demystified part1 | Big Data Demystified
Machine Learning Essentials Demystified part1 | Big Data Demystified
 
Deep learning introduction
Deep learning introductionDeep learning introduction
Deep learning introduction
 
Improving Pharo Snapshots
Improving Pharo SnapshotsImproving Pharo Snapshots
Improving Pharo Snapshots
 
Artificial Intelligence and The Complexity
Artificial Intelligence and The ComplexityArtificial Intelligence and The Complexity
Artificial Intelligence and The Complexity
 
Utah Code Camp 2014 - Learning from Data by Thomas Holloway
Utah Code Camp 2014 - Learning from Data by Thomas HollowayUtah Code Camp 2014 - Learning from Data by Thomas Holloway
Utah Code Camp 2014 - Learning from Data by Thomas Holloway
 
Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014
Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014
Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014
 

More from Hüseyin Ergin (6)

Is there life after code?
Is there life after code?Is there life after code?
Is there life after code?
 
SITE - CS Presentation
SITE - CS PresentationSITE - CS Presentation
SITE - CS Presentation
 
Software Versioning with Bitbucket and Eclipse
Software Versioning with Bitbucket and EclipseSoftware Versioning with Bitbucket and Eclipse
Software Versioning with Bitbucket and Eclipse
 
From the book: 50 Proven Ways to Be Persuasive
From the book: 50 Proven Ways to Be PersuasiveFrom the book: 50 Proven Ways to Be Persuasive
From the book: 50 Proven Ways to Be Persuasive
 
Proxy Pattern
Proxy PatternProxy Pattern
Proxy Pattern
 
Chain of Responsibility Pattern
Chain of Responsibility PatternChain of Responsibility Pattern
Chain of Responsibility Pattern
 

Recently uploaded

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Recently uploaded (20)

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 

Object Oriented Paradigm

  • 1. by Hüseyin Ergin Programming Concepts Focused on Object-oriented Programming
  • 2. Programming • Just writing some text in a specific format • There are thousands of programming languages! 2
  • 3. So, what is the trick here? • A compiler/interpreter does the actual job! • Read text file, interpret the contents, output another file more understandable by machines, less understandable by us  3
  • 4. Modern IDEs • Previous slide is not a modern day practice. – At least only computer engineers should do it. • Now we have IDEs (Integrated Development Environment) – Providing • Text editors • Compilers • And many other useful features 4
  • 6. What is a program? • Just some sequential lines to instruct the computer what to do! 6
  • 7. What is a programming language? • Provides the developer useful constructs – Defining variables – Looping – Conditionals – Many more… • And advanced constructs – Data management – Memory management – Networking – Scheduling 7
  • 8. Of course… • The programs are not small any more! – Chrome browser: 17 millions LOC (lines of code) – Office 2013: 45 millions LOC – Facebook: 60 millions LOC • You can’t just put all the lines sequentially and expect someone to understand! • Of course we needed extra structures to handle the complexity 8
  • 9. Down to actual examples • Lets see some code in action – DogV1.py • Sequential – DogV2.py • Functional • Exact level of functional details – AreaV1.py – AreaV2.py – AreaV3.py – AreaV4.py 9
  • 10. Which language to choose? • According to Google*: – The fastest language is C++ – BUT • If you don’t optimize your code, you may end up slower than other languages – See C++ Dbg vs other languages 10 *Loop Recognition in C++/Java/Go/Scala by Robert Hundt - Google
  • 11. Why did I choose Python? • Fast to develop a program – Really fast, no chit-chat • Lightweight IDE • Dynamic typing – I didn’t need to write the type of my variables • Either it is integer, string or any other things • Flexible 11
  • 12. Parameters, parameters… • DogV3.py – More parameters mean more generalization of that function • AreaV5.py 12
  • 13. How about storing some information? • AreaV6.py – Hiding some information inside of the function • AreaV7.py – More information hidden inside. 13
  • 14. Next? • If I can store some information inside a function, – Why not there is a special construct for these kind of scenarios? • There is one already! – Classes 14
  • 15. Object-oriented Programming • A.k.a. – Object-oriented paradigm – Object-oriented design – Object-oriented methodology 15
  • 16. Wouldn’t it be nice… • If there are specialized Rectangle element that – Knows how many sides are there. – Knows how to calculate its own area. • Whenever I need a Rectangle, I just use this element only. • Whenever I ask for its information, it answers itself. 16 Side = 4 Area = side*side Side Height Base Area = (height*base)/2 height base
  • 17. Objects • So everything we infer to have its own properties, methods etc. should be an object 17 Side = 4 Area = side*side Side Height Base Area = (height*base)/2 height base
  • 18. Objects • Car, glass, bike, bus – All objects • Dog, cat, person, laptop – Again objects • Running, walking etc… ? – Nop, these are not objects! – These are actions (methods) that can be done by an object • Like a Rectangle can compute its own area • A people can run. • Side, height, numberOfLegs etc… ? – Nop – These are properties (attributes) that can be owned by an object • Like side of a Rectangle. • Numberoflegs of a Person. 18
  • 19. Everything can be an object! • It just depends on the context! • Distance between two cities can be an object – If it is the main focus of the problem. • Relationship between two people can be an object – If we are solving a problem about this. 19
  • 20. Let’s identify objects! • Company XYZ is a manufacturing company that produces cartoon action figures for big entertainment companies. • This company is using an inventory and tracking system. • The inventory system keeps track of how many of each figurine is stored in each warehouse. • Figures are stored in cases. • Clients order the figurines and the cases are eventually shipped to clients. 20
  • 21. Objects Identified • Company XYZ is a manufacturing company that produces cartoon action figurines for big entertainment companies. • This company is using an inventory and tracking system. • The inventory system keeps track of how many of each figurine is stored in each warehouse. • Figures are stored in cases. • Clients order the figurines and the cases are eventually shipped to clients. 21
  • 22. Any more? • Company XYZ is a manufacturing company that produces cartoon action figurines for big entertainment companies. • This company is using an inventory and tracking system. • The inventory system keeps track of how many of each figurine is stored in each warehouse. • Figures are stored in cases. • Clients order the figurines and the cases are eventually shipped to clients. 22
  • 23. Identify on another example! • An ATM needs to allow a customer to identify themselves – Each customer has a debit card and PIN • Customers should be presented with some kind of menu to help direct them. • Customers can perform two actions: – They should be able to deposit funds – They should be able to withdraw funds upto $200 • These funds must be withdrawn in units of $20 • The ATM should tell some banking software to update the customers’ account at the end of transaction • The ATM should also give the customer some record of the transaction. 23
  • 24. Objects identified • An ATM needs to allow a customer to identify themselves – Each customer has a debit card and PIN • Customers should be presented with some kind of menu to help direct them. • Customers can perform two transactions: – They should be able to deposit funds – They should be able to withdraw funds upto $200 • These funds must be withdrawn in units of $20 • The ATM should tell some banking software to update the customers’ account at the end of transaction • The ATM should also give the customer some record of the transaction. 24
  • 25. Something unnecessary here • PIN can be a property of either the customer of the debit card! 25
  • 26. Object-oriented Languages • Most modern language now supports object- orientation – Java – C++ (C with Classes) – C# – Python – And many more 26
  • 27. UML Class Diagrams • Now we know objects, we need a way to design an object-oriented system • This system should be independent from any programming language. • UML is here. – Unified Modeling Language 27
  • 28. Basic UML concepts • Classes – Objects • Associations – Relations between objects • Attributes – Properties of an object 28
  • 29. Shape System • Abstract class – Abstract method • Generalization • Attributes – Specific to some classes or general for all • Methods 29
  • 30. Shape System (Code) • ShapeV1.py – Implementation of Shape and Square – Constructors • ShapeV2.py – Addition of Triangle to the system 30
  • 31. Let’s draw our animal system • Specific methods to some classes – According to their abilities 31
  • 32. Animal System (Code) • AnimalV1.py – Implementation with Animal, Dog and Cat 32
  • 33. Let’s add some spice • An animal has an owner • Association – And multiplicities – Exact read is in arrow direction • 0 or more animals can have 1 owner – But vice-versa is also ok • 1 person can have 0 or more dogs • See the new method in Animal class 33
  • 34. Animal System with Owner (Code) • AnimalV2.py – Implementation with Animal, Dog, Cat and Person 34
  • 35. Class vs Object • What is the relation between this Person and Huseyin? – Person is a class – Huseyin is an object (conforming to Person class) • Meaning Huseyin is a runtime element • In UML there is a separate diagram for this – Object diagram 35
  • 36. Object Diagram • Class Diagram • Code • Object Diagram 36
  • 37. More spice for class diagram • Compositions • A room is composed of many chairs and many tables etc. etc. • Code is same as “association” 37
  • 38. A little more spice • Interfaces – A blueprint of the implementing classes. – So Dogg should implement these methods • Eat and Travel • Code is same as “abstract” classes. • Animal.java – Implementation of Animal, Dog and Cat 38
  • 39. Fundamental Principles of Object-oriented Design • Encapsulation • Inheritance • Abstraction • Polymorphism • Cohesion • Coupling 39
  • 40. Encapsulation • Information hiding. – Hide unnecessary information from the public. • EncapsulationV1.java • Public and private methods! 40
  • 41. Inheritance • We already see in Animal or Shape system. • Dog inherits methods/attributes of Animal. 41
  • 42. Abstraction • Working with something we know how to use but we don’t know how it works internally. • We know animals make sounds and we know how to call it, but we don’t know internals. – Internals defined later on Dog, Cat or Bird classes. 42
  • 43. Polymorphism • Having more than one form. • We don’t know how to compute the areas of each shape, but we know it will have different forms in each of the subclasses. 43
  • 44. Cohesion • To what degree, a program’s various tasks and responsibilities are related to one another. • Strong cohesion means responsibilities are related to one another. – For example Math class in Java: • It does sin(), cos(), asin(), sqrt(), power(), exp() • It has constants like PI, E etc. – So this class performs only one task: math-related computation • If a class has: – Methods to print something, – Sending an email, – Working with trigonometric functions – How should we name it? Complicated, right? • Strong cohesion helps to build quality code. 44
  • 45. Coupling • The extent to which components/classes depend on one another. • CouplingV1.java – Loose coupling • CouplingV2.java – Tight coupling • Loose coupling helps to build quality code! 45
  • 46. Advanced Object-oriented Design Principles • S.O.L.I.D. – Single responsibility principle – Open-closed principle – Liskov substitution principle – Interface segregation principle – Dependency inversion principle • Principles by Robert Martin – A known software engineer and pioneer 46
  • 47. Single responsibility principle • A class should have one and only one reason to change. – A class should have one job! • SRPv1.java – Problems: • Area calculator is doing so many jobs – Computing areas, printing in some format – It should only sum the areas of the shapes, nothing more. • SRPv2.java – Solution to the printing problem • The logic to print in some different style in handled in a new class 47
  • 48. Open-closed principle • Objects should be open to extension, but close to modification – Basically, the classes should be easily extendable without modifying the inner structure. • OCPv1.java – Violation of the principle • OCPv2.java – Now we don’t need to modify sum method each time we add a new shape. 48
  • 49. Liskov substitution principle • Every subclass should be substitutable for their parent class. – We should use Dog class instead of Animal class whenever needed. • LSPv1.java – Added a new calculator by extending old one. – We can input this one wherever old one is asked. 49
  • 50. Interface segregation principle • A client should never be forced to implement an unnecessary method. • ISPv1.java – Forcing Square to implement volume method, which it doesn’t have. • ISPv2.java – Segregating Shape interface into two to satisfy principle. 50
  • 51. Dependency inversion principle • Entities must depend on abstractions/interfaces rather than actual classes. – So that they can be decoupled. • Other definitions: – High-level modules shouldn’t depend on low-level modules. Both should depend on abstractions. – Abstractions should not depend on details, details should depend on abstractions. • DIPv1.java – Manager depends on Worker, and SuperWorker is just sitting there. • DIPv2.java – Now Manager can manage any guy that CanWork. 51
  • 52. Principles are done • Now let’s see how we can proceed. 52
  • 53. Now the real deal • How will learning object-orientation help me write better codes? • First of all, your code will be more readable – Therefore, more maintainable – As a result, more understandable by “you” later on! – Especially, if you design your UML diagrams with your code too. • Second, is the design patterns. 53
  • 54. Design Patterns • People solved same problems for years • And collected good solutions as a collection of something called Design Patterns • First and most famous software related design patterns are in this book: – Design Patterns: Elements of Reusable Object- oriented Software • Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides 54
  • 55. A couple of them • Let’s apply some of the design patterns in real problems and see how it improves our performance or solve our problems. • We will see the structure in UML class diagram. – And see how it works in code. 55
  • 57. Chain of Responsibility Design Pattern • Scenario: – Any people sends help emails to help.desk@ua.edu – Any questions are welcome and it will be redirected to correct department – Intuitive solution: • A central help manager that has associations to all departments. 57
  • 58. Any principles violated? • What if I want to add more departments? – Open-closed principle • Other problems? – Workload of the help manager? 58
  • 59. Chain of Responsibility Structure • Handlers connected with successor links. • The request will be sent to first handler, then follows successors. 59
  • 60. New solution after design pattern • Order the departments with respect to the most workload to the least. • A new department can be added easily to the end. 60
  • 61. Code example for Chain of Responsibility • CheckAuthority.java – Purchase requests to be approved by: • Manager (upto 5000) • Director (upto 10000) • Vice-president (upto 20000) • President (upto 30000) • Rest needs a board meeting – It starts from manager and to upper level officials. 61
  • 63. Flyweight Design Pattern • Scenario: – Creating a forest of trees – Lots and lots of them – Intuitive solution 63
  • 64. Any problems? • What happens if I create millions? – Lots of objects, probably some of the attributes are same. • For example color: Green will probably be 95% of the object pool. 64
  • 65. Solution: Flyweight Pattern • Structure • Extrinsic states are properties that can be store in client and used as parameter – Like position. • Intrinsic states are stored in flyweight itself. – Like type, color. 65
  • 66. New solution after Flyweight • Now we have less TreeModel. – And the position is passed as parameter 66
  • 67. New class diagram • FlyweightV1.java • FlyweightV2.java • These are other circle drawing examples, but see the time difference between two implementations. 67
  • 68. Conclusion • This was the important parts of object-oriented programming. • Of course, there is a lot more when you dive into different problems. • Just take advantage of these principles and structures. • DON’T FORGET: If you don’t optimize, the fastest language may be slowest. 68