2013-06-23 7 views
7

मुझे स्कैला में अपरिवर्तनीय प्रधानाध्यापकों के साथ रहने के लिए सीखने में बहुत सारी समस्याएं चलती हैं, जिनमें से एक प्रश्न में वस्तु से क्लोनिंग (या बल्कि व्युत्पन्न) की अवधारणा है।अपरिवर्तनीय कक्षाएं और लक्षण?

इन समस्याओं में से एक लक्षण में मिश्रित की अवधारणा है - उदाहरण के

trait helper //modifies some kind behavior.. assume we want to to continue down the line 

class A (val x:int) { 

def add(y:int) = new A(x + y) 
} 

Example extends App { 

val a =new A(5) with helper 
val b = a.add(10) // mixed trait disappears 

} 

अब यह एक अधिक जटिल समस्या मैं पहले से ही विभिन्न कक्षाओं, कारखाने तरीकों को छिपाने के लिए चारों ओर आज का निर्माण किया था की सिर्फ एक सच में सरल संस्करण है कक्षा ए, आदि। यदि हम केवल एक विशेषता से निपट रहे हैं, तो मुझे पता है कि मैं बस इसके लिए परीक्षण कर सकता हूं और इसे आवश्यकतानुसार भेज सकता हूं। लेकिन 3 या अधिक लक्षणों के ऊपर मौजूद होने पर मैं क्या कर सकता हूं? मुझे उन सभी संयोजनों का परीक्षण करना होगा जो अवास्तविक हैं।

कार्यात्मक डिज़ाइन प्रिंसिपल का पालन करते समय आप विभिन्न गुणों के साथ एक मौजूदा ऑब्जेक्ट को तत्काल (क्लोन) कैसे करते हैं और/या इसके कुछ पहलू को संशोधित करते हैं?

बहुत धन्यवाद, - टिम

+0

यह बिल्कुल सरल नहीं है! यह एक प्रमुख कारण है कि मैं केक पैटर्न से बचने की कोशिश क्यों करता हूं - एक ही केक को फिर से बनाना वास्तव में मुश्किल है, खासकर अगर आप फ्लाई पर केक बनाते हैं। यह आमतौर पर आसान होता है यदि व्यवहार को संशोधित करने वाली चीज डेटा है (उदाहरण के लिए कक्षा में 'वैल', शायद निर्माता में)। –

+0

मैं सहमत हूं और दुख की बात है कि मैं विशेषता सजावट पैटर्न से भी क्यों बचता हूं ... यह एक किताब में बहुत अच्छा लग रहा है, लेकिन अकादमिक किताबें कभी भी इस बात को संबोधित नहीं करतीं कि ऑब्जेक्ट को क्लोन करने के बाद मैंने यहां बताया है। – LaloInDublin

+0

आपको मूल रूप से ऑब्जेक्ट के लिए एक निर्माता बनाना है, जो हर बार जब आप विशेषता में मिश्रण करना चाहते हैं तो बॉयलरप्लेट की एक और तीन या चार पंक्तियां (जब मैं इसे करता हूं) जोड़ता हूं। कभी-कभी यह अभी भी इसके लायक है। अक्सर यह नहीं है। –

उत्तर

3

संग्रह निहित बिल्डरों जो जानते हैं कि आप के साथ शुरू कर रहे हैं क्या से अपने लक्ष्य प्रकार का उत्पादन करने के लिए कैसे उपयोग करता है। वे दो प्रकार हमेशा एक ही चीज़ नहीं होते हैं।

typesafe बिल्डरों को नियंत्रित क्या उत्पादन किया जा सकता है, जैसे के बारे में कई संबंधित पोस्ट कर रहे हैं, http://nullary.blogspot.com/2011/10/builder-pattern-revisited-in-scala.html

इससे संबंधित एक सवाल यह है कि क्या आप मिश्रित प्रकार के एक संग्रह है यदि: Polymorphic updates in an immutable class hierarchy

मूल्य पर धुरी, एन्कोडिंग प्रकारों के बजाय मूल्यों को ट्रैक करना What is the Scala equivalent to a Java builder pattern?

