//TV接口
public interface TV{
public void play();
}
//TV工厂接口
public interface TVFactory{
public TV productTV();
}
//HaierTV实现TV接口
public class HaierTV implements TV{
public void play(){
System.out.println("-----------HaierTV------------");
}
}
//TclTV实现TV接口
public class TclTV implements TV{
public void play(){
System.out.println("-----------TclTV------------");
}
}
//HaierTV工厂类 实现TV工厂接口
public class HaierTVFactory implements TVFactory{
public TV productTV(){
System.out.println("HaierTVFactory create HaierTV");
return new HaierTV();
}
}
//TclTV工厂类 实现TV工厂接口
public class TclTVFactory implements TVFactory{
public TV productTV(){
System.out.println("TclTVFactory create TclTV");
return new TclTV();
}
}
//客户端类
public class ClientFactory{
public static void main(String[] args) {
try{
TV tv;
TVFactory factory =new HaierTVFactory();
tv = factory.productTV();
tv.play();
TV tv1;
TVFactory factory1 =new TclTVFactory();
tv1 = factory1.productTV();
tv1.play();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
结果:
HaierTVFactory create HaierTV
-----------HaierTV------------
TclTVFactory create TclTV
-----------TclTV------------
转载于:https://my.oschina.net/u/1401580/blog/225386