Introduction to Object Oriented Programming & Design Principles

Michael Redlich
Michael RedlichSystems Analyst, Polymer Physicist at Amateur Computer Group of New Jersey
1
Introduction to OOP
& Design Principles
Trenton Computer Festival
March 17, 2018
Michael P. Redlich
@mpredli
about.me/mpredli/
Who’s Mike?
• BS in CS from
• “Petrochemical Research Organization”
• Java Queue News Editor, InfoQ
• Ai-Logix, Inc. (now AudioCodes)
• Amateur Computer Group of New Jersey
2
Objectives (2)
• Object-Oriented Programming
• Object-Oriented Design Principles
• Live Demos (yea!)
3
Object-Oriented
Programming (OOP)
4
Some OOP Languages
• Ada
• C++
• Eiffel
• Java
• Modula-3
• Objective C
• OO-Cobol
• Python
• Simula
• Smalltalk
• Theta
5
What is OOP?
• A programming paradigm that is focused on
objects and data
• as opposed to actions and logic
• Objects are identified to model a system
• Objects are designed to interact with each
other
6
OOP Basics (1)
• Procedure-Oriented
• Top Down/Bottom Up
• Structured programming
• Centered around an
algorithm
• Identify tasks; how
something is done
• Object-Oriented
• Identify objects to be
modeled
• Concentrate on what an
object does
• Hide how an object
performs its task
• Identify behavior
7
OOP Basics (2)
• Abstract Data Type (ADT)
• user-defined data type
• use of objects through functions (methods)
without knowing the internal representation
8
OOP Basics (3)
• Interface
• functions (methods) provided in the ADT that
allow access to data
• Implementation
• underlying data structure(s) and business logic
within the ADT
9
OOP Basics (4)
• Class
• Defines a model
• Declares attributes
• Declares behavior
• Is an ADT
• Object
• Is an instance of a class
• Has state
• Has behavior
• May have many unique
objects of the same class
10
OOP Attributes
• Four (4) Main Attributes:
• data encapsulation
• data abstraction
• inheritance
• polymorphism
11
Data Encapsulation
• Separates the implementation from the
interface
• A public view of an object, but
implementation is private
• access to data is only allowed through a defined
interface
12
Data Abstraction
• A model of an entity
• Defines a data type by its functionality as
opposed to its implementation
13
Inheritance
• A means for defining a new class as an
extension of a previously defined class
• The derived class inherits all attributes and
behavior of the base class
• “IS-A” relationship
• Baseball is a Sport
14
Polymorphism
• The ability of different objects to respond
differently to the same function
• From the Greek meaning “many forms”
• A mechanism provided by an OOP
language as opposed to a programmer-
provided workaround
15
Advantages of OOP
• Interface can (and should) remain
unchanged when improving implementation
• Encourages modularity in application
development
• Better maintainability of code
• Code reuse
• Emphasis on what, not how
16
Classes (1)
• A user-defined abstract data type
• Extension of C structs
• Contain:
• constructor
• destructor
• data members and member functions (methods)
17
Classes (2)
• Static/Dynamic object instantiation
• Multiple Constructors:
• Sports(void);
• Sports(char *,int,int);
• Sports(float,char *,int);
18
Classes (3)
• Class scope (C++)
• scope resolution operator (::)
• Abstract Classes
• contain at least one pure virtual member
function (C++)
• contain at least one abstract method (Java)
19
Classes (3)
• Abstract Classes
• contain at least one pure virtual member
function (C++)
• contain at least one abstract method (Java)
20
Abstract Classes
• Pure virtual member function (C++)
• virtual void draw() = 0;
• Abstract method (Java)
• public abstract void draw();
21
Class Inheritance
22
Static Instantiation
(C++)
• Object creation:
• Baseball mets(“Mets”,97,65);
• Access to public member functions:
• mets.getWin(); // returns 97
23
Dynamic Instantiation
(C++)
• Object creation:
• Baseball *mets = new
Baseball(“Mets”,97,65);
• Access to public member functions:
• mets->getWin(); // returns 97
24
Dynamic Instantiation
(Java)
• Object creation:
• Baseball mets = new
Baseball(“Mets”,97,65);
• Access to public member functions:
• mets.getWin(); // returns 97
25
Deleting Objects (C++)
Baseball mets(“Mets”,97,65);
// object deleted when out of scope
Baseball *mets = new
Baseball(“Mets”,97,65);
delete mets; // required call
26
Deleting Objects (Java)
Baseball mets = new
Baseball(“Mets”,97,65);
// automatic garbage collection or:
System.gc(); // explicit call
27
Object-Oriented
Design Principles
28
What are OO Design
Principles?
• A set of underlying principles for creating
flexible designs that are easy to maintain
and adaptable to change
• Understanding the basics of OOP isn’t
enough
29
Some OO Design
Principles (1)
• Encapsulate WhatVaries
• Program to Interfaces, Not
Implementations
• Favor Composition Over Inheritance
• Classes Should Be Open for Extension, But
Closed for Modification
30
Some OO Design
Principles (2)
• Strive for Loosely Coupled Designs
Between Objects That Interact
• A Class Should Have Only One Reason to
Change
31
Encapsulate What
Varies
• Identify and encapsulate areas of code that
vary
• Encapsulated code can be altered without
affecting code that doesn’t vary
• Forms the basis for almost all of the
original Design Patterns
32
33
// OrderCars class
public class OrderCars {
public Car orderCar(String model) {
Car car;
if(model.equals(“Charger”))
car = new Dodge(model);
else if(model.equals(“Corvette”))
car = new Chevrolet(model);
else if(model.equals(“Mustang”))
car = new Ford(model);
car.buildCar();
car.testCar();
car.shipCar();
}
}
Demo Time…
34
Program to Interfaces,
Not Implementations
• Eliminates being locked-in to a specific
implementation
• An interface declares generic behavior
• Concrete class(es) implement methods
defined in an interface
35
36
// Dog class
public class Dog {
public void bark() {
System.out.println(“woof”);
}
// Cat class
public class Cat {
public void meow() {
System.out.println(“meow”);
}
}
37
// Animals class - main application
public class Animals {
public static void main(String[] args) {
Dog dog = new Dog();
dog.bark();
Cat cat = new Cat();
cat.meow();
}
}
// output
woof
meow
38
// Animal interface
public interface Animal {
public void makeNoise();
}
39
// Dog class (revised)
public class Dog implements Animal {
public void makeNoise() {
bark();
}
public void bark() {
System.out.println(“woof”);
}
// Cat class (revised)
public class Cat implements Animal {
public void makeNoise() {
meow();
}
public void meow() {
System.out.println(“meow”);
}
}
40
// Animals class - main application (revised)
public class Animals {
public static void main(String[] args) {
Animal dog = new Dog();
dog.makeNoise();
Animal cat = new Cat();
cat.makeNoise();
}
}
// output
woof
meow
Demo Time…
41
Favor Composition
Over Inheritance
• “HAS-A” can be better than “IS-A”
• Eliminates excessive use of subclassing
• An object’s behavior can be modified
through composition as opposed through
inheritance
• Allows change in object behavior at run-
time
42
Classes Should Be
Open for Extension...
• ...But Closed for Modification
• “Come in,We’re Open”
• extend the class to add new behavior
• “Sorry,We’re Closed”
• the code must remain closed to modification
43
A Simple Hierarchy...
44
...That Quickly
Becomes Complex!
45
Refactored Design
46
Demo Time…
47
Strive for Loosely
Coupled Designs...
• ...Between Objects That Interact
• Allows you to build flexible OO systems
that can handle change
• interdependency is minimized
• Changes to one object won’t affect another
object
• Objects can be used independently
48
Demo Time…
49
A Class Should Have...
• ...Only One Reason to Change
• Classes can inadvertently assume too many
responsibilities
• interdependency is minimized
• cross-cutting concerns
• Assign a responsibility to one class, and
only one class
50
Local C++ User
Groups
• ACGNJ C++ Users Group
• facilitated by Bruce Arnold
• acgnj.barnold.us
51
Local Java User Groups
(1)
• ACGNJ Java Users Group
• facilitated by Mike Redlich
• javasig.org
• Princeton Java Users Group
• facilitated byYakov Fain
• meetup.com/NJFlex
52
Local Java User Groups
(2)
• NYJavaSIG
• facilitated by Frank Greco
• javasig.com
• PhillyJUG
• facilitated by Martin Snyder, et. al.
• meetup.com/PhillyJUG
53
Local Java User Groups
(3)
• Capital District Java Developers Network
• facilitated by Dan Patsey
• cdjdn.com
• currently restructuring
54
Further Reading
55
56
Resources
•java.sun.com
•headfirstlabs.com
•themeteorbook.com
•eventedmind.com
•atmosphere.meteor.com
Upcoming Events
• ACGNJ Java Users Group
• Dr. Venkat Subramaniam
• Monday, March 19, 2018
• DorothyYoung Center for the Arts, Room 106
• Drew University
• 7:30-9:00pm
• “Twelve Ways to Make Code Suck Less”
57
58
Thanks!
mike@redlich.net
@mpredli
slideshare.net/mpredli01
github.com/mpredli01
https://www.surveymonkey.com/
r/B2BYTFH
Upcoming Events
• March 17-18, 2017
•tcf-nj.org
• April 18-19, 2017
•phillyemergingtech.com
59
1 of 59

Recommended

Introduction to Object Oriented Programming & Design Principles by
Introduction to Object Oriented Programming & Design PrinciplesIntroduction to Object Oriented Programming & Design Principles
Introduction to Object Oriented Programming & Design PrinciplesMichael Redlich
117 views59 slides
Getting Started with Java by
Getting Started with JavaGetting Started with Java
Getting Started with JavaMichael Redlich
237 views41 slides
The View object orientated programming in Lotuscript by
The View object orientated programming in LotuscriptThe View object orientated programming in Lotuscript
The View object orientated programming in LotuscriptBill Buchan
907 views63 slides
Introduction to java by
Introduction to javaIntroduction to java
Introduction to javaAbhishekMondal42
78 views15 slides
Intro to java programming by
Intro to java programmingIntro to java programming
Intro to java programmingLeah Stephens
223 views19 slides
Java by
Java Java
Java Prabhat gangwar
537 views50 slides

More Related Content

What's hot

Getting started with C++ by
Getting started with C++Getting started with C++
Getting started with C++Michael Redlich
78 views39 slides
Getting Started with C++ by
Getting Started with C++Getting Started with C++
Getting Started with C++Michael Redlich
249 views39 slides
Python by
PythonPython
PythonTrainme Softtech
132 views4 slides
Object oriented programming by
Object oriented programmingObject oriented programming
Object oriented programmingMH Abid
655 views26 slides
Abstract classes & interfaces by
Abstract classes & interfacesAbstract classes & interfaces
Abstract classes & interfacesmoazamali28
353 views16 slides
Introduction to Object-Oriented Programming & Design Principles (TCF 2014) by
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)Michael Redlich
2.4K views56 slides

What's hot(11)

Object oriented programming by MH Abid
Object oriented programmingObject oriented programming
Object oriented programming
MH Abid655 views
Abstract classes & interfaces by moazamali28
Abstract classes & interfacesAbstract classes & interfaces
Abstract classes & interfaces
moazamali28353 views
Introduction to Object-Oriented Programming & Design Principles (TCF 2014) by Michael Redlich
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Michael Redlich2.4K views
Miles Sabin Introduction To Scala For Java Developers by Skills Matter
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
Skills Matter1.4K views
Java 102 intro to object-oriented programming in java by agorolabs
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
agorolabs2.7K views
Statics in java | Constructors | Exceptions in Java | String in java| class 3 by Sagar Verma
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma355 views

Similar to Introduction to Object Oriented Programming & Design Principles

Preliminary committee presentation by
Preliminary committee presentationPreliminary committee presentation
Preliminary committee presentationRichard Drake
199 views29 slides
OOPs fundamentals session for freshers in my office (Aug 5, 13) by
OOPs fundamentals session for freshers in my office (Aug 5, 13)OOPs fundamentals session for freshers in my office (Aug 5, 13)
OOPs fundamentals session for freshers in my office (Aug 5, 13)Ashoka R K T
684 views17 slides
CISSP Prep: Ch 9. Software Development Security by
CISSP Prep: Ch 9. Software Development SecurityCISSP Prep: Ch 9. Software Development Security
CISSP Prep: Ch 9. Software Development SecuritySam Bowne
2.8K views82 slides
8. Software Development Security by
8. Software Development Security8. Software Development Security
8. Software Development SecuritySam Bowne
2.1K views85 slides
2009 training - tim m - object oriented programming by
2009   training - tim m - object oriented programming2009   training - tim m - object oriented programming
2009 training - tim m - object oriented programmingTim Mahy
405 views34 slides
Clean code presentation by
Clean code presentationClean code presentation
Clean code presentationBhavin Gandhi
496 views61 slides

Similar to Introduction to Object Oriented Programming & Design Principles(20)

Preliminary committee presentation by Richard Drake
Preliminary committee presentationPreliminary committee presentation
Preliminary committee presentation
Richard Drake199 views
OOPs fundamentals session for freshers in my office (Aug 5, 13) by Ashoka R K T
OOPs fundamentals session for freshers in my office (Aug 5, 13)OOPs fundamentals session for freshers in my office (Aug 5, 13)
OOPs fundamentals session for freshers in my office (Aug 5, 13)
Ashoka R K T684 views
CISSP Prep: Ch 9. Software Development Security by Sam Bowne
CISSP Prep: Ch 9. Software Development SecurityCISSP Prep: Ch 9. Software Development Security
CISSP Prep: Ch 9. Software Development Security
Sam Bowne2.8K views
8. Software Development Security by Sam Bowne
8. Software Development Security8. Software Development Security
8. Software Development Security
Sam Bowne2.1K views
2009 training - tim m - object oriented programming by Tim Mahy
2009   training - tim m - object oriented programming2009   training - tim m - object oriented programming
2009 training - tim m - object oriented programming
Tim Mahy405 views
Improving Software Quality Using Object Oriented Design Principles by Dr. Syed Hassan Amin
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design Principles
8. Software Development Security by Sam Bowne
8. Software Development Security8. Software Development Security
8. Software Development Security
Sam Bowne24 views
Objective-C for iOS Application Development by Dhaval Kaneria
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria3.1K views
C++ in object oriented programming by Saket Khopkar
C++ in object oriented programmingC++ in object oriented programming
C++ in object oriented programming
Saket Khopkar101 views
Software enginering.group-no-11 (1) by riarana10
Software enginering.group-no-11 (1)Software enginering.group-no-11 (1)
Software enginering.group-no-11 (1)
riarana1022 views
Objected-Oriented Programming with Java by Oum Saokosal
Objected-Oriented Programming with JavaObjected-Oriented Programming with Java
Objected-Oriented Programming with Java
Oum Saokosal560 views
Solid Principles Of Design (Design Series 01) by Heartin Jacob
Solid Principles Of Design (Design Series 01)Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)
Heartin Jacob638 views
"SOLID" Object Oriented Design Principles by Serhiy Oplakanets
"SOLID" Object Oriented Design Principles"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles
Serhiy Oplakanets5.6K views
Orthogonality: A Strategy for Reusable Code by rsebbe
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Code
rsebbe1.6K views
Object Oriented Programming in Swift Ch0 - Encapsulation by Chihyang Li
Object Oriented Programming in Swift Ch0 - EncapsulationObject Oriented Programming in Swift Ch0 - Encapsulation
Object Oriented Programming in Swift Ch0 - Encapsulation
Chihyang Li568 views
Marco Mancuso - Data Context Interaction by cosenzaLab
Marco Mancuso - Data Context InteractionMarco Mancuso - Data Context Interaction
Marco Mancuso - Data Context Interaction
cosenzaLab1.1K views

