Learn Data Structures and Algorithms with Golang
上QQ阅读APP看书,第一时间看更新

Private class data

The private class data pattern secures the data within a class. This pattern encapsulates the initialization of the class data. The write privileges of properties within the private class are protected, and properties are set during construction. The private class pattern prints the exposure of information by securing it in a class that retains the state. The encapsulation of class data initialization is a scenario where this pattern is applicable.

Account is a class with account details and a customer name. AccountDetails is the private attribute of Account , and CustomerName is the public attribute. JSON marshaling of Account has CustomerName as a public property. AccountDetails is the package property in Go (modeled as private class data):

//main package has examples shown
// in Hands-On Data Structures and algorithms with Go book
package main
// importing fmt and encoding/json packages
import (
"encoding/json"
"fmt"
)
//AccountDetails struct
type AccountDetails struct {
id string
accountType string
}
//Account struct
type Account struct {
details *AccountDetails
CustomerName string
}
// Account class method setDetails
func (account *Account) setDetails(id string, accountType string) {
account.details = &AccountDetails{id, accountType}
}

As shown in the following code, the Account class has the getId method, which returns the id private class attribute:

//Account class method getId
func (account *Account) getId() string{
return account.details.id
}
//Account class method getAccountType
func (account *Account) getAccountType() string{
return account.details.accountType
}

The main method calls the Account initializer with CustomerName. The details of the account are set details with the setDetails method:

// main method
func main() {
var account *Account = &Account{CustomerName: "John Smith"}
account.setDetails("4532","current")
jsonAccount, _ := json.Marshal(account)
fmt.Println("Private Class hidden",string(jsonAccount))
fmt.Println("Account Id",account.getId())
fmt.Println("Account Type",account.getAccountType())
}

Run the following commands:

go run privateclass.go

The following screenshot displays the output:

Let's take a look at Proxy pattern in the next section.