工厂模式

 

package com.mhm.factory;

import com.mhm.simplefactory.Car;

public interface Driver {
	public Car driverCar();
}

package com.mhm.factory;

import com.mhm.simplefactory.Benz;
import com.mhm.simplefactory.Car;

public class BenzDriver implements Driver {

	@Override
	public Car driverCar() {
		return new Benz();
	}
	
}

package com.mhm.factory;

import com.mhm.simplefactory.Bmw;
import com.mhm.simplefactory.Car;

public class BmwDriver implements Driver {

	@Override
	public Car driverCar() {
		return new Bmw();
	}
}


package com.mhm.factory;

import com.mhm.simplefactory.Car;

public class Magnate {
	public static void main(String[] args) {
		Driver driver = new BenzDriver();
		Car car = driver.driverCar();
		car.drive();
	}
}

 

 

你可能感兴趣的:(工厂模式)