More from Michael Redlich

Getting Started with GitHub by
Getting Started with GitHubGetting Started with GitHub
Getting Started with GitHubMichael Redlich
153 views30 slides
Building Microservices with Micronaut: A Full-Stack JVM-Based Framework by
Building Microservices with Micronaut:  A Full-Stack JVM-Based FrameworkBuilding Microservices with Micronaut:  A Full-Stack JVM-Based Framework
Building Microservices with Micronaut: A Full-Stack JVM-Based FrameworkMichael Redlich
416 views55 slides
Building Microservices with Helidon: Oracle's New Java Microservices Framework by
Building Microservices with Helidon:  Oracle's New Java Microservices FrameworkBuilding Microservices with Helidon:  Oracle's New Java Microservices Framework
Building Microservices with Helidon: Oracle's New Java Microservices FrameworkMichael Redlich
274 views58 slides
C++ Advanced Features by
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced FeaturesMichael Redlich
280 views62 slides
Java Advanced Features by
Java Advanced FeaturesJava Advanced Features
Java Advanced FeaturesMichael Redlich
250 views47 slides
C++ Advanced Features by
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced FeaturesMichael Redlich
96 views62 slides

More from Michael Redlich(16)

Building Microservices with Micronaut: A Full-Stack JVM-Based Framework by Michael Redlich
Building Microservices with Micronaut:  A Full-Stack JVM-Based FrameworkBuilding Microservices with Micronaut:  A Full-Stack JVM-Based Framework
Building Microservices with Micronaut: A Full-Stack JVM-Based Framework
Michael Redlich416 views
Building Microservices with Helidon: Oracle's New Java Microservices Framework by Michael Redlich
Building Microservices with Helidon:  Oracle's New Java Microservices FrameworkBuilding Microservices with Helidon:  Oracle's New Java Microservices Framework
Building Microservices with Helidon: Oracle's New Java Microservices Framework
Michael Redlich274 views
Building Realtime Access to Data Apps with jOOQ by Michael Redlich
Building Realtime Access to Data Apps with jOOQBuilding Realtime Access to Data Apps with jOOQ
Building Realtime Access to Data Apps with jOOQ
Michael Redlich225 views
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017) by Michael Redlich
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)
Michael Redlich281 views
Building Realtime Web Apps with Angular and Meteor by Michael Redlich
Building Realtime Web Apps with Angular and MeteorBuilding Realtime Web Apps with Angular and Meteor
Building Realtime Web Apps with Angular and Meteor
Michael Redlich319 views
Java Advanced Features (TCF 2014) by Michael Redlich
Java Advanced Features (TCF 2014)Java Advanced Features (TCF 2014)
Java Advanced Features (TCF 2014)
Michael Redlich754 views
Getting Started with Meteor (TCF ITPC 2014) by Michael Redlich
Getting Started with Meteor (TCF ITPC 2014)Getting Started with Meteor (TCF ITPC 2014)
Getting Started with Meteor (TCF ITPC 2014)
Michael Redlich902 views
Getting Started with C++ (TCF 2014) by Michael Redlich
Getting Started with C++ (TCF 2014)Getting Started with C++ (TCF 2014)
Getting Started with C++ (TCF 2014)
Michael Redlich1.3K views
C++ Advanced Features (TCF 2014) by Michael Redlich
C++ Advanced Features (TCF 2014)C++ Advanced Features (TCF 2014)
C++ Advanced Features (TCF 2014)
Michael Redlich1.4K views
Getting Started with MongoDB (TCF ITPC 2014) by Michael Redlich
Getting Started with MongoDB (TCF ITPC 2014)Getting Started with MongoDB (TCF ITPC 2014)
Getting Started with MongoDB (TCF ITPC 2014)
Michael Redlich2.2K views

