这篇文章给大家分享的是有关怎么用Java实现的设计模式的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
abstract class AProductRam {
public String getProduct() {
return this+" Product";
}
}
abstract class AProductCpu {
public String getProduct() {
return this+" Product";
}
}
class CProductMacRam extends AProductRam {
public String toString() {
return "MacRam";
}
}
class CProductWinRam extends AProductRam {
public String toString() {
return "WinRam";
}
}
class CProductMacCpu extends AProductCpu {
public String toString() {
return "MacCpu";
}
}
class CProductWinCpu extends AProductCpu {
public String toString() {
return "WinCpu";
}
}
interface AFactory {
public AProductRam CreateProductRam();
public AProductCpu CreateProductCpu();
}
class CFactoryMac implements AFactory {
public AProductRam CreateProductRam() {
return new CProductMacRam();
}
public AProductCpu CreateProductCpu() {
return new CProductMacCpu();
}
}
class CFactoryWin implements AFactory {
public AProductRam CreateProductRam() {
return new CProductWinRam();
}
public AProductCpu CreateProductCpu() {
return new CProductWinCpu();
}
}
class Factory {
private static void printProduct(String v) {
System.out.println(v);
}
public static void main(String[] args) {
AProductRam ProductMacRam=null,ProductWinRam=null;
AProductCpu ProductMacCpu=null,ProductWinCpu=null;
CFactoryMac FactoryMac=new CFactoryMac();
CFactoryWin FactoryWin=new CFactoryWin();
ProductMacRam=FactoryMac.CreateProductRam();
ProductMacCpu=FactoryMac.CreateProductCpu();
ProductWinRam=FactoryWin.CreateProductRam();
ProductWinCpu=FactoryWin.CreateProductCpu();
printProduct(ProductMacRam.getProduct());
printProduct(ProductMacCpu.getProduct());
printProduct(ProductWinRam.getProduct());
printProduct(ProductWinCpu.getProduct());
}
}
感谢各位的阅读!关于“怎么用Java实现的设计模式”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!