标签:而不是 pat system public 获得 抽象类 而且 .com first
抽象工厂类的典型代码如下:
1 public abstract class AbstractFactory 2 { 3 public abstract AbstractProductA createProductA(); 4 public abstract AbstractProductB createProductB(); 5 }
具体工厂类的典型代码如下:
1 public class ConcreteFactory1 extends AbstractFactory 2 { 3 public AbstractProductA createProductA() 4 { 5 return new ConcreteProductA1(); 6 } 7 public AbstractProductB createProductB() 8 { 9 return new ConcreteProductB1(); 10 } 11 }
实例代码(JAVA):
1 //抽象产品 Television 2 public interface Television 3 { 4 public void play(); 5 } 6 7 //具体产品 HaierTelevision 8 public class HaierTelevision implements Television 9 { 10 public void play() 11 { 12 System.out.println("海尔电视机播放中......"); 13 } 14 } 15 16 //具体产品 TCLTelevision 17 public class TCLTelevision implements Television 18 { 19 public void play() 20 { 21 System.out.println("TCL电视机播放中......"); 22 } 23 } 24 25 //抽象产品 AirConditioner 26 public interface AirConditioner 27 { 28 public void changeTemperature(); 29 } 30 31 //具体产品 HaierAirConditioner 32 public class HaierAirConditioner implements AirConditioner 33 { 34 public void changeTemperature() 35 { 36 System.out.println("海尔空调温度改变中......"); 37 } 38 } 39 40 //具体产品 TCLAirConditioner 41 public class TCLAirConditioner implements AirConditioner 42 { 43 public void changeTemperature() 44 { 45 System.out.println("TCL空调温度改变中......"); 46 } 47 } 48 49 //抽象工厂 EFactory 50 public interface EFactory 51 { 52 public Television produceTelevision(); 53 public AirConditioner produceAirConditioner(); 54 } 55 56 //具体工厂 HaierFactory 57 public class HaierFactory implements EFactory 58 { 59 public Television produceTelevision() 60 { 61 return new HaierTelevision(); 62 } 63 64 public AirConditioner produceAirConditioner() 65 { 66 return new HaierAirConditioner(); 67 } 68 } 69 70 //具体工厂 TCLFactory 71 public class TCLFactory implements EFactory 72 { 73 public Television produceTelevision() 74 { 75 return new TCLTelevision(); 76 } 77 78 public AirConditioner produceAirConditioner() 79 { 80 return new TCLAirConditioner(); 81 } 82 } 83 84 //配置文件 config.xml 85 <?xml version="1.0"?> 86 <config> 87 <className>HaierFactory</className> 88 </config> 89 90 //通过反射获得具体工厂的实例 XMLUtil 91 import javax.xml.parsers.*; 92 import org.w3c.dom.*; 93 import org.xml.sax.SAXException; 94 import java.io.*; 95 public class XMLUtil 96 { 97 //该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象 98 public static Object getBean() 99 { 100 try 101 { 102 //创建文档对象 103 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); 104 DocumentBuilder builder = dFactory.newDocumentBuilder(); 105 Document doc; 106 doc = builder.parse(new File("config.xml")); 107 108 //获取包含类名的文本节点 109 NodeList nl = doc.getElementsByTagName("className"); 110 Node classNode=nl.item(0).getFirstChild(); 111 String cName=classNode.getNodeValue(); 112 113 //通过类名生成实例对象并将其返回 114 Class c=Class.forName(cName); 115 Object obj=c.newInstance(); 116 return obj; 117 } 118 catch(Exception e) 119 { 120 e.printStackTrace(); 121 return null; 122 } 123 } 124 } 125 126 //客户端类 Client 127 public class Client 128 { 129 public static void main(String args[]) 130 { 131 try 132 { 133 EFactory factory; 134 Television tv; 135 AirConditioner ac; 136 factory=(EFactory)XMLUtil.getBean(); 137 tv=factory.produceTelevision(); 138 tv.play(); 139 ac=factory.produceAirConditioner(); 140 ac.changeTemperature(); 141 } 142 catch(Exception e) 143 { 144 System.out.println(e.getMessage()); 145 } 146 } 147 }
实例代码(C++):
1 // 抽象工厂模式 2 #include <iostream> 3 using namespace std; 4 5 //抽象产品类 Television 6 class Television 7 { 8 public: 9 virtual void play() = 0; 10 }; 11 12 //具体产品类 HaierTelevision 13 class HaierTelevision:public Television 14 { 15 public: 16 void play() override 17 { 18 cout << "海尔电视播放中..." << endl; 19 } 20 }; 21 22 //具体产品类 TCLTelevision 23 class TCLTelevision : public Television 24 { 25 public: 26 void play() override 27 { 28 cout << "TCL电视播放中..." << endl; 29 } 30 }; 31 32 //抽象产品 AirConditioner 33 class AirConditioner 34 { 35 public: 36 virtual void changeTemperature() = 0; 37 }; 38 39 //具体产品 HaierAirConditioner 40 class HaierAirConditioner : public AirConditioner 41 { 42 public: 43 void changeTemperature() override 44 { 45 cout << "海尔空调温度改变中..." << endl; 46 } 47 }; 48 49 //具体产品 TCLAirConditioner 50 class TCLAirConditioner : public AirConditioner 51 { 52 public: 53 void changeTemperature() override 54 { 55 cout << "TCL空调温度改变中..." << endl; 56 } 57 }; 58 59 //抽象工厂 EFactory 60 class EFactory 61 { 62 public: 63 virtual Television* productTelevision() = 0; 64 virtual AirConditioner* productAirConditioner() = 0; 65 }; 66 67 //具体工厂 HaierFactory 68 class HaierFactory : public EFactory 69 { 70 public: 71 Television* productTelevision() override 72 { 73 return new HaierTelevision(); 74 } 75 76 AirConditioner* productAirConditioner() override 77 { 78 return new HaierAirConditioner(); 79 } 80 }; 81 82 //具体工厂 TCLFactory 83 class TCLFactory : public EFactory 84 { 85 public: 86 Television* productTelevision() override 87 { 88 return new TCLTelevision(); 89 } 90 91 AirConditioner* productAirConditioner() override 92 { 93 return new TCLAirConditioner(); 94 } 95 }; 96 97 //客户端 98 int main() 99 { 100 EFactory* factory; 101 Television* tv; 102 AirConditioner* ac; 103 factory = new HaierFactory(); 104 tv = factory->productTelevision(); 105 tv->play(); 106 ac = factory->productAirConditioner(); 107 ac->changeTemperature(); 108 109 factory = new TCLFactory(); 110 tv = factory->productTelevision(); 111 tv->play(); 112 ac = factory->productAirConditioner(); 113 ac->changeTemperature(); 114 return 0; 115 }
标签:而不是 pat system public 获得 抽象类 而且 .com first
原文地址:https://www.cnblogs.com/WindSun/p/10253248.html