Recently uploaded

Mobile App Development Company by
Mobile App Development CompanyMobile App Development Company
Mobile App Development CompanyRichestsoft
5 views6 slides
Playwright Retries by
Playwright RetriesPlaywright Retries
Playwright Retriesartembondar5
7 views1 slide
Introduction to Maven by
Introduction to MavenIntroduction to Maven
Introduction to MavenJohn Valentino
7 views10 slides
Advanced API Mocking Techniques Using Wiremock by
Advanced API Mocking Techniques Using WiremockAdvanced API Mocking Techniques Using Wiremock
Advanced API Mocking Techniques Using WiremockDimpy Adhikary
5 views11 slides
JioEngage_Presentation.pptx by
JioEngage_Presentation.pptxJioEngage_Presentation.pptx
JioEngage_Presentation.pptxadmin125455
9 views4 slides
How to build dyanmic dashboards and ensure they always work by
How to build dyanmic dashboards and ensure they always workHow to build dyanmic dashboards and ensure they always work
How to build dyanmic dashboards and ensure they always workWiiisdom
16 views13 slides

Recently uploaded(20)

Mobile App Development Company by Richestsoft
Mobile App Development CompanyMobile App Development Company
Mobile App Development Company
Richestsoft 5 views
Advanced API Mocking Techniques Using Wiremock by Dimpy Adhikary
Advanced API Mocking Techniques Using WiremockAdvanced API Mocking Techniques Using Wiremock
Advanced API Mocking Techniques Using Wiremock
Dimpy Adhikary5 views
JioEngage_Presentation.pptx by admin125455
JioEngage_Presentation.pptxJioEngage_Presentation.pptx
JioEngage_Presentation.pptx
admin1254559 views
How to build dyanmic dashboards and ensure they always work by Wiiisdom
How to build dyanmic dashboards and ensure they always workHow to build dyanmic dashboards and ensure they always work
How to build dyanmic dashboards and ensure they always work
Wiiisdom16 views
Bootstrapping vs Venture Capital.pptx by Zeljko Svedic
Bootstrapping vs Venture Capital.pptxBootstrapping vs Venture Capital.pptx
Bootstrapping vs Venture Capital.pptx
Zeljko Svedic16 views
FOSSLight Community Day 2023-11-30 by Shane Coughlan
FOSSLight Community Day 2023-11-30FOSSLight Community Day 2023-11-30
FOSSLight Community Day 2023-11-30
Shane Coughlan8 views
Supercharging your Python Development Environment with VS Code and Dev Contai... by Dawn Wages
Supercharging your Python Development Environment with VS Code and Dev Contai...Supercharging your Python Development Environment with VS Code and Dev Contai...
Supercharging your Python Development Environment with VS Code and Dev Contai...
Dawn Wages5 views
Top-5-production-devconMunich-2023-v2.pptx by Tier1 app
Top-5-production-devconMunich-2023-v2.pptxTop-5-production-devconMunich-2023-v2.pptx
Top-5-production-devconMunich-2023-v2.pptx
Tier1 app9 views
How Workforce Management Software Empowers SMEs | TraQSuite by TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuiteHow Workforce Management Software Empowers SMEs | TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuite
TraQSuite7 views
aATP - New Correlation Confirmation Feature.pptx by EsatEsenek1
aATP - New Correlation Confirmation Feature.pptxaATP - New Correlation Confirmation Feature.pptx
aATP - New Correlation Confirmation Feature.pptx
EsatEsenek1222 views
Streamlining Your Business Operations with Enterprise Application Integration... by Flexsin
Streamlining Your Business Operations with Enterprise Application Integration...Streamlining Your Business Operations with Enterprise Application Integration...
Streamlining Your Business Operations with Enterprise Application Integration...
Flexsin 5 views

