《어떤 패턴들》-추상적 공장

5868 단어 ext교통.
 package com.macrotea.pattern.factory.abstr;

import com.macrotea.pattern.factory.base.Food;
import com.macrotea.pattern.factory.base.Vehicle;
import com.macrotea.pattern.factory.base.Weapon;


public abstract class AbstractFactory {
	public abstract Food createFood();
	public abstract Vehicle createVehicle();
	public abstract Weapon createWeapon();
}

 
 
package com.macrotea.pattern.factory.ext;

import com.macrotea.pattern.factory.abstr.AbstractFactory;
import com.macrotea.pattern.factory.base.Food;
import com.macrotea.pattern.factory.base.Vehicle;
import com.macrotea.pattern.factory.base.Weapon;
import com.macrotea.pattern.factory.sub.DongfengCar;
import com.macrotea.pattern.factory.sub.LongjingTea;
import com.macrotea.pattern.factory.sub.XiaolongPlane;

public class ChinaFactory extends AbstractFactory{

	@Override
	public Food createFood() {
		return new LongjingTea();
	}

	@Override
	public Vehicle createVehicle() {
		return new DongfengCar();
	}

	@Override
	public Weapon createWeapon() {
		return new XiaolongPlane();
	}


}

 
 
 
package com.macrotea.pattern.factory.ext;

import com.macrotea.pattern.factory.abstr.AbstractFactory;
import com.macrotea.pattern.factory.base.Food;
import com.macrotea.pattern.factory.base.Vehicle;
import com.macrotea.pattern.factory.base.Weapon;
import com.macrotea.pattern.factory.sub.Car;
import com.macrotea.pattern.factory.sub.Gun;
import com.macrotea.pattern.factory.sub.Nut;

public class DefaultFactory extends AbstractFactory{

	@Override
	public Food createFood() {
		return new Nut();
	}

	@Override
	public Vehicle createVehicle() {
		return new Car();
	}

	@Override
	public Weapon createWeapon() {
		return new Gun();
	}


}

 
 
package com.macrotea.pattern.factory.base;

public abstract class Food {
	public abstract void eat();
}

 
 
package com.macrotea.pattern.factory.base;

public abstract class Vehicle {
	public abstract void run();
}

 
 
package com.macrotea.pattern.factory.base;

public abstract class Weapon {
	public abstract void shoot();
}

 
 
package com.macrotea.pattern.factory.sub;

import com.macrotea.pattern.factory.base.Vehicle;

public class Car extends Vehicle{

	@Override
	public void run() {
		System.out.println("Car-run()");
	}

}

 
 
package com.macrotea.pattern.factory.sub;

import com.macrotea.pattern.factory.base.Vehicle;

public class DongfengCar extends Vehicle {

	@Override
	public void run() {
		System.out.println(" ...");
	}

}

 
 
package com.macrotea.pattern.factory.sub;

import com.macrotea.pattern.factory.base.Weapon;

public class Gun extends Weapon{

	@Override
	public void shoot() {
		System.out.println("Gun-shoot()");
	}

}

 
 
package com.macrotea.pattern.factory.sub;

import com.macrotea.pattern.factory.base.Food;

public class LongjingTea extends Food {

	@Override
	public void eat() {
		System.out.println(" ...");
	}

}

 
 
package com.macrotea.pattern.factory.sub;

import com.macrotea.pattern.factory.base.Food;

public class Nut extends Food{

	@Override
	public void eat() {
		System.out.println("Nut-eat()");
	}

}

 
package com.macrotea.pattern.factory.sub;

import com.macrotea.pattern.factory.base.Weapon;

public class XiaolongPlane extends Weapon {

	@Override
	public void shoot() {
		System.out.println(" ...");
	}

}

 
 
package com.macrotea.pattern.factory.test;

import com.macrotea.pattern.factory.abstr.AbstractFactory;
import com.macrotea.pattern.factory.base.Food;
import com.macrotea.pattern.factory.base.Vehicle;
import com.macrotea.pattern.factory.base.Weapon;
import com.macrotea.pattern.factory.ext.DefaultFactory;

public class Mainer {
	public static void main(String[] args) {
		/* */
		AbstractFactory defFactory=new DefaultFactory();
		// 
		Food food=defFactory.createFood();
		food.eat();
		// 
		Vehicle vehicle=defFactory.createVehicle();
		vehicle.run();
		// 
		Weapon weapon=defFactory.createWeapon();
		weapon.shoot();
		
/*		
		 
		AbstractFactory cnFactory=new ChinaFactory();
		// 
		Food food=cnFactory.createFood();
		food.eat();
		// 
		Vehicle vehicle=cnFactory.createVehicle();
		vehicle.run();
		// 
		Weapon weapon=cnFactory.createWeapon();
		weapon.shoot();
		*/
		
	}

}

 
 
 
추상 공장 편리 제어 제품 시리즈
일반 공장은 제품의 종류를 통제하기 편리하다

좋은 웹페이지 즐겨찾기