Proxy design pattern is to isolate the client from the actual implementation mainly due to overheads, intellectual or sensitive processes protection, or standardizes client facing interface. This is commonly seen in web API implementation, where the API server calls the internal proxy to execute sensitive or asynchronous work. The pattern is as follows:
Here is an example in Go:
package mainimport ( "fmt")// this the acutal task hidden internallytype task struct {}func (t *task) Print(message string) { fmt.Println(message)}// PrintAPI is a proxy struct facing clienttype PrintAPI struct { task *task}func NewPrintAPI() *PrintAPI { return &PrintAPI{ task: &task{}, }}func (p *PrintAPI) Print(message string) { p.task.Print(message)}// client sidefunc main() { p := NewPrintAPI() p.Print("Hello World")}// Output:Hello WorldNotice that on Client side, it only uses the NewPrintAPI. Print and successfully isolated the internal task.Print method. NewPrintAPI acts as a proxy between the client and the internal struct functions.
This is commonly seen on network servers, where functionalities are often proxies using an API server, be it HTTP, RPC, RESTFUL, or custom TCP/UDP protocols.