Introduction to Object Oriented Programming & Design Principles

  • 1. 1 Introduction to OOP & Design Principles Trenton Computer Festival March 17, 2018 Michael P. Redlich @mpredli about.me/mpredli/
  • 2. Who’s Mike? • BS in CS from • “Petrochemical Research Organization” • Java Queue News Editor, InfoQ • Ai-Logix, Inc. (now AudioCodes) • Amateur Computer Group of New Jersey 2
  • 3. Objectives (2) • Object-Oriented Programming • Object-Oriented Design Principles • Live Demos (yea!) 3
  • 5. Some OOP Languages • Ada • C++ • Eiffel • Java • Modula-3 • Objective C • OO-Cobol • Python • Simula • Smalltalk • Theta 5
  • 6. What is OOP? • A programming paradigm that is focused on objects and data • as opposed to actions and logic • Objects are identified to model a system • Objects are designed to interact with each other 6
  • 7. OOP Basics (1) • Procedure-Oriented • Top Down/Bottom Up • Structured programming • Centered around an algorithm • Identify tasks; how something is done • Object-Oriented • Identify objects to be modeled • Concentrate on what an object does • Hide how an object performs its task • Identify behavior 7
  • 8. OOP Basics (2) • Abstract Data Type (ADT) • user-defined data type • use of objects through functions (methods) without knowing the internal representation 8
  • 9. OOP Basics (3) • Interface • functions (methods) provided in the ADT that allow access to data • Implementation • underlying data structure(s) and business logic within the ADT 9
  • 10. OOP Basics (4) • Class • Defines a model • Declares attributes • Declares behavior • Is an ADT • Object • Is an instance of a class • Has state • Has behavior • May have many unique objects of the same class 10
  • 11. OOP Attributes • Four (4) Main Attributes: • data encapsulation • data abstraction • inheritance • polymorphism 11
  • 12. Data Encapsulation • Separates the implementation from the interface • A public view of an object, but implementation is private • access to data is only allowed through a defined interface 12
  • 13. Data Abstraction • A model of an entity • Defines a data type by its functionality as opposed to its implementation 13
  • 14. Inheritance • A means for defining a new class as an extension of a previously defined class • The derived class inherits all attributes and behavior of the base class • “IS-A” relationship • Baseball is a Sport 14
  • 15. Polymorphism • The ability of different objects to respond differently to the same function • From the Greek meaning “many forms” • A mechanism provided by an OOP language as opposed to a programmer- provided workaround 15
  • 16. Advantages of OOP • Interface can (and should) remain unchanged when improving implementation • Encourages modularity in application development • Better maintainability of code • Code reuse • Emphasis on what, not how 16
  • 17. Classes (1) • A user-defined abstract data type • Extension of C structs • Contain: • constructor • destructor • data members and member functions (methods) 17
  • 18. Classes (2) • Static/Dynamic object instantiation • Multiple Constructors: • Sports(void); • Sports(char *,int,int); • Sports(float,char *,int); 18
  • 19. Classes (3) • Class scope (C++) • scope resolution operator (::) • Abstract Classes • contain at least one pure virtual member function (C++) • contain at least one abstract method (Java) 19
  • 20. Classes (3) • Abstract Classes • contain at least one pure virtual member function (C++) • contain at least one abstract method (Java) 20
  • 21. Abstract Classes • Pure virtual member function (C++) • virtual void draw() = 0; • Abstract method (Java) • public abstract void draw(); 21
  • 23. Static Instantiation (C++) • Object creation: • Baseball mets(“Mets”,97,65); • Access to public member functions: • mets.getWin(); // returns 97 23
  • 24. Dynamic Instantiation (C++) • Object creation: • Baseball *mets = new Baseball(“Mets”,97,65); • Access to public member functions: • mets->getWin(); // returns 97 24
  • 25. Dynamic Instantiation (Java) • Object creation: • Baseball mets = new Baseball(“Mets”,97,65); • Access to public member functions: • mets.getWin(); // returns 97 25
  • 26. Deleting Objects (C++) Baseball mets(“Mets”,97,65); // object deleted when out of scope Baseball *mets = new Baseball(“Mets”,97,65); delete mets; // required call 26
  • 27. Deleting Objects (Java) Baseball mets = new Baseball(“Mets”,97,65); // automatic garbage collection or: System.gc(); // explicit call 27
  • 29. What are OO Design Principles? • A set of underlying principles for creating flexible designs that are easy to maintain and adaptable to change • Understanding the basics of OOP isn’t enough 29
  • 30. Some OO Design Principles (1) • Encapsulate WhatVaries • Program to Interfaces, Not Implementations • Favor Composition Over Inheritance • Classes Should Be Open for Extension, But Closed for Modification 30
  • 31. Some OO Design Principles (2) • Strive for Loosely Coupled Designs Between Objects That Interact • A Class Should Have Only One Reason to Change 31
  • 32. Encapsulate What Varies • Identify and encapsulate areas of code that vary • Encapsulated code can be altered without affecting code that doesn’t vary • Forms the basis for almost all of the original Design Patterns 32
  • 33. 33 // OrderCars class public class OrderCars { public Car orderCar(String model) { Car car; if(model.equals(“Charger”)) car = new Dodge(model); else if(model.equals(“Corvette”)) car = new Chevrolet(model); else if(model.equals(“Mustang”)) car = new Ford(model); car.buildCar(); car.testCar(); car.shipCar(); } }
  • 35. Program to Interfaces, Not Implementations • Eliminates being locked-in to a specific implementation • An interface declares generic behavior • Concrete class(es) implement methods defined in an interface 35
  • 36. 36 // Dog class public class Dog { public void bark() { System.out.println(“woof”); } // Cat class public class Cat { public void meow() { System.out.println(“meow”); } }
  • 37. 37 // Animals class - main application public class Animals { public static void main(String[] args) { Dog dog = new Dog(); dog.bark(); Cat cat = new Cat(); cat.meow(); } } // output woof meow
  • 38. 38 // Animal interface public interface Animal { public void makeNoise(); }
  • 39. 39 // Dog class (revised) public class Dog implements Animal { public void makeNoise() { bark(); } public void bark() { System.out.println(“woof”); } // Cat class (revised) public class Cat implements Animal { public void makeNoise() { meow(); } public void meow() { System.out.println(“meow”); } }
  • 40. 40 // Animals class - main application (revised) public class Animals { public static void main(String[] args) { Animal dog = new Dog(); dog.makeNoise(); Animal cat = new Cat(); cat.makeNoise(); } } // output woof meow
  • 42. Favor Composition Over Inheritance • “HAS-A” can be better than “IS-A” • Eliminates excessive use of subclassing • An object’s behavior can be modified through composition as opposed through inheritance • Allows change in object behavior at run- time 42
  • 43. Classes Should Be Open for Extension... • ...But Closed for Modification • “Come in,We’re Open” • extend the class to add new behavior • “Sorry,We’re Closed” • the code must remain closed to modification 43
  • 48. Strive for Loosely Coupled Designs... • ...Between Objects That Interact • Allows you to build flexible OO systems that can handle change • interdependency is minimized • Changes to one object won’t affect another object • Objects can be used independently 48
  • 50. A Class Should Have... • ...Only One Reason to Change • Classes can inadvertently assume too many responsibilities • interdependency is minimized • cross-cutting concerns • Assign a responsibility to one class, and only one class 50
  • 51. Local C++ User Groups • ACGNJ C++ Users Group • facilitated by Bruce Arnold • acgnj.barnold.us 51
  • 52. Local Java User Groups (1) • ACGNJ Java Users Group • facilitated by Mike Redlich • javasig.org • Princeton Java Users Group • facilitated byYakov Fain • meetup.com/NJFlex 52
  • 53. Local Java User Groups (2) • NYJavaSIG • facilitated by Frank Greco • javasig.com • PhillyJUG • facilitated by Martin Snyder, et. al. • meetup.com/PhillyJUG 53
  • 54. Local Java User Groups (3) • Capital District Java Developers Network • facilitated by Dan Patsey • cdjdn.com • currently restructuring 54
  • 57. Upcoming Events • ACGNJ Java Users Group • Dr. Venkat Subramaniam • Monday, March 19, 2018 • DorothyYoung Center for the Arts, Room 106 • Drew University • 7:30-9:00pm • “Twelve Ways to Make Code Suck Less” 57
  • 59. Upcoming Events • March 17-18, 2017 •tcf-nj.org • April 18-19, 2017 •phillyemergingtech.com 59