First, Mode analysis
Flyweight in boxing is the most lightweight, that is, "The fly level" or "rainfall level", where the choice to use the "enjoy meta-mode" of the free translation, because it is more reflective of the intention of the model. The enjoy meta mode is the structure mode of the object. The enjoy meta mode efficiently supports a large number of fine-grained objects in a shared manner.
enjoy meta mode : The main purpose is to save the shared object in a cached manner when creating the object, and to create the external object separately
Mode essentials:
1, enjoy the meta-model of the object is divided into two parts: the general part and the personalization part, the common part is that each object is consistent or multiple objects can be shared part, the personalization part refers to the difference is larger, each class is different parts
2, the general part of the abstraction of this mode is the enjoyment of meta-objects
3, enjoy meta-objects are generally created and saved through a
4, to enjoy the yuan factory based on the number of objects to achieve a singleton or multi-sample mode to create the enjoyment of meta-objects
5, the enjoyment meta-object can be pre-set in the enjoy meta-factory, you can also create a collection, each time you create an object, see if the object exists, does not exist to add
Second, the mode code
1. Create a meta-interface
Package Flyweight.patten; // enjoy meta interface Public Interface FlyWeight { publicvoid opertion ();}
2. Create a specific class of enjoyment
Package Flyweight.patten; Public class Implements FlyWeight { public String name; Public concreteflyweight (String name) { this.name=name; } @Override publicvoid opertion () { System.out.println ("execution-sharing class" ); }}
3. Create a meta factory
PackageFlyweight.patten;ImportJava.util.HashMap;ImportJava.util.Map;//enjoy meta-factory to provide the client with a meta-class Public classFlyweightfactory {//use an internal map to ensure that the object is created only once PrivateMap<string,flyweight> map=NewHashmap<string,flyweight>(); PublicFlyWeight getflyweight (String name) {FlyWeight FlyWeight=map.get (name); if(Map.get (name) = =NULL|| Map.get (name). Equals ("") ) {FlyWeight=Newconcreteflyweight (name); Map.put (name, flyWeight); } returnFlyWeight; }}
5, the Client
PackageFlyweight.patten; Public classClient { Public Static voidMain (string[] args) {Flyweightfactory factory=Newflyweightfactory (); FlyWeight flyWeight1=factory.getflyweight ("Zhang San"); FlyWeight flyWeight2=factory.getflyweight ("John Doe"); FlyWeight FLYWEIGHT3=factory.getflyweight ("Zhang San"); System.out.println (flyWeight1==flyWeight2); System.out.println (flyWeight1==FLYWEIGHT3); }}
6, execution results, you can see, if the name is the same, each reference to the same object is the same, in line with the multi-sample mode
false true
Note: The standard code for this mode is simply the "enjoy meta" mode, which does not fully conform to the above-described pattern, because the object parameters he created are all of the contents of the content, so we write a full element of the module code in the following instance.
Third, the application scenario
The application of the meta-mode is quite extensive, similar to the Foreign Key Association of the database, so we use the most common bank card in the work example:
Customer account attributes include: Bank card type, bank card name, bank card number, balance, customer name and other factors. When you create a card object, there are only a few types and names for each bank, so you create a privilege object to save it.
Four, Mode code
1, create a bank card class, that is, to enjoy the meta-object
Packageflyweight.example;/*** Card Type *@authorLenovo **/ Public classCard {PrivateString Cardtype; PrivateString Cardname; PublicString Getcardname () {returnCardname; } Public voidsetcardname (String cardname) { This. Cardname =Cardname; } PublicString Getcardtype () {returnCardtype; } Public voidSetcardtype (String cardtype) { This. Cardtype =Cardtype; } PublicCard (String cardtype,string cardname) { This. cardtype=Cardtype; This. cardname=Cardname; } @Override PublicString toString () {return"Cardtype [cardtype=" + Cardtype + ", cardname=" + Cardname + "]"; } }
2. Create an Account interface
Package flyweight.example; /** @author*/Publicinterface countinterface { /** * Show Card contents * /public void Show ();
3. Create Account Class
Packageflyweight.example;ImportJava.math.BigDecimal;/*** Specific account attributes *@authorLenovo **/ Public classConcretecountImplementsCountinterface {PrivateString Cardno; PrivateBigDecimal balance; PrivateCard Card; /*** Create user objects based on parameters *@paramCardno *@paramBalance *@paramCardtype*/ PublicConcretecount (String Cardno, BigDecimal balance, card) { This. Cardno =Cardno; This. Balance =balance; This. Card =Card; } @Override PublicString toString () {return"Concretecount [cardno=" + Cardno + ", balance=" + Balance + ", card=" + card + "]"; } @Override Public voidShow () {System.out.println ( This. toString ()); }}
5, the creation of the Yuan factory, the Yuan factory to return to customer account instances, in the creation of customer accounts, the first to determine whether the card attributes exist, the existence of directly from the enjoyment meta-object to obtain card properties, otherwise create a new card
Packageflyweight.example;ImportJava.math.BigDecimal;ImportJava.util.HashMap;ImportJava.util.Map;/*** Enjoy meta factory, mainly used to create objects *@authorLenovo **/ Public classCountfactory {//save a map of the sharing meta objectMap<string,card> cardtypemap=NewHashmap<string, card>(); Publicconcretecount Getconcretecount (String cardno, BigDecimal balance,string cardtype,string cardname) {Car D Card=NULL; //determines whether the privilege object already exists, if it exists, is used, does not exist, and creates additional if(Cardtypemap.get (cardtype+cardname)! =NULL) {Card=cardtypemap.get (cardtype+cardname); }Else{card=NewCard (Cardtype, cardname); Cardtypemap.put (Cardtype+cardname, card); } return NewConcretecount (Cardno, balance, card); }}
5, the Client
Packageflyweight.example;ImportJava.math.BigDecimal; Public classCardtest {/*** Test to enjoy meta mode *@paramargs*/ Public Static voidMain (string[] args) {Countfactory factory=Newcountfactory (); Concretecount Count=factory.getconcretecount ("001",NewBigDecimal (100), "1", "Credit card"); Concretecount Count2=factory.getconcretecount ("002",NewBigDecimal (200), "2", "Debit card"); Concretecount Count3=factory.getconcretecount ("003",NewBigDecimal (300), "1", "Credit card"); Concretecount Count4=factory.getconcretecount ("004",NewBigDecimal (400), "2", "Debit card"); Count.show (); Count2.show (); Count3.show (); Count4.show (); }}
6. Results
Concretecount [cardno=001, balance=100, Card=cardtype [cardtype=1, cardname= credit card]]concretecount [CardNo= 002, balance=200, Card=cardtype [cardtype=2, cardname= debit card]]concretecount [Cardno=003, balance=300, Card=CardType [Cardtype=1, cardname= credit card]] Concretecount [Cardno=004, balance=400, Card=cardtype [cardtype=2, cardname= Debit]]
Five, the analysis
As said in the third part, the most of the mode of sharing is in the form of database foreign key in the system, so the use of the system to enjoy the idea of many, but the use of the standard mode of using the object is still relatively small, so the design mode is to pay attention to thought.
[design mode at work] Enjoy meta-mode mode flyweight