Achieve Mixin-like implementation inheritance: Ruby module, Java interface, PHP trait

Overview

--Introducing sample code that realizes Mixin without using general class inheritance function

What is Mixin (Mix-in)?

It is a function that allows a certain function to be implemented by inheriting the function, such as a class inheritance function of a programming language. Some programming languages implement Mixins without using common class inheritance features.

Mixin - Wikipedia

A mixin is a class in an object-oriented programming language that provides functions by being inherited by subclasses and is not intended to operate independently.

Trait is a concept similar to Mixin.

Trait -Wikipedia

A trait is a concept in computer programming, which is a collection of methods used as a simple conceptual model for structurally performing object-oriented programming.

Traits are similar to mix-ins, but mix-ins let you synthesize methods only by inheritance operations, while traits allow you to synthesize methods in more ways, including symmetric additions, method exclusions, and aliasing. Traits also differ from interfaces in that they not only specify method types but also implement them during synthesis.

Achieve Mixin with Ruby module

In Ruby, you can implement Mixin by using module.

Introduction \ (Ruby 2 \ .6 \ .0 )

Ruby intentionally doesn't have multiple inheritance in view of the fact that multiple inheritance is a source of complexity, but modules can be used to share implementations across the class hierarchy. This feature is called "Mix-in".

Sample code

module Printer

  #Output content
  def print
    #On the class side@Read and output the content variable
    puts "#{@content}"
  end
end

class Message

  #Mix Printer module-in
  include Printer

  def initialize(content)
    @content = content
  end
end

#Message class mixes Printer module-Because it is in
#print method becomes available
m = Message.new('Hello, world')
m.print

Operation check environment and execution result

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]

$ ruby mixin.rb 
Hello, world

Achieve Mixins with PHP traits

In PHP, you can use traits to implement Mixins.

PHP: Trait -Manual

PHP 5.4.0 and later introduced a mechanism called "traits" for reusing code. Traits are a mechanism for reusing code in a single inheritance language like PHP. Traits are designed to reduce the constraints of single inheritance and allow several methods to be reused in independent classes in different class hierarchies. The combined trait and class syntax reduces complexity and also avoids common problems associated with multiple inheritance and mixins. Traits are similar to classes, but traits are just for grouping together some features. You cannot create an instance of the trait itself. It adds functionality to the old-fashioned inheritance, allowing you to configure behavior horizontally. In other words, you can add to class members without inheriting.

Sample code

<?php

trait Printer {

  /**
   *Returns the content.
   * @return string content string
   */
  abstract protected function getContent();

  /**
   *Output the content.
   */
  public function print() {
    echo $this->getContent(), "\n";
  }
}

class Message {

  #Mix Printer Trait-in
  use Printer;

  private $content;

  /**
   *constructor.
   * @param string $content content string
   */
  public function __construct(string $content) {
    $this->content = $content;
  }

  /**
   *Returns the content.
   * @return string content string
   */
  protected function getContent() {
    return $this->content;
  }
}

#Message class mixes Printer traits-Because it is in
#print method becomes available
$m = new Message('Hello, world');
$m->print();

Operation check environment and execution result

$ php --version
PHP 7.3.7 (cli) (built: Jul  5 2019 12:44:05) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.7, Copyright (c) 1999-2018, by Zend Technologies

$ php mixin.php 
Hello, world

Achieve Mixins with Java interface

In Java, Mixin can be realized by using the default method of interface.

Evolving Java Interface (Java Magazine Vol.37 JANUARY / FEBRUARY 2018)

The default method is the method body, that is, the method of the interface that has the default implementation. The default method is defined by prefixing the method signature with the default modifier and has the body of the complete method.

Sample code

interface Printer {

  /**
   *Returns the content.
   * @return content string
   */
  String getContent();

  /**
   *Output the content.
   */
  default void print() {
    //From the getContent method implemented on the class side
    //Get content and output
    System.out.println(getContent());
  }
}
//Mix Printer interface-in
public class Message implements Printer {

  private String content;

  /**
   *constructor.
   * @param content content string
   */
  public Message(String content) {
    this.content = content;
  }

  /**
   *Returns the content.
   * @return content string
   */
  @Override
  public String getContent() {
    return content;
  }

  public static void main(String[] args) {
    //Message class mixes Printer interface-Because it is in
    //print method becomes available
    Message m = new Message("Hello, world");
    m.print();
  }
}

Operation check environment and execution result

$ java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

$ javac Printer.java Message.java 

$ java Message
Hello, world

Recommended Posts

Achieve Mixin-like implementation inheritance: Ruby module, Java interface, PHP trait
[ev3 × Java] Interface, implementation and inheritance (event handling)
Advanced inheritance abstract, interface -java
JAVA learning history interface inheritance
Differences in writing in Ruby, PHP, Java, JS
Achieve OpenSSL compatible encryption with Java / PHP