I generally have a pretty good feel for how Scala traits work, and how they can be used for different needs. As one example, a few years ago I learned that it’s best to define abstract fields in traits using
def. But there are still a few things I wonder about.
Today I had a few free moments and I decided to look at what happens under the covers when you use
var fields in traits, and then mix-in or extend those traits with classes. So I created some examples, compiled them with
scalac -Xprint:all, and then decompiled them with JAD to see what everything looks like under the covers.
I was initially going to write a summary here, but if you want to know how things work under the hood, I think it helps to work through the examples, so for today I’ll leave that as an exercise for the reader.
Java Design Patterns FAQ: Can you provide an example of the Factory Pattern in Java?
Sure. In this article we'll look at a small-but-complete example of the Factory Pattern ("Factory Design Pattern") implemented in Java.
Twitter UI redesign: After using Twitter for the last several months, it seems like their user interface is out of sync with how I want to use their data. As I was waiting for dinner to cook last night I spent a few minutes thinking about the Twitter user interface (UI) and how I might redesign it. Here are my thoughts.
The current Twitter web interface
The things the Twitter web interface does well currently are:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 17.7, “How to write Scala traits so they can be used from Java code.”
You’ve written a Scala trait with implemented methods and need to be able to use those methods from a Java application.
You can’t use the implemented methods of a Scala trait from Java, so wrap the trait in a class.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 8.9, “How to extend a Java interface like a Scala trait.”
You want to implement a Java interface in a Scala application.
In your Scala application, use the
with keywords to implement your Java interfaces, just as though they were Scala traits.
Given these three Java interfaces:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.7, “How to declare that a Scala trait can only be mixed into a type that has a specific method.”
You only want to allow a trait to be mixed into a type (class, abstract class, or trait) that has a method with a given signature.
Use a variation of the self-type syntax that lets you declare that any class that attempts to mix in the trait must implement the method you specify.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.5, “How to limit which classes can use a trait by inheritance.”
You want to limit a trait so it can only be added to classes that extend a superclass or another trait.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.4, “How to use Scala traits as simple mixins (or, How to mix in Scala traits).”
You want to design a solution where multiple traits can be mixed into a class to provide a robust design.
To implement a simple mixin, define the methods you want in your trait, then add the trait to your class using
with. For instance, the following code defines a
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 8.3, “How to use a Scala trait like an abstract class.”
You want to use a trait as something like an
abstract class in Java.
Define methods in your trait just like regular Scala methods. In the class that extends the trait, you can override those methods or use them as they are defined in the trait.