Skip to content

Instantly share code, notes, and snippets.

@acmacalister
Created April 14, 2014 18:58
Show Gist options
  • Select an option

  • Save acmacalister/10674344 to your computer and use it in GitHub Desktop.

Select an option

Save acmacalister/10674344 to your computer and use it in GitHub Desktop.
Indirection with idiomatic interfaces - http://vluxe.io/indirection-idiomatic-interfaces.html
#include <stdio.h>
/* our animal structure! */
typedef struct {
void (*run)(void);
} Animal;
/* dogs like to run */
void dog_run(void)
{
printf("running!!!\n");
}
/* horses like to gallop! */
void horse_run(void)
{
printf("galloping!!!\n");
}
int main(int argc, char *argv[])
{
/* create a dog */
Animal dog = {
.run = dog_run
};
/* create a horse */
Animal horse = {
.run = horse_run
};
/* let our animal's run away! */
dog.run();
horse.run();
return 0;
}
package main
import "fmt"
//For animals that like to chew
type Chewable interface {
Chew()
}
//For animals that can soar through the sky
type Flying interface {
Fly()
}
//Our base Animal type
type Animal struct{}
func (animal *Animal) Run() {
fmt.Println("running!")
}
//Now here is a Dog type with an embedded Animal type
type Dog struct {
Animal
}
//Here is the Dog type implementing the Chewable interface
func (dog *Dog) Chew() {
fmt.Println("chewing away!")
}
//Now here is the Bird type that also embeds the Animal type
type Bird struct {
Animal
}
//Here the Bird is "overriding" the Animal type function
func (bird *Bird) Run() {
fmt.Println("running, psh try flying sometime!")
}
//Bird's can Fly... by implementing the Flying interface
func (bird *Bird) Fly() {
fmt.Println("Look ma, I'm Flying!")
}
//Good ole main
func main() {
dog := Dog{}
bird := new(Bird)
//Both can run from our embedded animal type
dog.Run()
bird.Run()
//each implement their own interface for something specific to that animal
dog.Chew()
bird.Fly()
}
/* For animals that like to chew */
interface Chewable {
public void chew();
}
/* For animals that can soar through the sky */
interface Flying {
public void fly();
}
/* nice base animal class. Giving us some common methods for all animals. */
class Animal
{
public void run() {
System.out.println("running!!!");
}
}
/* Dog class. Well dogs do chew. As we all probably now. */
class Dog extends Animal implements Chewable
{
public void chew() {
System.out.println("chewing away!");
}
}
/* Bird class. Of course bird's don't chew. */
class Bird extends Animal implements Flying
{
public void run() {
System.out.println("running, psh try flying sometime!");
}
public void fly() {
System.out.println("Look ma, I'm Flying!");
}
}
/* a place for our animals to strech their legs */
class Yard
{
public static void main(String[] args) {
Dog dog = new Dog();
Bird bird = new Bird();
/* both can run from our base class */
dog.run();
bird.run();
/* each implement their own interface for something specific to that animal */
dog.chew();
bird.fly();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment