SlideShare a Scribd company logo
1 of 68
Download to read offline
Coding
the Happy Meal
A little bit about procedural code
Organization wants an app that will collect all the members and send
them an email. But it’s a bit more complicated. The emails should be
different for managers and minions. Also managers should be able to
opt out of emails and not get them, minions, no so much.
Once the emails are sent, we want to have all the recipients and their
roles in the organization also collected and another email with those
lists sent to each of the executives at the organization
After that we want to…
A little bit about procedural code
1. Query database for list of organization members
2. Loop through members
3. Check if member’s settings allow for emails to be sent (but only for managers)
4. If user is a manager and emails are allowed
• Query database and get email message for managers
If user is a minion
• Query database and get email message for minions
5. Get member’s name and, if there is a preferred name, use it otherwise use first name
6. Construct email
7. Connect to SMTP server and send email
8. Add member’s email address to a list of Recipients
9. Query the database to update member’s record that email was sent to them
10. Query the database of all related executives
11. Loop through them
12. Send them an email with a list of all recipients
13. …
A little bit about procedural code
• Task based
• Sequential
• Many decision trees
• If/Else statements
• Repetitive
• In the bigger picture, VERY repetitive.
• Querying the database for the same information in multiple places
• Making the same decisions in multiple places. Like using preferred name
instead of first.
• Due to the specificity of the tasks being accomplished, the more
complex the system becomes, the more difficult to change it.
How is OOP different?
• Model the world you are interacting with
• Member
• Manager
• Minion
• Executive
• Email
• Settings
• Email Service
• Build behavior into your objects outside of a specific set of tasks:
member.preferred_name() #return preferred,first if not available
member.get_message() #return role specific message
email.send(message) #sends email via SMTP server
What is an Object
Super simple explanation
An object is a data construct that has attributes and behaviors.
Attributes are also known as properties. A car might be an object, it
may have attributes of color (green) and top speed (125)
Behaviors are known as methods or functions. An object may have
many methods. A car may have methods “stop” and “go”
Python Object
class Soda:
def __init__(self, syrup, water):
self.syrup = syrup
self.water = water
self._calories = 100
def calories(self):
return self._calories
def refill(self):
# do something
pass
Object Domain
You are presented with a set of features or functions your program
needs to have.
• What are the real world objects that are involved?
• What are their properties?
• What are their behaviors?
• How do they relate to each other?
A good Object Domain will
• allow you to ask it questions and get back answers
• allow you to extend it, building in new features
• add new objects without affecting the current ones
• allow you to fix broken parts without affecting the overall system
Four Core Principles
Four Core Principles
• Encapsulation
Four Core Principles
• Encapsulation
• Inheritance
Four Core Principles
• Encapsulation
• Inheritance
• Composition
Four Core Principles
• Encapsulation
• Inheritance
• Composition
• Polymorphism
Encapsulation
• All data associated with an object is “private”
• Properties/Attributes are private
• Methods to access properties are public
• Methods can also be private
• You have to “ask” the object to get or change a property
• Core methods are called:
• Accessors
• Getters and Setters
• Mutators
Inheritance
• Objects can inherit properties and behaviors from other objects
• Sub Classes
• Super Classes
• Sub classes “inherit” properties and behaviors from its super class
• Sub classes “overwrite” properties and methods that are specific to it
Inheritance
• Super Class: Car
• Property: Top speed
• Method: go(‘fast’)
• Sub Class 1 : Clunker
• “Type” of Car
• Top Speed property is set to 25
• It inherits a “go” method, but can only go 25mph
• Sub Class 2: Sports Car
• “Type” of Car
• Top Speed property is set to 125
• It inherits a “go” method, but it can go 125mph
Inheritance
“Is A”
• A clunker “is a” car
• A dolphin “is a” mammal
• A mammal “is a” animal
• A dolphin “is a” animal
• A Sprite “is a” drink
Composition
• Single Objects are composed of multiple other objects
• Is generally preferred over inheritance
• Allows for greater flexibility as systems become more complex
• Does not prevent inheritance
• Often, the nature of a composed object is determined more by the
objects it is composed of rather than it’s own properties or attributes
Composition
“Has a”
• A car “has a” engine
• A car “has” 4 wheels
• A human “has” opposable thumbs
• A happy meal “has a” drink
• A happy meal “has a” hamburger
• A happy meal “has a” small fries
Composition vs. Inheritance
Inheritance Example
Object: Car
• Sports Car is a car
• Clunker is a car
• Sports Car/Clunker “extends” the car super class
• The sub classes overwrite the top speed property
Composition vs. Inheritance
Composition Example
Object: Car
• Has a transmission
• Has four wheels
• Has an engine
• Engine object has a top speed property
• Unlike inheritance, top speed is not determined by the car’s top
speed attribute but by the type of engine it is composed with.
Polymorphism
• Two or more objects of different classes respond to the same method
• Example: The Cut() Method
• Doctors Cut
• Hair Stylists Cut
• Directors Cut
• Insults Cut
• In each case, the object “cuts”, but the action is different
Polymorphism
for n in my_collection_of_things:
print(n)
• What are you looping through?
• Lists
• Arrays
• Numpy Arrays
• Panda Series
• etc…
The Happy Meal
Three Simple Items
Three Simple Items
Is it really that simple?
Three Simple Items
Three Simple Items
Three Simple Items
Three Simple Items
Three Simple Items
Three Simple Items
Composition
Not so simple after all…
/ (╯°□°)╯︵ ┻━┻
Composition
Composition allows for flexibility. The variety of different
combinations of a “Happy Meal” object can be left to the objects that
actually compose the meal.
Outside of the variety, a happy meal IS actually pretty simple!
• A Happy Meal has a drink
• A Happy Meal has a main course
• A Happy Meal has a side dish
Composition
Composition allows for flexibility. The variety of different
combinations of a “Happy Meal” object can be left to the objects that
actually compose the meal.
Outside of the variety, a happy meal IS actually pretty simple!
• A Happy Meal has a drink
• A Happy Meal has a main course
• A Happy Meal has a side dish
• A Happy Meal has a toy
Encapsulation
How many calories are in this happy meal?
Three Simple Items
Let’s break this down into an Object Domain using all 4
principles of object oriented programming…
Inheritance: The Soft Drink
Inheritance: The Soft Drink
If many possible objects share the same characteristic or properties,
then inheritance will allow you to consolidate all the shared
characteristics in a “Super Class”.
A generic soft drink has several possible properties:
1. Carbonated Water
2. Syrup
3. Ice
4. Syrup to carbonated water ratio
Syrup is what differentiates all soft drinks.
The Soft Drink: Coke
A Coke inherits from “Soft
Drink” and overwrites syrup
property with “coke”
The Soft Drink: Coke
A Dr. Pepper inherits from
“Soft Drink” and overwrites
syrup property with “dr.
pepper”
The Soft Drink: Coke
A Sprite Zero inherits from
“Soft Drink” and overwrites
syrup property with “sprite
zero”
Inheritance: The Soft Drink
With all three of these soft drinks. The only difference is the syrup
used.
Inheritance: Profit
McDonald’s want to maximize profits by saving on the now
outrageously priced syrup used in their soda fountains. It would be
pretty tedious to go and alter every single different type of soda…
Inheritance: Profit
Remember these 4 properties?
1. Carbonated Water
2. Syrup
3. Ice
4. Syrup to carbonated water ratio
Inheritance: Profit
Remember these 4 properties?
1. Carbonated Water
2. Syrup
3. Ice
4. Syrup to carbonated water ratio
Inheritance: Profit
Remember these 4 properties?
1. Carbonated Water
2. Syrup
3. Ice
4. Syrup to carbonated water ratio
Update all syrup to water ratios for ALL soft
drinks by updating the super class!
The Hamburger
The Hamburger
The Hamburger
A hamburger is composed of:
• Meat
• Bun / Bread
• A variety of condiments
• Could have no condiments
• Could have a ton of condiments
Side Dishes?
At this point. Fries are just fries. And
apples are just apples. Just two different
objects that neither inherit from another
object or are composed of multiple
objects.
The Happy Meal
This little happy meal has become pretty
complex!!!
Happy meal is composed of:
1 Drink that inherits from “Soft Drink”. The drink
will swap out the syrup to make it the correct type.
1 Main Meal that could be chicken nuggets OR a
hamburger. If a hamburger, then the main meal is
actually an object that is composed of several
other objects. We don’t actually know how many.
1 Side Dish that is just a plain object. No
inheritance or composition. Just a boring object.
The Happy Meal
But how many calories are in this happy
meal?
Encapsulation!
Wouldn’t it be nice if we could just ask the happy meal how many calories
it is?
Sounds like a difficult question to answer!
Wouldn’t it be nice if we could ask a soft drink how many calories it is?
Well that sounds easier. It’s just the syrup.
Wouldn’t it be nice to ask a hamburger how many calories it is and have the
answer be immediately adjusted based on the number and types of
condiments that were added?
Good grief…
Encapsulation: Soft Drinks
A Soft Drink object can respond to a method asking
how many calories it has. The object will look up
it’s “diet” Boolean flag (true or false) and return
either 120 or 0, if diet.
• Nothing but the Soft Drink object needs to know
how this is determined.
• At a later date, a more exact calculation based
on syrup and “syrup to water” ratios can be
implemented.
• Any sub type of Soft Drink (sprite, coke, fanta)
can respond to this method because it has been
inherited.
Encapsulation: Side Dishes
Both Fries and Apples are independent
objects that simple have a calories
property and a Getter method
implemented. In both of these cases, the
number of calories is a static value that is
returned
Encapsulation: The Hamburger
Encapsulation: Condiments
Condiments has become a new object used in
constructing a happy meal.
Condiments are very simple objects that can also
respond to a calories method.
Encapsulation: Hamburger
Hamburger also responds to calories. It determines
total calories by calculating 150 for the bun and
200 for the all beef patty. Then, on top of the 350
calories, it will ask each and every condiment how
many calories it is and then add the responding
numbers
Encapsulation: Hamburger
350 Calories
+ 15 calories
+ 10 calories
+ 5 calories
+ 30 Calories
410 Total Calories
Encapsulation: Hamburger
The hamburger responds to a simple calories
method. No other object knows how it is
determining the number.
The hamburger, likewise, is asking each
condiment how many calories.
It has no idea how the condiments are
determining their number.
The Happy Meal
But how many calories are in this happy
meal?
The Happy Meal
But how many calories are in this happy
meal?
Ask the happy meal. It will tell you. You will
have no idea how the number is determined
But the Happy Meal…
1. Asks the drink how many calories
2. Asks the side dish how many calories
3. Asks the main meal how many calories
4. Add them together
What about Polymorphism?
What about Polymorphism?
We have been asking a lot of different types of objects how many
calories they have…
In code:
condiment.calories()
sprite.calories()
fries.calories()
hamburger.calories()
happy_meal.calories()
What about Polymorphism?
What about Polymorphism?

