Successfully reported this slideshow.
CH8. Expanding Our Horizons
© Taka Wang, 2017 1
• Handling varia/on
• Commonality and variability analysis
• Abstract class and its derived classes
© Taka Wang, 2017 2
Objects: Tradi/onal view and new view
• Data with methods - smart data
• too narrow from implementa-on perspec4ve
• From conceptual perspec+ve
• an object is an en#ty that has responsibili#es ( ), these responsibili+es deﬁne
the behavior of the object. Or an en#ty that has speciﬁc 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
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
• data hiding
• 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
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 diﬀerent 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
© Taka Wang, 2017 6
• reuse of classes
• achived by crea1ng classes and then deriving new (spcialized) classes bases on these base (generalized) classes
• 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
— 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
• Containing a reference of this abstract class or interface type
(aggrega6on) hides these derived classes that represent varia,ons in
• In eﬀect, many design pa+erns use encapsula1on to create layers
© Taka Wang, 2017 9
Containing varia+on in data vs containing varia+on in
Handling varia+on in data
• Have a data member that tells me what type of movement my object has.
• Have two diﬀerent types of Animals (both derived from the base Animal class)
— one for walking and one for ﬂying.
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
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
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
deﬁned by the associated commonality analysis.
In other words, if varia1ons are the speciﬁc concrete cases in the
domain, commonality deﬁnes 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
Beneﬁts of using abstract classes for specializa4on
© Taka Wang, 2017 14
Two-Step Procedure for Design
• When deﬁning an abstract class (commonality):
• What interface is needed to handle all the responsibili9es
(core concepts from the conceptual perspec9ve) of this class?
• When deﬁning derived classes:
• Given this par9cular implementa9on (this varia/on), how can I
implement it (varia9on) with the given speciﬁca9on?
© Taka Wang, 2017 15
Think object-oriented in a broad way.
• Object: an en#ty that has responsibili#es (speciﬁc behavior)
• Encapsula1on: any kind of hiding (instan1a1on rule, type..)
• Inheritance: use for specializa.on and classify classes as things that behave the
Find what is varying and encapsulate it (in behavior).
Commonality, variability and abstract class: use inheritance to classify varia7ons in
© Taka Wang, 2017 16
© Taka Wang, 2017 17