Scala stacking traits implementation enhancements to methods

Source: Internet
Author: User

Scala stacking traits implementation of enhancements to methods or functions

In simple terms, stacking trait is similar to AOP, which implements the enhancement of the function of the method without modifying the logic of the original method.

For example, if we implement a queue, we can define an abstract class of queues, implement the queue and team operations, write in Scala, like Java,


Abstract class Intqueue {def get (): Int def put (X:int)}

We're going to implement this abstract class,

Class Basicintqueue extends Intqueue {private Val buf = new Arraybuffer[int] override def get () = Buf.remove (0) Overri De def put (x:int) {buf + = x}}

Test it, please.

Object Main87 {def main (args:array[string]) {val queue = new Basicintqueue Queue.put (Ten) queue.put (PRI) Ntln (Queue.get ()) println (Queue.get ())}}

This allows us to implement our functions. But we want to implement more functions on the put method, such as doubling the value of the queue.

You might say that it is possible to modify the logic of the put method directly, but it is not extensible. The better approach can also be to inherit the parent class in the implementation of a specific class, the logic of the Put method is doubled.

Class BasicIntQueue2 extends Intqueue {private Val buf = new Arraybuffer[int] override def get () = Buf.remove (0) Overr IDE def put (x:int) {buf + = 2 * x}}

If the logic changes now, we have to filter the value of the queue, for example, the value is less than or equal to 0, then discard, do not queue, and then double the filtered value. Is it necessary to implement a specific class to implement such a logic? Of course you can do this. In Java, however, the idea of using AOP allows you to write a slice of the put method that doubles the Put method. Add logic, it is necessary to write slices, this is not very good. The ability to implement this in Scala is to stack trait.

We define the doubling of the trait,

Trait doubling extends Intqueue {abstract override def put (X:int) {println ("doubling") super.put (2 * x)}}

Define the trait of the filter,

Trait Filtering extends Intqueue {abstract override def put (X:int) {println ("Filtering") if (x >= 0) Super.pu T (x)}}

These two trait are equivalent to one facet, how can these logic be added to the underlying put method? We can do this.

Class MyQueue00 extends basicintqueue with doubling with Filteringobject Main87 {def main (args:array[string]) {val Q = new MyQueue00 q.put ( -1) q.put (0) q.put (1) println (Q.get ()) println (Q.get ())}}

Run the results,

Filtering

Filtering

Doubling

Filtering

Doubling

0

2

The problem is so elegant that the benefit of doing so is that you can reuse the code very well.

A completed Demo

package com.usoft4import scala.collection.mutable.arraybufferabstract class intqueue  {  def get ():  int  def put (X: int)}class basicintqueue  extends intqueue {  private val buf = new arraybuffer[int]   override def get ()  = buf.remove (0)   override def put (x: Int )  {    buf += x  }}class BasicIntQueue2 extends  Intqueue {  private val buf = new arraybuffer[int]  override  def get ()  = buf.remove (0)   override def put (x: Int)  {     buf += 2 * x  }}trait doubling extends intqueue  {  abstract override def put (X: int)  {    println ("doubling")     super.put (2 * x)   }}trait incrementing extends intqueue  {  abstract override def put (X: int)  {    println ("incrementing")     super.put (x + 1)   }}trait Filtering  Extends intqueue {  abstract override def put (X: INT)  {     println ("Filtering")     if  (x >= 0)  super.put (x)   }}class myqueue0 extends basicintqueue with doublingclass myqueue00  extends basicintqueue with doubling with filteringobject main87 {   def main (args: array[string])  {    val queue =  New basicintqueue    queue.put (    queue.put)      println (Queue.get())     println (Queue.get ())     val q = new  Myqueue00    q.put ( -1)     q.put (0)     q.put (1)     println (Q.get ())     println (Q.get ())     val  myqueue = new myqueue0    myqueue.put (Ten)      println (Myqueue.get ())     val myQueue2 =  (New basicintqueue with  filtering with incrementing)     myqueue2.put ( -1)      println ("Put end")     myqueue2.put (0)     println ("Put end")     myqueue2.put (1)     println ("Put end")      println (Myqueue2.get ())   }}

=====================end=====================

Scala stacking traits implementation enhancements to methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.