The golden rule of object-oriented programming

Program to an interface not an implementation

Favor object composition over class inheritance

C# interface programming

Defines the interface

public interface IPerson
{
    public string Name { get; set; }
    public int Age { get; set; }
    
    void Introduction();
    string GetName();
}
Copy the code

Implementing an interface

public class Student : IPerson { public string Name { get; set; } public int Age { get; set; } public Student(string name) { Name = name; } public void Introduction() { Console.WriteLine("I am teacher:" + Name); } public string GetName() { return Name; }}Copy the code

Go interface programming

The interface definition

type Person interface {
   Introduction()
   GetName() string
}
Copy the code

Student implementation interface

Type Student struct {Name string} func (receiver Student) Introduction() {FMT.Println("I am Student: " + receiver.Name) } func (receiver Student) GetName() string { return receiver.Name } func DemoRun() { student := Student{Name: "Moon"} student.Introduction() }Copy the code

Compare Go and C#

C# interface implementation

  1. Define the interface.
  2. Define the object Student that inherits from IPerson.
  3. Implement properties, interfaces.
  4. Support for class inheritance. A subclass has all the capabilities of its parent class.

Go interface implementation

  1. Define the interface Person.
  2. Define the object Student.
  3. Implement the interface.

Conclusion comparing

  • As you can see, the syntax design of Go has a sense of simplicity from definition to implementation. As long as I, Student, implement the interface in Person, THEN I’ve implemented programming to the interface.
  • In C# and Java, we have to declare the inherited interface on the class and then implement the specific interface. Secondly, C# and Java support the inheritance of objects, which can realize the reuse of the methods of the parent class. It is not an encouraging practice to realize inheritance as a way of code reuse. So often we sayComposition over Inheritance.
    • Java and C# are both single-inheritance languages. Each class can only have one parent class. Once the inheritance position is occupied by the implementation inheritance, it is difficult to do interface inheritance.
    • When a class inherits from a previous interface, it can form a god class if the excuse is complex enough.
  • In order to realize polymorphism, object-oriented languages such as Java and C# introduce the concept of inheritance. Although it brings convenience in coding, it brings unnecessary mental burden: originally, the only construction method of conforming objects is combination, but now there is another option, inheritance. The Go language, however, abandons the concept of inheritance and comprehensively strengthens the ability of combination.

Go combination polymorphism

type Person interface { Introduction() GetName() string } type Info struct { Name string Age int } type Student struct {  Info } type Teacher struct { Info } func (receiver Teacher) Introduction() { fmt.Println("I am teacher:" + } func (receiver Student) Introduction() {FMT.Println("I am Student: "+ receiver.Name)} func (info info) GetName() string {return info.name} // Say func Say(person person) { person.Introduction() person.GetName() } func DemoRun() { student := Student{Info{Name: "Moon"}} student.Introduction() teacher := Teacher{Info{Name: "Moon"}} Say(student) Say(teacher) }Copy the code
  • Student and Teacher both have Name and Age. Here we can pull out a structure, Info, and embed Student and Teacher.
  • The Say function, with the Person interface as an incoming parameter, implements polymorphism.

The Go language follows the golden rule of face object programming — “Program to an Interface not an implementation.”