*********************************************** Declaration *********************** *******************************
Original works, from the "Xiaofeng Moon XJ" blog, Welcome to reprint, please be sure to indicate the source (HTTP://BLOG.CSDN.NET/XIAOFENGCANYUEXJ).
Due to various reasons, there may be many shortcomings, welcome treatise!
*************** ******************************************************************************************
Before on many occasions have seen the design pattern of shadow, has been invested in the main time in the algorithm and data structure, and later found that the design pattern is really important. Sometimes the code is maintainable, reusable, and extensible, and it's more efficient than simple algorithms. So pick up Daniel book "Big Talk Design mode" at the same time refer to the online blog of Daniel, began my design mode of travel. Because of the usual programming with C + +, now because of future work needs in the re-learning Java, also practiced Java grammar. For the importance of design patterns, in the recent preparation for graduation lesson to experience particularly deep: many times not to write the relevant algorithm module, but always feel their code readability, reusability, extensibility is poor, not conducive to migration.
Let's introduce the enjoy meta-mode today.
1, Concept:
(Flyweight): Use sharing technology to effectively support a large number of fine-grained objects.
2. Realize:
1), abstract enjoy meta role (Flyweight)
is the base class for all the specific classes of the class that provide the public interfaces that need to be implemented.
2), the specific enjoy meta role (concreteflyweight) role
Implements the interface specified by the abstract-privileges meta-role. If there is an internal state, you must be responsible for providing storage space for the internal state. The internal state of the enjoyment meta-object must be independent of the environment in which the object is located, allowing the object to be shared within the system. Sometimes the specific enjoy meta-role is also called the simple specific enjoy meta role, because the compound to enjoy the meta-role is composed of simple and specific to the meta-role through the composite.
3), non-exclusive meta role (unsharableflyweight) role
The object represented is not shareable.
4), enjoy the meta-factory role (Flyweightfactoiy) role
Responsible for creating and managing the privileges role. This role must ensure that the sharing meta-object can be properly shared by the system. When a client object requests an object for a privilege, the enjoy Meta factory role needs to check to see if there is an eligible object in the system, and if it does, it should provide the existing one, and if there is not an appropriate element of the object in the system, The enjoy meta-factory role should create a new and appropriate sharing object.
5), Client (Main) role
This role also needs to store the external state of all the object's objects on its own.
3. Advantages:
Realize the sharing of computer resources, representing a large number of objects with a small number of object instances, preventing the frequent creation of objects resulting in reduced efficiency. Common thread pool, memory pool technology should be the shadow of meta-mode.
4. Disadvantages:
To differentiate between internal and external states can cause logical problems. For distributed or multithreaded mode, the enjoy meta-mode may cause synchronization problems.
5. Sample code:
Import java.util.hashtable;/** * Created by XUJINXJ on 2014/12/8. */abstract class flyweight{String m_strname; Public Flyweight (String tname) {m_strname=tname; } public abstract void Show (); Class Concreteflyweight extends flyweight{public concreteflyweight (String tname) {super (tname); public void Show () {System.out.println (super.m_strname); }}class Unshareconcreteflyweight extends flyweight{public unshareconcreteflyweight (String tname) {super (TN AME); public void Show () {System.out.println (m_strname); }}class flyweightfactory{Private Hashtable<string,flyweight>m_hashset =new hashtable<string,flyweight> (); Public Flyweight getflyweight (String tname) {if (!m_hashset.containskey (tname)) M_hashset.put (tname,n EW Concreteflyweight (tname)); System.out.println ("flyweightfactory ' s size =" +m_hashset.size ()); Return M_hashset.get (Tname);}}public class Main {public static void Main (String[]arg) {String str[]={"Xujin", "Xuchao", "Xujin"}; Flyweightfactory fac=new flyweightfactory (); Flyweight ins=fac.getflyweight (str[0]); Ins.show (); Ins=fac.getflyweight (str[1]); Ins.show (); Ins=fac.getflyweight (str[2]); Ins.show (); }}
Due to the
Limited time, in the process of writing a few references to some of the literature, thank you, at the same time, given the level of reasons, you inevitably have shortcomings, welcome treatise!
Design mode-Enjoy meta mode