More Related Content

Recently uploaded

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 

Recently uploaded (20)

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Coding the Happy Meal

  • 2. A little bit about procedural code Organization wants an app that will collect all the members and send them an email. But it’s a bit more complicated. The emails should be different for managers and minions. Also managers should be able to opt out of emails and not get them, minions, no so much. Once the emails are sent, we want to have all the recipients and their roles in the organization also collected and another email with those lists sent to each of the executives at the organization After that we want to…
  • 3. A little bit about procedural code 1. Query database for list of organization members 2. Loop through members 3. Check if member’s settings allow for emails to be sent (but only for managers) 4. If user is a manager and emails are allowed • Query database and get email message for managers If user is a minion • Query database and get email message for minions 5. Get member’s name and, if there is a preferred name, use it otherwise use first name 6. Construct email 7. Connect to SMTP server and send email 8. Add member’s email address to a list of Recipients 9. Query the database to update member’s record that email was sent to them 10. Query the database of all related executives 11. Loop through them 12. Send them an email with a list of all recipients 13. …
  • 4. A little bit about procedural code • Task based • Sequential • Many decision trees • If/Else statements • Repetitive • In the bigger picture, VERY repetitive. • Querying the database for the same information in multiple places • Making the same decisions in multiple places. Like using preferred name instead of first. • Due to the specificity of the tasks being accomplished, the more complex the system becomes, the more difficult to change it.
  • 5. How is OOP different? • Model the world you are interacting with • Member • Manager • Minion • Executive • Email • Settings • Email Service • Build behavior into your objects outside of a specific set of tasks: member.preferred_name() #return preferred,first if not available member.get_message() #return role specific message email.send(message) #sends email via SMTP server
  • 6. What is an Object Super simple explanation An object is a data construct that has attributes and behaviors. Attributes are also known as properties. A car might be an object, it may have attributes of color (green) and top speed (125) Behaviors are known as methods or functions. An object may have many methods. A car may have methods “stop” and “go”
  • 7. Python Object class Soda: def __init__(self, syrup, water): self.syrup = syrup self.water = water self._calories = 100 def calories(self): return self._calories def refill(self): # do something pass
  • 8. Object Domain You are presented with a set of features or functions your program needs to have. • What are the real world objects that are involved? • What are their properties? • What are their behaviors? • How do they relate to each other? A good Object Domain will • allow you to ask it questions and get back answers • allow you to extend it, building in new features • add new objects without affecting the current ones • allow you to fix broken parts without affecting the overall system
  • 10. Four Core Principles • Encapsulation
  • 11. Four Core Principles • Encapsulation • Inheritance
  • 12. Four Core Principles • Encapsulation • Inheritance • Composition
  • 13. Four Core Principles • Encapsulation • Inheritance • Composition • Polymorphism
  • 14. Encapsulation • All data associated with an object is “private” • Properties/Attributes are private • Methods to access properties are public • Methods can also be private • You have to “ask” the object to get or change a property • Core methods are called: • Accessors • Getters and Setters • Mutators
  • 15. Inheritance • Objects can inherit properties and behaviors from other objects • Sub Classes • Super Classes • Sub classes “inherit” properties and behaviors from its super class • Sub classes “overwrite” properties and methods that are specific to it
  • 16. Inheritance • Super Class: Car • Property: Top speed • Method: go(‘fast’) • Sub Class 1 : Clunker • “Type” of Car • Top Speed property is set to 25 • It inherits a “go” method, but can only go 25mph • Sub Class 2: Sports Car • “Type” of Car • Top Speed property is set to 125 • It inherits a “go” method, but it can go 125mph
  • 17. Inheritance “Is A” • A clunker “is a” car • A dolphin “is a” mammal • A mammal “is a” animal • A dolphin “is a” animal • A Sprite “is a” drink
  • 18. Composition • Single Objects are composed of multiple other objects • Is generally preferred over inheritance • Allows for greater flexibility as systems become more complex • Does not prevent inheritance • Often, the nature of a composed object is determined more by the objects it is composed of rather than it’s own properties or attributes
  • 19. Composition “Has a” • A car “has a” engine • A car “has” 4 wheels • A human “has” opposable thumbs • A happy meal “has a” drink • A happy meal “has a” hamburger • A happy meal “has a” small fries
  • 20. Composition vs. Inheritance Inheritance Example Object: Car • Sports Car is a car • Clunker is a car • Sports Car/Clunker “extends” the car super class • The sub classes overwrite the top speed property
  • 21. Composition vs. Inheritance Composition Example Object: Car • Has a transmission • Has four wheels • Has an engine • Engine object has a top speed property • Unlike inheritance, top speed is not determined by the car’s top speed attribute but by the type of engine it is composed with.
  • 22. Polymorphism • Two or more objects of different classes respond to the same method • Example: The Cut() Method • Doctors Cut • Hair Stylists Cut • Directors Cut • Insults Cut • In each case, the object “cuts”, but the action is different
  • 23. Polymorphism for n in my_collection_of_things: print(n) • What are you looping through? • Lists • Arrays • Numpy Arrays • Panda Series • etc…
  • 26. Three Simple Items Is it really that simple?
  • 33. Composition Not so simple after all… / (╯°□°)╯︵ ┻━┻
  • 34. Composition Composition allows for flexibility. The variety of different combinations of a “Happy Meal” object can be left to the objects that actually compose the meal. Outside of the variety, a happy meal IS actually pretty simple! • A Happy Meal has a drink • A Happy Meal has a main course • A Happy Meal has a side dish
  • 35. Composition Composition allows for flexibility. The variety of different combinations of a “Happy Meal” object can be left to the objects that actually compose the meal. Outside of the variety, a happy meal IS actually pretty simple! • A Happy Meal has a drink • A Happy Meal has a main course • A Happy Meal has a side dish • A Happy Meal has a toy
  • 36. Encapsulation How many calories are in this happy meal?
  • 37. Three Simple Items Let’s break this down into an Object Domain using all 4 principles of object oriented programming…
  • 39. Inheritance: The Soft Drink If many possible objects share the same characteristic or properties, then inheritance will allow you to consolidate all the shared characteristics in a “Super Class”. A generic soft drink has several possible properties: 1. Carbonated Water 2. Syrup 3. Ice 4. Syrup to carbonated water ratio Syrup is what differentiates all soft drinks.
  • 40. The Soft Drink: Coke A Coke inherits from “Soft Drink” and overwrites syrup property with “coke”
  • 41. The Soft Drink: Coke A Dr. Pepper inherits from “Soft Drink” and overwrites syrup property with “dr. pepper”
  • 42. The Soft Drink: Coke A Sprite Zero inherits from “Soft Drink” and overwrites syrup property with “sprite zero”
  • 43. Inheritance: The Soft Drink With all three of these soft drinks. The only difference is the syrup used.
  • 44.
  • 45. Inheritance: Profit McDonald’s want to maximize profits by saving on the now outrageously priced syrup used in their soda fountains. It would be pretty tedious to go and alter every single different type of soda…
  • 46. Inheritance: Profit Remember these 4 properties? 1. Carbonated Water 2. Syrup 3. Ice 4. Syrup to carbonated water ratio
  • 47. Inheritance: Profit Remember these 4 properties? 1. Carbonated Water 2. Syrup 3. Ice 4. Syrup to carbonated water ratio
  • 48. Inheritance: Profit Remember these 4 properties? 1. Carbonated Water 2. Syrup 3. Ice 4. Syrup to carbonated water ratio Update all syrup to water ratios for ALL soft drinks by updating the super class!
  • 51. The Hamburger A hamburger is composed of: • Meat • Bun / Bread • A variety of condiments • Could have no condiments • Could have a ton of condiments
  • 52. Side Dishes? At this point. Fries are just fries. And apples are just apples. Just two different objects that neither inherit from another object or are composed of multiple objects.
  • 53. The Happy Meal This little happy meal has become pretty complex!!! Happy meal is composed of: 1 Drink that inherits from “Soft Drink”. The drink will swap out the syrup to make it the correct type. 1 Main Meal that could be chicken nuggets OR a hamburger. If a hamburger, then the main meal is actually an object that is composed of several other objects. We don’t actually know how many. 1 Side Dish that is just a plain object. No inheritance or composition. Just a boring object.
  • 54. The Happy Meal But how many calories are in this happy meal?
  • 55. Encapsulation! Wouldn’t it be nice if we could just ask the happy meal how many calories it is? Sounds like a difficult question to answer! Wouldn’t it be nice if we could ask a soft drink how many calories it is? Well that sounds easier. It’s just the syrup. Wouldn’t it be nice to ask a hamburger how many calories it is and have the answer be immediately adjusted based on the number and types of condiments that were added? Good grief…
  • 56. Encapsulation: Soft Drinks A Soft Drink object can respond to a method asking how many calories it has. The object will look up it’s “diet” Boolean flag (true or false) and return either 120 or 0, if diet. • Nothing but the Soft Drink object needs to know how this is determined. • At a later date, a more exact calculation based on syrup and “syrup to water” ratios can be implemented. • Any sub type of Soft Drink (sprite, coke, fanta) can respond to this method because it has been inherited.
  • 57. Encapsulation: Side Dishes Both Fries and Apples are independent objects that simple have a calories property and a Getter method implemented. In both of these cases, the number of calories is a static value that is returned
  • 59. Encapsulation: Condiments Condiments has become a new object used in constructing a happy meal. Condiments are very simple objects that can also respond to a calories method.
  • 60. Encapsulation: Hamburger Hamburger also responds to calories. It determines total calories by calculating 150 for the bun and 200 for the all beef patty. Then, on top of the 350 calories, it will ask each and every condiment how many calories it is and then add the responding numbers
  • 61. Encapsulation: Hamburger 350 Calories + 15 calories + 10 calories + 5 calories + 30 Calories 410 Total Calories
  • 62. Encapsulation: Hamburger The hamburger responds to a simple calories method. No other object knows how it is determining the number. The hamburger, likewise, is asking each condiment how many calories. It has no idea how the condiments are determining their number.
  • 63. The Happy Meal But how many calories are in this happy meal?
  • 64. The Happy Meal But how many calories are in this happy meal? Ask the happy meal. It will tell you. You will have no idea how the number is determined But the Happy Meal… 1. Asks the drink how many calories 2. Asks the side dish how many calories 3. Asks the main meal how many calories 4. Add them together
  • 66. What about Polymorphism? We have been asking a lot of different types of objects how many calories they have… In code: condiment.calories() sprite.calories() fries.calories() hamburger.calories() happy_meal.calories()