菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

软件设计--抽象工厂模式

就我的理解来说,抽象工厂模式就是,创建接口和抽象类,把分类逐渐总到一起~

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

先对性别进行分类:

package Tutorial04;

public interface Person {
    public String Is();
}
package Tutorial04;

public class Man implements Person{
    @Override
    public String Is() {
        return "男人";
    }
}
package Tutorial04;

public class Woman implements Person{
    @Override
    public String Is() {
        return "女人";
    }
}

再对肤色进行分类:

package Tutorial04;

public interface Color {
    public String SurfaceIs();
}
package Tutorial04;

public class Yellow implements Color {
    @Override
    public String SurfaceIs() {
        return "Yellow";
    }
}
package Tutorial04;

public class White implements Color{
    @Override
    public String SurfaceIs() {
        return "White";
    }
}
package Tutorial04;

public class Black implements Color{
    @Override
    public String SurfaceIs() {
        return "Black";
    }
}

然后创建抽象工厂(同时创建性别和肤色工厂),来获取性别和肤色工厂:

package Tutorial04;

public abstract class AbstractFactory {
    public abstract Color getColor(String color);
    public abstract Person getPerson(String person);
}
复制代码
package Tutorial04;

public class PersonFactory extends AbstractFactory{
    @Override
    public Person getPerson(String person) {
        if(person==null){
            return null;
        }
        if(person.equalsIgnoreCase("Man")){
            return new Man();
        } else if (person.equalsIgnoreCase("Woman")) {
            return new Woman();
        }
        return null;
    }
    @Override
    public Color getColor(String color) {
        return null;
    }

}
复制代码
复制代码
package Tutorial04;

public class ColorFactory extends AbstractFactory{
    @Override
    public Color getColor(String color) {
        if(color == null){
            return null;
        }
        if(color.equalsIgnoreCase("Yellow")){
            return new Yellow();
        } else if (color.equalsIgnoreCase("Black")) {
            return new Black();
        } else if (color.equalsIgnoreCase("White")) {
            return new White();
        }
        return null;
    }

    @Override
    public Person getPerson(String person) {
        return null;
    }
}
复制代码

然后创建工厂生成器:

复制代码
package Tutorial04;

public class FactoryProducer {
    public static AbstractFactory getFactory(String choice){
        if(choice.equalsIgnoreCase("Person")){
            return new PersonFactory();
        } else if (choice.equalsIgnoreCase("Color")) {
            return new ColorFactory();
        }
        return null;
    }
}
复制代码

最后实现:

复制代码
package Tutorial04;

public class AbstractFactoryPatternDemo {
    public static void main(String[] args) {

        AbstractFactory personFactory = FactoryProducer.getFactory("Person");
        Man man = (Man) personFactory.getPerson("Man");
        Woman woman = (Woman) personFactory.getPerson("Woman");

        AbstractFactory colorFactory = FactoryProducer.getFactory("Color");
        Yellow yellow = (Yellow) colorFactory.getColor("Yellow");
        Black black = (Black) colorFactory.getColor("Black");
        White white = (White) colorFactory.getColor("White");

        System.out.println("This "+man.Is()+" is "+yellow.SurfaceIs()+" one.");
        System.out.println("This "+woman.Is()+" is "+yellow.SurfaceIs()+" one.");
        System.out.println("This "+man.Is()+" is "+black.SurfaceIs()+" one.");
        System.out.println("This "+woman.Is()+" is "+black.SurfaceIs()+" one.");
        System.out.println("This "+man.Is()+" is "+white.SurfaceIs()+" one.");
        System.out.println("This "+woman.Is()+" is "+white.SurfaceIs()+" one.");


    }
}
复制代码

结果如下:

This 男人 is Yellow one.
This 女人 is Yellow one.
This 男人 is Black one.
This 女人 is Black one.
This 男人 is White one.
This 女人 is White one.

 

posted on   hhmzd233  阅读(7)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示