--Introducing sample code that realizes Mixin without using general class inheritance function
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.
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.
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.
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".
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
$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
$ ruby mixin.rb
Hello, world
In PHP, you can use traits to implement Mixins.
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.
<?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();
$ 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
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.
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();
}
}
$ 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