package com.ruanyun;
/**
* @Auther: maxw
* @Date: 2018/11/12 10:42
* @Description:简单工厂模式:一个抽象的接口,多个抽象接口的实现类,一个工厂类,用来实例化抽象的接口。
*/
public class Test5 {
public static void main(String args[]){
Car car = CarFactory.getInstance("bc");
if(car != null){
car.run();
car.stop();
}else {
System.out.println("该工厂造不出这种车。。。");
}
}
}
interface Car{
void run();
void stop();
}
class Benz implements Car{
@Override
public void run() {
System.out.println("宝马跑起来。。。");
}
@Override
public void stop() {
System.out.println("宝马刹车。。。");
}
}
class Benc implements Car{
@Override
public void run() {
System.out.println("奔驰跑起来。。。");
}
@Override
public void stop() {
System.out.println("奔驰刹车。。。");
}
}
//汽车工厂
class CarFactory{
public static Car getInstance(String type){
Car car = null;
if("bm".equals(type)){
car = new Benz();
}
if("bc".equals(type)){
car = new Benc();
}
return car;
}
}