Flyweight Design Pattern: shared metadata Design mode, flyweightpattern
Is to use a class to complete a variety of tasks, do not create a new class every time.
I personally think that this design pattern is in C ++. It seems like a function can be used instead. It seems similar to calling this function repeatedly to complete the task and repeating the class.
However, since it is a design pattern, classes are used to complete the task. Java is not process-oriented, so this design pattern must be used.
Here I design a repository to save such classes and use them repeatedly when necessary.
Very simple design mode:
#include <stdio.h>class ReusedObject{public:ReusedObject() {}void finishTask(){puts("Use reused object to do task.");}};class Warehouse{ReusedObject *obj;public:Warehouse(){obj = new ReusedObject;}~Warehouse(){delete obj;}ReusedObject *getObject(){return obj;}};int main(){Warehouse wh;ReusedObject *obj = wh.getObject();obj->finishTask();return 0;}
Automatic scaling of design mode status Mode
Assume that the redirection code of A-> B is context. changeState (context. getStateB (). Now you only need to change it to context. changeState (context. getStateA1 ()). In A1, the code for status redirection is context. changeState (context. getStateB ()).
Difficulty arrangement of 23 Java Design Patterns
Creation Mode
1. FACTORY
Try to catch up with MM. Both the chicken wings of McDonald's and the chicken wings of KFC are what MM loves. Although the taste is different, whether you take MM to McDonald's or KFC, just say "four chicken wings" to the waiter. McDonald's and KFC are Factory for chicken wings production.
Factory model: the customer class and the factory class are separated. A consumer needs a product at any time and only requests from the factory. Consumers can accept new products without modification. The disadvantage is that the factory class must be modified when the product is modified. For example, how to create and provide data to clients.
Reply
2 Floor 2, BUILDER
MM is most fond of listening to the sentence "I Love You". When I see MM in different places, I want to be able to speak to her in their dialects. I have a multi-language translator, there is a button in each of the above languages. When you see MM, you only need to press the corresponding key to say "I love you" in the corresponding language, foreign MM can also be easily implemented, which is my "I love you" builder. (This must be better than the US military's translation server in Iraq)
Construction mode: separates the internal appearance of a product from the product generation process, so that a building process generates product objects with different internal appearances. The construction mode allows the product's internal appearance to change independently, so that the customer does not have to know the details of the product's internal composition. The construction model enforces a step-by-step construction process.
Third floor 3, FACTORY METHOD
Ask MM to go to McDonald's for a hamburger. Different MM has different tastes. Remember that it is an annoying thing. I usually use the Factory Method mode and take MM to the waiter, say "I want a hamburger". What kind of hamburger do I need? Just let MM talk to the waiter directly.
Factory method mode: the Core factory category is no longer responsible for the creation of all products. Instead, it submits the specific creation work to the subclass to become an abstract factory role, it is only responsible for providing the interface that must be implemented by a specific factory class, without touching the details of which product class should be instantiated.
4/F 4, PROTOTYPE
When chatting with MM via QQ, I must say some affectionate words. I have collected a lot of emotional words. If necessary, I just need to copy them and put them in QQ, this is my prototype. (100 yuan a copy, do you want)
Original Model mode: specify the type of the object to be created by giving a prototype object, and then create more objects of the same type by copying the prototype object. The original model mode allows you to dynamically add or remove product classes. The product class does not need to have any predefined level structure. The original model mode applies to any level structure. The disadvantage is that each class must have a clone method.
5/F, SINGLETON
I have 6 beautiful wives. Their Husbands are me. I am Sigleton, our family's husband. All they need to say is "husband, that's me (I had a dream just now. How can this be a good thing)
Singleton mode: Singleton mode ensures that a class has only one instance, and instantiate the instance and provides the singleton mode to the entire system. The Singleton mode can only be used when there is a real "Single Instance" demand.
6-floor Structural Model
6. ADAPTER
I met Sarah, a beautiful girl from Hong Kong at a friend's party. But I can't speak Cantonese, but she can't speak Mandarin, so I have to turn to my friend kent, as an Adapter between me and Sarah, Sarah and I can talk to each other (I don't know if he will play with me)
Adapter (transformer) mode: converts an interface of a class into another interface that the client expects, so that the two classes that cannot work together due to interface mismatch can work together. The adaptation class can return a suitable instance to the client based on the parameters.
7. BRIDGE
MM in the morning, good morning, MM in the evening, good evening, and MM in new clothes, nice clothes, and new hair styles, say your hair looks pretty. Don't ask me the question of "how to say MM has a new hairstyle in the morning". If I use BRIDGE to combine it myself, don't you... the rest is full>