实验4:抽象工厂模式

实验4:抽象工厂模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解抽象工厂模式的动机,掌握该模式的结构;

2、能够利用抽象工厂模式解决实际问题。

 

[实验任务一]:人与肤色

使用抽象工厂模式,完成下述产品等级结构:

实验要求:

1. 画出对应的类图;

2.提交源代码;

3.注意编程规范。

1.BlackFactory

public class BlackFactory implements HumanFactory{

    @Override

    public Woman createWoman() {

        return new BLackWoman();

    }

 

    @Override

    public Man createMan() {

        return new BlackMan();

    }

}

2.BlackMan

public class BlackMan implements Man{

    @Override

    public void manEat() {

        System.out.println("black man eat");

    }

}

3.BlackWoman

public class BLackWoman implements Woman{

    @Override

    public void womanEat() {

        System.out.println("black woman eat");

    }

}

4.WhiteFactory

public class WhiteFactory implements HumanFactory{

    @Override

    public Woman createWoman() {

        return new WhiteWoman();

    }

 

    @Override

    public Man createMan() {

        return new WhiteMan();

    }

}

5.WhiteMan

public class WhiteMan implements Man{

    @Override

    public void manEat() {

        System.out.println("white man eat");

    }

}

6.WhiteWoman

public class WhiteWoman implements Woman{

    @Override

    public void womanEat() {

        System.out.println("white woman eat");

    }

}

 

7.YellowFactory

public class YellowFactory implements HumanFactory{

    @Override

    public Woman createWoman() {

        return new YellowWoman();

    }

    @Override

    public Man createMan() {

        return new YellowMan();

    }

}

8.YellowMan

public class YellowMan implements Man{

    @Override

    public void manEat() {

        System.out.println("yellow man eat");

    }

}

9.YellowWoman

public class YellowWoman implements Woman{

    @Override

    public void womanEat() {

        System.out.println("yellow woman eat");

    }

} 

10.HumanFactory

public interface HumanFactory {

     Woman createWoman();

     Man createMan();

}

11.Man

public interface Man {

    void manEat();

}

12.Woman

public interface Woman {

    void womanEat();

}

posted @ 2023-11-06 08:51  会秃头的小白  阅读(20)  评论(0编辑  收藏  举报