java GOF23设计模式-简单工厂模式进阶
2020-12-13 15:07
标签:create ace 代码 pre tor gof23 java 设计模式 rgs } 接口: } } } } 实现3: java GOF23设计模式-简单工厂模式进阶 标签:create ace 代码 pre tor gof23 java 设计模式 rgs 原文地址:https://blog.51cto.com/14437184/2440448
不和所有类打交道,只和总接口的实现类打交道
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();
}
}
public class ByadiFactory implements CarFactory {
public Car createCar()
{
return new Byadi();
}
}
上一篇:抓包获取百度音乐API
下一篇:JavaScript_资料