Created
January 24, 2023 18:51
-
-
Save rohanjai777/ccc2657a240ca1e4eccc93dd06bc429f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Online Go compiler to run Golang program online | |
// Print "Hello World!" message | |
package main | |
import "fmt" | |
type Client struct{ | |
} | |
func (c *Client) InsertLightningConnectorIntoComputer(com Computer){ | |
fmt.Println("Client inserts Lightning connector into computer.") | |
com.InsertIntoLightningPort() | |
} | |
type Computer interface { //struct which has this func as receiver will implement this interface | |
InsertIntoLightningPort() | |
} | |
type Mac struct { | |
} | |
func (m *Mac) InsertIntoLightningPort() { //implement Computer interface | |
fmt.Println("Lightning connector is plugged into mac machine.") | |
} | |
type Windows struct{} | |
func (w *Windows) insertIntoUSBPort() { //it doesn't implement Computer interface, but it uses a adapter. | |
fmt.Println("USB connector is plugged into windows machine.") | |
} | |
type WindowsAdapter struct { //its an adapter for Windows machine. | |
windowMachine *Windows //attaching WindowMachine with adapter | |
} | |
func (w *WindowsAdapter) InsertIntoLightningPort() { //adapter implements Computer interface which further calls method of windows. | |
fmt.Println("Adapter converts Lightning signal to USB.") | |
w.windowMachine.insertIntoUSBPort() | |
} | |
func main() { | |
client := &Client{} //make a client which InsertLightningConnector | |
mac := &Mac{} //make a mac | |
client.InsertLightningConnectorIntoComputer(mac) //client insert lightning connecter with mac. | |
windowsMachine := &Windows{} //make a windows | |
windowsMachineAdapter := &WindowsAdapter{ //join adapter with windows | |
windowMachine: windowsMachine, | |
} | |
client.InsertLightningConnectorIntoComputer(windowsMachineAdapter) //now client insert into windows adapter. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment