The agent model of evolution in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Do not know why, so many design patterns inside, the proxy mode has a special like. It is like this aspect of thinking, will be a number of similar methods of common pre-and post-abstraction out of the realization, really feel great. This half-yearly turn Golang development, finally in the previous period of time to toss out the Golang style proxy mode-in fact, is not a proxy mode, more appropriate to the argument should be called Golang one of the tips

There is no difference between the show, with Go and Java implementation of a new user to insert the demo

Java style

First look at the implementation in Java

UserService interface: public interface UserService {void save (user user);}    Userserviceproxy proxy class: public class Userserviceproxy implements userservice{private UserService userservice;        Public Userserviceproxy (UserService userservice) {super ();    This.userservice = UserService;        @Override public void Save (user user) {System.out.println ("--------Open transaction--------");        Userservice.save (user);    System.out.println ("--------End of Transaction--------");        }}userserviceimpl Business class: public class Userserviceimpl implements UserService {@Override public void Save (user user) {    System.out.println ("Save user" +user);    }}user Entity class: public class User {private String name;    Public User (String name) {this.name = name;        } @Override Public String toString () {StringBuilder builder = new StringBuilder ();        Builder.append ("User [name="). Append (name). Append ("]");    return builder.tostring (); }} Test class: public class Main {public static void MAin (string[] args) {Userserviceproxy proxy = new Userserviceproxy (new Userserviceimpl ());    Proxy.save (New User ("Sivan")); }}

Results:


Implementation of Java
The code is not analyzed, the proxy mode in the previous article mentioned that there is a portal

Golang style

Golang because of the characteristic of a function variable parameter, it is doomed to implement an abstract pre-and post-action operation similar to the proxy pattern, which does not need to be so complicated

package mainimport (    "fmt")func main() {    saveUser(&user{"sivan"})}type user struct {    Name string}func (u *user) String() string {    return u.Name}func saveUser(user *user) {    withTx(func() {        fmt.Printf("保存用户 %s\n", user.Name)    })}func withTx(fn func()) {    fmt.Println("开启事务")    fn()    fmt.Println("结束事务")}

Yes, you are right, go to implement this aspect of the action, just a few lines of code, and then look at the test results


A streamlined, yet powerful go implementation

PostScript: Each language has the characteristics of each language, a language of an algorithm to achieve, for another language, it is possible, to change a thought to achieve, should not have its shape.

Lack of strength, inevitably wrong, but also please comment on the area.

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.