Interface की Composition
In this page:
type Name interface {
Interface1
Interface2
Method()
}
Embedding Interfaces
एक interface बस इसे अपनी body के अंदर नाम देकर किसी दूसरे interface को embed कर सकता है, जो उस interface की सभी method requirements को उनके signatures दोहराए बिना खींच लाता है।
उदाहरण: Embedding Interfaces
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define an interface named Reader (a set of method signatures)
type Reader interface {
Read() string
}
// Define an interface named Writer (a set of method signatures)
type Writer interface {
Write(string)
}
type ReadWriter interface {
Reader
Writer
}
// Define a struct type named Buffer
type Buffer struct {
data string
}
// Define the method Read on type Buffer
func (b *Buffer) Read() string { return b.data }
// Define the method Write on type Buffer
func (b *Buffer) Write(s string) { b.data += s }
// main is where the program starts running
func main() {
var rw ReadWriter = &Buffer{}
rw.Write("hello")
fmt.Println(rw.Read())
}
Login to try C/C++/Java/PHP code in the editor
The Standard Library's io.ReadWriter
Standard library इसी exact pattern का पालन io.ReadWriter के साथ करती है, जो io.Reader और io.Writer दोनों को embed करता है -- दोनों Read और Write methods implement करने वाली कोई भी type बिना किसी अतिरिक्त code के io.ReadWriter को अपने आप satisfy करती है।
उदाहरण: The Standard Library's io.ReadWriter
package main
import (
"bytes"
"fmt"
)
func main() {
var buf bytes.Buffer // *bytes.Buffer satisfies io.ReadWriter
buf.Write([]byte("composed interfaces"))
fmt.Println(buf.String())
}
Login to try C/C++/Java/PHP code in the editor
Satisfying a Composed Interface
किसी composed interface को satisfy करने के लिए, किसी type को बस हर embedded interface द्वारा मांगी गई हर method implement करनी होती है -- implementing type के नज़रिए से composition में कुछ खास नहीं है; यह बस पूरा करने के लिए एक बड़ा method set है।
उदाहरण: Satisfying a Composed Interface
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define an interface named Named (a set of method signatures)
type Named interface {
Name() string
}
// Define an interface named Aged (a set of method signatures)
type Aged interface {
Age() int
}
type Person interface {
Named
Aged
}
// Define a struct type named Employee
type Employee struct {
name string
age int
}
// Define the method Name on type Employee
func (e Employee) Name() string { return e.name }
// Define the method Age on type Employee
func (e Employee) Age() int { return e.age }
// main is where the program starts running
func main() {
var p Person = Employee{name: "Nikhil", age: 29}
fmt.Println(p.Name(), p.Age())
}
Login to try C/C++/Java/PHP code in the editor
- किसी दूसरे interface को उसके नाम से बस embed करने के बजाय उसकी हर method manually फिर से list करना।
- यह मान लेना कि एक composed interface satisfaction के काम करने का तरीका बदल देता है -- किसी type को अब भी सभी embedded interfaces से हर method implement करनी चाहिए, कुछ भी optional नहीं।
- गहराई से nested composed interfaces बनाना जिन्हें उनके ज़रूरी method set तक trace करना मुश्किल हो जाता है।
- Interfaces दूसरे interfaces को embed कर सकते हैं, उनके method sets को एक बड़े interface में combine करते हुए।
- io.ReadWriter एक standard-library उदाहरण है: यह io.Reader और io.Writer दोनों को embed करता है।
- किसी type को composed interface satisfy करने के लिए सभी embedded interfaces से हर method implement करनी चाहिए।
- Composition आपको method lists दोहराने के बजाय छोटे, अच्छी तरह समझे टुकड़ों से बड़े contracts बनाने देता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: