工厂模式
interface Human{
public void eat();
public void sleep();
public void beat();
}
// 创建实现类 Male
class Male implements Human{
public void eat(){
System.out.println("Male can eat.");
}
public void sleep(){
System.out.println("Male can sleep.");
}
public void beat(){
System.out.println("Male can beat.");
}
}
//创建实现类 Female
class Female implements Human{
public void eat(){
System.out.println("Female can eat.");
}
public void sleep(){
System.out.println("Female can sleep.");
}
public void beat(){
System.out.println("Female can beat.");
}
}
// 创建普通工厂类
class HumanFactory{
public Human createHuman(String gender){
if( gender.equals("male") ){
return new Male();
}else if( gender.equals("female")){
return new Female();
}else {
System.out.println("请输入正确的类型!");
return null;
}
}
}
// 工厂测试类
public class FactoryTest {
public static void main(String[] args){
HumanFactory factory = new HumanFactory();
Human male = factory.createHuman("male");
male.eat();
male.sleep();
male.beat();
}
}
多个工厂模式
// 多个工厂方法
public class HumanFactory{
public Male createMale() {
return new Male();
}
public Female createFemale() {
return new Female();
}
}
// 工厂测试类
public class FactoryTest {
public static void main(String[] args){
HumanFactory factory = new HumanFactory();
Human male = factory.createMale();
male.eat();
male.sleep();
male.beat();
}
}
静态工厂模式
// 多个工厂方法
public class HumanFactory{
public static Male createMale() {
return new Male();
}
public static Female createFemale() {
return new Female();
}
}
// 工厂测试类
public class FactoryTest {
public static void main(String[] args){
Human male = HumanFactory.createMale();
male.eat();
male.sleep();
male.beat();
}
}
工厂模式应用
java.util.Calendar - getInstance()
java.util.Calendar - getInstance(TimeZone zone)
java.util.Calendar - getInstance(Locale aLocale)
java.util.Calendar - getInstance(TimeZone zone, Locale aLocale)
java.text.NumberFormat - getInstance()
java.text.NumberFormat - getInstance(Locale inLocale)