Common Sky (Flyweight)

zhaozj2021-02-16  52

Many cases. A large number of similar situations make us have to use less poor memory for duplicate data. So

The situation is very bad. In this case. We must find a good way. Solidify those possible data

This is a reasonable thing. Ok .. We should take the following aspects. First, find out the object

The common place. This part is to share. Then you need to package into a class. So the rest cannot be shared.

I have to encapsulate to another class. In other words, it is to expand the particle size of the object. Let Flyweight control the original

Maybe multiple small particle size. Sharing the shared place. Form a unified management. And some parts must be

Dynamic production. Obviously it is to use Factory mode. This is how many of Flyweight uses the factory model.

And the factory maintains a shared pool. The role of this pool is to make it. The client can enrich the perimeter

Read shared data. If the customer searches the data. Then return a shared object. If not found. So

Add to this shared pool .Abstract class flyweight {Abstract public void execute (OtherData Odata);

Let's consider a possible problem. If a publisher is like this.

Book No. Author Publish date price

Then there can be a lot of repetitions in addition to the book number. In order to make the memory reduction investment we can consider

Flyweight mode.

Public class book {private int booknum; private string author; private float price; public void execute () {}}

It can be seen that most of the data in the Book may be repeated. So waste a lot of memory. So let's carry out

Sharing management.

Public class authorflyweight: flyweight {private string author; public string author {get {keturn author;}}}}

Public class pubdateflyweight: flyweight {private DateTime Pubdate; public datetime pubdate;} set {pubdate = value;}}}

Public class priceflyweight: flyweight {prich;}}}}}}}

// Abstract factory Abstract Class FlyweightFactory {// Store data protecthatable pool = new hashtable (); public abstract flyweight getflyweight (String key) {}}}

public class AuthorFactory: FlyweightFactory {public override Flyweight getFlyweight (string key) {AuthorFlyWeight reValue; reValue = (AuthorFlyWeight) pool.get (key); if (reValue == null) {reValue = new AuthorFlyWeight (); pool.put (key REVALUE;} Return Revale;

}

转载请注明原文地址:https://www.9cbs.com/read-27427.html

New Post(0)