SlideShare uma empresa Scribd logo
1 de 17
CH8. Expanding Our Horizons
© Taka Wang, 2017 1
Overview
• Object
• Encapsula/on
• Inheritance
• Handling varia/on
• Commonality and variability analysis
• Abstract class and its derived classes
© Taka Wang, 2017 2
Objects: Tradi/onal view and new view
Tradi&onal View
• Data with methods - smart data
• too narrow from implementa-on perspec4ve
Broad View
• From conceptual perspec+ve
• an object is an en#ty that has responsibili#es ( ), these responsibili+es define
the behavior of the object. Or an en#ty that has specific behavior ( ).
© Taka Wang, 2017 3
Focus on inten+on/mo+va+on not implementa+on
This view enables us to build so1ware in two steps:
1. Make a preliminary design without worrying about all the details involved.
2. Implement the design.
The reason this works is that we only have to focus on the object’s public
interface — the communica9on window through which I ask the object to do
something.
Hiding implementa-ons behind interfaces essen-ally decouples them from
the using objects.
© Taka Wang, 2017 4
Encapsula)on: Tradi)onal view and new view
Tradi&onal View
• data hiding
Broad View
• any kind of hiding
• Implementa*ons (data, methods..)
• Drived classes (Encapsula6on of type is achieved when there is an abstract class with deriva*ons (or an
interface with implementa*ons) that are used polymorphically)
• Design details
• Instan*a*on rules (ex. crea*onal pa>erns)
© Taka Wang, 2017 5
Advantage
It gives us a be,er way to split up (decompose) our programs. The
encapsula)ng layers become the interfaces we design to. (
)
By encapsula,ng different kinds of subclasses (encapsula)on of
type), we can add new ones without changing any of the client
programs using them. (GoF typically means when they men2on
encapsula2on)
© Taka Wang, 2017 6
Inheritance
Tradi&onal View
• reuse of classes
• achived by crea1ng classes and then deriving new (spcialized) classes bases on these base (generalized) classes
Broad View
• using inheritance for specializa3on, however
• can cause weak cohesion
• reduces possibility of reuse
• does not scale well with varia3on
• to classify classes as things that behave the same way. (placeholder)
© Taka Wang, 2017 7
Find What Is Varying and Encapsulate It
Consider what should be variable in your design. This approach is
the opposite of focusing on the cause of redesign. Instead of
considering what might force a change to a design, consider what
you want to be able to change without redesign. The focus here is
on encapsula*ng the concept that varies, a theme of many design
pa<erns.
— GoF, Design Pa/erns
© Taka Wang, 2017 8
More about GoF's Encapsula3on
• Design pa+erns use inheritance to classify varia6ons in behaviors.
• Hiding classes with an abstract class or interface — type
encapsula,on.
• Containing a reference of this abstract class or interface type
(aggrega6on) hides these derived classes that represent varia,ons in
behavior.
• In effect, many design pa+erns use encapsula1on to create layers
between objects.
© Taka Wang, 2017 9
Containing varia+on in data vs containing varia+on in
behavior
Handling varia+on in data
• Have a data member that tells me what type of movement my object has.
• Have two different types of Animals (both derived from the base Animal class)
— one for walking and one for flying.
Handling varia+on in behavior with objects
Using objects to contain varia0on in a1ributes and using objects to contain
varia0on in behavior are very similar. Don't afraid.
© Taka Wang, 2017 10
Commonality and Variability
Iden%fy where things vary (“commonality analysis”) and then iden%fy how they vary (“variability
analysis”).
Commonality analysis is the search for common elements that helps us understand how family
members are the same.
Variability analysis reveals how family members vary. Variability only makes sense within a given
commonality.
Ex. Whiteboard marker, pencil, ballpoint pen
• Commonality: wri/ng instrument
• Variability: material to write, shape..
© Taka Wang, 2017 11
Commonality and Variability and Abstract class
Commonality analysis seeks structure that is unlikely to change over
2me, while variability analysis captures structure that is likely to
change. Variability analysis makes sense only in terms of the context
defined by the associated commonality analysis.
In other words, if varia1ons are the specific concrete cases in the
domain, commonality defines the concepts in the domain that 1e
them together. The common concepts will be represented by abstract
classes. The varia.ons found by variability analysis will be
implemented by the concrete classes.
© Taka Wang, 2017 12
Rela%onship between Commonality and Variability,
perspec%ves, and abstract classes
© Taka Wang, 2017 13
Benefits of using abstract classes for specializa4on
© Taka Wang, 2017 14
Two-Step Procedure for Design
Ask yourself:
• When defining an abstract class (commonality):
• What interface is needed to handle all the responsibili9es
(core concepts from the conceptual perspec9ve) of this class?
• When defining derived classes:
• Given this par9cular implementa9on (this varia/on), how can I
implement it (varia9on) with the given specifica9on?
© Taka Wang, 2017 15
Take away
Think object-oriented in a broad way.
• Object: an en#ty that has responsibili#es (specific behavior)
• Encapsula1on: any kind of hiding (instan1a1on rule, type..)
• Inheritance: use for specializa.on and classify classes as things that behave the
same way.
Find what is varying and encapsulate it (in behavior).
Commonality, variability and abstract class: use inheritance to classify varia7ons in
behaviors.
© Taka Wang, 2017 16
Thank you
© Taka Wang, 2017 17

