Tải bản đầy đủ (.pdf) (30 trang)

Software design: Lecture 37 - Sheraz Pervaiz

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (400.98 KB, 30 trang )

1

Software Design
Lecture : 37


2

Flyweight Design Pattern


3

Motivation for Flyweight Design Pattern
 At time some programs require a large number of objects 
that have some shared state among them.

 Every object can be viewed as consisting of one or both 
of the following two sets of information:

i.

Intrinsic

ii. Extrinsic


4

Intrinsic Information ­ Shareable
 The  intrinsic  information  of  an  object  is  independent  of 


the  object  context.  That  means  the  intrinsic  information 
is the common information that remains constant among 
different instances of a given class.

 For example, the company information on a visiting card 
is the same for all employees.


5

Extrinsic Information – Not 
Shareable
 The extrinsic information of an object is dependent upon 
and  varies  with  the  object  context.  That  means  the 
extrinsic  information  is  unique  for  every  instance  of  a 
given class.

 For example, the employee id , employee name and title 
are extrinsic on a visiting card as this information is 
unique for every employee.


6

Heavyweight Object nightmare
 Consider an application scenario that involves creating a 
large number of objects that are unique only in terms of 
a few parameters. In other words, these objects contain 
some intrinsic, invariant data that is common among all 
objects.


 This intrinsic data needs to be created and maintained as part 
of every object that is being created. The overall creation and 
maintenance  of  a  large  group  of  such  objects  can  be  very 


7

Intent of  Flyweight Design Pattern
 The  intent  of  this  pattern  is  to  use  sharing  to  support  a 
large  number  of  objects  that  have  part  of  their  internal 
state in common where the other part of state can vary.




8

Flyweight Pattern Defined
“Facilitates the reuse of many fine grained 
objects, making the utilization of large 
numbers of objects more efficient”


9

Applicability
i.

An application uses large number of objects.


ii. Storage costs are high due to quantity of objects.
iii. Most object state can be made extrinsic.
iv. Many  group  of  objects  may  be  replaced  by  relatively 
few shared objects once extrinsic objects are removed.
v. The application doesn’t  depend upon on object identify 
as flyweight objects can be shared and identity test will 
return distinct objects.


10

Suggested Approach in Flyweight
 The  Flyweight  pattern  suggests  separating  all  the 
intrinsic  common  data  into  a  separate  object  referred  to 
as a Flyweight object.

• The  group  of  objects  being  created  can  share  the 
flyweight  object as it represents their intrinsic state.


11

Suggested Approach
 This  eliminates  the  need  for  storing  the  same  invariant, 
intrinsic  information  in  every  object;  instead  it  is  stored 
only once in the form of a single flyweight object.

 As a result, the client application can realize considerable 
savings in terms of the memory­usage and the time.



12

Connection Pooling


13

 A connection 

pool is 

a cache of database 

connections maintained  so  that  the  connections  can  be 
reused  when  future  requests  to  the  database  are 
required.  Connection  pools  are  used  to  enhance  the 
performance  of  executing  commands  on  a  database. 
Opening and maintaining a database connection for each 
user,  especially  requests  made  to  a  dynamic  database­
driven website application, 
resources. 

is 

costly 

and 


wastes 


14

Connection Pooling
 In connection pooling, after a connection is created, it is 
placed in the pool and it is used over again so that a new 
connection  does  not  have  to  be  established.  If  all  the 
connections  are  being  used,  a  new  connection  is  made 
and  is  added  to  the  pool.  Connection  pooling  also  cuts 
down  on  the  amount  of  time  a  user  must  wait  to 
establish a connection to the database.


15


16

Class Diagram


17


18

Flyweight ­  Declares  an  interface  through  which 
flyweights can receive and act on extrinsic state.


 Concrete 

Flyweight ­ 

Implements 

the 

Flyweight 

interface and stores intrinsic state. A Concrete Flyweight 
object  must  be  sharable.  The  Concrete  flyweight  object 
must maintain state that it is intrinsic to it, and must be 
able to manipulate state that is extrinsic


19

Flyweight Factory ­ The factory creates and manages 
flyweight objects. In addition the factory ensures sharing 
of the flyweight objects. The factory maintains a pool of 
different flyweight objects and returns an object from the 
pool  if  it  is  already  created,  adds  one  to  the  pool  and 
returns it in case it is new.

 Client ­  A  client  maintains  references  to  flyweights  in 
addition to computing and maintaining extrinsic state



20

Flow of the application


21

A client needs a flyweight object; it calls the factory 
to  get  the  flyweight  object.  The  factory  checks  a 
pool of flyweights to determine if a flyweight object 
of the requested type is in the pool, if there is, the 
reference  to  that  object  is  returned.  If  there  is  no 
object  of  the  required  type,  the  factory  creates  a 
flyweight of the requested type, adds it to the pool, 
and returns a reference to the flyweight. 


22

The flyweight maintains intrinsic state (state that 
is shared among the large number of objects that 
we  have  created  the  flyweight  for) and provides 
methods to manipulate external state (State that 
vary  from  object  to  object  and  is  not  common 
among the objects we have created the flyweight 
for).


23


Flyweight and other design patterns
Factory and Singleton patterns ­ 
Flyweights are usually created using a factory and 
the singleton is applied to that factory so that for 
each  type  or  category  of  flyweights  a  single 
instance is returned.


24

Consequence
Flyweight  pattern  saves  memory  by  sharing 
flyweight objects among clients. 

The amount of memory saved generally depends 
on the number of flyweight categories saved 


25

Example Scenario


×