Professional Documents
Culture Documents
L3-2 FacadAdapterAbstractionReadonly
L3-2 FacadAdapterAbstractionReadonly
L3-2 FacadAdapterAbstractionReadonly
Design Patterns
(Adapter, Façade, Read Only Interface,
Abstraction Occurrence)
▪ Pattern Templates.
▪ Resulting context
—the state of the system after pattern has been applied
▪ Rationale
— explanation of steps or rules in the pattern
▪ Related patterns
▪ Known use
—occurrence of the pattern and its application within
existing system
Pattern Forms: 2. GOF template
▪ Problem:
▪ How to convert the interface of a class into another
interface the clients expect.
▪ Adapter lets classes work together that could not otherwise
because of incompatible interfaces.
▪ We will develop a new class circle. Circle class derives from the shape class.
▪ Circle has the shape class interface and contains an object to XXCircle.
▪ Circle (adapter) passes requests made to circle object to XXCircle class
(adaptee).
Adapter Pattern
▪ Example:
Adapter Pattern
▪ Context:
—You are building an inheritance hierarchy and want to
incorporate an existing class into it.
—The reused class is also often already part of its own
inheritance hierarchy.
▪ Problem:
—How to obtain the power of polymorphism when reusing a
class whose methods:
- have the same function but not the same signature as the
other methods in the hierarchy?
—i.e. How to convert the interface of a class into another
interface the clients expect.
▪ Forces:
—You do not have access to multiple inheritance or you do not
want to use it.
Adapter Pattern
▪ Solution:
Rather than directly incorporating the reused class into
your inheritance hierarchy, instead incorporate an “Adapter”
class. Adapter class is connected by an association to the reused
class (Adaptee). The polymorphic methods of the Adapter class
delegate to the methods of the Adaptee class.
Adapter Pattern
https://refactoring.guru/design-patterns/facade
Façade vs. Adaptor
▪ Superficial difference Facade hides many classes; Adapter hides only one.
But adapter can wrap multiple objects at once in order to access all the
functionality it needs.
▪ Problem:
—How do you create a situation where some classes see a class as
read-only whereas others are able to make modifications?
▪ Forces:
—Restricting access by using the public, protected and private
keywords is not adequately selective.
—Making access public makes it public for both reading and
writing
Read-only Interface
▪ Solution: Create a “Mutable” class as you would create any other
class. You pass instances of this class to methods that need to make
changes.
Then create a public interface “ReadOnlyInterface” , that has only the
read-only operations of “Mutable”. You associate the ReadOnly
interface “ to classes that don’t need to make changes to Mutable
attributes.
Read-only Interface anti-Patterns
This doesn’t work as you want a single class with different sets
of access rights.
Abstraction-Occurrence Pattern
▪ Context:
▪ In a domain model you find a set of related objects
“occurrences”; the members of such a set share common
information but also differ from each other in important ways.
▪ Examples:
▪ All copies of the same book in a library, have the same
author name, #of pages, publisher; but each copy has a
different barcode number.
▪ Flights that leave at the same time for the same destination,
have the same flight number and airlines; but they leave at
different dates, with different passengers and crew.
Abstraction-Occurrence Pattern
▪ Problem:
▪ What is the best way to represent such sets of occurrences?
▪ Forces:
▪ You want to represent the members of each set of occurrences
without duplicating the common information.
Abstraction-Occurrence Anti-Patterns
▪ Using single class, there will be an object for each book ,
—This solution leads to duplication of information in the
multiple copies of the book.
▪ Inheritance also will lead to the same result.
Abstraction-Occurrence Pattern
▪ Solution:
▪ Create an “abstraction” class that contains the common
data.
▪ Then create an “occurrence ” class representing the
occurrences of this abstraction.
▪ Connect these classes with a one-to-many association
Abstraction-Occurrence Pattern
▪ Example1:
▪ Example 2:
References