[Study Notes in headfirst design mode] Free pick up in 15 yuan Mode

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

This model is not detailed in this book. In fact, this model is more or lessSingleton mode + simple factory Mode. In Java, the string object is managed in the enjoy meta mode -- Java stores all fixed strings in a constant pool, only one copy is saved for the same string. The benefits of this method are obvious, and the memory usage and object creation overhead are reduced. This mode applies to the creation of common fine-grained shared objects.

Let's imagine a problem scenario: Time class used to express time in an appointment Program

  1: public class Time {
  2:    private byte hour;
  3:    private byte minute;
  4: 
  5:    public Time(byte hour, byte minute) {
  6:       this.hour = hour;
  7:       this.minute = minute;
  8:    }
  9: 
 10:    public byte hour() {
 11:       return hour;
 12:    }
 13: 
 14:    public byte minute() {
 15:       return minute;
 16:    }
 17: 
 18:    public String toString() {
 19:       return hour + ":" + minute;
 20:    }
 21: }
 22: 

This class may be used many times in a program. If the hour and minute are the same, the memory objects generated by this class can be shared without affecting the function. In particular, in this application scenario, the reservation time is usually not a positive or half point, and may be at most 15 or 45 minutes, if you can share the memory, you will obviously save a lot of memory objects.

This shared memory operation is provided by a simple factory method. In this class, a hashmap is maintained and functions similar to the memory pool are completed, if there is no time in hashmap, a new time object will be created and put into it for future maintenance. If there is a time object, it will be returned directly:

  1: public class TimeFactory {
  2:    private static Map
   
     times = 
   new HashMap
   
    ();
   </STRING,TIME></STRING,TIME>
  3:    public static Time create(byte hour, byte minute) {
  4:       String key = hour + ":" + minute;
  5:       Time time = times.get(key);
  6:       if (time == null) {
  7:          time = new Time(hour, minute);
  8:          times.put(key, time);
  9:       }
 10:       return time;
 11:    }
 12: }

Finally, let's take a look at the UML diagram:

In this UML diagram, flywright further abstracts the unsharedconcreteflyweight class and the concreteflyweight class inherit from the flyweight class (which defines the abstract method operation ), the difference between the two classes is that flyweightfactory only maintains concreteflyweight, but does not participate in unsharedconcreteflyweight. In fact, unsharedconcreteflyweight is not a required class in the metadata mode. Here it is just an abstract need. It is created using a common new operation.

Http://v.youku.com/v_show/id_XMjU2Njk0NzE2.html online video

References: http://www.developer.com/tech/article.php/10923_3677501_2/Working-with-Design-Patterns-Flyweight.htm

 

Author: gnuhpc

Source: http://www.cnblogs.com/gnuhpc/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.