ICE09 . playing with java, scala, groovy and spring .

Playing with Spring

Archive for September, 2009

Scala Mashup Series: Simple Factory, Factory Method and Abstract Factory

Posted by ice09 on September 15, 2009

Up to now, I identified three different kinds of factory patterns, which are similar to each other.

Simple, but powerful: The Simple Factory

The simple factory in Java is implemented as a static method, which returns a object of a certain type. This object can be newly created or a singleton (in contrast to object creation with constructors, which create new objects always).

Advantages [taken from Effective Java (2nd Edition)]

  1. Factory methods have names
  2. Constructors differ only by signature. With factory methods, the method itself can describe the kind of object created.

  3. Factory methods decide what kind of objects are created
  4. With factory methods, newly created objects can be returned as well as singletons.

  5. Factory methods can return objects of any subtype
  6. Depending on the submitted arguments, the method can decide to return subtypes of the return type (see example below).

  7. Verbosity of creating parameterized type instances is reduced
  8. Redundant specification of the form Map map = new HashMap() is not necessary with factory methods, since the compiler is able to infer the type by type inference.

Disadvantages:

  1. Factory methods do not favor inheritance
  2. Returned (non-public) types of factory methods cannot be subclasses (if they have no public or protected constructors).

  3. Factory methods cannot be identified as creational methods as easy as constructors
  4. This can be leveled by using conventions, eg. valueOf, getInstance, of depending on the context.

public class Factory {

    public static Item createItem( Object decideUpon ) {
        
        int itemType = figureOutItemType( decideUpon );

        switch( itemType ) {
            case Constants.Type1:
                return new Type1();
            case Constants.Type2:
                return new Type2();
            // etc.
        }
    }
}

And now in Scala:

object Factory {

  def createItem( decideUpon:String ):Item = decideUpon match {
    case "type1" => new Item1Type
    case "type2" => new Item2Type
    case _ => error("Unknown option.")	  
  }

}

The real Factory Method

The Factory Method is used to create several concrete objects of one abstract class.
In contrast to the Simple Factory described above, the factory itself is subclassed, therefore a concrete factory which creates concrete classes can be created depending on the context.

public abstract class Factory {

    public abstract Item createItem();

}
public class Item1Factory extends Factory {

    public Item createItem() {
        return new Type1();
    }

}
public class Item2Factory extends Factory {

    public Item createItem() {
        return new Type2();
    }

}

In Scala, the code is almost the same, is might just be simplyfied by the use of pattern matching (not equal to the Java version).

trait Factory {  
 
  def create( decideUpon:String ): Item
 
}
object Item1Factory extends Factory {
 
  def create(): Item = instanceType match {
    case "type1" => new Type1Item
    case _ => error("Unknown option.")	  
  }
 
}

Last, not least: Abstract Factory

In short, an Abstract Factory is used to create families of related objects.
The creation methods inside the Abstract Factories are usually implemented as Factory Methods.

Advertisements

Posted in Patterns | Comments Off on Scala Mashup Series: Simple Factory, Factory Method and Abstract Factory

Scala Mashup Series

Posted by ice09 on September 13, 2009

This is the first post of a series of mashup post which try to combine the following books in a Scala way:

Having completely read the Head First Design Patterns and (partly) the second edition of Effective Java, I highly recommend those two books to Java developers.
I am right now studying Programming in Scala and I am not sure if I can recommend it.

The mashup will try to find common patterns in Effective Java and Head First and try to related these patterns to the Scala world. Hopefully, this approach will represent a different way to learn Scala – highly Java focused and with mixing design patterns and effective programming style in.

Posted in Scala | Leave a Comment »