Learn Scala's definition factory objects

Source: Internet
Author: User

You now have the class hierarchy of the layout elements. This level can be presented to your customers "according to the original". But you might choose to hide the hierarchy behind the factory object. The Factory object contains methods for building other objects. Customers and benefits use these factory methods to implement object construction rather than directly using the new construct object. One advantage of this approach is that object creation can be centralized and objects actually represent the details of the class can be hidden. This kind of concealment simplifies the client's understanding of your library, because less detail is exposed, and on the other hand gives you more opportunities to change the implementation of the library later without damaging the client code.

The first task of building a factory for a layout element is to choose where the factory method should be placed. Should they be a single Instance object member or a class member? What should the object or class containing them call? There are many possibilities here. The most straightforward scenario is the factory method that creates the companion object of the class element and makes it into a layout element. For this way, the only thing you want to expose to the client is the element's class/object portfolio, which hides its three implementation classes Arrayelement,lineelement and Uniformelement.

Code 10.10 follows the design of this scenario. The element companion object contains three overloaded Elem method variants. Each variant builds a different layout object.

Object element {
def elem (contents:array[string]): Element =
new Arrayelement (contents)
def elem (Chr:char , Width:int, height:int): element =
new Uniformelement (CHR, width, height)
def elem (line:string): element =
  
   new lineelement (line)
}
  

Code 10.10 factory objects with factory methods

These factory methods make it possible to change the implementation of the class element by using the Elem factory method instead of explicitly creating a new arrayelement instance. To invoke the factory method without using the name of a single Instance object, Element, authentication, we will refer to Element.elem on the top of the source file. In other words, instead of using Element.elem to invoke the factory method inside the element class, we'll refer to Element.elem so that we can invoke the factory method simply by using their simplified name, Elem. Code 10.11 shows how the class element looks after these changes.

Import Element.elem
abstract class Element {
def contents:array[string]
def width:int =
if (height = 0) 0 Else contents (0). length
def height:int = Contents.length
def above (that:element): Element =
Elem (this.co Ntents + + that.contents
def beside (that:element): Element =
Elem (
for (
line1, line2) <-this.co ntents zip that.contents
) yield line1 + line2
)
override def toString = contents mkstring "\ n"
}

Code 10.11 refactoring to use a factory method class element

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.