Artikel mit Tag traits

myndian.de

Artikel mit Tag traits

Verwandte Tags

object orientation performance mixins jade adom scala java composition rpg optimization groovy fun c# nice

Samstag, 7. Februar 2009

Toying around ... with Scala

After a long time here is a post about Scala again. I played around with composition of objects an classes from traits.


This post is also a reaction to Thomas Biskups post Toying around ..., where he played around with Qi4j to perhaps find a better architecture for his rogue like rpg “JADE”. We sometimes talking about this and I once mentioned Scalas traits.


So here is a little example of object and class composition with traits in Scala from the domain of role play games. I implemented some simple traits which I used to compose a class and a (singelton) object.


package de.myndian.scala.rpg

// Something that has a “name”
trait Named
{
  def name : String
}

// Something that has “power”
trait Powered
{
  def power : Int
}

// A namend thing that can be hitted 
trait Hitable
{
  this: Named =>
  
  var hitpoints : Int
  
  def hit(points : Int)
  {
    hitpoints -= points
    print(name + “ was hitted with ” + points + “ points. ”) 
    if(hitpoints <= 0)
      println(name + " is dead.")
    else
      println(name + " has " + hitpoints + " hitpoints remaining.")
  }
}

// A named thing that can use its power to hit a hittable
trait Fighter
{
  this : Named with Powered =>
  
  val rnd = new Random(System.currentTimeMillis)
  
  def hit(target : Named with Hitable)
  {
    println(name + “ hits ” + target.name)
    target.hit(rnd.nextInt(power))
  }
}

class Door(val name : String, var hitpoints : Int) 
    extends Named
    with Hitable


// In this fight the dwarf “kills” a door 
object DemoFight
    extends Application
{
  var aDoor = new Door(“a door”, 10)

  object TheDwarf
      extends Named
      with Hitable
      with Fighter
      with Powered
  {
    val name = “the dwarf”
    val power = 10
    var hitpoints = 20
  }

  while(aDoor.hitpoints > 0)
    TheDwarf.hit(aDoor)
}

Some sample output:

the dwarf hits a door
a door was hitted with 3 points. a door has 7 hitpoints remaining.
the dwarf hits a door
a door was hitted with 7 points. a door is dead.

So Thomas, what about “SADE”? ;-)

Geschrieben von Jörg in Softwaretechnik um 15:41 | Kommentare (0) | Trackbacks (0)
Tags für diesen Artikel: adom, composition, jade, mixins, object orientation, rpg, scala, traits
Artikel mit ähnlichen Themen:
  • Premature optimization and object orientation
  • last language war language trolling
  • Scala makes things "just work"
(Seite 1 von 1, insgesamt 1 Einträge)

Suche

Inhalt

Startseite
Galerien
Impressum

Kategorien

  • XML Alltag
  • XML Internet
  • XML Musik
  • XML Politik
  • XML Softwaretechnik
  • XML Sonstiges
  • XML Visuelles

Alle Kategorien

Archive

Juli 2010
Juni 2010
Mai 2010
Das Neueste ...
Älteres ...

Blog abonnieren

XML RSS 2.0 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Kommentare

Login

Verwaltung des Blogs

Login

Aktuelle Einträge

Netzwerkkultur verändert die Gesellschaft
Dienstag, 17. November

Absolute and relative date and time
Sonntag, 18. Oktober

Oren Lavie - Her Morning Elegance
Dienstag, 6. Oktober

Twitter & Blogroll
Samstag, 8. August

Read It Later: Round-Trip-Integration mit Firefox und Google-Reader
Montag, 3. August

Blogroll

* Jörg bei Twitter
* Jens bei Twitter
* Nils bei Twitter

* Beetlebum
* a life less ordinary?
* Martin Fowler's Bliki
* Springify
* BILDblog
* Plazeboalarm
* LawBlog
* ADOM Blog
* Being busy
* Dr. Gero Presser

Links

* Heise
* The Scala Programming Language
Nils' Fotos bei fotocommunity.de
Jogi auf Qype
Get Firefox!
Use OpenOffice.org

Heise News

* Alcatel-Lucent bleibt in den roten Zahlen

* IBM kauft Kompressionsspezialisten

* Speicherhersteller erwarten Preissteigerung

* Britischer Datenschützer findet in Googles WLAN-Datensammlung keine persönliche Daten

* Österreich wegen verpasster Einführung der Vorratsdatenspeicherung verurteilt

kostenloser Counter