实验4:抽象工厂模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解抽象工厂模式的动机,掌握该模式的结构;
2、能够利用抽象工厂模式解决实际问题。
[实验任务一]:人与肤色
使用抽象工厂模式,完成下述产品等级结构:
实验要求:
源代码
package org.example;
// 抽象产品A:Person
abstract class Person {
abstract String getMalePerson();
abstract String getFemalePerson();
}
// 具体产品A1:AsianPerson
class AsianPerson extends Person {
@Override
String getMalePerson() {
return "男性";
}
@Override
String getFemalePerson() {
return "女性";
}
}
// 具体产品A2:AfricanPerson
class AfricanPerson extends Person {
@Override
String getMalePerson() {
return "男性";
}
@Override
String getFemalePerson() {
return "女性";
}
}
class EuropePerson extends Person {
@Override
String getMalePerson() {
return "男性";
}
@Override
String getFemalePerson() {
return "女性";
}
}
// 抽象产品B:SkinColor
abstract class SkinColor {
abstract String getColor();
}
// 具体产品B1:AsianSkinColor
class AsianSkinColor extends SkinColor {
@Override
String getColor() {
return "Yellow"; // 简化表示,实际情况可能更复杂
}
}
// 具体产品B2:AfricanSkinColor
class AfricanSkinColor extends SkinColor {
@Override
String getColor() {
return "Black"; // 简化表示,实际情况可能更复杂
}
}
class EuropeSkinColor extends SkinColor {
@Override
String getColor() {
return "White"; // 简化表示,实际情况可能更复杂
}
}
// 抽象工厂:PersonFactory
interface PersonFactory {
Person createPerson();
SkinColor createSkinColor();
}
// 具体工厂1:AsianFactory
class AsianFactory implements PersonFactory {
@Override
public Person createPerson() {
return new AsianPerson();
}
@Override
public SkinColor createSkinColor() {
return new AsianSkinColor();
}
}
// 具体工厂2:AfricanFactory
class AfricanFactory implements PersonFactory {
@Override
public Person createPerson() {
return new AfricanPerson();
}
@Override
public SkinColor createSkinColor() {
return new AfricanSkinColor();
}
}
class EuropeFactory implements PersonFactory {
@Override
public Person createPerson() {
return new EuropePerson();
}
@Override
public SkinColor createSkinColor() {
return new EuropeSkinColor();
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
PersonFactory factory;
// 使用亚洲工厂创建亚洲人及其肤色
factory = new AsianFactory();
Person asianPerson = factory.createPerson();
SkinColor asianSkinColor = factory.createSkinColor();
System.out.println("亚洲"+asianPerson.getMalePerson()+"性肤色: " + asianSkinColor.getColor());
System.out.println("亚洲"+asianPerson.getFemalePerson()+"性肤色: " + asianSkinColor.getColor());
// 使用非洲工厂创建非洲人及其肤色
factory = new AfricanFactory();
Person africanPerson = factory.createPerson();
SkinColor africanSkinColor = factory.createSkinColor();
System.out.println("非洲"+africanPerson.getMalePerson()+"性肤色: " + africanSkinColor.getColor());
System.out.println("非洲"+africanPerson.getFemalePerson()+"性肤色: " + africanSkinColor.getColor());
factory =new EuropeFactory();
Person EuropePerson = factory.createPerson();
SkinColor EuropeSkinColor = factory.createSkinColor();
System.out.println("欧洲"+EuropePerson.getMalePerson()+"性肤色: " + EuropeSkinColor.getColor());
System.out.println("欧洲"+EuropePerson.getFemalePerson()+"性肤色: " + EuropeSkinColor.getColor());
}
}
标签:return,String,10.30,factory,Person,Override,SkinColor From: https://www.cnblogs.com/zzqq1314/p/18545020