Mais conteúdo relacionado

Mais procurados (7)

[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)
 
Semi-Supervised Learning with Variational Bayesian Inference and Maximum Unce...
Semi-Supervised Learning with Variational Bayesian Inference and Maximum Unce...Semi-Supervised Learning with Variational Bayesian Inference and Maximum Unce...
Semi-Supervised Learning with Variational Bayesian Inference and Maximum Unce...
 
Mediator Pattern
Mediator PatternMediator Pattern
Mediator Pattern
 
E3
E3E3
E3
 
Cohesion and coupling software desgin engineering
Cohesion and coupling  software desgin engineeringCohesion and coupling  software desgin engineering
Cohesion and coupling software desgin engineering
 
Generation of Descriptive Elements for Text
Generation of Descriptive Elements for TextGeneration of Descriptive Elements for Text
Generation of Descriptive Elements for Text
 
Final sdp ppt
Final sdp pptFinal sdp ppt
Final sdp ppt
 

Semelhante a Design Pattern Explained CH8

Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptRushikeshChikane1
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptRushikeshChikane2
 
Semantic Web - Ontology 101
Semantic Web - Ontology 101Semantic Web - Ontology 101
Semantic Web - Ontology 101Luigi De Russis
 
Behavioral pattern By:-Priyanka Pradhan
Behavioral pattern By:-Priyanka PradhanBehavioral pattern By:-Priyanka Pradhan
Behavioral pattern By:-Priyanka PradhanPriyanka Pradhan
 
Intro to oop.pptx
Intro to oop.pptxIntro to oop.pptx
Intro to oop.pptxUmerUmer25
 
Unit No 6 Design Patterns.pptx
Unit No 6 Design Patterns.pptxUnit No 6 Design Patterns.pptx
Unit No 6 Design Patterns.pptxDrYogeshDeshmukh1
 
Module 2 design patterns-2
Module 2   design patterns-2Module 2   design patterns-2
Module 2 design patterns-2Ankit Dubey
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design patternMindfire Solutions
 
Methods for Ontology Design Patterns reuse
Methods for Ontology Design Patterns reuseMethods for Ontology Design Patterns reuse
Methods for Ontology Design Patterns reuseValentina Presutti
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSkillwise Group
 
ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW
ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEWONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW
ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEWijait
 
ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW
ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW
ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW ijait
 
Design Pattern lecture 1
Design Pattern lecture 1Design Pattern lecture 1
Design Pattern lecture 1Julie Iskander
 

Semelhante a Design Pattern Explained CH8 (20)

Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Semantic Web - Ontology 101
Semantic Web - Ontology 101Semantic Web - Ontology 101
Semantic Web - Ontology 101
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Introduction
IntroductionIntroduction
Introduction
 
Behavioral pattern By:-Priyanka Pradhan
Behavioral pattern By:-Priyanka PradhanBehavioral pattern By:-Priyanka Pradhan
Behavioral pattern By:-Priyanka Pradhan
 
Intro to oop.pptx
Intro to oop.pptxIntro to oop.pptx
Intro to oop.pptx
 
Unit No 6 Design Patterns.pptx
Unit No 6 Design Patterns.pptxUnit No 6 Design Patterns.pptx
Unit No 6 Design Patterns.pptx
 
