抽象工厂AbstractFactory

抽象工厂AbstractFactory

 

public interface Gardener {

}

 

public interface Fruit {

}

 

public interface Veggie {

}

 

public class NorthernGardener implements Gardener {
	
	/**
	 * 水果的工厂方法
	 * @param name
	 * @return
	 */
	public Fruit createFruit(String name) {
		return new NorthernFruit(name);
	}
	
	/**
	 * 蔬菜的工厂方法
	 * @param name
	 * @return
	 */
	public Veggie createVeggie(String name) {
		return new NorthernVeggie(name);
	}
	
}

 

public class TropicalGardener implements Gardener {
	
	/**
	 * 水果的工厂方法
	 * @param name
	 * @return
	 */
	public Fruit createFruit(String name) {
		return new TropicalFruit(name);
	}
	
	/**
	 * 蔬菜的工厂方法
	 * @param name
	 * @return
	 */
	public Veggie createVeggie(String name) {
		return new TropicalVeggie(name);
	}
	
}

 

public class NorthernFruit implements Fruit {
	
	private String name;
	
	public NorthernFruit(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 

public class NorthernVeggie implements Veggie {
	
	private String name;
	
	public NorthernVeggie(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 

public class TropicalFruit implements Fruit {
	
	private String name;
	
	public TropicalFruit(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 

public class TropicalVeggie implements Veggie {
	
	private String name;
	
	public TropicalVeggie(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}

 

public class TestAbstractFactory {
	
	public static void main(String[] args) {
		NorthernGardener northernGardener = new NorthernGardener();
		NorthernFruit northernFruit = (NorthernFruit) northernGardener.createFruit("Apple");
		String fruitName = northernFruit.getName();
		System.out.println(fruitName);
	}

}

 

你可能感兴趣的:(java,java设计模式)