使用抽象工厂模式,完成下述产品等级结构:
类图
代码结构
java
package rjsj.no4; public class BlackSkinWomen implements Women{ public void play() { System.out.println("黑色皮肤的女人出现"); } }
package rjsj.no4; public class Client { public static void main(String args[]) { try { MWFactory factory; Man m; Women w; factory=(MWFactory) XMLUtil.getBean(); m=factory.produceMan(); m.play(); w=factory.produceWomen(); w.play(); } catch(Exception e) { System.out.println(e.getMessage()); } } }
package rjsj.no4; public class BlackFactory implements MWFactory{ public Man produceMan() { return new BlackSkinMan(); } public Women produceWomen() { return new BlackSkinWomen(); } }
package rjsj.no4; public class BlackSkinMan implements Man{ public void play() { System.out.println("黑色皮肤的男人出现"); } }
package rjsj.no4; public class WhiteFactory implements MWFactory{ public Man produceMan() { return new WhiteSkinMan(); } public Women produceWomen() { return new WhiteSkinWomen(); } }
package rjsj.no4; public class WhiteSkinMan implements Man{ public void play() { System.out.println("白色皮肤的男人出现"); } }
package rjsj.no4; public class WhiteSkinWomen implements Women{ public void play() { System.out.println("白色皮肤的女人出现"); } }
<?xml version="1.0"?> <config> <className>YellowFactory</className> </config>
package rjsj.no4; public interface Man { public void play(); }
package rjsj.no4; public interface MWFactory { public Man produceMan(); public Women produceWomen(); }
package rjsj.no4; public class YellowFactory implements MWFactory{ public Man produceMan() { return new YellowSkinMan(); } public Women produceWomen() { return new YellowSkinWomen(); } }
package rjsj.no4; public class YellowSkinMan implements Man{ public void play() { System.out.println("黄色皮肤男人出现"); } }
package rjsj.no4; public class YellowSkinWomen implements Women{ public void play() { System.out.println("黄色皮肤的女人出现"); } }
package rjsj.no4; public interface Women { public void play(); }
package rjsj.no4; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.SAXException; import java.io.*; public class XMLUtil { //该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象 public static Object getBean() { try { //创建文档对象 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document doc; doc = builder.parse(new File("src/rjsj/no4/config.xml")); //获取包含类名的文本节点 NodeList nl = doc.getElementsByTagName("className"); //System.out.println(nl); Node classNode=nl.item(0).getFirstChild(); String cName=classNode.getNodeValue(); //通过类名生成实例对象并将其返回 Class c=Class.forName("rjsj.no4."+cName); Object obj=c.newInstance(); return obj; } catch(Exception e) { e.printStackTrace(); return null; } } }
C++
#include <iostream> using namespace std; class SexFactory{ public: virtual void produce(){ cout<<"无性别孩子"<<endl; } }; class girlFactory:public SexFactory{ public: void produce(){ // TODO Auto-generated method stub cout<<"女孩"<<endl; } }; class boyFactory:public SexFactory{ public: void produce(){ // TODO Auto-generated method stub cout<<"男孩"<<endl; } }; class ColorFactory{ public: virtual void produce(){ cout<<"透明"; }; }; class yellowFactory:public ColorFactory{ public: void produce(){ // TODO Auto-generated method stub cout<<"黄种"; } }; class whiteFactory:public ColorFactory{ public: void produce(){ // TODO Auto-generated method stub cout<<"白种"; } }; class blackFactory:public ColorFactory{ public: void produce(){ // TODO Auto-generated method stub cout<<"黑种"; } }; class Person{ public: Person(){ SexFactory* sex; ColorFactory* color; int s,c; cout<<"请输入你要生产的小人的性别 1是女孩 2是男孩"<<endl; cin>>s; cout<<"请输入你要生产的小人的颜色 1是黄种 2是黑种 3是白种"<<endl; cin>>c; cout<<"您生产了一个"; yellowFactory y; blackFactory b; whiteFactory w; switch(c) { case 1: color=&y; break; case 2: color=&b; break; case 3: color=&w; break; default:; } color->produce(); girlFactory g; boyFactory bo; switch(s) { case 1: sex=&g; break; case 2: sex=&bo; break; default:; } sex->produce(); } }; int main() { Person person; return 0; }
运行截图
标签:package,class,模式,工厂,抽象,void,no4,rjsj,public From: https://www.cnblogs.com/libin159/p/16869546.html