Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the call chain of Trait in Scala

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to implement the call chain of Trait in Scala". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "How to implement the call chain of Trait in Scala"!

2. Call Chain for Trait Implementation

If multiple Ttraits contain the same method, Scala's class inherits multiple Traits and supports calling the same method of all parent Traits, just call the super method at the end of the method.

Define Trait as BaseChecker, and the two sub-Traits are

DataChecker check field is not empty,

LengthChecker checks whether the field length is legal

Field class Field inherits two check classes sequentially respectively

package com.hadoop.ljs.spark.study.TraitTest/** * @author: Created By lujisen * @company ChinaUnicom Software JiNan * @date: 2020-02-15 16:45 * @version: v1.0 * @description: com.hadoop.ljs.spark.study.TraitTest */ trait BaseChecker{ /* Here is a knowledge point to supplement the explanation, if checkResult() If it is an abstract method, when the inheritance class is called by super., */ /* needs to add the abstract keyword, otherwise the compiler will report an error */ def checkResut(name:String): Unit ={ println("BaseChecker "+name) }}trait DataChecker extends BaseChecker { override def checkResut(name:String): Unit ={ println("DataChecker: data is not empty"+name) //this must call super.checkResut() super.checkResut(name) }}trait LengthChecker extends BaseChecker { override def checkResut(name:String): Unit ={ println("LengthChecker: field length is legal"+name) //this must call super.checkResut() super.checkResut(name) }}class Field(data:String) extends LengthChecker with DataChecker { def printResult(): Unit ={ println("field is: "+data) checkResut(data) }}

Main function test class:

package com.hadoop.ljs.spark.study.TraitTest/** * @author: Created By lujisen * @company ChinaUnicom Software JiNan * @date: 2020-02-15 16:50 * @version: v1.0 * @description: com.hadoop.ljs.spark.study.TraitTest */object TraitChainTest { def main(args: Array[String]): Unit = { val field=new Field("field1") //Here DataChecker, LengthChecker are called from right to left in order of inheritance //, finally call BaseChecker to print field.printResult() }}

The resulting output is:

Input field is: field1DataChecker: data is not empty field1LengthChecker: field length is legal field1BaseChecker field1

According to the result, we can see that the function printResult() will call the unified function checkResut() of the parent Trait from right to left according to the inheritance order of the class Field, and finally call the function checkResut() in BaseChecker. This feature can be used to verify the data. Different Traits implement different verification divisions. This is similar to the concrete implementation of the chain of responsibility pattern in java.

Related knowledge supplement:

Java Chain of Responsibility:

As the name suggests, the Chain of Responsibility Pattern creates a chain of receiver objects for a request. This pattern gives the type of request and decouples the sender and receiver of the request. This type of design pattern is a behavioral pattern.

In this pattern, typically each receiver contains a reference to another receiver. If an object cannot handle the request, it passes the same request on to the next recipient, and so on.

At this point, I believe everyone has a deeper understanding of "how to implement Trait's call chain in Scala," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report