آموزش دیزاین پترن پروکسی (Proxy) در گولنگ

قبل از اینکه شروع کنم درباره پروکسی، میخوام در مورد دیزاین پترن منیجیر صحبت کنم. این دیزاین پترن یک سری منیجر جدا میسازه و براساس دیتایی که دارید اون منیجر مخصوص کاری صدا زده میشه (به صورت خلاصه).

منیجیر پترن میتونه توسط struct و متد ها میتونه پیاده سازی بشه🔗


Proxy Design Pattern🔗

این پترن به این درد میخوره که لاگ، کش یا چک های امنیتی انجام بدید بدون نیاز به تغییر آبجکت اصلی! در گولنگ برای پیاده سازی این پترن از struct و interfaces ها استفاده میکنیم.

یک مثال از دیزاین پترن پروکسی میبینیم:

package main
import (
"database/sql"
"errors"
"fmt"
"time"
_ "github.com/mattn/go-sqlite3"
)
// Database defines the interface that the real database and proxy will implement.
type Database interface {
Connect() (*sql.DB, error)
}
// RealDatabase implements the Database interface and represents the actual database resource.
type RealDatabase struct{}
func (r *RealDatabase) Connect() (*sql.DB, error) {
db, err := sql.Open("sqlite3", "test.db")
if err != nil {
return nil, err
}
// Simulate a slow connection by waiting for a few seconds.
time.Sleep(2 * time.Second)
return db, nil
}
// ProxyDatabase implements the Database interface and provides a surrogate or placeholder for the real database connection.
type ProxyDatabase struct {
realDatabase *RealDatabase
}
func (p *ProxyDatabase) Connect() (*sql.DB, error) {
if p.realDatabase == nil {
p.realDatabase = &RealDatabase{}
}
// Perform some security checks before allowing access to the real database.
// For example, check if the user is authorized to access the database.
if !isUserAuthorized() {
return nil, errors.New("user is not authorized to access the database")
}
// Log the access to the database.
fmt.Println("User accessed the database at", time.Now().Format(time.RFC3339))
return p.realDatabase.Connect()
}
func isUserAuthorized() bool {
// Perform some logic to determine if the user is authorized to access the database.
return true
}
func main() {
var db Database = &ProxyDatabase{}
connection, err := db.Connect()
if err != nil {
fmt.Println("Failed to connect to the database:", err)
return
}
defer connection.Close()
// Use the database connection.
fmt.Println("Successfully connected to the database.")
}

در این مثال ما یک اینترفیس به نام Database تعریف کردیم، که هر دو تایپ RealDatabase و ProxyDatabase ازش پیروی میکنند. تایپ RealDatabase یک متد به نام "Connect()" داره که کار ارتباط با دیتابیس انجام میده. حالا ما تایپ ProxyDatabase علاوه بر اینکه میتونه به دیتابیس کانکت بشه یک چک امنیتی و لاگ انجام میشه.

در ادامه اگه توجه داشته باشید ما میایم چک میکنیم که اگه فیلد realDatabase در ProxyDatabase وجود نداشته باشه، بیاد یکی بسازه.

در نهایت هم در فانکشن main اومدیم از پروکسی خودمون استفاده کردیم و به همین سادگی یک پروکسی ساختیم🤟

 

0 🔥
0 🎉
0 😮
0 👍
0 💜
0 👏
میلاد خسروی
نویسنده کد نیوز

برنامه نویس فان | Fun Developer یک آدم ساده که عاشق برنامه نویسی و کد زدنه :) تلاش میکنه تا به بقیه کمک کنه. توسعه دهنده هسته لاراول و فضای اوپن سورس. فاندر پرانتز و کد نیوز.

0+ نظر

برای ثبت نظر ابتدا ورود کنید.

0 نظر

    اولین نفر باش که نظر ثبت میکنی :) یعنی یه کامنت به ما نمیرسه 😁