github.com/maikovskiys/l1tasks@v0.0.0-20230927052451-6436d7687dc9/develop/dev21/main.go (about)

     1  package main
     2  
     3  /*
     4  Реализовать паттерн «адаптер» на любом примере.
     5  */
     6  func main() {
     7  	var db Database
     8  	adapter := New(db)
     9  	var id int
    10  	adapter.Get(id)
    11  }
    12  
    13  //Target
    14  type DbClient interface {
    15  	Get(id int)
    16  }
    17  type DBClient struct{}
    18  
    19  func (c *DBClient) Get(id int) {
    20  }
    21  
    22  //adapter
    23  type Adapter struct {
    24  	db Database
    25  }
    26  
    27  func New(db Database) DbClient {
    28  	return &Adapter{
    29  		db: db,
    30  	}
    31  }
    32  func (a *Adapter) Get(id int) {
    33  	dbId := Dbid(id)
    34  	a.db.GetFromDb(dbId)
    35  }
    36  
    37  //Adaptee
    38  type Database interface {
    39  	GetFromDb(id Dbid)
    40  }
    41  type Dbid int