Skip to content

Instantly share code, notes, and snippets.

@bivas
Created August 30, 2011 12:39
Show Gist options
  • Save bivas/1180801 to your computer and use it in GitHub Desktop.
Save bivas/1180801 to your computer and use it in GitHub Desktop.
Factory Pattern
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());
}
}
package com.example.factory;
public final class DressFactory {
public static Dress createDress() {
if ( isPassedMidnight() ) {
return new DirtyDress();
}
return new PrincessDress();
}
}
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