Big Data Scala programming. Problem Set (02)

Source: Internet
Author: User

Big Data Scala programming. Problem Set (02)


by Gao

dongting International Intelligent hardware Testing base chief architect, & Cloud Big Data Center (IDC)

Micro-Blog: @ Gao _ Taipei


Q-02: scala language trait what are the design implications?

Answer:

We all know the concept of Interface (Interface) and know that a class or module can implement multiple interfaces. Just like a room can have multiple doors, or a courtyard can have multiple doorways. such as:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/56/C3/wKiom1SM0p7gRO4uAADKRTiSOPE682.jpg "title=" qq02_ 01.png "alt=" Wkiom1sm0p7gro4uaadkrtisope682.jpg "/>

The concept of the courtyard to the software, a software class can practice a number of interfaces, such as:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/56/C0/wKioL1SM03vygOLMAABQoRJ-gwg376.jpg "title=" qq02_ 02.png "alt=" Wkiol1sm03vygolmaabqorj-gwg376.jpg "/>

Now, let's take a look at the design architecture of a class and an interface, such as:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/56/C3/wKiom1SM0wfTSFiDAAB3es6DqvE969.jpg "title=" qq02_ 03.png "alt=" Wkiom1sm0wftsfidaab3es6dqve969.jpg "/>

in general software design, the interface (Interface) means: it is a purely abstract class. In other words, it contains one or more abstract functions, and is a common (public) function. Gamma and others in their Design Patterns book advocate:


 " Program to a interface, not an implementation ."

(Write programming for interfaces, not for implementations.) )


This means that the architect (team) usually designs the interface first, and then the engineer (team) develops the implementation code in accordance with the definition of the interface. As a result, the team of architects and engineers, two teams are working together (collaboration).

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/56/C3/wKiom1SM07fDBp1kAACtkx2mgkc079.jpg "title=" qq02_ 04.png "alt=" Wkiom1sm07fdbp1kaactkx2mgkc079.jpg "/>

Because the architect team often contains developers (or architects and engineers), they often write code to implement abstract functions in the interface, providing some interface defaults (default behavior). At this point, the architecture you are designing becomes:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/56/C0/wKioL1SM1H7Q4pq7AACgbQqvf8Q770.jpg "title=" qq02_ 05.png "alt=" Wkiol1sm1h7q4pq7aacgbqqvf8q770.jpg "/>

The IA interface and the Ia_impl class are considered as a whole, especially as features (Trait), such as:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/56/C3/wKiom1SM1AOC513MAACQJYBn4G4923.jpg "title=" qq02_ 06.png "alt=" Wkiom1sm1aoc513maacqjybn4g4923.jpg "/>

The above is thought from the perspective of architectural design, if the corresponding to the Scala code, can be expressed as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/56/C0/wKioL1SM1QGDAigvAABmpLyvYGc967.jpg "title=" qq02_ 07.png "alt=" Wkiol1sm1qgdaigvaabmplyvygc967.jpg "/>

Definition of trait

Trait TA {

Def transact () { //default behavior code

Ontransact //Call abstract function

}

def ontransact //abstract function

}

Definition of Class

Class Evaplane extends TA {

var manufacturer:string = ""

var capacity:int = 0

var mfr_date:string = ""

      

def pr_capacity {

println (capacity)

def ontransact {

println ("This is Ontransact.")

}

MyApp

Object MYAPP {

def main (args:array[string]) {

val p1 = new Evaplane

val P2 = new Evaplane

P1.transact

P2.transact

}


because of Scala code compilation (Compile), it translates into the byecode that the JVM engine can perform, which is limited by the single-inheritance of the JVM engine. In the Scala language, it is not possible to practice the architecture of the architect's multiple interfaces and their default behavior, as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/56/C3/wKiom1SM1ITxLAsHAACrUHzklZU110.jpg "title=" qq02_ 08.png "alt=" Wkiom1sm1itxlashaacruhzklzu110.jpg "/>

However, it is possible to use Scala's trait to practice it as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/56/C3/wKiom1SM1TPQkAmfAACLI5AenS8168.jpg "title=" qq02_ 09.png "alt=" Wkiom1sm1tpqkamfaacli5aens8168.jpg "/>

Scala relies on the JVM engine's multi-Interface Implementation (Implement) mechanism, as well as the object delegation (delegation) mechanism to practice the trait extends mechanism, rather than relying on the original class extends mechanism of the JVM engine. Such as:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/56/C0/wKioL1SM1fiyKBBRAACv6Yisetw082.jpg "title=" qq02_ 10.png "alt=" Wkiol1sm1fiykbbraacv6yisetw082.jpg "/>

Use the JVM engine-level implement and delegation mechanisms to practice Scala's trait extends mechanism at the source level. Such as:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/56/C0/wKioL1SM1kjzFhMVAACyCdZKLbg560.jpg "title=" qq02_ 11.png "alt=" Wkiol1sm1kjzfhmvaacycdzklbg560.jpg "/>

Thus, on the one hand, the architect team can define its interface (INTERFACE), such as the design of the IA and IB, and compose the default behavior of its interface (behavior) code, such as the Ia_impl and Ib_impl classes in the and express it in the form of Scala's trait.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/56/C3/wKiom1SM1dziAlMhAADRJ-iW_l0892.jpg "title=" qq02_ 12.png "alt=" Wkiom1sm1dzialmhaadrj-iw_l0892.jpg "/>

the trait is then handed over to the team of engineers to write the implementation code for the Evaplane class or its subclasses. We have achieved a good division of work between the team of architects and engineers, and we have also practicedthe program-to-an interface-not-an-implementation. (Write programming for interfaces, not for implementations.) ) Design principles.


Please continue learning: High teacher's video

Related article: Big Data Scala programming Problem set


~ End ~


Big Data Scala programming. Problem Set (02)

Related Article

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.