Design mode-proxy mode (Go language description)

Source: Internet
Author: User

Hello everyone, I am handsome, because recently changed, so I decided to be my deputy small Sangolai to write this blog, but the content can be assured, or I come to complete, little handsome man's life is my content for my issue.

&_& still go to the point, today this blog we continue to learn design patterns, after learning the above several design patterns, we have not found that the design pattern is actually very simple, is it the place is the right model, which is the most important place in the design mode, We can not use the design patterns we have learnt. Understand just fine ... Let's start by introducing a new design pattern- proxy mode .

Pattern definition

What is code mode? In fact, the beginning of the article is not happy, in fact, that is the real life of a proxy mode, you and I before through an intermediary to complete the communication, in fact, it is so simple. Although simple, we still need to look at the definition of the summary:

Provides a proxy for other objects to control access to this object. In some cases, an object does not fit or cannot directly reference another object, and a proxy object can act as an intermediary between the client and the target object.

How do you understand it? There is a joke on the internet is very good, such as you like a sister, embarrassed to talk to others, this time you may be through her roommate to express your affection. Let's take a look at the class diagram of the proxy mode, usually, after reading the definition of a daze, reading the class diagram may be sticky, and so after reading the code example, we thoroughly understand.

As you can see, there are 4 roles in the proxy mode, one of which is abstract.

  1. ClientYou are the one in the joke above, you are the leader of the Act.
  2. Subjectis an abstract interface of agents and agents
  3. RealSubjectThe object being represented, the sister above
  4. ProxyAgent, corresponding to the above-mentioned sister roommate

However, looking closely at the class diagram, I began to doubt the necessity of proxy mode. Is it necessary? I go directly to call RealSubject not on the line, but also so troublesome, through a proxy to call, this is not to take off the pants fart? In fact, in many cases, the real object is not accessible to us, or the provider does not want us to access, this time we need an agent to help us complete. An example? Very simple, we visit a website, data is not put in the database? However, the provision of the site does not allow us to operate the database directly, this time provides a page, we can indirectly through the page to insert data into the database.

Code Demo

Well, the text is not much of it, let's use the code to demonstrate it, in today's code, let's simulate the process we use GitHub, we are in the clone project from GitHub is usually done by the command line, In fact, this command line can be seen as a proxy for GitHub.

Based on the class diagram above, we will first provide an abstract interface to standardize the behavior of agents and objects

typeinterface {    stringbool}

OK, very simple, there is only one Clone method that indicates that we want to clone the project from a git source. Let's go ahead and implement the real object, namely Github .

typestruct{}funcstringbool {    if"https") {        fmt.Println("clone from " + url)        returntrue    }    fmt.Println("failed to clone from " + url)    returnfalse}

GitHubImplementation of the Clone method, so it implements the Git interface, as Clone for the logic of the method, we do not care about, there is an important role we have not seen, to see the agent how to write it.

typestruct {    Gitcmd Git}funcstringbool {    return p.Gitcmd.Clone(url)}

This is our command line, it holds a Git type of variable, and he still first the Clone method, we call the method, in Clone fact, is indirectly called the method of this Git type variable Clone , this is the proxy!

There's one last character left, and that's us Coder , and we're going to be a lot more casual.

type Coder struct{}func (p Coder) GetCode(url string) {    gitBash := GetGit(1)    if gitBash.Clone(url) {        fmt.Println("success")    else {        fmt.Println("failed")    }}

GetGitWhat's that? is actually the callable interface provided by the provider to us.

funcint) Git {    if t == 1 {        return GitBash{Gitcmd: GitHub{}}    }    returnnil// 可能还有其他的git源}

First of all, we know we are going from GitHub the clone project, so we tell the agent to GitHub give me the Clone project from the top, don't run up GitLab . The next step is to invoke the proxy Clone method, which we can fully understand through the preceding code, which is definitely GitHub the Clone method called. Finally, let's look at the results of the operation.

Perfect, now understand what is the proxy mode. Proxy mode In fact, the main purpose is that when we can not directly access an object, the proxy way to complete the access to the object. The proxy mode is highly scalable, and if we need to GitLab add an object from the top, only the provider needs to be added GitLab , then we GetGit can use it when we call GitLab .

Today's blog is very simple, in fact, the code of the pattern is very simple, do not be the design mode of these four words scare. We'll see you June!

Code on GitHub, welcome Star:https://github.com/qibin0506/go-designpattern.

Design mode-proxy mode (Go language description)

Related Article

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.