Definition and characteristics of sharing mode
Definition of Flyweight mode:
Sharing technology is used to effectively support the reuse of a large number of fine-grained objects. By sharing existing objects, it can greatly reduce the number of objects to be created and avoid the overhead of a large number of similar classes, so as to improve the utilization of system resources.
The main advantages of Xiangyuan mode are:
Only one copy of the same object is saved, which reduces the number of objects in the system, thus reducing the pressure on memory caused by fine-grained objects in the system.
Its main disadvantages are:
In order for objects to be shared, some states that cannot be shared need to be externalized, which will increase the complexity of the program.
Reading the external state of the shared mode will make the running time slightly longer.
Structure and implementation of shared meta model
The definition of shared meta pattern puts forward two requirements, fine granularity and shared object. Because fine granularity is required, it is inevitable that there will be a large number of objects with similar properties. At this time, we will divide the information of these objects into two parts: internal state and external state.
Internal state refers to the information shared by the object, which is stored in the shared meta information and does not change with the change of the environment;
External state refers to a mark that an object can rely on. It changes with the change of environment and cannot be shared.
Pattern structure
Abstract shared element role (Flyweight): it is the base class of all specific shared element classes. It is the public interface to be implemented by the specific shared element specification. The external state of non shared elements is passed in the form of parameters through methods.
Concrete Flyweight role: implement the interface specified in the abstract flyweight role.
Unsharable flyweight role: it is an external state that cannot be shared. It is injected into the relevant methods of specific shared elements in the form of parameters.
Flyweight Factory role: it is responsible for creating and managing a membership role. When a client object requests a membership object, the membership factory checks whether there is a qualified membership object in the system. If so, it will provide it to the client; if not, it will create a new membership object.
Implementation of pattern
public class FlyweightPattern { public static void main(String[] args) { FlyweightFactory factory = new FlyweightFactory(); Flyweight f01 = factory.getFlyweight("a"); Flyweight f02 = factory.getFlyweight("a"); Flyweight f03 = factory.getFlyweight("a"); Flyweight f11 = factory.getFlyweight("b"); Flyweight f12 = factory.getFlyweight("b"); f01.operation(new UnsharedConcreteFlyweight("First call a. ")); f02.operation(new UnsharedConcreteFlyweight("Second call a. ")); f03.operation(new UnsharedConcreteFlyweight("3rd call a. ")); f11.operation(new UnsharedConcreteFlyweight("First call b. ")); f12.operation(new UnsharedConcreteFlyweight("Second call b. ")); } } //Non meta role class UnsharedConcreteFlyweight { private String info; UnsharedConcreteFlyweight(String info) { this.info = info; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } } //Abstract meta role interface Flyweight { public void operation(UnsharedConcreteFlyweight state); } //Specific meta role class ConcreteFlyweight implements Flyweight { private String key; ConcreteFlyweight(String key) { this.key = key; System.out.println("Specific share yuan" + key + "Created!"); } public void operation(UnsharedConcreteFlyweight outState) { System.out.print("Specific share yuan" + key + "Called,"); System.out.println("Non shared meta information is:" + outState.getInfo()); } } //Enjoy yuan factory role class FlyweightFactory { private HashMap<String, Flyweight> flyweights = new HashMap<String, Flyweight>(); public Flyweight getFlyweight(String key) { Flyweight flyweight = (Flyweight) flyweights.get(key); if (flyweight != null) { System.out.println("Specific share yuan" + key + "It already exists and was successfully obtained!"); } else { flyweight = new ConcreteFlyweight(key); flyweights.put(key, flyweight); } return flyweight; } }
example
public interface ChessPieces {//Abstract meta role public void operation(Type type); } public class Type {//Non meta role private String info; public Type(String info) { this.info = info; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } } public class WhitePieces implements ChessPieces {//Specific meta role @Override public void operation(Type type) { System.out.print("Sharing meta information: This is Baizi"); System.out.println("Unshared meta information:"+type.getInfo()); } public class BlackPieces implements ChessPieces {//Specific meta role @Override public void operation(Type type) { // TODO Auto-generated method stub System.out.print("Shared meta information: This is a sunspot"); System.out.println("Unshared meta information:"+type.getInfo()); } } public class WeiqiFactory {//Xiangyuan role factory private ArrayList<ChessPieces> qz; public WeiqiFactory() { qz = new ArrayList<ChessPieces>(); ChessPieces w = new WhitePieces(); qz.add(w); ChessPieces b = new BlackPieces(); qz.add(b); } public ChessPieces getChessPieces(String ty) { if (ty.equalsIgnoreCase("w")) { return (ChessPieces) qz.get(0); } else if (ty.equalsIgnoreCase("b")) { return (ChessPieces) qz.get(1); } else { return null; } } } public class Chessboard {//Test class public static void main(String[] args) { WeiqiFactory w1=new WeiqiFactory(); ChessPieces ches=w1.getChessPieces("w"); ches.operation(new Type("White jade")); } }