• [Design-Pattern]工厂模式


    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
    "); }
    18     virtual void method2() { printf("Implementation1 method2
    "); }
    19 };
    20 
    21 class Implementation2 :public Service {
    22 public:
    23     Implementation2() {}
    24     virtual void method1() { printf("Implementation2 method1
    "); }
    25     virtual void method2() { printf("Implementation2 method2
    "); }
    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使用匿名内部类后的版本

     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在使用了匿名内部类后,代码明显少了很多,省去了Implementation1ServiceFactory、Implementation2ServiceFactory类的创建。

  • 相关阅读:
    (二十四)JSP标签之基本标签(<jsp:标签名>)
    (二十三)JSP指令
    (二十二)JSP基础语法
    (二十一)JSP基础
    java类的初始化过程
    redis的主从复制和哨兵支持的主从切换
    redis事务和乐观锁
    redis的持久化
    linux修改进程的名字
    在c代码中获取用户环境变量
  • 原文地址:https://www.cnblogs.com/brianyi/p/6565191.html
Copyright © 2020-2023  润新知