1. Definitions
Shared meta mode is a structural mode. If an object instance is immutable once it is created, it is not necessary to create the same instance repeatedly. Just return a shared instance to the caller directly, which not only saves memory, but also reduces the process of creating objects and improves the running speed.
When using an Integer, when the value used is - 128 ~ 127, a new Integer will not be created, but the original one is used to avoid repeated creation.
We have many books in books. Books contain information such as author and title, but the books in the library are not for nothing. Of course, they are purchased by ourselves. If there is no book that students want to read, they need to buy it, that is, create a Book object, but if it exists, they don't need to buy it, but use it directly.
2. Structure diagram of Xiangyuan mode
The structure diagram of the sharing meta mode is very simple. BookFactory is equivalent to a factory, which is used for the production of books. At the same time, it can cache books and check whether the Book exists in the library through Map. It can only be created when it does not exist. There are books that use the cache directly, while Book is the entity class of books, including basic information such as Book name and author.

3. Implementation of sharing mode
In fact, the Book class simply defines the Book name and the author of the Book. The reason why the constructor uses protected is that the permission is only given to the calls of the same package and subclasses, that is, only to the BookFactory for production, and other callers are not allowed to directly use the operation of new Book(). If it is used, our sharing mode is useless.
public class Book { /** * title */ private String name; /** * author */ private String author; protected Book(String name, String author) { this.name = name; this.author = author; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + '}'; } }
The Map in BookFactory is used to cache instances. In fact, the size should be limited. Otherwise, if too many instances are stored, OOM will be generated. At the same time, BookFactory is defined as singleton mode, because the factory does not need multiple instances. If multiple instances are stored, the cached Book objects will be transferred to different bookfactories.
The existence of a book is determined by the title + author name. After all, there are too many books with the same title. The reason why createBook uses synchronized is that in the case of concurrency, duplicate Book instances will be created.
public class BookFactory { private static final BookFactory bookFactory = new BookFactory(); private Map<String,Book> cache = new HashMap(); private BookFactory() { } public static BookFactory getInstance(){ return bookFactory; } /** * Create a book and specify the title and author * @param bookName * @param author * @return */ public synchronized Book createBook(String bookName,String author){ String bookInfo = bookName + ":" + author; Book oldBook = cache.get(bookInfo); if(oldBook == null){ Book newBook = new Book(bookName, author); cache.put(bookInfo,newBook); return newBook; } return oldBook; } }
The actual test is as follows. When the name of the created book is the same as the author, the instance in the cache will not be generated at this time.
public class Test { public static void main(String[] args) { Book book = BookFactory.getInstance().createBook("Graphic design mode", "Jiechenghao"); System.out.println(book); Book book2 = BookFactory.getInstance().createBook("Graphic design mode", "Jiechenghao"); System.out.println(book2); System.out.println(book == book2); } }
Book{name='Graphic design mode', author='Jiechenghao'} Book{name='Graphic design mode', author='Jiechenghao'} true
Roles in meta mode
Flyweight (lightweight) refers to instances that need to be shared. It can be understood that if the creation of an object is too large or it is not necessary to create it frequently, the object is the flyweight role. In this paper, the Book class plays this role.
Flyweightfactory (lightweight factory) is used to create the Flyweight role. In this paper, the BookFactory class plays this role.
Client (requester), the person who needs to use the lightweight role, that is, the person who needs to use Book. In this paper, the Test class plays this role.
Reference: graphic design pattern
Code acquisition address: https://gitee.com/bughong/design-pattern