【java】设计模式-抽象工厂模式
本帖最后由 白冥 于 2024-5-20 16:29 编辑public interface Heitai {
void fack ();
}
public interface Clothes {
void dress ();
}
public class CuteHentai implements Heitai {
public CuteHentai () {}
public void fack () {
System.out.println ("A cuteHentai is facked.");
}
}
public class HandsomeHentai implements Heitai {
public HandsomeHentai () {}
public void fack() {
System.out.println ("A handsomeHentai is facked.");
}
}
public class MurcularHentai implements Heitai {
public MurcularHentai () {}
public void fack () {
System.out.println ("A murcularHentai is facked.");
}
}
public class Hoodie implements Clothes {
public Hoodie () {}
public void dress() {
System.out.println ("A hantai dresses a hoodie.");
}
}
public class SailorSuit implements Clothes {
public SailorSuit () {}
public void dress() {
System.out.println ("A hantai dressed a sailor suit.");
}
}
public class Jyoshikoukousei implements Clothes {
public Jyoshikoukousei () {}
public void dress () {
System.out.println ("A hentai dressed a jyoshikoukousei.");
}
}
public abstract class AbstractFactory {
public abstract Heitai getHeitai (String HeitaiType);
public abstract Clothes getClothes (String ClothesType);
}
public class HeitaiFactory extends AbstractFactory {
public HeitaiFactory () {}
public Heitai getHeitai(String HeitaiType) {
switch (HeitaiType) {
case null:
return null;
case "CuteHentai":
return new CuteHentai ();
case "HandsomeHentai":
return new HandsomeHentai ();
case "MurcularHentai":
return new MurcularHentai ();
default:
return null;
}
}
public Clothes getClothes (String ClothesType) {
return null;
}
}
public class ClothesFactory extends AbstractFactory {
public ClothesFactory () {}
public Heitai getHeitai (String HeitaiType) {
return null;
}
public Clothes getClothes (String ClothesType) {
switch (ClothesType) {
case null:
return null;
case "Hoodie":
return new Hoodie ();
case "SailorSuit":
return new SailorSuit ();
case "Jyoshikoukousei":
return new Jyoshikoukousei ();
default:
return null;
}
}
}
public class AbstractFactoryProducer {
public AbstractFactoryProducer () {}
public AbstractFactory getFactory (String factoryType) {
switch (factoryType) {
case null:
return null;
case "Heitai":
return new HeitaiFactory ();
case "Clothes":
return new ClothesFactory ();
default:
return null;
}
}
}
比如我们可以在抽象工厂生成器中输入“Hentai”,就能返回一个Hentai工厂;在Hentai工厂中输入“CuteHentai”,就能返回一个CuteHentai对象。 挺实用的代码,复制一下尝尝看。 本帖最后由 koh 于 2024-5-20 16:19 编辑
只看到不同的类继承了两大接口~后面不知道了 这种设计模式挺好的,跟接口进行交互,但我开发总是偷懒最后代码又糊成一大块了 hhh 现在已经不会写Java了,不过这样的设计思路还在用 很棒的设计模式惹,自己先去了解一下好了{:6_167:} 我的天,小白学会这个要多久呀 来观摩下大佬的脚本写的方式 以后可能会用到呢( 只看懂了Hentai,这工厂一定会排出很多黄色废料,好在不用担心污染环境(嗯?)。 路過,看大老寫Code,也許解釋可以多點 我只会点py,看来每种语言逻辑与表达方式还是很不同的 QAQ 小白头晕 只能悄咪咪收藏期待自己那天开窍了jpg 膜拜楼上面的大佬,他们似乎看得懂代码的用途{:6_194:} 好厉害啊
虽然看不懂但是应该会很有用~ 挺实用的,等会试试看咯
页:
[1]