标签:模式 string 医院 return 简单工厂模式 修改 public run create
不修改已有代码,而是添加代码
不和所有类打交道,只和总接口的实现类打交道
public class Cilent {
public static void main(String[] args) {
Car c1=new AudiFactory().createCar();
Car c2=new ByadiFactory().createCar();
Car c3=new BenzFactory().createCar();
c1.run();
c2.run();
c3.run();
}
}
接口:
public interface Car {
void run();
}
实现1:
public class Benz implements Car {
public void run()
{
System.out.println("Benz");
}
}
实现2:
public class Byadi implements Car{
public void run()
{
System.out.println("Byadi");
}
}
实现3:
public class Audi implements Car {
public void run()
{
System.out.println("Audi");
}
}
工厂:
接口:
public interface CarFactory {
Car createCar();
}
实现1:
public class AudiFactory implements CarFactory{
public Car createCar()
{
return new Audi();
}
}
实现2:
public class BenzFactory implements CarFactory {
public Car createCar()
{
return new Benz();
}
郑州男科医院:http://www.63556355.com/郑州看男科哪家好:http://www.63556355.com/郑州割包皮多少钱:http://www.63556355.com/
}
实现3:
public class ByadiFactory implements CarFactory {
public Car createCar()
{
return new Byadi();
}
}
标签:模式 string 医院 return 简单工厂模式 修改 public run create
原文地址:https://www.cnblogs.com/sushine1/p/11603812.html