Nhóm 10 Design Pattern 1
Nhóm 10 Design Pattern 1
DESIGN
01 PATTERN 02 VISITOR 03 FLYWEIGHT
01
DESIGN PATTERN
DESIGN PATTERN
A solution to a common software problem in a context
○ Describes a recurring software structure
○ Abstract from programming language
○ Identifies classes and their roles in the solution to a
problem
○ Patterns are not code or designs; must be
instantiated/applied
BENEFITS
The Visitor interface declares a
set of visiting methods:
• Can take concrete elements of
an object structure as
arguments
• May have the same names if
the program is written in a
language that supports
overloading
• The type of their parameters
must be different.
STRUCTURE
Concrete Visitor
Each Concrete
Visitor implements several
versions of the same behaviors.
Element
The Element interface declares a
method for “accepting” visitors.
This method should have one
parameter declared with the type
of the visitor interface.
Concrete Element
Each Concrete Element must
implement the acceptance method.
Purpose: redirect the call to the proper
visitor’s method corresponding to the
current element class.
Even if a base element class
implements this method, all subclasses
must still override this method in their
own classes and call the appropriate
method on the visitor object.
Client
The Client usually represents a
collection or some other complex
object (for example, a Composite
tree).
Clients work with objects from that
collection via some abstract interface
=> Usually, they aren’t aware of all the
concrete element classes
PROS AND CONS
Open/Closed Principle. You can You need to update all visitors each time
introduce a new behavior that can a class gets added to or removed from the
work with objects of different classes element hierarchy.
without changing these classes. Visitors might lack the necessary access
Single Responsibility Principle. You to the private fields and methods of the
can move multiple versions of the elements that they’re supposed to work
same behavior into the same class. with.
A visitor object can accumulate
some useful information while
working with various objects. This
might be handy when you want to
traverse some complex object
structure, such as an object tree, and
apply the visitor to each object of
this structure.
DEMO
Single Dispatch Double Dispatch (Visitor)
DEMO
With Visitor
Without Visitor
DEMO
With Visitor
Without Visitor
03
FLYWEIGHT
PATTERN
Problem
What is flyweight pattern?
• Each instance does not contain its own state but stores it
externally.
Declares an interface through Creates and manages flyweight objects. Maintains a reference to
which flyweights can receive Ensures that flyweights are shared properly. flyweight(s). Computes or
and act on extrinsic state. When a client requests a flyweight, the stores the extrinsic state of
Flyweight Factory object supplies an flyweight(s)
existing instance or creates one, if none
exists.
3. Concrete Flyweight 4. Unshared Concrete
(Character) Flyweight
• Implements the Flyweight interface and adds • Not all Flyweight subclasses need to be shared.
storage for intrinsic state, if any. • The Flyweight interface enables sharing; it
• A ConcreteFlyweight object must be sharable. doesn't enforce it.
• Any state it stores must be intrinsic; that is, it must • It’s common for Unshared Concrete Flyweight
be independent of the ConcreteFlyweight object's objects to have Concrete Flyweight objects as
context. children at some level in the flyweight object
structure (as the Row and Column classes
have).
ADVANTAGE AND DISADVANTAGE
Advantage
● Reduce the number of objects created by sharing objects. So save memory and necessary storage
devices
Disadvantage
● Trade-off in terms of CPU usage when flyweight objects are accessed
multiple times.
● The code becomes much more complicated