Fly Weight
Design
Pattern
SAIFULLAH FA20-BSE-102
RASHID FA20-BSE-081
Content
What is Design Pattern
What is Flyweight Design Pattern
When should we use the design patterns?
Types of Design Pattern
Flyweight Design Pattern
Advantage of Flyweight Pattern
Usage of Flyweight Pattern
How to Implement
Example
What is Design Pattern
A. Design Pattern is a well-proved solution for solving the specific
problem/task.
B. It is a general reusable solution to a commonly occurring problem in
software design.
C. Example.....
What is Design Pattern
A. A design pattern isn't a finished design that can be transformed
directly into code.
B. It is a description or template for how to solve a problem that can
be used in many different situations.
When should we use the design
patterns?
We must use the design patterns during the analysis and
requirement phase of SDLC(Software Development Life
Cycle).
Types of Design Pattern
1. Creational
2. Structural
3. Behavioral
Flyweight
Design Pattern
1. It is a structural design
Pattern
2. Flyweight pattern is primarily
used to reuse already
existing similar kind of
objects by storing them and
create new object when no
matching object is found:
Advantage of Flyweight Pattern
1. It reduces the number of objects.
2. It reduces the amount of memory and storage devices required if the objects
are persisted
Usage of Flyweight Pattern
1. When an application uses number of objects
2. When the storage cost is high because of the quantity of objects.
3. When the application does not depend on object identity.
How to Implement
1.Divide fields of a class that will become a
flyweight into two parts:
1. The intrinsic state: the fields that contain unchanging data
duplicated across many objects
2. The extrinsic state: the fields that contain contextual data
unique to each object
How to Implement
2. Leave the fields that represent the intrinsic state in the class
3. Go over methods that use fields of the extrinsic state. For
each field used in the method, introduce a new parameter and
use it instead of the field.
Real world example of flyweight pattern
1. Suppose we have a pen which can exist with/without refill.
2. A refill can be of any color thus a pen can be used to create drawings
having N number of colors.
3. Here Pen can be flyweight object with refill as extrinsic attribute.
4. All other attributes such as pen body, pointer etc. can be intrinsic
attributes which will be common to all pens.
5. A pen will be distinguished by its refill color only, nothing else.
Real world example of flyweight pattern
All application modules which need to access a red pen – can use the
same instance of red pen (shared object). Only when a different
color pen is needed, application module will ask for another pen
from flyweight factory.
Thank You……

Fly Weight Design Pattern.pptx

  • 1.
  • 2.
    Content What is DesignPattern What is Flyweight Design Pattern When should we use the design patterns? Types of Design Pattern Flyweight Design Pattern Advantage of Flyweight Pattern Usage of Flyweight Pattern How to Implement Example
  • 3.
    What is DesignPattern A. Design Pattern is a well-proved solution for solving the specific problem/task. B. It is a general reusable solution to a commonly occurring problem in software design. C. Example.....
  • 4.
    What is DesignPattern A. A design pattern isn't a finished design that can be transformed directly into code. B. It is a description or template for how to solve a problem that can be used in many different situations.
  • 5.
    When should weuse the design patterns? We must use the design patterns during the analysis and requirement phase of SDLC(Software Development Life Cycle).
  • 6.
    Types of DesignPattern 1. Creational 2. Structural 3. Behavioral
  • 7.
    Flyweight Design Pattern 1. Itis a structural design Pattern 2. Flyweight pattern is primarily used to reuse already existing similar kind of objects by storing them and create new object when no matching object is found:
  • 8.
    Advantage of FlyweightPattern 1. It reduces the number of objects. 2. It reduces the amount of memory and storage devices required if the objects are persisted
  • 9.
    Usage of FlyweightPattern 1. When an application uses number of objects 2. When the storage cost is high because of the quantity of objects. 3. When the application does not depend on object identity.
  • 10.
    How to Implement 1.Dividefields of a class that will become a flyweight into two parts: 1. The intrinsic state: the fields that contain unchanging data duplicated across many objects 2. The extrinsic state: the fields that contain contextual data unique to each object
  • 11.
    How to Implement 2.Leave the fields that represent the intrinsic state in the class 3. Go over methods that use fields of the extrinsic state. For each field used in the method, introduce a new parameter and use it instead of the field.
  • 12.
    Real world exampleof flyweight pattern 1. Suppose we have a pen which can exist with/without refill. 2. A refill can be of any color thus a pen can be used to create drawings having N number of colors. 3. Here Pen can be flyweight object with refill as extrinsic attribute. 4. All other attributes such as pen body, pointer etc. can be intrinsic attributes which will be common to all pens. 5. A pen will be distinguished by its refill color only, nothing else.
  • 13.
    Real world exampleof flyweight pattern All application modules which need to access a red pen – can use the same instance of red pen (shared object). Only when a different color pen is needed, application module will ask for another pen from flyweight factory.
  • 14.