一、抽象工厂模式
二、版本
(一)C++版本:
① 类图:
② 效果:
③ 代码:
abstract.cpp
1 #include<iostream> 2 using namespace std; 3 4 //男 5 class AbstractMan { 6 public: 7 virtual void sex() = 0; 8 }; 9 //女 10 class AbstractWoman { 11 public: 12 virtual void sex() = 0; 13 }; 14 15 //黄色男性 16 class ManYellow : public AbstractMan { 17 public: 18 virtual void sex() { 19 std::cout << "黄色男性!" << std::endl; 20 } 21 }; 22 //黄色女性 23 class WomanYellow : public AbstractWoman { 24 public: 25 virtual void sex() { 26 std::cout << "黄色女性!" << std::endl; 27 } 28 }; 29 30 //黑色男性 31 class ManBlack : public AbstractMan { 32 public: 33 virtual void sex() { 34 std::cout << "黑色男性!" << std::endl; 35 } 36 }; 37 //黑色女性 38 class WomanBlack : public AbstractWoman { 39 public: 40 virtual void sex() { 41 std::cout << "黑色女性!" << std::endl; 42 } 43 }; 44 45 //白色男性 46 class ManWhite : public AbstractMan { 47 public: 48 virtual void sex() { 49 std::cout << "白色男性!" << std::endl; 50 } 51 }; 52 //白色女性 53 class WomanWhite : public AbstractWoman { 54 public: 55 virtual void sex() { 56 std::cout << "白色女性!" << std::endl; 57 } 58 }; 59 60 //抽象工厂 61 class AbstractFactory { 62 public: 63 virtual AbstractMan* CreateMan() = 0; 64 virtual AbstractWoman* CreateWoman() = 0; 65 }; 66 class YellowFactory : public AbstractFactory { 67 public: 68 virtual AbstractMan* CreateMan() { 69 return new ManYellow(); 70 } 71 virtual AbstractWoman* CreateWoman() { 72 return new WomanYellow(); 73 } 74 }; 75 class BlackFactory : public AbstractFactory { 76 public: 77 virtual AbstractMan* CreateMan() { 78 return new ManBlack(); 79 } 80 virtual AbstractWoman* CreateWoman() { 81 return new WomanBlack(); 82 } 83 }; 84 class WhiteFactory : public AbstractFactory { 85 public: 86 virtual AbstractMan* CreateMan() { 87 return new ManWhite(); 88 } 89 virtual AbstractWoman* CreateWoman() { 90 return new WomanWhite(); 91 } 92 }; 93 94 void yellow() {//黄 95 AbstractFactory* factory = NULL; 96 factory = new YellowFactory; 97 98 AbstractMan* man = factory->CreateMan(); 99 man->sex(); 100 delete man; 101 man = NULL; 102 103 AbstractWoman* woman = factory->CreateWoman(); 104 woman->sex(); 105 delete woman; 106 woman = NULL; 107 108 delete factory; 109 factory = NULL; 110 } 111 void black() {//黑 112 AbstractFactory* factory = NULL; 113 factory = new BlackFactory; 114 115 AbstractMan* man = factory->CreateMan(); 116 man->sex(); 117 delete man; 118 man = NULL; 119 120 AbstractWoman* woman = factory->CreateWoman(); 121 woman->sex(); 122 delete woman; 123 woman = NULL; 124 125 delete factory; 126 factory = NULL; 127 } 128 void white() {//白 129 AbstractFactory* factory = NULL; 130 factory = new WhiteFactory; 131 132 AbstractMan* man = factory->CreateMan(); 133 man->sex(); 134 delete man; 135 man = NULL; 136 137 AbstractWoman* woman = factory->CreateWoman(); 138 woman->sex(); 139 delete woman; 140 woman = NULL; 141 142 delete factory; 143 factory = NULL; 144 } 145 int main() { 146 yellow(); 147 black(); 148 white(); 149 return 0; 150 }
参考链接:https://blog.csdn.net/u013216156/article/details/94861127
(二)Java版本
①类图:
②效果图:
③目录结构:
④代码:
BlackHuman.java
1 package PersonAndSkin; 2 3 public abstract class BlackHuman implements Human{ 4 public void Color() { 5 System.out.println("黑色人种的皮肤颜色是黑色的!"); 6 } 7 public void Talk() { 8 System.out.println("黑色人种说话!"); 9 } 10 }
FemaleBlackHuman.java
1 package PersonAndSkin; 2 3 public class FemaleBlackHuman extends BlackHuman { 4 public void Sex() { 5 System.out.println("黑人女性"); 6 } 7 }
FemaleFactory.java
1 package PersonAndSkin; 2 3 public class FemaleFactory implements HumanFactory { 4 public Human createBlackHuman() { 5 return new FemaleBlackHuman(); 6 } 7 public Human createWhiteHuman() { 8 return new FemaleWhiteHuman(); 9 } 10 public Human createYellowHuman() { 11 return new FemaleYellowHuman(); 12 } 13 }
FemaleWhiteHuman.java
1 package PersonAndSkin; 2 3 public class FemaleWhiteHuman extends WhiteHuman{ 4 public void Sex() { 5 System.out.println("白人女性"); 6 } 7 }
FemaleYellowHuman.java
1 package PersonAndSkin; 2 3 public class FemaleYellowHuman extends YellowHuman { 4 public void Sex() { 5 System.out.println("黄人女性"); 6 } 7 }
Human.java
1 package PersonAndSkin; 2 3 public interface Human { 4 void Color();//人种颜色 5 void Talk();//说话 6 void Sex();//性别 7 }
HumanFactory.java
1 package PersonAndSkin; 2 3 public interface HumanFactory { 4 public Human createYellowHuman(); 5 public Human createWhiteHuman(); 6 public Human createBlackHuman(); 7 }
MaleBlackHuman.java
1 package PersonAndSkin; 2 3 public class MaleBlackHuman extends BlackHuman { 4 public void Sex() { 5 System.out.println("黑人男性"); 6 } 7 }
MaleFactory.java
1 package PersonAndSkin; 2 3 public class MaleFactory implements HumanFactory { 4 public Human createBlackHuman() { 5 return new MaleBlackHuman(); 6 } 7 public Human createWhiteHuman() { 8 return new MaleWhiteHuman(); 9 } 10 public Human createYellowHuman() { 11 return new MaleYellowHuman(); 12 } 13 }
MaleWhiteHuman.java
1 package PersonAndSkin; 2 3 public class MaleWhiteHuman extends WhiteHuman{ 4 public void Sex() { 5 System.out.println("白人男性"); 6 } 7 }
MaleYellowHuman.java
1 package PersonAndSkin; 2 3 public class MaleYellowHuman extends YellowHuman { 4 public void Sex() { 5 System.out.println("黄人男性"); 6 } 7 }
Nvwa.java
1 package PersonAndSkin; 2 3 public class NvWa { 4 public static void main(String[] args) { 5 HumanFactory maleHumanFactory = new MaleFactory(); 6 HumanFactory femaleHumanFactory = new FemaleFactory(); 7 // 黄种人 8 Human maleYellowHuman = maleHumanFactory.createYellowHuman(); 9 Human femaleYellowHuman = femaleHumanFactory.createYellowHuman(); 10 System.out.println("--黄种人--"); 11 femaleYellowHuman.Color(); 12 femaleYellowHuman.Talk(); 13 femaleYellowHuman.Sex(); 14 maleYellowHuman.Color(); 15 maleYellowHuman.Talk(); 16 maleYellowHuman.Sex(); 17 // 白种人 18 Human maleWhiteHuman = maleHumanFactory.createWhiteHuman(); 19 Human femaleWhiteHuman = femaleHumanFactory.createWhiteHuman(); 20 System.out.println("--白种人--"); 21 femaleWhiteHuman.Color(); 22 femaleWhiteHuman.Talk(); 23 femaleWhiteHuman.Sex(); 24 maleWhiteHuman.Color(); 25 maleWhiteHuman.Talk(); 26 maleWhiteHuman.Sex(); 27 // 黑种人 28 Human maleBlackHuman = maleHumanFactory.createBlackHuman(); 29 Human femaleBlackHuman = femaleHumanFactory.createBlackHuman(); 30 System.out.println("--黑种人--"); 31 femaleBlackHuman.Color(); 32 femaleBlackHuman.Talk(); 33 femaleBlackHuman.Sex(); 34 maleBlackHuman.Color(); 35 maleBlackHuman.Talk(); 36 maleBlackHuman.Sex(); 37 } 38 }
WhiteHuman.java
1 package PersonAndSkin; 2 3 public abstract class WhiteHuman implements Human { 4 public void Color() { 5 System.out.println("白色人种的皮肤颜色是白色的!"); 6 } 7 public void Talk() { 8 System.out.println("白色人种说话!"); 9 } 10 }
YellowHuman.java
1 package PersonAndSkin; 2 3 public abstract class YellowHuman implements Human { 4 public void Color() { 5 System.out.println("黄色人种的皮肤颜色是黄色的!"); 6 } 7 public void Talk() { 8 System.out.println("黄色人种说话!"); 9 } 10 }