日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

[Design-Pattern]工厂模式

發(fā)布時(shí)間:2025/3/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Design-Pattern]工厂模式 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Java版本

1 package interfaces;2 3 interface Service {4 void method1();5 void method2();6 } 7 8 interface ServiceFactory { 9 Service getService(); 10 } 11 12 class Implementation1 implements Service { 13 Implementation1() {} 14 public void method1() { System.out.println("Implementation1 method1"); } 15 public void method2() { System.out.println("Implementation1 method2"); } 16 } 17 18 class Implementation1Factory implements ServiceFactory { 19 public Service getService() { 20 return new Implementation1(); 21 } 22 } 23 24 class Implementation2 implements Service { 25 Implementation2() {} 26 public void method1() { System.out.println("Implementation2 method1"); } 27 public void method2() { System.out.println("Implementation2 method2"); } 28 } 29 30 class Implementation2Factory implements ServiceFactory { 31 public Service getService() { 32 return new Implementation2(); 33 } 34 } 35 36 public class Factories { 37 public static void serviceConsumer(ServiceFactory fact) { 38 Service s = fact.getService(); 39 s.method1(); 40 s.method2(); 41 } 42 public static void main(String[] args) { 43 serviceConsumer(new Implementation1Factory()); 44 serviceConsumer(new Implementation2Factory()); 45 } 46 }

C++版本

1 #include <stdio.h>2 3 class Service {4 public:5 virtual void method1() = 0;6 virtual void method2() = 0;7 }; 8 9 class ServiceFactory { 10 public: 11 virtual Service* getService() = 0; 12 }; 13 14 class Implementation1 :public Service { 15 public: 16 Implementation1() {} 17 virtual void method1() { printf("Implementation1 method1\n"); } 18 virtual void method2() { printf("Implementation1 method2\n"); } 19 }; 20 21 class Implementation2 :public Service { 22 public: 23 Implementation2() {} 24 virtual void method1() { printf("Implementation2 method1\n"); } 25 virtual void method2() { printf("Implementation2 method2\n"); } 26 }; 27 28 class Implementation1ServiceFactory :public ServiceFactory { 29 public: 30 Implementation1ServiceFactory() {} 31 virtual Service* getService() { 32 return new Implementation1(); 33 } 34 }; 35 36 class Implementation2ServiceFactory :public ServiceFactory { 37 public: 38 Implementation2ServiceFactory() {} 39 virtual Service* getService() { 40 return new Implementation2(); 41 } 42 }; 43 44 void serviceConsumer(ServiceFactory* fact) { 45 Service* s = fact->getService(); 46 s->method1(); 47 s->method2(); 48 } 49 50 int main() 51 { 52 serviceConsumer(new Implementation1ServiceFactory()); 53 serviceConsumer(new Implementation2ServiceFactory()); 54 return 0; 55 }

Java使用匿名內(nèi)部類后的版本

1 package innerclasses;2 3 interface Service {4 void method1();5 void method2();6 } 7 8 interface ServiceFactory { 9 Service getService(); 10 } 11 12 class Implementation1 implements Service { 13 private Implementation1() {} 14 public void method1() { System.out.println("Implementation1 method1"); } 15 public void method2() { System.out.println("Implementation1 method2"); } 16 public static ServiceFactory factory = new ServiceFactory() { 17 @Override 18 public Service getService() { 19 return new Implementation1(); 20 } 21 }; 22 } 23 24 class Implementation2 implements Service { 25 private Implementation2() {} 26 public void method1() { System.out.println("Implementation2 method1"); } 27 public void method2() { System.out.println("Implementation2 method2"); } 28 public static ServiceFactory factory = new ServiceFactory() { 29 @Override 30 public Service getService() { 31 return new Implementation2(); 32 } 33 }; 34 } 35 36 public class Factories { 37 public static void serviceConsumer(ServiceFactory fact) { 38 Service s = fact.getService(); 39 s.method1(); 40 s.method2(); 41 } 42 public static void main(String[] args) { 43 serviceConsumer(Implementation1.factory); 44 serviceConsumer(Implementation2.factory); 45 } 46 }

可以看出,Java在使用了匿名內(nèi)部類后,代碼明顯少了很多,省去了Implementation1ServiceFactory、Implementation2ServiceFactory類的創(chuàng)建。

轉(zhuǎn)載于:https://www.cnblogs.com/brianyi/p/6565191.html

總結(jié)

以上是生活随笔為你收集整理的[Design-Pattern]工厂模式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。