Scala Akka Road 5 (Scala trait application scenario analysis)

Source: Internet
Author: User
Tags traits

The trait definition in Scala includes a collection of fields, behaviors (methods/functions/actions), and some non-implemented functional interfaces that can be easily extended or mixed into existing classes or abstract classes.

Scala's trait (trait) is a very useful feature that can be better abstracted in the design. The program is more extensible by making programs more focused on their features and better splitting the program into multiple trait modules. Familiar with Java's classmates. Ability to interpret traits as abstract classes. But Scala can mix multiple traits at the same time in a class (using extends or with). Whereas a class in Java can only inherit an abstract class, it is assumed that multiple inheritance is required to implement more than one abstract class. Many of the basic Types feature extensions in Scala use the characteristics of attributes. For example, the Boolean operation in the underlying type and the function extension of the comparison interface are implemented in Ordered[t], the operation of the iterative function is mainly implemented in Traversableonce[+a],gentraversableonce[a], The traversableonce was mixed with gentraversableonce traits. The small elephant feels that many other use traits can better stand on the abstract level of design programs and system frameworks.

Using ordered traits in Scala to implement basic types of <,><=,>= functionality

Trait Ordered[a] extends any with java.lang.comparable[a] {def compare (that:a): Int def <  (that:a): Boolean = (t  His compare that) <  0 def >  (that:a): Boolean = (this compare that) >  0 def <= (that:a): Boolean = (this compare, <= 0 def >= (that:a): Boolean = (this compare that) >= 0 def compareTo (that:a): Int = Co Mpare (That)}

During the overall design phase of the system. When doing a core structure or module abstraction, it is possible to abstract features with similar behavior and attributes to form a trait, and then mix one or more required traits into the top level (super abstract Class) or core or subtype of the type.

The advantage of this is that it can decompose and abstract the main functions of the type according to the characteristics, better task division, and improve the extensibility and maintainability of the later module function.

This is a feature in the hands of each developer. Post-function extensions are more efficient and clear. For example, the development of a consumer e-commerce purchasing power of a data mining system, if users according to the age of user classification simple classification-youth. Middle age, old age. The basic characteristics of measuring user purchasing ability are divided into the following five categories: time processing characteristics. Location processing characteristics, consumption processing characteristics. Commodity type processing characteristics and potential purchase characteristics; Abstract classes define multiple hook functions according to the computed dimension. Realize the evaluation of the user's Purchasing ability Index calculation model, feature aggregation and so on. Subclasses mainly represent different types of user's platform personality information, computational factors. Weights and other basic information. In this way, the core analytic calculation function can be defined in the abstract class to achieve a higher clustering of the function (professional people do professional things).


Let's assume that Java implementations, whether using multiple inheritance or interfaces, are large and not easy to develop and post-maintenance. There are too many functions implemented in an abstract class and it is very difficult to understand maintenance.




Scala Akka Road 5 (Scala trait application scenario analysis)

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.