Enjoy meta-mode-flyweight (Java implementation)

Source: Internet
Author: User
Tags ming

Enjoy meta mode-flyweight

The main purpose of the enjoy meta-mode is to share the shared pool, which can reduce the overhead of memory when there are many objects in the system, and is usually used in conjunction with the Factory mode.

The examples in this article are as follows:

Using the enjoy meta-mode: Xiao Ming wants to see the book of programming technology, on the bookshelf in the home to take, if there is a direct look, did not go to buy a copy, go home to see. After reading the shelves on the home, and then want to see the directly on the shelves to take, do not need to buy the same book.

Do not apply to enjoy the meta-mode: Xiao Ming want to read the programming technology books, go to the bookstore to buy a home to see, after the purchase finished, the book will not know where to go. Next time you want to see the time will not find, or to go to the bookstore to buy again ... And you have to spend money .... However, not a long memory, this time to buy, finished, and throw aside ... Next time you have to buy again ...

Instantiating an object in Java vs Xiao Ming buying a book

Instantiating an object in Java, like buying a book, objects in Java will lose their references as the scope exits, and then be tagged and recycled by the garbage collector.

The equivalent of Xiao Ming bought a book, the book was dropped on one side, the mother to clean up the house to the book for Garbage, it was thrown away. (By the mother to take away is the passive cause of Xiao Ming every time to buy a book, it is also possible that Xiao Ming actively read every time to buy a book, rich wayward ...)

in Java, objects are placed in a container for maintenance vs. Xiao Ming read the book and put it on the bookshelf. (the same color below indicates the same behavior and can be compared to each other)

In Java: After using a temporary instantiation of the object, if you want to reuse later, then you can put into a container (object manager), or more directly, such as stored in a hashmap, need to use the time from the inside of the direct fetch out. In this way HashMap holds a reference to the instantiated object, it will not be GC , so that the object can be resident memory , can be reused, no longer instantiate the same object .

Xiao Ming: After reading a book, put the book on the Bookshelf , so that the mother knew that this book is what Xiao Ming needs, will not treat it as garbage . So this book will always be at home , Xiaoming want to see when, to the home of the Bookshelf take can see, no need to buy the same book again.

Book Interface

A unified definition of the book. The book is shared in the lining in the meta-mode. Should be put on the shelf to be reused instead of buying it all over again.

/** * Uniform abstraction of books, books can be read */public interface book {    void Read ();}
Headfirstjavascript class
/** * <
Kotlininaction class
/** * <<kotlin Combat >> */public class Kotlininaction implements book {    @Override public    Void Read () {
   
    system.out.printf ("This is a <<kotlin combat >>. (The number of the book is:%s) \ n ", System.identityhashcode (This));}    }
   
Pythoncookbook class
/** * <<python Programming Manual >> */public class Pythoncookbook implements book {    @Override public    Void Read () {
   system.out.printf ("This is a <<python programming manual >>. (The number of the book is:%s) \ n ", System.identityhashcode (This));}    }
Bookfactory class
Import Java.util.enummap;import Java.util.map;public class Bookfactory {public enum BookType {PYTHON, JAVASCRI    PT, KOTLIN} private final map<booktype, book> Shelf;    Public bookfactory () {shelf = new enummap<> (booktype.class);     /** * If you want to read a book, get it through here. * If there is a bookshelf, take it from the bookshelf * if not in the Bookshelf, buy one from the bookstore and put it on the bookshelf. */Public book GetBook (booktype type) {Books book = sh        Elf.get (type);                     if (book = = null) {switch (type) {case python:book = new Pythoncookbook ();                    Shelf.put (type, book);                Break                    Case Javascript:book = new Headfirstjavascript ();                    Shelf.put (type, book);                Break                    Case Kotlin:book = new kotlininaction ();                    Shelf.put (type, book);                Break            Default:break; }        } return book; }}
Main

Run/Simulate a scene

public class Main {public    static void Main (string[] args) {        bookfactory bookfactory = new Bookfactory ();        Bookfactory.getbook (BookFactory.BookType.JAVASCRIPT). read ();        Bookfactory.getbook (BookFactory.BookType.JAVASCRIPT). read ();        Bookfactory.getbook (BookFactory.BookType.PYTHON). read ();        Bookfactory.getbook (BookFactory.BookType.PYTHON). read ();        Bookfactory.getbook (BookFactory.BookType.KOTLIN). read ();        Bookfactory.getbook (BookFactory.BookType.KOTLIN). read ();        Bookfactory.getbook (BookFactory.BookType.KOTLIN). read ();        The book is numbered, the instructions are reused, not every time you buy a new    }

The results are as follows:

If the object is not shared, that is, the non-enjoy meta-mode, then the <<kotlin combat >> three times the book number will be different, because every time you read this book, are new buy, will eventually lead to buy three <<kotlin actual combat >> this book  , the same book to buy three times more waste ah. The example of this article uses the enjoy meta-mode, took three times <<kotlin combat >> This book, each time the number is 1360875712, indicating from beginning to end are the same book, did not cause waste.

Enjoy meta-mode-flyweight (Java implementation)

Related Article

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.