且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

工厂设计模式。。应用

更新时间:2022-08-13 07:43:05

interface Service{
 void service() ;
}
interface  ServiceFactory{
 Service getService() ;

class Product1 implements Service{
 @Override
 public void service() {
  System.out.println("产品1服务中.....");
 }
}   
class ProductFactory1 implements ServiceFactory{
 @Override
 public Service getService() {
  // TODO Auto-generated method stub
  return new Product1();
 } 
}
class Product2 implements Service{
 @Override
 public void service() {
  System.out.println("产品2服务中.....");
 }
}   
class ProductFactory2 implements ServiceFactory{
 public Service getService() {
  // TODO Auto-generated method stub
  return new Product2();
 } 
}
public class FactoryTest
{    
  public static void service(ServiceFactory src){
   Service s=src.getService();
   s.service() ;
  }
   public static void main(String []agrs){
    FactoryTest.service(new ProductFactory1())  ;
    FactoryTest.service(new ProductFactory2() ) ;
   }

}