TV:
public abstract class TV { public abstract void play(); }
HaierTV:
public class HaierTV extends TV{ @Override public void play() { System.out.println("This is HaierTV!"); } }
HisenseTV:
public class HisenseTV extends TV{ @Override public void play() { System.out.println("This is HisenseTV!"); } }
TVFactory:
public class TVFactory { public static TV produceTV(String brand){ if(brand.equalsIgnoreCase("HaierTV")){ return new HaierTV(); }else{ return new HisenseTV(); } } }
TVFactory:
public class SimpleFactory { public static void main(String[] args) { TV tv_1 = TVFactory.produceTV("HaierTV"); TV tv_2 = TVFactory.produceTV("HisenseTV"); tv_1.play(); tv_2.play(); } }
Result:
This is HaierTV!
This is HisenseTV!