Module 2 design patterns-2
Module 2   design patterns-2Module 2   design patterns-2
Module 2 design patterns-2
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
Methods for Ontology Design Patterns reuse
Methods for Ontology Design Patterns reuseMethods for Ontology Design Patterns reuse
Methods for Ontology Design Patterns reuse
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
 
ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW
ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEWONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW
ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW
 
ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW
ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW
ONTOLOGY VISUALIZATION PROTÉGÉ TOOLS – A REVIEW
 
Design Pattern lecture 1
Design Pattern lecture 1Design Pattern lecture 1
Design Pattern lecture 1
 
Ooad
OoadOoad
Ooad
 
Ooad
OoadOoad
Ooad
 
Oos Short Q N
Oos Short Q NOos Short Q N
Oos Short Q N
 
Design Patterns.ppt
Design Patterns.pptDesign Patterns.ppt
Design Patterns.ppt
 

Mais de Jamie (Taka) Wang

Mais de Jamie (Taka) Wang (20)

20200606_insight_Ignition
20200606_insight_Ignition20200606_insight_Ignition
20200606_insight_Ignition
 
20200727_Insight workstation
20200727_Insight workstation20200727_Insight workstation
20200727_Insight workstation
 
20200723_insight_release_plan
20200723_insight_release_plan20200723_insight_release_plan
20200723_insight_release_plan
 
20210105_量產技轉
20210105_量產技轉20210105_量產技轉
20210105_量產技轉
 
20200808自營電商平台策略討論
20200808自營電商平台策略討論20200808自營電商平台策略討論
20200808自營電商平台策略討論
 
20200427_hardware
20200427_hardware20200427_hardware
20200427_hardware
 
20200429_ec
20200429_ec20200429_ec
20200429_ec
 
20200607_insight_sync
20200607_insight_sync20200607_insight_sync
20200607_insight_sync
 
20220113_product_day
20220113_product_day20220113_product_day
20220113_product_day
 
20200429_software
20200429_software20200429_software
20200429_software
 
20200602_insight_business
20200602_insight_business20200602_insight_business
20200602_insight_business
 
20200408_gen11_sequence_diagram
20200408_gen11_sequence_diagram20200408_gen11_sequence_diagram
20200408_gen11_sequence_diagram
 
20190827_activity_diagram
20190827_activity_diagram20190827_activity_diagram
20190827_activity_diagram
 
20150722 - AGV
20150722 - AGV20150722 - AGV
20150722 - AGV
 
20161220 - microservice
20161220 - microservice20161220 - microservice
20161220 - microservice
 
20160217 - Overview of Vortex Intelligent Data Sharing Platform
20160217 - Overview of Vortex Intelligent Data Sharing Platform20160217 - Overview of Vortex Intelligent Data Sharing Platform
20160217 - Overview of Vortex Intelligent Data Sharing Platform
 
20151111 - IoT Sync Up
20151111 - IoT Sync Up20151111 - IoT Sync Up
20151111 - IoT Sync Up
 
20151207 - iot strategy
20151207 - iot strategy20151207 - iot strategy
20151207 - iot strategy
 
20141210 - Microservice Container
20141210 - Microservice Container20141210 - Microservice Container
20141210 - Microservice Container
 
20161027 - edge part2
20161027 - edge part220161027 - edge part2
20161027 - edge part2
 

