对简单工厂的特殊情况进行测试——工厂角色与具体产品角色合并
代码如下:
abstract class AbstractClass{
private static String name;
public final static AbstractClass format(String value){
name = value;
return new Embody();
}
public String getMsg(){
return name;
}
public abstract void fun1();
}
class Embody extends AbstractClass{
public void fun1(){
System.out.println("the enbody extends AbstractClass" + getMsg());
}
}
public class tt {
public static void main(String args[]){
AbstractClass.format("name").fun1();
}
}
上面的代码中通过工厂方法把子类具体的实例化隐藏了起来。
下面是对一个普通的简单工厂模式的实现:
代码测试:
package sky;
//产品抽象类
abstract class People{
public String name; //名字
public String occuptationl; //职业
abstract public String getMsg();
}
//学生类——具体产品
class Student extends People{
private String name;
private String occupation;
private String grade;
public Student(String name,String occupation){
this.name = name;
this.occupation = occupation;
}
public void setGrade(String grade){
this.grade = grade;
}
public String getGrade(){
return grade;
}
public String getMsg(){
return getOccupation()+getName()+getGrade();
}
public String getOccupation(){
return occupation;
}
public String getName(){
return name;
}
}
//教师类——具体产品类
class Teacherextends People{
private String name;
private String occupation;
private int wage;
public Teacher(String name,String occupation){
this.name = name;
this.occupation = occupation;
}
public void setWage(int wage){
this.wage = wage;
}
public String getMsg(){
return getOccupation()+getName() + getWage();
}
public String getOccupation(){
return occupation;
}
public String getName(){
return name;
}
public int getWage(){
return wage;
}
}
//工厂类
class factory{
public static People build(String name,String occupation) throws BadPeopleException{
if(occupation.equals("student")){
return new Student(name,occupation);
}
else if(occupation.equals("teacher")){
return new Teacher(name,occupation);
}else{
throw new BadPeopleException("can not found the people");
}
}
}
//异常处理
class BadPeopleException extends Exception{
public BadPeopleException(String msg){
super(msg);
}
}
public class test{
public static void main(String args[]){
try {
Student t = (Student) factory.build("sky","student");
//从这个显示转换申明中我并没有看到简单工厂的好处
factory.build("fly","teacher");
/*难道只有这样才可以看到它的优点吗?——能够避免客户决定什么时候创建哪个实例*/
t.setGrade("three");
System.out.println(t.getMsg());
} catch (BadPeopleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
问题:接口和抽象类的区别在哪里?
第13章 工厂方法模式 <java与模式> ---电子书 page146
组成为:抽象工厂、具体工厂、抽象产品、具体产品
其中抽象工厂是为外部访问内部的一个接口,它的好处是可以通过扩张具体产品和具体工厂而不用改变客户端的代码.
一个简单的测试代码如下:
interface Fruit{
void grow();
void harvest();
}
class Apple implements Fruit{
private int treeNumber;
public void grow(){
System.out.println("Apple.grow()");
}
public void harvest(){
System.out.println("Apple.harvest()");
}
public void setTreeNumber(int number){
treeNumber = number;
}
public int getTreeNumber(){
return treeNumber;
}
}
class StrawBerry implements Fruit{
public void grow(){
System.out.println("StrawBerry.grow()");
}
public void harvest(){
System.out.println("StrawBerry.harvest()");
}
}
abstract class Gardener{
abstract public Fruit product();
}
class AppleGardener extends Gardener{
public Fruit product(){
return new Apple();
}
}
class StrawBerryGardener extends Gardener{
public Fruit product(){
return new StrawBerry();
}
}
public class Client {
public static void main(String args[]){
Fruit apple,strawberry;
AppleGardener applegarder = new AppleGardener();
StrawBerryGardener strawberrygarder = new StrawBerryGardener();
apple = applegarder.product();
strawberry = strawberrygarder.product();
apple.grow();
strawberry.grow();
}
}
posted on 2005-07-30 20:54
sky 阅读(132)
评论(0) 编辑 收藏