Flyweight and Singleton - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Flyweight and Singleton

Description:

The Flyweight pattern was used by the designers of Java for better performance. ... a particular hardware interface or serial port ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 17
Provided by: mer3
Category:

less

Transcript and Presenter's Notes

Title: Flyweight and Singleton


1
Flyweight and Singleton
2
Constructing Objects
  • Constructing some objects has some expense (CPU
    time, network time, memory)
  • Constructing lots of objects can have a lot of
    expense
  • It is sometimes desirable to reduce the number of
    objects created  
  • One way to reduce object construction is to
    employ the flyweight Design pattern

3
Flyweight Design Pattern
  • A flyweight is a shared object that can be used
    in multiple contexts simultaneously
  • The same objects can be reused instead of
    constructing and storing new ones 
  • Flyweight avoids the expense of multiple
    instances that contain the same information
  • Different parts of a program share one instance

4
The String class
  • The Flyweight pattern was used by the designers
    of Java for better performance.
  • Equivalent Strings in Java are flyweighted
  • If a program creates two String references that
    refer to the String hello, both reference
    variables will refer to the same String object

5
Strings are Flyweights
  • This saves time and memory by avoiding creating a
    second identical hello object
  • It is legal and safe to do this, because Strings
    are immutable in Javathey cannot be directly
    modified.
  • There is no danger when many references refer to
    the same String the object cannot be modified

6
Example 2 BorderFactory
  • Sun documents BorderFactory with
  • "Wherever possible, this factory will hand out
    references to shared Border instances"
  • "If you need a reference to a border say,
    because you want to use it in multiple components
    you can save it in a variable of type Border"

7
Implementing Flyweight
  • With the existing class, add a Factory
  • Add a static method to that factory with the
    following algorithm
  • if flyweight exists
  • return existing flyweight
  • else
  • construct new flyweight
  • add to pool of flyweights
  • return new flyweight

8
Example A Character Factory
  • public class CharacterFactory
  •   private HashMap characters new
    HashMap()public Character GetCharacter(char
    key) // "lazy initialization" (could have
    pooled all) Character charObj(Character)charac
    ters.get(key) if(charObj null) // does not
    exist  // need to construct a new one for
    key    if(key 'A')
  • charObj new CharacterA()    else
    if(key 'B')
  • charObj new CharacterA() //
  •      characters.out( ""key, charObj )return
    character

9
One Flyweight
  • public class CharacterA  public CharacterA(
    )      this.symbol 'A'    this.height
    100    this.width 120    this.ascent
    70    this.descent 0 
  • public void Draw(int pointSize) //

10
Restricting Object Creation Again
  • Sometimes only need one instance
  • a particular hardware interface or serial port
  • instance of a collection used by different
    objects
  • Can use Singleton Design Pattern
  • Motivation
  • A part of the program decides it needs a resource
  • Don't need to worry if others are sharing it
  • Can keep sharing code in one place

11
Singleton Pattern
  • Singleton ensures that a class has only one
    instance and provides a global point of access to
    it
  • Takes responsibility of managing that instance
    away from the programmer
  • makes it impossible to construct more instances

12
Implementing Singleton in Java
  • Make constructor(s) private so they can not be
    called from outside
  • Declare a single static private instance of the
    class.
  • Write a public getInstance() or similar method
    that allows access to the single instance.

13
Structural Form
  • class Singleton
  • private static Singleton instance
  • private Singleton()
  • public static Singleton getInstance()
  • // Uses "Lazy initialization"
  • if( instance null )
  • instance new Singleton()
  • return instance

14
Singleton Example
  • public class RandomGenerator
  • private static RandomGenerator gen
  • private RandomGenerator()
  • public static RandomGenerator getInstance()
  • if (gen null)
  • gen new RandomGenerator()
  • return gen
  • public double nextNumber()
  • return Math.random()

15
UML View
16
Singleton Example
  • public class PlayerList
  • private static ArrayList players
  • private PlayerList()
  • public static PlayerList getInstance()
  • if (players null)
  • players new PlayerList()
  • players.add(new Player("one", 100))
  • players.add(new Player("two", 200))
  • players.add(new Player("tre", 300))
  • return players
  • public Player getPlayer(int index)
  • return (Player)player.get(index)
Write a Comment
User Comments (0)
About PowerShow.com