程式扎記: [Scala IA] The Basics : Ch3. OOP in Scala - Case class

標籤

2016年8月13日 星期六

[Scala IA] The Basics : Ch3. OOP in Scala - Case class

Case class (p78) 
Case classes are a special kind of class created using the keyword case. When the Scala compiler sees a case class, it automatically generates boilerplate code so you don’t have to do it. Here’s an example of a Person class: 
scala> case class Person(firstName:String, lastName:String)
defined class Person

In this code example, you’re creating a Person case class with firstName and lastName parameters. But when you prefix a class with case, the following things will happen automatically: 
* Scala prefixes all the parameters with val, and that will make them public value. But remember that you still never access the value directly; you always access through accessors.
* Both equals and hashCode are implemented for you based on the given parameters.
* The compiler implements the toString method that returns the class name and its parameters.
* Every case class has a method named copy that allows you to easily create a modified copy of the class’s instance. You’ll learn about this later in this chapter.
* A companion object is created with the appropriate apply method, which takes the same arguments as declared in the class.
* The compiler adds a method called unapply, which allows the class name to be used as an extractor for pattern matching (more on this later).
* A default implementation is provided for serialization:
scala> val me = Person("Lee", "John")
me: Person = Person(Lee,John)

scala> val myself = Person("Lee", "John")
myself: Person = Person(Lee,John)

scala> me.equals(myself)
res0: Boolean = true

scala> me.hashCode
res1: Int = -1614407238

scala> myself.hashCode
res2: Int = -1614407238

Now think about how many times you’ve created a data transfer object (DTO) with only accessors for the purpose of wrapping some data. Scala’s case classes will make that easier for you the next time. Both equals and hashCodeimplementations also make it safer to use with collections. 
NOTE. 
You’re allowed to prefix the parameters to the case class with var if you want both accessors and mutators. Scala defaults it to val because it encourages immutability.

Like any other class, a case class can extend other classes, including trait and case classes. When you declare an abstract case class, Scala won’t generate the apply method in the companion object. That makes sense because you can’t create an instance of an abstract class. You can also create case objects that are singleton and serializable: 
  1. trait Boolean  
  2. case object Yes extends Boolean  
  3. case object No extends Boolean  
Scala case classes and objects make it easy to send serializable messages over the network. You’ll see a lot of them when you learn about Scala actors. 
NOTE. 
From Scala 2.8 on, case classes without a parameter list are deprecated. If you have a need, you can declare your case class without a parameter. Use () as a parameter list or use the case object.

Let’s put your recently gained knowledge of case classes to use in the MongoDB driver. So far, you’ve implemented basic find methods in your driver. It’s great, but you could do one more thing to the driver to make it more useful. MongoDB supports multiple query options like Sort, Skip, and Limit that you don’t support in your driver. Using case classes and a little pattern matching, you could do this easily. You’ll add a new finder method to the collection to find by query and query options. But first, let’s define the query options you’re going to support: 
- QueryOption.scala 
  1. package ch3  
  2.   
  3. import com.mongodb.DBObject  
  4.   
  5. sealed trait QueryOption   
  6.   
  7. case object NoOption extends QueryOption  
  8. case class Sort(sorting: DBObject, anotherOption: QueryOption) extends QueryOption  
  9. case class Skip(number: Int, anotherOption: QueryOption) extends QueryOption  
  10. case class Limit(limit: Int, anotherOption: QueryOption) extends QueryOption  
Here you’re creating four options: SortSkipLimit, and NoOption. The NoOption case is used when no option is provided for the query. Each query option could have another query option because you’ll support multiple query options at the same time. The Sort option takes another DBObject in which users can specify sorting criteria. Note that all the option case classes extend an empty trait, and it’s marked as sealed. I’ll talk about modifiers in detail later in the chapter, but for now a sealed modifier stops everyone from extending the trait, with a small exception. To extend a sealed trait, all the classes need to be in the same source file

For the Query class, you’ll wrap your good old friend DBObject and expose methods like sort, skip, and limit so that users can specify query options: 
- Query.scala 
  1. package ch3  
  2.   
  3. import com.mongodb.DBObject  
  4.   
  5. case class Query(q: DBObject, option: QueryOption = NoOption) {  
  6.   def sort(sorting: DBObject) = Query(q, Sort(sorting, option))  
  7.   def skip(skip: Int) = Query(q, Skip(skip, option))  
  8.   def limit(limit: Int) = Query(q, Limit(limit, option))  
  9. }  
