← Back to Go Course | Chapter 7: Interfaces | Lesson 7 of 7

Interface की Composition

Interface composition दो छोटी job descriptions को साथ staple करके एक बड़ी बनाने जैसा है -- किसी type को दोनों lists की हर मांग करने में सक्षम होना चाहिए।
Syntax
go
type Name interface {
    Interface1
    Interface2
    Method()
}

Embedding Interfaces

एक interface बस इसे अपनी body के अंदर नाम देकर किसी दूसरे interface को embed कर सकता है, जो उस interface की सभी method requirements को उनके signatures दोहराए बिना खींच लाता है।

उदाहरण: Embedding Interfaces

markup
// 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())
}

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 करती है।

Note: यह composition pattern बिल्कुल वही है जिससे असली io.ReadWriter type define की गई है।

उदाहरण: The Standard Library's io.ReadWriter

markup
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())
}

Satisfying a Composed Interface

किसी composed interface को satisfy करने के लिए, किसी type को बस हर embedded interface द्वारा मांगी गई हर method implement करनी होती है -- implementing type के नज़रिए से composition में कुछ खास नहीं है; यह बस पूरा करने के लिए एक बड़ा method set है।

उदाहरण: Satisfying a Composed Interface

markup
// 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())
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी दूसरे interface को उसके नाम से बस embed करने के बजाय उसकी हर method manually फिर से list करना।
  2. यह मान लेना कि एक composed interface satisfaction के काम करने का तरीका बदल देता है -- किसी type को अब भी सभी embedded interfaces से हर method implement करनी चाहिए, कुछ भी optional नहीं।
  3. गहराई से 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.