| 
				  
				  
				  
		
				  
				  
				  
				   | 
                      
                          
                            
                               
                              |  Design Pattern | 
                             
                                
                                   | 
                                 
                                
                                  
                                      
                                        The UML diagram for this pattern is like this: 
                                           
                                           
                                           
                                          This UML diagram shows that, Flyweights are typically instantiated by a
                                           flyweight factory that creates a limited number of flyweights and sends them out, one at a time to its
                                          clients. 
                                           
                                          Clients don’t instantiate  flyweights
                                          directly; instead they get them from a Flyweight Factory. The factory first
                                          checks to see if it has a flyweight that fits specific criteria; if so, the factory returns a reference to the flyweight. If the factory can’t locate a flyweight for the specified criteria, it instantiates one, adds it to the pool, and returns it to the client. 
                                           
                                          Flyweight declares an interface through which flyweights can receive and act on extrinsic state.
                                           ConcreteFlyweight implements the Flyweight interface and adds storage for intrinsic state, if any in order to share an object.
                                           FlyweightFactory creates and manages flyweight objects. We can understand the flyweight pattern better
                                          using a simple example. Suppose, you want to show a file system with folders to show the
                                          directories or subdirectories, then you don’t need to load all the files or directories at one loading time. You may show the upper level folders first. If the user clicks a folder, then load its subdirectories and files. The shared trigger is mouse-clicked. The composite pattern may be combined to define the flyweight system. | 
                                          | 
                                        Let us see an example of flyweight
                                          pattern. 
                                          FlyweightIntr.java
                                          interface
                                          FlyweightIntr { 
                                          public String getName(); 
                                          public String getAddress(); 
                                          } 
                                          The FlyweightIntr is an interface thatreturns the name and address of theemployees based on their specific
                                          criteria“division”. 
                                          FlyweightClient.java 
                                          import java.util.HashMap; 
                                          import java.util.StringTokenizer; 
                                          import java.util.Vector; 
                                          public class FlyweightClient { 
                                          public static void main(String[] args) throws 
                                          Exception { 
                                          Vector empList = store(); 
                                          FlyweightFactory factory = 
                                          FlyweightFactory.getInstance(); 
                                          for (int i = 0; i < empList.size(); i++) { 
                                          StringTokenizer st = new StringTokenizer(); 
                                          String division = st.nextToken(); 
                                          FlyweightIntr flyweight = 
                                          factory.getFlyweight(division); 
                                          // associate the flyweight 
                                          // with the extrinsic data object. 
                                          VCard card = new VCard(name, flyweight); 
                                          card.print(); 
                                          } 
                                          } 
                                          private static Vector store() { 
                                          Vector v = new Vector(); 
                                          v.add(“North”); 
                                          v.add(“South”); 
                                          v.add(“North”); 
                                          return v; 
                                          } 
                                          }
                                           
                                          
                                         | 
                                       
                                      
                                        |   | 
                                          | 
                                          | 
                                       
                                    | 
                                 
                                
                                   Nov 2007 | Java Jazz Up | 41  | 
                                 
                                
                                  
                                      
                                        |   | 
                                       
                                      
                                         | 
                                       
                                      
                                        |   | 
                                       
                                      
                                        
| View All Topics | 
 
| All Pages of this Issue | 
 
  
Pages:
1,
 2,
3,
 4,
 5,
 6, 
7, 
8, 
9, 
10, 
11,
 12,
 13, 
 14,
  15,
   16, 
   17,
    18,
	 19,
	 20,
	  21,
	   22,
	    23, 
		24,
		 25,
		  26, 
		  27,
		   28,
		    29,
			  
			  
			 30,
			 
				
	31,
	 32, 
	 33,
	  34, 
	  35, 
	  36,
	   37, 
	   38,
	    39, 
	    40, 
	    41, 
	    42,
	     43, 
		44, 
		45,
		 46, 
		47, 
		48, 
		49,
		 50,
		  51,
		  52, 
		  53 ,
		   54, 
		  55,
		   56, 
		  57,
		    
		   
		  58,
		   59, 
		    
		    60,
		    61,
		    62,
		    63 ,  
		    64,
		    65 ,
		    66 ,
		    67 ,
		    68 ,
		    69   Download PDF  | 
   
 
        | 
                                       
                                    | 
                                 
                              | 
                           
                        | 
                      
				  
				  
				  
				  
				   | 
                     
                  | 
               
              
                |   | 
               
              
                |   | 
               
            |