Here each method creates a new instance of a query object with an appropriate query option so that, like a fluent interface (http://martinfowler.com/bliki/Fluent Interface.html), you can chain the methods together as in the following: 
  1. var rangeQuery = new BasicDBObject("i"new BasicDBObject("$gt"20))  
  2. var richQuery = Query(rangeQuery).skip(20).limit(10)  
Here you’re searching documents for which the i > 20 condition is true. From the result set you skip 20 documents and limit your result set to 10 documents. The most extraordinary part of the code is the last parameter of the Queryclass: option: QueryOption = NoOption. Here you’re assigning a default value to the parameter so that when the second parameter isn’t specified, as in the previous snippet, the default value will be used. You’ll look at default parameters in the next section. I’m sure that, as a focused reader, you’ve already spotted the use of the companion object that Scala generates for case classes. When creating an instance of a case class, you don’t have to use new because of the companion object. To use the new query class, add the following new method to the ReadOnly trait: 
  1. def find (query: Query) = { "..." }  
Before discussing implementation of the find-by-query method, let’s see how case classes help in pattern matching. You’ll be using pattern matching to implement the method. You learned about pattern matching in chapter 2, but I haven’t discussed case classes and how they could be used with pattern matching. One of the most common reasons for creating case classes is the pattern-matching feature that comes free with case classes. Let’s take the Person case class once again, but this time you’ll extract firstName and lastName from the object using pattern matching: 
scala> val p = Person("Lee", "John")
p: Person = Person(Lee,John)

scala> p match { case Person(first, last) => println(">>>> " + first + ", " + last) }
>>>> Lee, John

Look how you extracted the first and last names from the object using pattern matching. The case clause should be familiar to you; here you’re using a variable pattern in which the matching values get assigned to the first and lastvariables. Under the hood, Scala handles this pattern matching using a method called unapply. If you have to handcode the companion object that gets generated for Person, it will look like following: 
  1. object Person {  
  2.     def apply(firstName:String, lastName:String) = {  
  3.         new Person(firstName, lastName)  
  4.     }  
  5.     def unapply(p:Person): Option[(String, String)] =  
  6.         Some((p.firstName, p.lastName))  
  7.     }  
  8. }  
The apply method is simple; it returns an instance of the Person class and it is called when you create an instance of a case class. The unapply method gets called when the case instance is used for pattern matching.Typically, the unapply method is supposed to unwrap the case instance and return the elements (parameters used to create the instanceof the case class. I’ll talk about the Option type in Scala in detail in the next chapter, but for now think of it as a container that holds a value. If a case class has one element, the Option container holds that value. But because you have more than one, you have to return a tuple of two elements. 
NOTE. 
Sometimes instead of unapply, another method called unapplySeq could get generated if the case class parameters end with a repeated parameter (variable argument). I’ll discuss that in a later chapter.

In the discussion of for-comprehensions in chapter 2, I didn’t mention that the generator part of for-comprehensions uses pattern matching. I can best describe this with an example. Here you’re creating a list of persons and looping through them using pattern matching: 
scala> val people = List(Person("Simon", "Kish"), Person("Eric", "Weimer"))
people: List[Person] = List(Person(Simon,Kish), Person(Eric,Weimer))

scala> for(Person(first, last) <- first="" font="" last="" people="" yield="">
res4: List[String] = List(Simon,Kish, Eric,Weimer)

You’ll see more examples of extractors and pattern matching throughout the book. Before we leave this section, I still owe you the implementation of the find-by-query method, so here you go (see the following listing). 
- Listing 3.10 ReadOnly trait 
  1. trait ReadOnly {  
  2.   val underlying: MongoDBCollection  
  3.   def name = underlying getName  
  4.   def fullName = underlying getFullName    
  5.   def find(doc: DBObject): DBCursor = underlying find doc    // Explicitly specify return type  
  6.   def findOne(doc: DBObject) = underlying findOne doc  
  7.   def findOne = underlying findOne  
  8.   def getCount(doc: DBObject) = underlying getCount doc  
  9.   // Find method takes query object  
  10.   def find(query: Query): DBCursor = {  
  11.     def applyOptions(cursor:DBCursor, option: QueryOption): DBCursor = {  
  12.       option match {  
  13.         case Skip(skip, next) => applyOptions(cursor.skip(skip), next)  
  14.         case Sort(sorting, next)=> applyOptions(cursor.sort(sorting), next)  
  15.         case Limit(limit, next) => applyOptions(cursor.limit(limit), next)  
  16.         case NoOption => cursor  
  17.       }  
  18.     }  
  19.     applyOptions(find(query.q), query.option)  
  20.   }  
  21. }  
Here you’re using pattern matching to apply each query option to the result returned by the find method—in this case, DBCursor. The nested applyOptions function is applied recursively because each query option could wrap another query option identified by the next variable, and you bail out when it matches NoOption

When it comes to overload methods (methods with the same name), you have to specify the return type; otherwise, the code won’t compile. You have a similar limitation for recursive method calls. Scala type inference can’t infer the type of recursive methods or functions. In case of type errors, it’s always helpful to add type information. Using the test client in the following listing, you could test your new finder method. 

- Listing 3.11 TestFindByQuery.scala 
  1. import com.scalainaction.mongo._  
  2. import com.mongodb.BasicDBObject  
  3. def client = new MongoClient  
  4. def db = client.db("mydb")  
  5. val col = db.readOnlyCollection("test")  
  6. val updatableCol = db.updatableCollection("test")  
  7. for(i <- nbsp="" span="">1 to 100) updatableCol += new BasicDBObject("i", i)  
  8. val rangeQuery = new BasicDBObject("i"new BasicDBObject("$gt"20))  
  9. val richQuery = Query(rangeQuery).skip(20).limit(10)  
  10. val cursor = col.find(richQuery)  
  11. while(cursor.hasNext()) {  
  12.     println(cursor.next());  
  13. }  
 

Supplement 
Gossip@DesignPattern : Behavioral - Visitor 模式 
Scala Gossic : 繼續深入 - 模式比對 (案例類別)

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!