Session 7
Main topics:
Mixins
Mixins
A mixin class contains methods to be used by other classes
without being their parent. This is how mixins differ from
interfaces and abstract classes.
Mixins are declared using keyword mixin
Let's understand this with help of the example, we have
Person class: The most common properties could be
abstracted in class Person that other classes would extend
from. Common properties (like age, name etc.) and common
behaviors (like eat and sleep) can go in here.
Artist class: Our artist is a Person. Makes landscapes
sketches.
Engineer class: The engineer is a Person. Makes
buildings sketches and reads research papers on
construction.
Doctor class: The doctor is a Person. Reads daily on
latest health news and likes to do workout.
Boxer class: The boxer is a type of Athlete. Athlete is
a Person. Boxer does routine exercises as well practices
punches.
As we see from above that artist does sketching. Engineer does
sketching as well as reading. Doctor does reading and exercise.
The athlete does exercise. The boxer-a type of athlete, also does
boxing.
Creating Mixins
Sketching
The Sketching mixin defines the common sketch() method.
It takes a message and prints it on console to keep things simple
for demonstration.
Reading
The Reading mixin defines
dailyReading(String topic) method. A reading topic is
passed as parameter.
Exercise
The Exercise mixin defines methods for running and weight
training. The running(int mile) method
passes mile parameter to print the message.
The weightTraining(int weights) method prints the value
for weights parameter.
Boxing
The Boxing mixin defines practicing punches behavior of an
athlete. As per our requirements, only athletes are allowed to
practice punches. In such cases, we may want to restrict the
usage of mixin only by classes of type Athlete. We can apply
such restriction on Boxing mixin by using on keyword followed
by the Athlete - the class which is allowed the usage.
Using Mixins
Artist class
The artist makes landscapes sketches. It
defines sketchLandscape() to do so. This method
calls sketch(...) method from Sketching mixin.
Engineer class
The engineer does make building sketches and reads research
on building construction. The sketchBuildings() method
uses sketch() method from Sketching mixin.
The readResearchPaper() calls dailyReading(topic) fro
m Reading mixin.
Doctor class
The doctor reads health reports and does workouts to keep it.
The readReports() method uses Reading mixin.
The workout() method uses Exercise mixin to carve
personalized workout plan.
Athlete class
The athlete is a person as well like other people we discussed so
far. All athletes do their exercise, and have a general workout
routine. The method generalRoutine() provide the general
workout routine definition.
Boxer class
The boxer is an Athlete who does boxing. The Boxing mixin
provides the punch() to define punchPractice() method for
boxer. The routineExercise() method defines exercise
routine for this specific athlete with help of Exercise mixin.
Running Code
Let’s run all the methods for each class
Output:
Note: checkout the source code at GitHub here
Thanks, Good Luck!