Created
August 30, 2011 12:39
-
-
Save bivas/1180801 to your computer and use it in GitHub Desktop.
Factory Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.factory; | |
public interface CarFactory { | |
Car createCar(); | |
} | |
public FordCarFactory implements CarFactory { | |
@Override | |
public Car createCar() { | |
return new FordCar(); | |
} | |
} | |
public ToyotaCarFactory implements CarFactory { | |
@Override | |
public Car createCar() { | |
return new ToyotaCar(); | |
} | |
} | |
public Application { | |
public static void main(String[] args) { | |
final CarFactory carFactory = createFactoryFromProperty(); | |
final Car car = carFactory.createCar(); | |
System.out.println("I got a new car ! My car model is " + car.getModel()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.factory; | |
public final class DressFactory { | |
public static Dress createDress() { | |
if ( isPassedMidnight() ) { | |
return new DirtyDress(); | |
} | |
return new PrincessDress(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.factory; | |
/** | |
* Based on Java's java.util.regex.Pattern implementation | |
*/ | |
public final class Pattern { | |
public enum Flag { | |
UNIX_LINES(0x01), | |
CASE_INSENSITIVE(0x02), | |
// ... some more constants | |
CANON_EQ(0x80); | |
// methods & fields | |
} | |
public static Pattern compile(String regex, Flag ... flags) { | |
int internalFlag = 0; | |
for (flag : flags) { | |
internalFlag |= flag.value(); | |
} | |
return new Pattern(regex, internalFlag); | |
} | |
private Pattern(String regex, int flag) { | |
// initialization code | |
} | |
/* | |
* rest of class method and members | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment