《어떤 패턴들》-추상적 공장
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();
*/
}
}
추상 공장 편리 제어 제품 시리즈
일반 공장은 제품의 종류를 통제하기 편리하다
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
ExtJS 3.2 학습 노트(3) 사용자 정의 이벤트Extjs에서 모든 상속은 Ext.util에서 합니다.Observable 클래스의 컨트롤은 이벤트를 지원할 수 있습니다. 클래스에 대해 이벤트를 사용자 정의하려면 다음 절차를 따르십시오. 1, 먼저 클래스를 정의합니...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.