In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to realize java facade mode". In daily operation, I believe many people have doubts about how to realize java facade mode. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize java facade mode". Next, please follow the editor to study!
We are all people with high IQ, and we have all written paper letters, such as writing love letters to our girlfriends. We all remember the process of writing a letter. First, write the contents of the letter, then write the envelope, and then put the letter in the envelope, seal it, and deliver it to the mailbox. This process is relatively simple, although simple, these four steps are to run, ah, letters are still troublesome, such as Valentine's Day. In order to look for a needle in a haystack and send love letters to ten girls, you have to run like this. Don't you be tired to death, let alone send an advertising letter or something. If you send 10 million e-mails all at once, isn't that the end? What are we going to do? Fortunately, now the post office has developed a new business. As long as you speed up the necessary information of the letter to me, I will send it to you, and I will do the four processes. Just leave the letter to me.
In this environment, the person who is most tired is the letter writer. there are four steps to send a letter out, and these four steps cannot be reversed. You can't put the letter in the envelope without writing a letter. The letter writer needs to know these four steps. But also know the order of these four steps. Horror, let's first see how this process shows.
First, take a look at the process interface of letter writing, and define the four steps of letter writing:
Package com.cbf4life.facade;* defines a process of writing a letter * / public interface LetterProcess {/ / the content to be written first public void writeContext (String context); / then write the envelope public void fillEnvelope (String address); / / put the letter in the envelope public void letterInotoEnvelope (); / / then mail it to public void sendLetter ();}
The concrete realization of the letter-writing process:
Package com.cbf4life.facade;/**.* letter specific implementation of * / public class LetterProcessImpl implements LetterProcess {/ / letter public void writeContext (String context) {System.out.println ("fill in the letter." + context);} / / fill in the necessary information on the envelope public void fillEnvelope (String address) {System.out.println ("fill in the recipient's address and name." + address) } / / put the letter in the envelope and seal public void letterInotoEnvelope () {System.out.println ("put the letter in the envelope");} / / put it in the mailbox and post public void sendLetter () {System.out.println ("Mail letter");}}
And then someone started writing letters in this process:
Package com.cbf4life.facade;/*** I started writing to friends * / public class Client {public static void main (String [] args) {/ / create a process for handling letters LetterProcess letterProcess = new LetterProcessImpl (); / / start writing letterProcess.writeContext ("Hello,It's me,do you know who I am? I'm your old lover. Irrelevant like to.... "); / / start to write the envelope letterProcess.fillEnvelope (" Happy Road No. 666 God Province,Heaven "); / / put the letter in the envelope and encapsulate the letterProcess.letterInotoEnvelope (); / / run to the post office to stuff the letter into the mailbox and deliver letterProcess.sendLetter ();}}
Then this process is a far cry from the requirement of high cohesion. You think, you need to know these four steps, and you also need to know the order of these four steps. Once something goes wrong, it is impossible for the letter to be mailed. How can we improve it? Take a look at the class diagram first:
、
This is the facade mode, which is relatively simple, and Sub System is more complex. In order to make it more convenient for callers to call, we encapsulate Sub System and add a facade. When Client is called, we just call the facade method directly. We do not need to know the specific implementation method and the related business order. Let's look at the change of the program. The LetterProcess interface and implementation class have not changed, but a ModenPostOffice class has been added. Our list of java programs is as follows
Package com.cbf4life.facade;public class ModenPostOffice {private LetterProcess letterProcess = new LetterProcessImpl (); / / letter, package, delivery, integration public void sendLetter (String context,String address) {/ / help you write letterProcess.writeContext (context); / / write the envelope letterProcess.fillEnvelope (address); / / put the letter in the envelope letterProcess.letterInotoEnvelope () / / Mail letterProcess.sendLetter ();}}
What does this category mean? that is to say, another service called Hell Road PostOffice (Hell Road Post Office) provides a new service. As long as customers give them the content and address of the letter, they will write, seal, and send the letter. This service is very popular when it is proposed. This is simple. Customers have reduced a lot of work. Let's see how customers call it. The list of Client.java programs is as follows:
Package com.cbf4life.facade;/*** I began to write to my friend * / public class Client {public static void main (String [] args) {/ modern post office with this service, the name of the post office is Hell Road ModenPostOffice hellRoadPostOffice = new ModenPostOffice (); / / as long as you give him the content of the letter and the address of the addressee, he will help you finish a series of work String address = "Happy Road No. 666 God Province,Heaven"; / / define an address String context = "Hello,It's me,do you know who I am? I'm your old lover.I'd like to.... "; hellRoadPostOffice.sendLetter (context, address);}}
See, the customer is much simpler. After providing this mode, the scalability of the system has also been greatly improved. Suddenly, during an extraordinary period, all mail sent to God Province (God's Province) has to undergo security check, so it is easy for us to deal with this. Look at the class diagram:
Look at this red box, only this part is added, and the other parts do not need to be changed on the class diagram, so let's take a look at the source code:
Package com.cbf4life.facade;public class ModenPostOffice {private LetterProcess letterProcess = new LetterProcessImpl (); private Police letterPolice = new Police (); / / letter, package, delivery, integration public void sendLetter (String context,String address) {/ / help you write letterProcess.writeContext (context); / / write the envelope letterProcess.fillEnvelope (address); / / the police want to check the letter letterPolice.checkLetter (letterProcess) / / put the letter in the envelope letterProcess.letterInotoEnvelope (); / / Mail the letter letterProcess.sendLetter ();}}
Just add a declaration of a letterPolice variable and a method call, and the process of writing a letter becomes like this: first write a letter, then write an envelope, then the police start to check it, and then put the letter in the envelope and then send it out. Then this change is transparent to the customer. He can't see anyone checking his email at all, and he doesn't need to know. Anyway, modern mail has been done for him. That's what he likes.
At this point, the study on "how to realize the java facade model" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.