Easy to learn design mode Reading Notes (7), design mode Reading Notes

Source: Internet
Author: User

Easy to learn design mode Reading Notes (7), design mode Reading Notes


Proxy Mode

1. Example of System Log Record: Add logs to the business logic in the system
(1): the simplest and most direct approach

Public class Test {private Logger logger = Loger. getLogger (this. getClass (). getName (); public void doLgic (String name) {logger. log (name + "start business logic processing... "); // business logic processing related program System. out. println ("business logic processing related programs"); logger. log (name + "end of business logic processing... ") ;}} the business logic of other classes also needs to be logged: public class Test1 {private Logger logger = Loger. getLogger (this. getClass (). getName (); public void doLgic (String name) {logger. log (name + "start business logic processing... "); // business logic processing related program System. out. println ("business logic processing related programs"); logger. log (name + "end of business logic processing... ");}}


 

To record logs, these two classes add log processing methods to the code that processes the business logic,
These methods are similar to each other and confuse the single responsibility of the class. What can be done to solve this problem?

(2): static proxy

Define an interface class with business logic: public interface Test {public void doLogic (String name);} specific logic processing class: public class TestImpl implements Test {public void doLogic () {System. out. println ("business logic processing") ;}} log agent class: public class TestProxy implements Test {private Logger logger = Loger. getLogger (this. getClass (). getName (); private Test test; public TestProxy (Test test) {this. test = test;} public void doLogic (String name) {logger. log ("start business logic processing... "); // test. doLogic (name); logger. log ("the end of business logic processing... ") ;}} call the proxy class to implement log output: public class Client {public static void main (String [] args) {TestProxy testProxy = new TestProxy (new TestImpl ()); testProxy. doLogic ("John ");}}

The Service Logic proxy class is used to call specific business logic, and the log record is also realized,
In addition, the log records are separated from the business logic, which is a static proxy.
There is a problem: each method requires a proxy class. If every class in the system requires logging,
There are many proxies. The solution is dynamic proxy.

(3): Dynamic proxy

JDK-based Dynamic Proxy:

Public LogProxy implements InvocationHandler {private Logger logger = Loger. getLogger (this. getClass (). getName (); private Object delegate; // proxy Object public Object bind (Object delegate) {this. delegate = delegate; return Proxy. newProxyInstance (delegate. getClass (). getClassLoader (), delegate. getClass (). getInterfaces (), this);} public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {Object result = null; try {logger. log ("start business logic processing... "); // business logic processing result = method. invoke (delegate, args); logger. log ("the end of business logic processing... ");} catch (Exception e) {logger. log (e. toString ();} return result;} public class Client {public static void main (String [] args) {LogProxy logProxy = new LogProxy (); Test test = (Test) logProxy. bind (new TestImpl (); test. doLogin ("5th ");}}

JDK dynamic proxies can only be used for interface proxies. To implement dynamic proxies for classes, you can use cglib dynamic proxies or other class libraries.

Interface-oriented programming is generally recommended.

2. Introduction to dynamic proxy mode:
[Definition]
The proxy mode provides a proxy object for an object.
The reference of the original object enables the proxy object to act as an intermediary between the original object and the client.

Principle]
The proxy mode consists of three parts:
Abstract The target class, specific target class and proxy class.

[Time to use]
When the system requires additional control over an object.


7 Reading Notes uploaded by celebrities (about 100 words)

Post-Reading celebrity biography
I have been deeply touched by Roman Roland's celebrity biography. Roman Roland is a famous French writer in the 20th century. His work is a strong spiritual pillar of people. The story of "Biography of celebrities" is written in the stories of Beethoven, Picasso, and thosteth. They have gone through various hardships but have not given in to fate. In the last second of their lives, they are still indomitable, eventually became a great man. This also tells us the truth: difficulties and setbacks are the best temper for fate and life!
Among the three great men, I was most impressed by Beethoven. He is passionate about music and has successively created many excellent works. His work is profound, brilliant, and spectacular. But the disaster has come to him relentlessly. In 1802, he realized that his hearing impairment could not be cured and would deteriorate quickly. That means he may no longer be able to create it! What a big blow! However, he is able to fight hard against fate. This may be the reason why he can write so many immortal works later. This kind of spirit that never compromises on life is exactly what we should learn. This great and unyielding soul shines in the dark world and gives life hope.
After reading the story of Beethoven, I think of Helen Keller, a person with the same strong perseverance. Her life is also unfortunate. Her childhood illness turned her into a blind, deaf, and dumb little girl. In her world, there is no color, no sound, and no way to express your thoughts. But the injustice of fate did not make her lose hope. She was eager to understand everything, so she redouble her efforts and eventually became one of the most influential figures in the 19th century.
Beethoven and Helen Keller encounter different hardships in their lives, but they share the same spirit-never give in to fate! This not only caused my admiration, but also caused my deep thinking. As a middle school student in the 21st century, the competition in society is more intense. As we have more and more homework and more learning difficulties, we are faced with heavy difficulties and pressure, do we also need this spirit? Yes! In the face of difficulties, when our academic performance drops, let us "use the light of Beethoven to Polish our eyes again ." Face setbacks with a firm belief and fulfill the ideal with tenacious perseverance
2
The story of celebrities was written by French writer Roland. The book contains famous figures on sanghhe. The first one is the German composer: Beethoven; the other is the genius sculptor of Italy: Picasso; the last one is the famous Russian writer: thostez.
I was most touched by the story of Beethoven in this famous biography. Beethoven is a music genius. His talent has been discovered by his father for a long time. Unfortunately, Beethoven's father is not a good father. He trains Beethoven every day, regardless of his mood, he trained him hard, and sometimes even put Beethoven and a violin together into a room to close it. It was a whole day, and he was forced to learn music with violence. Beethoven's childhood was miserable. His mother died when he was sixteen years old, and his father turned into a squandering drunkard. These misfortunes put them together on Beethoven's head, with deep scars in his heart, which led to his bad temper and strange. However, Beethoven was not so immersed. He put all his energy into his favorite music career. Thanks to his talent and diligence, he soon became famous. When He indulge in the happiness that music brings to him, the unfortunate thing happened again: his ears are deaf. For a musician, the most important thing is his ears. A great musician like Beethoven who makes a living by music has lost his ears. This blow is unacceptable to ordinary people.
Beethoven's life was miserable and difficult, but why did he still succeed? Why can't a normal person do anything he can? This caused me to think deeply. I believe that Beethoven succeeded because of his perseverance and struggle spirit. He was not afraid of the difficulties. This is the secret to his success.
In our daily life, when we encounter difficulties, we often think of asking for help from others, rather than directly facing the difficulties. We are determined to solve them. However, because of his strange temper, no one is willing to be friends with him. Therefore, in the face of difficulties, he can only fight with one another. Despite being lonely, I learned what others cannot learn: As long as I give myself Unlimited Courage, I can defeat any terrible enemy.
Finally, I hope that people all over the world can remember Beethoven, like Beethoven... the rest of the article>

I will give 200 points for 7 Reading Notes.

Andersen fairy tale Reading Notes (1)
Last Christmas, Grandpa Christmas quietly sent me a special gift on that day, the book "Andersen's Fairy Tale. When talking about the fairy tale of Andersen, I am afraid no one in the world will know it. There are funny stories about the emperor's new clothes, the beetle, and the daughter of the sea. Among them, my favorite story is "the little girl who buys matches".

The first story "the little girl who buys matches" deeply attracted me. The story tells me that on a cold night, a little girl sold matches in her mother's shoes, the pair of shoes were too big. When she was on the road, she was lost by the speeding carriage, and the other shoes she gave to a poor little boy, in the cold night of snow float, the girl walked barefoot in the street to sell matches. It was so cold that no one could sell matches. She could not sell the matches, for fear that her father would scold her, she dared not go home ...... You know, it's Christmas day. Rich people are eating a rich Christmas dinner. It's really attractive. Unfortunately, the little girl can only smell it and cannot eat it, cold and hungry, she curled up under the corner and ignited the first match, which made her feel warm, as if she was sitting by the fire, and soon the match was destroyed, the fire disappeared, only to see the snow floating, she lit the second match, saw a rich Christmas dinner, when she was about to eat, the match was gone, the big meal was gone, the belly was flat, and she lit the third match. She saw a beautifully decorated Christmas tree, but when she reached out her hands and wanted to hug the tree, the match went out again, the tree was gone, and she lit the fourth match. She saw her kind grandmother. She was afraid her grandmother would soon disappear like a big meal and a Christmas tree, and she lit all the matches, she saw her grandmother in the light of a match. Her grandmother put her in her arms and took her to a cold, hunger-free world ......

After reading this story, I think the girl's life is miserable. If someone bought her match, if she had a father of charity, if she could meet someone who was as kind as she was ...... Then she will not be taken away by her grandmother. Seeing the match girl reminds me of the tsunami that occurred in Indonesia some days ago, which made many children as pitiful as the little girl, but the orphans who lost their loved ones in the tsunami, unlike the girls, are lucky to get assistance from all over the world, I am several thousand times happier than them. I have teachers who care about me, Mom and Dad who love me, and grandparents. I never worry that hunger will freeze, I should cherish the good times, the loved ones around me, treat everyone kindly and sincerely, and do my best to help those who need help, so that love will shine around me, make the world full of love.

Andersen fairy tale Reading Notes (2)
Fairy tales are a book that every child must read. The most famous ones in the fairy tale world are Andersen's Fairy Tales and greenfairy tales. However, you can't see greenfairy tale before you go to bed! Therefore, "Andersen's Fairy Tale" became my "best reading ".

Andersen was a great realistic writer and poet in Denmark in the 19th century. He was born in, and his father was a shosser. He died very early, and his childhood Andersen suffered from poverty, but he loved writing from an early age. In 1827, he published his first poem and his first work at the University of Copenhagen. He has written more than 160 fairy tales and stories in his life. His work has been translated into more than 80 languages.

In the fairy tale of Andersen, Andersen wrote the following figures to life, including the little girl, the Princess peas, the little cross and the big cross, the strong xibing, the Snow White, the little girl who sells matches, and the stupid Hans. Reading a fairy tale, as if they came to their side, watching the Emperor wear "New Clothes", watching the ugly duckling be bullied by animals, watching grand Krause kill his grandmother ......

The little girl who buys matches is my favorite story, which attracts me deeply. The story tells that in the cold snow wind, the girl walked barefoot in the street to sell matches. It was so cold that no one came to buy matches. She could not sell one of them, she dared not go home, afraid her father would scold her ,...... You know, it was Christmas Day. The rich people were eating a rich Christmas dinner, while the cold and hungry girl curled up in the corner and lit a match to warm up. The first match lit up, A trace of warmth made her feel like sitting by a warm stove ,...... remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.