Último

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Último (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

Design Pattern Explained CH8

  • 1. CH8. Expanding Our Horizons © Taka Wang, 2017 1
  • 2. Overview • Object • Encapsula/on • Inheritance • Handling varia/on • Commonality and variability analysis • Abstract class and its derived classes © Taka Wang, 2017 2
  • 3. Objects: Tradi/onal view and new view Tradi&onal View • Data with methods - smart data • too narrow from implementa-on perspec4ve Broad View • From conceptual perspec+ve • an object is an en#ty that has responsibili#es ( ), these responsibili+es define the behavior of the object. Or an en#ty that has specific behavior ( ). © Taka Wang, 2017 3
  • 4. Focus on inten+on/mo+va+on not implementa+on This view enables us to build so1ware in two steps: 1. Make a preliminary design without worrying about all the details involved. 2. Implement the design. The reason this works is that we only have to focus on the object’s public interface — the communica9on window through which I ask the object to do something. Hiding implementa-ons behind interfaces essen-ally decouples them from the using objects. © Taka Wang, 2017 4
  • 5. Encapsula)on: Tradi)onal view and new view Tradi&onal View • data hiding Broad View • any kind of hiding • Implementa*ons (data, methods..) • Drived classes (Encapsula6on of type is achieved when there is an abstract class with deriva*ons (or an interface with implementa*ons) that are used polymorphically) • Design details • Instan*a*on rules (ex. crea*onal pa>erns) © Taka Wang, 2017 5
  • 6. Advantage It gives us a be,er way to split up (decompose) our programs. The encapsula)ng layers become the interfaces we design to. ( ) By encapsula,ng different kinds of subclasses (encapsula)on of type), we can add new ones without changing any of the client programs using them. (GoF typically means when they men2on encapsula2on) © Taka Wang, 2017 6
  • 7. Inheritance Tradi&onal View • reuse of classes • achived by crea1ng classes and then deriving new (spcialized) classes bases on these base (generalized) classes Broad View • using inheritance for specializa3on, however • can cause weak cohesion • reduces possibility of reuse • does not scale well with varia3on • to classify classes as things that behave the same way. (placeholder) © Taka Wang, 2017 7
  • 8. Find What Is Varying and Encapsulate It Consider what should be variable in your design. This approach is the opposite of focusing on the cause of redesign. Instead of considering what might force a change to a design, consider what you want to be able to change without redesign. The focus here is on encapsula*ng the concept that varies, a theme of many design pa<erns. — GoF, Design Pa/erns © Taka Wang, 2017 8
  • 9. More about GoF's Encapsula3on • Design pa+erns use inheritance to classify varia6ons in behaviors. • Hiding classes with an abstract class or interface — type encapsula,on. • Containing a reference of this abstract class or interface type (aggrega6on) hides these derived classes that represent varia,ons in behavior. • In effect, many design pa+erns use encapsula1on to create layers between objects. © Taka Wang, 2017 9
  • 10. Containing varia+on in data vs containing varia+on in behavior Handling varia+on in data • Have a data member that tells me what type of movement my object has. • Have two different types of Animals (both derived from the base Animal class) — one for walking and one for flying. Handling varia+on in behavior with objects Using objects to contain varia0on in a1ributes and using objects to contain varia0on in behavior are very similar. Don't afraid. © Taka Wang, 2017 10
  • 11. Commonality and Variability Iden%fy where things vary (“commonality analysis”) and then iden%fy how they vary (“variability analysis”). Commonality analysis is the search for common elements that helps us understand how family members are the same. Variability analysis reveals how family members vary. Variability only makes sense within a given commonality. Ex. Whiteboard marker, pencil, ballpoint pen • Commonality: wri/ng instrument • Variability: material to write, shape.. © Taka Wang, 2017 11
  • 12. Commonality and Variability and Abstract class Commonality analysis seeks structure that is unlikely to change over 2me, while variability analysis captures structure that is likely to change. Variability analysis makes sense only in terms of the context defined by the associated commonality analysis. In other words, if varia1ons are the specific concrete cases in the domain, commonality defines the concepts in the domain that 1e them together. The common concepts will be represented by abstract classes. The varia.ons found by variability analysis will be implemented by the concrete classes. © Taka Wang, 2017 12
  • 13. Rela%onship between Commonality and Variability, perspec%ves, and abstract classes © Taka Wang, 2017 13
  • 14. Benefits of using abstract classes for specializa4on © Taka Wang, 2017 14
  • 15. Two-Step Procedure for Design Ask yourself: • When defining an abstract class (commonality): • What interface is needed to handle all the responsibili9es (core concepts from the conceptual perspec9ve) of this class? • When defining derived classes: • Given this par9cular implementa9on (this varia/on), how can I implement it (varia9on) with the given specifica9on? © Taka Wang, 2017 15
  • 16. Take away Think object-oriented in a broad way. • Object: an en#ty that has responsibili#es (specific behavior) • Encapsula1on: any kind of hiding (instan1a1on rule, type..) • Inheritance: use for specializa.on and classify classes as things that behave the same way. Find what is varying and encapsulate it (in behavior). Commonality, variability and abstract class: use inheritance to classify varia7ons in behaviors. © Taka Wang, 2017 16
  • 17. Thank you © Taka Wang, 2017 17