画类图
用人的肤色和性别进行抽象工厂方法模式的实现,肤色和性别组成产品等级结构和产品族;
具体代码实现
项目结构:
//AbstractFactory.java
package org.example.design002;
public interface AbstractFactory {
public Man createMan();
public Woman createWoman();
}
//YellowFactory.java
package org.example.design002;
public class YellowFactory implements AbstractFactory{
@Override
public Man createMan() {
return new YellowMan();
}
@Override
public Woman createWoman() {
return new YellowWoman();
}
}
//BlackFactory.java
package org.example.design002;
public class BlackFactory implements AbstractFactory{
@Override
public Man createMan() {
return new BlackMan();
}
@Override
public Woman createWoman() {
return new BlackWoman();
}
}
//WhiteFactory.java
package org.example.design002;
public class WhiteFactory implements AbstractFactory{
@Override
public Man createMan() {
return new WhiteMan();
}
@Override
public Woman createWoman() {
return new WhiteWoman();
}
}
//Man.java
package org.example.design002;
public interface Man {
public void show();
}
//Woman.java
package org.example.design002;
public interface Woman {
public void show();
}
//YellowMan.java
package org.example.design002;
import java.sql.SQLOutput;
public class YellowMan implements Man{
@Override
public void show() {
System.out.println("黄皮肤的男性");
}
}
//YellowWoman.java
package org.example.design002;
public class YellowWoman implements Woman{
@Override
public void show() {
System.out.println("黄皮肤的女性");
}
}
//BlackMan.java
package org.example.design002;
public class BlackMan implements Man{
@Override
public void show() {
System.out.println("黑皮肤的男性");
}
}
//BlackWoman.java
package org.example.design002;
public class BlackWoman implements Woman{
@Override
public void show() {
System.out.println("黑皮肤的男性");
}
}
//WhiteMan.java
package org.example.design002;
public class WhiteMan implements Man{
@Override
public void show() {
System.out.println("白皮肤的男性");
}
}
//WhiteWoman.java
package org.example.design002;
public class WhiteWoman implements Woman{
@Override
public void show() {
System.out.println("白皮肤的女性");
}
}
//Main.java
package org.example.design002;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
AbstractFactory abf;
Man m;
Woman w;
System.out.println("请选择:1、白皮肤;2、黑皮肤;3、黄皮肤");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
if(a==1){
abf=new WhiteFactory();
m=abf.createMan();
m.show();
w=abf.createWoman();
w.show();
}else if(a==2){
abf=new BlackFactory();
m=abf.createMan();
m.show();
w=abf.createWoman();
w.show();
}else if(a==3){
abf=new YellowFactory();
m=abf.createMan();
m.show();
w=abf.createWoman();
w.show();
}else{
System.out.println("不存在这种工厂模式哦~");
}
}
}
标签:Java,show,--,package,design002,抽象,org,java,public
From: https://www.cnblogs.com/liuzijin/p/17763669.html