标签:show Gender 模式 工厂 抽象 Skin Override new public
使用抽象工厂模式,完成下述产品等级结构:
1. 画出对应的类图;
2. 提交源代码;
public interface Skin {
void show();
}
public interface Gender {
void show();
}
public interface HumanFactory {
Skin createSkin();
Gender createGender();
}
public class WhiteSkin implements Skin {
@Override
public void show() {
System.out.println("White skin");
}
}
public class YellowSkin implements Skin {
@Override
public void show() {
System.out.println("Yellow skin");
}
}
public class BlackSkin implements Skin {
@Override
public void show() {
System.out.println("Black skin");
}
}
public class AfricanHumanFactory implements HumanFactory {
@Override
public Skin createSkin() {
return new BlackSkin();
}
@Override
public Gender createGender() {
// 根据具体需求选择性别
return new MaleGender(); // 示例为男性
}
}
public class AsianHumanFactory implements HumanFactory {
@Override
public Skin createSkin() {
return new YellowSkin();
}
@Override
public Gender createGender() {
// 根据具体需求选择性别
return new FemaleGender(); // 示例为女性
}
}
public class CaucasianHumanFactory implements HumanFactory {
@Override
public Skin createSkin() {
return new WhiteSkin();
}
@Override
public Gender createGender() {
// 根据具体需求选择性别
return new FemaleGender(); // 示例为女性
}
}
public class FemaleGender implements Gender {
@Override
public void show() {
System.out.println("Female gender");
}
}
public class MaleGender implements Gender {
@Override
public void show() {
System.out.println("Male gender");
}
}
public class Main{
public static void main(String[] args) {
HumanFactory asianFactory = new AsianHumanFactory();
Skin asianSkin = asianFactory.createSkin();
Gender asianGender = asianFactory.createGender();
asianSkin.show();
asianGender.show();
HumanFactory africanFactory = new AfricanHumanFactory();
Skin africanSkin = africanFactory.createSkin();
Gender africanGender = africanFactory.createGender();
africanSkin.show();
africanGender.show();
HumanFactory caucasianFactory = new CaucasianHumanFactory();
Skin caucasianSkin = caucasianFactory.createSkin();
Gender caucasianGender = caucasianFactory.createGender();
caucasianSkin.show();
caucasianGender.show();
}
}
标签:show,
Gender,
模式,
工厂,
抽象,
Skin,
Override,
new,
public
From: https://www.cnblogs.com/pinganxile/p/18639503