अपडेट किया गया: कुछ ऐसा ही समय के दौरान आया था। पैटर्न में आरईएस का उपयोग एमएल और here पर वर्णित है।

package object interpat { 
    implicit class MySContext (val sc : StringContext) { 
    object mys { 
     def apply (args : Any*) : String = sc.s (args : _*) 
     def unapplySeq (s : String) : Option[Seq[String]] = { 
     val regexp = sc.parts.mkString ("(.+)").r 
     regexp.unapplySeq (s) 
     } 
    } 
    } 
    implicit class SBContext (val sc : StringContext) { 
    def sb(args: Any*): SB = new SB(sc.s (args : _*)) 
    } 
    implicit class toHasPattern(sb: SB) { 
    def /(pp: String) = new SB(sb.s) with HasPattern { 
     val p = pp 
    } 
    } 
    implicit class toHasRepl(hasp: SB with HasPattern) { 
    def /(rr: String) = new SB(hasp.s) with HasPattern with HasRepl with Updateable { 
     val p = hasp.p 
     val r = rr 
    } 
    } 
    // disallow it 
    implicit class noway(hasr: SB with HasPattern with HasRepl) { 
    def /(rr: String) = ??? 
    } 
    implicit class noway2(hasr: SB with HasPattern with HasRepl) { 
    def /(rr: String) = ??? 
    } 
} 

प्रकार पैरामीटर के साथ implicits को नियंत्रित करने के उपयोग और विकल्प के साथ।

package interpat { 
    import scala.util.Try 
    object I { def unapply(x: String): Option[Int] = Try(x.toInt).toOption } 

    trait XX { 
    type HasIt 
    type Yes <: HasIt 
    type No <: HasIt 
    } 
    object XX extends XX { 
    implicit class XContext (val sc : StringContext) { 
     def x(args: Any*) = new X[No, No] { 
     val s = sc.s(args : _*) 
     } 
    } 
    implicit class xPat(x: X[No, No]) { 
     def /(pp: String) = new X[Yes, No] with HasPattern { 
     val s = x.s 
     val p = pp 
     } 
    } 
    implicit class xRep(x: X[Yes, No] with HasPattern) { 
     def /(rr: String) = new X[Yes, Yes] with HasPattern with HasRepl { 
     val s = x.s 
     val p = x.p 
     val r = rr 
     override def toString = s replaceAll (p, r) 
     } 
    } 
    implicit class xable(xx: X[Yes, Yes]) { 
     def x = xx.toString 
    } 
    } 
    import XX._ 

    trait X[HasP <: HasIt, HasR <: HasIt] { 
    def s: String 
    } 

    trait HasPattern { 
    def p: String 
    } 

    trait HasRepl { 
    def r: String 
    } 

    trait Updateable { this: HasPattern with HasRepl => 
    def update(p: String, r: String) 
    override def toString = { 
     update(p, r) 
     super.toString 
    } 
    } 

    class SB(val s: String) { 
    final val sb = new StringBuilder(s) 
    def update(p: String, r: String): Unit = 
     sb.replace(0, sb.length, sb.toString.replaceAll(p, r)) 
    override def toString = sb.toString 
    } 

    object Test extends App { 
    val msg = "The sky is blue" match { 
     case mys"The $thing is $colour" => mys"A $colour thing is $thing" 
     case _ => "no match" 
    } 
    println (msg) 
    val mys"The $thing is $colour" = "The sky is blue" 
    Console println s"$thing/$colour" 
    val mys"The ${I(number)} is blue" = "The 3 is blue" 
    Console println s"$number" 
    //sb"The sky is blue".update("blue","red") 
    // no way to get a value out! 
    sb"The sky is blue"("blue") = "red" 
    val ugh = sb"The sky is blue" 
    ugh("blue") = "red" 
    Console println ugh 
    val sx = sb"The sky is $colour"/"blue"/"red" 
    Console println sx 
    //sb"The sky is $colour"/"blue"/"red"/"yellow" 
    Console println sx 
    Console println x"The sky is $colour"/"blue"/"red" 
    Console println (x"The sky is $colour"/"blue"/"red").x 
    //Console println x"The sky is $colour"/"blue"/"red"/"yellow" 
    } 
} 
संबंधित मुद्दे