Golang Single-state mode (singleton)

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

In the previous article, I said that the introduction of Golang in the project to develop a terminal program, to realize the golang of some very attractive characteristics, want to use a language flexibly, of course, can not only stay in the Hello world stage.

Warm so know new, I want to through the design mode of review, to gradually master the use of Golang, nonsense not much said, come on.

There is an interesting phenomenon, if you have participated in the software engineer's interview, when asked the design pattern question, more than 80% people's answer will refer to Singleton, the reason is very simple, the single state mode is one of simplest and most easy to learn design pattern, below we take a look at the application of the single state mode in Golang ...

The singleton mode is very good memory, as its name describes, provides an instance of an object, and guarantees that there is no duplication throughout the application. When this class is first called, an instance is created and reused in other parts of the application.

Let's review some of the scenarios of the single-state mode

    1. When you want to use only one database connection to access the data
    2. When you want to use an SSH connection to perform some tasks, and do not want to reopen the new connection while you are working on other tasks.
    3. If you want to restrict access to a parameter or space, you can use the single-state mode as a gate for the parameter.
    4. If you want to limit the number of calls in a place, you can use a single instance to limit the number of calls to an acceptable range

Here is a counter case to show the use of the single-state mode

Requirements and acceptance rules

    1. When there is no counter, a new counter is created and initialized to 0
    2. If the counter already exists, it returns an instance of the counter that exists
    3. If the counter addone is called, then the count in the counter refueling

Golang's single-state mode is slightly different from Java implementation, and first reviews the implementation in Java

public class Singleton {    private static Singleton instance;    public static Singleton getInstance(){        if(instance == null){            instance = new Singleton();        }        return instance;    }}

However, there is no static keyword in Golang to identify global static variables, so how do we implement a single-state mode? But the variable scope of Golang can also achieve the same effect.

type singleton struct{}var instance * singletonfunc GetInstance() *singleton{    if instance == nil {        instance = new(singleton)    }    return instance}

For Golang code, it is necessary to do some explaining
There is no reserved keyword for private and public in Golang, so how do we control the scope of access? case, right, letter case. Interestingly, Scala uses case-by-case to differentiate constants and variables, and Golang to differentiate access by letter-case, so the founders of these languages have an almost perverted aesthetic in coding specifications.

The above code in the type singleton struct is for the Java Class Singleton is our single state type, now there is a single state type, then, now we also need a method to identify the increase of the counter, In other words, we need to add a method to Sigleton. So, how do you do it?

func (s *singleton) Addone() int{}

So we've added a method to Signleton.

Now let's look at the complete code

package countertype singleton struct{    count int}var instance *singletonfunc GetInstance()* singleton{    if instance == nil{        instance = new(singleton)    }    return instance}func (s *singleton) Addone() int{    s.count ++    return s.count}

The complete implementation of Java in comparison

public class Counter{    private static instance Counter;    private static int count;    public static Counter getInstance(){        if(instance == null){            instance = new Counter();        }        return instance;    }    public static int addOne(){        count++;        return count;    }}

Okay, here's the introduction.

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.