commit 354bacf257de761c637a06e0dda8d0c371389bce Author: congqing1226 <577942424@qq.com> Date: Wed Nov 23 16:08:00 2022 +0800 frist commit diff --git a/frame_design_pattern/.idea/compiler.xml b/frame_design_pattern/.idea/compiler.xml new file mode 100644 index 0000000..f5258e9 --- /dev/null +++ b/frame_design_pattern/.idea/compiler.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/easyCodeTableSetting.xml b/frame_design_pattern/.idea/easyCodeTableSetting.xml new file mode 100644 index 0000000..dac3281 --- /dev/null +++ b/frame_design_pattern/.idea/easyCodeTableSetting.xml @@ -0,0 +1,166 @@ + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/encodings.xml b/frame_design_pattern/.idea/encodings.xml new file mode 100644 index 0000000..7df892b --- /dev/null +++ b/frame_design_pattern/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/frame_design_pattern.iml b/frame_design_pattern/.idea/frame_design_pattern.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/frame_design_pattern/.idea/frame_design_pattern.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__ch_qos_logback_logback_classic_1_2_3.xml b/frame_design_pattern/.idea/libraries/Maven__ch_qos_logback_logback_classic_1_2_3.xml new file mode 100644 index 0000000..6fec8f4 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__ch_qos_logback_logback_classic_1_2_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__ch_qos_logback_logback_core_1_2_3.xml b/frame_design_pattern/.idea/libraries/Maven__ch_qos_logback_logback_core_1_2_3.xml new file mode 100644 index 0000000..9eb8596 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__ch_qos_logback_logback_core_1_2_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_annotations_2_11_2.xml b/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_annotations_2_11_2.xml new file mode 100644 index 0000000..6447563 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_annotations_2_11_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_11_2.xml b/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_11_2.xml new file mode 100644 index 0000000..4923956 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_11_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_11_2.xml b/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_11_2.xml new file mode 100644 index 0000000..bb59715 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_11_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_11_2.xml b/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_11_2.xml new file mode 100644 index 0000000..c22af69 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_11_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_11_2.xml b/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_11_2.xml new file mode 100644 index 0000000..dd6fb59 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_11_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_11_2.xml b/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_11_2.xml new file mode 100644 index 0000000..22aba0a --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_11_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__com_jayway_jsonpath_json_path_2_4_0.xml b/frame_design_pattern/.idea/libraries/Maven__com_jayway_jsonpath_json_path_2_4_0.xml new file mode 100644 index 0000000..f19f6eb --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__com_jayway_jsonpath_json_path_2_4_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml b/frame_design_pattern/.idea/libraries/Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml new file mode 100644 index 0000000..b8581a6 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__jakarta_activation_jakarta_activation_api_1_2_2.xml b/frame_design_pattern/.idea/libraries/Maven__jakarta_activation_jakarta_activation_api_1_2_2.xml new file mode 100644 index 0000000..be90656 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__jakarta_activation_jakarta_activation_api_1_2_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__jakarta_annotation_jakarta_annotation_api_1_3_5.xml b/frame_design_pattern/.idea/libraries/Maven__jakarta_annotation_jakarta_annotation_api_1_3_5.xml new file mode 100644 index 0000000..cba9dd2 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__jakarta_annotation_jakarta_annotation_api_1_3_5.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__jakarta_xml_bind_jakarta_xml_bind_api_2_3_3.xml b/frame_design_pattern/.idea/libraries/Maven__jakarta_xml_bind_jakarta_xml_bind_api_2_3_3.xml new file mode 100644 index 0000000..04213f7 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__jakarta_xml_bind_jakarta_xml_bind_api_2_3_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__junit_junit_4_12.xml b/frame_design_pattern/.idea/libraries/Maven__junit_junit_4_12.xml new file mode 100644 index 0000000..d411041 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__junit_junit_4_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__junit_junit_4_13.xml b/frame_design_pattern/.idea/libraries/Maven__junit_junit_4_13.xml new file mode 100644 index 0000000..59fc5c4 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__junit_junit_4_13.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__log4j_log4j_1_2_17.xml b/frame_design_pattern/.idea/libraries/Maven__log4j_log4j_1_2_17.xml new file mode 100644 index 0000000..e383c1b --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__log4j_log4j_1_2_17.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__mysql_mysql_connector_java_5_1_6.xml b/frame_design_pattern/.idea/libraries/Maven__mysql_mysql_connector_java_5_1_6.xml new file mode 100644 index 0000000..2b062b5 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__mysql_mysql_connector_java_5_1_6.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__net_bytebuddy_byte_buddy_1_10_14.xml b/frame_design_pattern/.idea/libraries/Maven__net_bytebuddy_byte_buddy_1_10_14.xml new file mode 100644 index 0000000..395cf07 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__net_bytebuddy_byte_buddy_1_10_14.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__net_bytebuddy_byte_buddy_agent_1_10_14.xml b/frame_design_pattern/.idea/libraries/Maven__net_bytebuddy_byte_buddy_agent_1_10_14.xml new file mode 100644 index 0000000..9e75f7e --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__net_bytebuddy_byte_buddy_agent_1_10_14.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__net_minidev_accessors_smart_1_2.xml b/frame_design_pattern/.idea/libraries/Maven__net_minidev_accessors_smart_1_2.xml new file mode 100644 index 0000000..b3d3858 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__net_minidev_accessors_smart_1_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__net_minidev_json_smart_2_3.xml b/frame_design_pattern/.idea/libraries/Maven__net_minidev_json_smart_2_3.xml new file mode 100644 index 0000000..1083023 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__net_minidev_json_smart_2_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_apache_logging_log4j_log4j_api_2_13_3.xml b/frame_design_pattern/.idea/libraries/Maven__org_apache_logging_log4j_log4j_api_2_13_3.xml new file mode 100644 index 0000000..8ad4996 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_apache_logging_log4j_log4j_api_2_13_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_apache_logging_log4j_log4j_to_slf4j_2_13_3.xml b/frame_design_pattern/.idea/libraries/Maven__org_apache_logging_log4j_log4j_to_slf4j_2_13_3.xml new file mode 100644 index 0000000..57e6ac4 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_apache_logging_log4j_log4j_to_slf4j_2_13_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_38.xml b/frame_design_pattern/.idea/libraries/Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_38.xml new file mode 100644 index 0000000..7320a14 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_38.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_apache_tomcat_embed_tomcat_embed_websocket_9_0_38.xml b/frame_design_pattern/.idea/libraries/Maven__org_apache_tomcat_embed_tomcat_embed_websocket_9_0_38.xml new file mode 100644 index 0000000..8016c5f --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_apache_tomcat_embed_tomcat_embed_websocket_9_0_38.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_apiguardian_apiguardian_api_1_1_0.xml b/frame_design_pattern/.idea/libraries/Maven__org_apiguardian_apiguardian_api_1_1_0.xml new file mode 100644 index 0000000..f854ab0 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_apiguardian_apiguardian_api_1_1_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_assertj_assertj_core_3_16_1.xml b/frame_design_pattern/.idea/libraries/Maven__org_assertj_assertj_core_3_16_1.xml new file mode 100644 index 0000000..8976500 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_assertj_assertj_core_3_16_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_dom4j_dom4j_2_1_3.xml b/frame_design_pattern/.idea/libraries/Maven__org_dom4j_dom4j_2_1_3.xml new file mode 100644 index 0000000..10fb46a --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_dom4j_dom4j_2_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_glassfish_jakarta_el_3_0_3.xml b/frame_design_pattern/.idea/libraries/Maven__org_glassfish_jakarta_el_3_0_3.xml new file mode 100644 index 0000000..ae5020d --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_glassfish_jakarta_el_3_0_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_hamcrest_hamcrest_2_2.xml b/frame_design_pattern/.idea/libraries/Maven__org_hamcrest_hamcrest_2_2.xml new file mode 100644 index 0000000..6b5496f --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_hamcrest_hamcrest_2_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/frame_design_pattern/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_5_6_2.xml b/frame_design_pattern/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_5_6_2.xml new file mode 100644 index 0000000..7171153 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_5_6_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_api_5_6_2.xml b/frame_design_pattern/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_api_5_6_2.xml new file mode 100644 index 0000000..ef160d2 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_api_5_6_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_engine_5_6_2.xml b/frame_design_pattern/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_engine_5_6_2.xml new file mode 100644 index 0000000..fc4c644 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_engine_5_6_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_params_5_6_2.xml b/frame_design_pattern/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_params_5_6_2.xml new file mode 100644 index 0000000..45b4af3 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_params_5_6_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_junit_platform_junit_platform_commons_1_6_2.xml b/frame_design_pattern/.idea/libraries/Maven__org_junit_platform_junit_platform_commons_1_6_2.xml new file mode 100644 index 0000000..f2699b9 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_junit_platform_junit_platform_commons_1_6_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_junit_platform_junit_platform_engine_1_6_2.xml b/frame_design_pattern/.idea/libraries/Maven__org_junit_platform_junit_platform_engine_1_6_2.xml new file mode 100644 index 0000000..c13a302 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_junit_platform_junit_platform_engine_1_6_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_junit_vintage_junit_vintage_engine_5_6_2.xml b/frame_design_pattern/.idea/libraries/Maven__org_junit_vintage_junit_vintage_engine_5_6_2.xml new file mode 100644 index 0000000..697c91f --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_junit_vintage_junit_vintage_engine_5_6_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_mockito_mockito_core_3_3_3.xml b/frame_design_pattern/.idea/libraries/Maven__org_mockito_mockito_core_3_3_3.xml new file mode 100644 index 0000000..d2f4afa --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_mockito_mockito_core_3_3_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_mockito_mockito_junit_jupiter_3_3_3.xml b/frame_design_pattern/.idea/libraries/Maven__org_mockito_mockito_junit_jupiter_3_3_3.xml new file mode 100644 index 0000000..6c02a63 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_mockito_mockito_junit_jupiter_3_3_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_mybatis_mybatis_3_5_4.xml b/frame_design_pattern/.idea/libraries/Maven__org_mybatis_mybatis_3_5_4.xml new file mode 100644 index 0000000..69f0acd --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_mybatis_mybatis_3_5_4.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_objenesis_objenesis_2_6.xml b/frame_design_pattern/.idea/libraries/Maven__org_objenesis_objenesis_2_6.xml new file mode 100644 index 0000000..af41e3b --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_objenesis_objenesis_2_6.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_opentest4j_opentest4j_1_2_0.xml b/frame_design_pattern/.idea/libraries/Maven__org_opentest4j_opentest4j_1_2_0.xml new file mode 100644 index 0000000..fbc1b16 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_opentest4j_opentest4j_1_2_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_ow2_asm_asm_5_0_4.xml b/frame_design_pattern/.idea/libraries/Maven__org_ow2_asm_asm_5_0_4.xml new file mode 100644 index 0000000..0bf8cf2 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_ow2_asm_asm_5_0_4.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_skyscreamer_jsonassert_1_5_0.xml b/frame_design_pattern/.idea/libraries/Maven__org_skyscreamer_jsonassert_1_5_0.xml new file mode 100644 index 0000000..c4c54d6 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_skyscreamer_jsonassert_1_5_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_slf4j_jul_to_slf4j_1_7_30.xml b/frame_design_pattern/.idea/libraries/Maven__org_slf4j_jul_to_slf4j_1_7_30.xml new file mode 100644 index 0000000..27229ce --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_slf4j_jul_to_slf4j_1_7_30.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_30.xml b/frame_design_pattern/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_30.xml new file mode 100644 index 0000000..02b6812 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_30.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_2_3_4_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_2_3_4_RELEASE.xml new file mode 100644 index 0000000..0ba1daf --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_2_3_4_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_autoconfigure_2_3_4_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_autoconfigure_2_3_4_RELEASE.xml new file mode 100644 index 0000000..d6f441c --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_autoconfigure_2_3_4_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_2_3_4_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_2_3_4_RELEASE.xml new file mode 100644 index 0000000..c007c47 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_2_3_4_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_json_2_3_4_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_json_2_3_4_RELEASE.xml new file mode 100644 index 0000000..c157f28 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_json_2_3_4_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_logging_2_3_4_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_logging_2_3_4_RELEASE.xml new file mode 100644 index 0000000..1aad2ef --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_logging_2_3_4_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_test_2_3_4_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_test_2_3_4_RELEASE.xml new file mode 100644 index 0000000..679ec08 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_test_2_3_4_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_tomcat_2_3_4_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_tomcat_2_3_4_RELEASE.xml new file mode 100644 index 0000000..2f9c2ea --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_tomcat_2_3_4_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_web_2_3_4_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_web_2_3_4_RELEASE.xml new file mode 100644 index 0000000..3996a93 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_web_2_3_4_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_test_2_3_4_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_test_2_3_4_RELEASE.xml new file mode 100644 index 0000000..6ba62cf --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_test_2_3_4_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_3_4_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_3_4_RELEASE.xml new file mode 100644 index 0000000..90918e3 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_3_4_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_aop_5_2_9_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_aop_5_2_9_RELEASE.xml new file mode 100644 index 0000000..b72db4e --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_aop_5_2_9_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_beans_5_2_9_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_beans_5_2_9_RELEASE.xml new file mode 100644 index 0000000..3792885 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_beans_5_2_9_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_context_5_2_9_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_context_5_2_9_RELEASE.xml new file mode 100644 index 0000000..05820c0 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_context_5_2_9_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_core_5_2_9_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_core_5_2_9_RELEASE.xml new file mode 100644 index 0000000..02d3f7b --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_core_5_2_9_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_expression_5_2_9_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_expression_5_2_9_RELEASE.xml new file mode 100644 index 0000000..58f4ea4 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_expression_5_2_9_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_jcl_5_2_9_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_jcl_5_2_9_RELEASE.xml new file mode 100644 index 0000000..e5a2868 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_jcl_5_2_9_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_test_5_2_9_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_test_5_2_9_RELEASE.xml new file mode 100644 index 0000000..05fbe34 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_test_5_2_9_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_web_5_2_9_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_web_5_2_9_RELEASE.xml new file mode 100644 index 0000000..a2afb66 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_web_5_2_9_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_webmvc_5_2_9_RELEASE.xml b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_webmvc_5_2_9_RELEASE.xml new file mode 100644 index 0000000..30b2a5d --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_springframework_spring_webmvc_5_2_9_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_xmlunit_xmlunit_core_2_7_0.xml b/frame_design_pattern/.idea/libraries/Maven__org_xmlunit_xmlunit_core_2_7_0.xml new file mode 100644 index 0000000..006a8d3 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_xmlunit_xmlunit_core_2_7_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/libraries/Maven__org_yaml_snakeyaml_1_26.xml b/frame_design_pattern/.idea/libraries/Maven__org_yaml_snakeyaml_1_26.xml new file mode 100644 index 0000000..e998675 --- /dev/null +++ b/frame_design_pattern/.idea/libraries/Maven__org_yaml_snakeyaml_1_26.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/misc.xml b/frame_design_pattern/.idea/misc.xml new file mode 100644 index 0000000..b89895e --- /dev/null +++ b/frame_design_pattern/.idea/misc.xml @@ -0,0 +1,17 @@ + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/modules.xml b/frame_design_pattern/.idea/modules.xml new file mode 100644 index 0000000..6cfab6b --- /dev/null +++ b/frame_design_pattern/.idea/modules.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/uiDesigner.xml b/frame_design_pattern/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/frame_design_pattern/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/.idea/workspace.xml b/frame_design_pattern/.idea/workspace.xml new file mode 100644 index 0000000..27b1915 --- /dev/null +++ b/frame_design_pattern/.idea/workspace.xml @@ -0,0 +1,290 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1666602035862 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/springioc_test/src/main/java/com/mashibing/controller/CourseController.java + 13 + + + file://$PROJECT_DIR$/user_defined_springioc/src/main/java/com/mashibing/framework/context/support/ClassPathXmlApplicationContext.java + 20 + + + file://$PROJECT_DIR$/user_defined_springioc/src/main/java/com/mashibing/framework/context/support/AbstractApplicationContext.java + 31 + + + file://$PROJECT_DIR$/user_defined_springioc/src/main/java/com/mashibing/framework/context/support/ClassPathXmlApplicationContext.java + 82 + + + + + \ No newline at end of file diff --git a/frame_design_pattern/mybatis_design_pattern/mybatis_design_pattern.iml b/frame_design_pattern/mybatis_design_pattern/mybatis_design_pattern.iml new file mode 100644 index 0000000..a9d1925 --- /dev/null +++ b/frame_design_pattern/mybatis_design_pattern/mybatis_design_pattern.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/mybatis_design_pattern/pom.xml b/frame_design_pattern/mybatis_design_pattern/pom.xml new file mode 100644 index 0000000..3aa6a9a --- /dev/null +++ b/frame_design_pattern/mybatis_design_pattern/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + com.mashibing + mybatis_design_pattern + 1.0-SNAPSHOT + + + + + org.mybatis + mybatis + 3.5.4 + + + + mysql + mysql-connector-java + 5.1.6 + runtime + + + + junit + junit + 4.12 + test + + + + log4j + log4j + 1.2.17 + + + \ No newline at end of file diff --git a/frame_design_pattern/mybatis_design_pattern/src/main/java/com/mashibing/MainApp.java b/frame_design_pattern/mybatis_design_pattern/src/main/java/com/mashibing/MainApp.java new file mode 100644 index 0000000..d6d5da7 --- /dev/null +++ b/frame_design_pattern/mybatis_design_pattern/src/main/java/com/mashibing/MainApp.java @@ -0,0 +1,50 @@ +package com.mashibing; + +import com.mashibing.domain.User; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; + +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +/** + * @author spikeCong + * @date 2022/10/29 + **/ +public class MainApp { + + public static void main(String[] args) throws IOException { + + // 加载核心配置文件 + InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml"); + + // 获取SqlSessionFactory工厂对象 + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); + + // 获取SqlSession会话对象 + SqlSession sqlSession = sqlSessionFactory.openSession(); + + // 执行sql +// List list = sqlSession.selectList("com.mashibing.mapper.UserMapper.findAll"); +// for (User user : list) { +// System.out.println(user); +// } + + User user = sqlSession.selectOne("com.mashibing.mapper.UserMapper.findById", 1); + System.out.println(user); + + sqlSession.delete("com.mashibing.mapper.UserMapper.deleteById",3); + sqlSession.commit(); + + User user2 = sqlSession.selectOne("com.mashibing.mapper.UserMapper.findById", 1); + System.out.println(user2); + + System.out.println(user == user2); + + // 释放资源 + sqlSession.close(); + } +} diff --git a/frame_design_pattern/mybatis_design_pattern/src/main/java/com/mashibing/domain/User.java b/frame_design_pattern/mybatis_design_pattern/src/main/java/com/mashibing/domain/User.java new file mode 100644 index 0000000..d222637 --- /dev/null +++ b/frame_design_pattern/mybatis_design_pattern/src/main/java/com/mashibing/domain/User.java @@ -0,0 +1,78 @@ +package com.mashibing.domain; + +import java.util.Date; + +/** + * @author spikeCong + * @date 2022/10/29 + **/ +public class User { + + private Integer id; + private String username; + private Date birthday; + private String sex; + private String address; + + public User() { + } + + public User(Integer id, String username, Date birthday, String sex, String address) { + this.id = id; + this.username = username; + this.birthday = birthday; + this.sex = sex; + this.address = address; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public Date getBirthday() { + return birthday; + } + + public void setBirthday(Date birthday) { + this.birthday = birthday; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", username='" + username + '\'' + + ", birthday=" + birthday + + ", sex='" + sex + '\'' + + ", address='" + address + '\'' + + '}'; + } +} diff --git a/frame_design_pattern/mybatis_design_pattern/src/main/java/com/mashibing/mapper/UserMapper.java b/frame_design_pattern/mybatis_design_pattern/src/main/java/com/mashibing/mapper/UserMapper.java new file mode 100644 index 0000000..813ed7a --- /dev/null +++ b/frame_design_pattern/mybatis_design_pattern/src/main/java/com/mashibing/mapper/UserMapper.java @@ -0,0 +1,18 @@ +package com.mashibing.mapper; + +import com.mashibing.domain.User; + +import java.util.List; + +/** + * @author spikeCong + * @date 2022/10/29 + **/ +public interface UserMapper { + + public List findAll(); + + User findById(Integer id); + + int deleteById(int id); +} diff --git a/frame_design_pattern/mybatis_design_pattern/src/main/resources/SqlMapConfig.xml b/frame_design_pattern/mybatis_design_pattern/src/main/resources/SqlMapConfig.xml new file mode 100644 index 0000000..87e3af5 --- /dev/null +++ b/frame_design_pattern/mybatis_design_pattern/src/main/resources/SqlMapConfig.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/mybatis_design_pattern/src/main/resources/com/mashibing/mapper/UserMapper.xml b/frame_design_pattern/mybatis_design_pattern/src/main/resources/com/mashibing/mapper/UserMapper.xml new file mode 100644 index 0000000..b24026a --- /dev/null +++ b/frame_design_pattern/mybatis_design_pattern/src/main/resources/com/mashibing/mapper/UserMapper.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + delete from user where id= #{id}; + + + \ No newline at end of file diff --git a/frame_design_pattern/mybatis_design_pattern/target/classes/META-INF/mybatis_design_pattern.kotlin_module b/frame_design_pattern/mybatis_design_pattern/target/classes/META-INF/mybatis_design_pattern.kotlin_module new file mode 100644 index 0000000..a49347a Binary files /dev/null and b/frame_design_pattern/mybatis_design_pattern/target/classes/META-INF/mybatis_design_pattern.kotlin_module differ diff --git a/frame_design_pattern/mybatis_design_pattern/target/classes/SqlMapConfig.xml b/frame_design_pattern/mybatis_design_pattern/target/classes/SqlMapConfig.xml new file mode 100644 index 0000000..87e3af5 --- /dev/null +++ b/frame_design_pattern/mybatis_design_pattern/target/classes/SqlMapConfig.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/mybatis_design_pattern/target/classes/com/mashibing/MainApp.class b/frame_design_pattern/mybatis_design_pattern/target/classes/com/mashibing/MainApp.class new file mode 100644 index 0000000..72c814c Binary files /dev/null and b/frame_design_pattern/mybatis_design_pattern/target/classes/com/mashibing/MainApp.class differ diff --git a/frame_design_pattern/mybatis_design_pattern/target/classes/com/mashibing/domain/User.class b/frame_design_pattern/mybatis_design_pattern/target/classes/com/mashibing/domain/User.class new file mode 100644 index 0000000..46905bd Binary files /dev/null and b/frame_design_pattern/mybatis_design_pattern/target/classes/com/mashibing/domain/User.class differ diff --git a/frame_design_pattern/mybatis_design_pattern/target/classes/com/mashibing/mapper/UserMapper.class b/frame_design_pattern/mybatis_design_pattern/target/classes/com/mashibing/mapper/UserMapper.class new file mode 100644 index 0000000..e9b0421 Binary files /dev/null and b/frame_design_pattern/mybatis_design_pattern/target/classes/com/mashibing/mapper/UserMapper.class differ diff --git a/frame_design_pattern/mybatis_design_pattern/target/classes/com/mashibing/mapper/UserMapper.xml b/frame_design_pattern/mybatis_design_pattern/target/classes/com/mashibing/mapper/UserMapper.xml new file mode 100644 index 0000000..b24026a --- /dev/null +++ b/frame_design_pattern/mybatis_design_pattern/target/classes/com/mashibing/mapper/UserMapper.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + delete from user where id= #{id}; + + + \ No newline at end of file diff --git a/frame_design_pattern/spring_design_pattern/HELP.md b/frame_design_pattern/spring_design_pattern/HELP.md new file mode 100644 index 0000000..5dfe498 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/HELP.md @@ -0,0 +1,17 @@ +# Getting Started + +### Reference Documentation +For further reference, please consider the following sections: + +* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) +* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.7.5/maven-plugin/reference/html/) +* [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.7.5/maven-plugin/reference/html/#build-image) +* [Spring Web](https://docs.spring.io/spring-boot/docs/2.7.5/reference/htmlsingle/#web) + +### Guides +The following guides illustrate how to use some features concretely: + +* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/) +* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/) +* [Building REST services with Spring](https://spring.io/guides/tutorials/rest/) + diff --git a/frame_design_pattern/spring_design_pattern/pom.xml b/frame_design_pattern/spring_design_pattern/pom.xml new file mode 100644 index 0000000..f64567e --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.4.RELEASE + + + com.mashibing + spring_design_pattern + 0.0.1-SNAPSHOT + spring_design_pattern + Demo project for Spring Boot + + 17 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.mashibing + user_defined_springioc + 1.0-SNAPSHOT + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/frame_design_pattern/spring_design_pattern/spring_design_pattern.iml b/frame_design_pattern/spring_design_pattern/spring_design_pattern.iml new file mode 100644 index 0000000..38ae24d --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/spring_design_pattern.iml @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo01/Friends.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo01/Friends.java new file mode 100644 index 0000000..2f7cabe --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo01/Friends.java @@ -0,0 +1,23 @@ +package com.mashibing.spring01.demo01; + +import java.util.List; + +/** + * @author spikeCong + * @date 2022/10/25 + **/ +public class Friends { + + private List names; + + public Friends() { + } + + public List getNames() { + return names; + } + + public void setNames(List names) { + this.names = names; + } +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo01/SpringTest01.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo01/SpringTest01.java new file mode 100644 index 0000000..beb390e --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo01/SpringTest01.java @@ -0,0 +1,30 @@ +package com.mashibing.spring01.demo01; + +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionReader; +import org.springframework.beans.factory.xml.XmlBeanFactory; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.core.io.ClassPathResource; + +import java.util.List; + +/** + * @author spikeCong + * @date 2022/10/25 + **/ +public class SpringTest01 { + + public static void main(String[] args) { + + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); + + User user = context.getBean("user", User.class); + user.setId(1001); + user.setName("大远"); + + List names = user.getFriends().getNames(); + for (String name : names) { + System.out.println(user.getName() + "的朋友包括: " + name); + } + } +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo01/User.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo01/User.java new file mode 100644 index 0000000..af08d40 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo01/User.java @@ -0,0 +1,54 @@ +package com.mashibing.spring01.demo01; + +/** + * @author spikeCong + * @date 2022/10/25 + **/ +public class User { + + private int id; + + private String name; + + private Friends friends; + + public User() { + } + + public User(Friends friends) { + this.friends = friends; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Friends getFriends() { + return friends; + } + + public void setFriends(Friends friends) { + this.friends = friends; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + ", friends=" + friends + + '}'; + } +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo02/AppConfig.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo02/AppConfig.java new file mode 100644 index 0000000..69647ed --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo02/AppConfig.java @@ -0,0 +1,13 @@ +package com.mashibing.spring01.demo02; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +/** + * @author spikeCong + * @date 2022/10/25 + **/ +@Configuration +@ComponentScan("com.mashibing.spring01.demo02") +public class AppConfig { +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo02/SpringTest.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo02/SpringTest.java new file mode 100644 index 0000000..ec92fed --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo02/SpringTest.java @@ -0,0 +1,25 @@ +package com.mashibing.spring01.demo02; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +/** + * @author spikeCong + * @date 2022/10/25 + **/ +public class SpringTest { + + public static void main(String[] args) { + + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); + +// StudentBean studentBean = (StudentBean) context.getBean("studentBean"); +// System.out.println(studentBean); + + //当用户使用容器本身时,可以使用转义符 "&" 来得到FactoryBean本身,以此来区分Factorybean产生的实例对象和FactoryBean本身 + StudentBean studentBean = (StudentBean) context.getBean("&studentBean"); + studentBean.study(); + + TeacherBean teacherBean = (TeacherBean) context.getBean("studentBean"); + teacherBean.teach(); + } +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo02/StudentBean.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo02/StudentBean.java new file mode 100644 index 0000000..2b1041b --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo02/StudentBean.java @@ -0,0 +1,33 @@ +package com.mashibing.spring01.demo02; + +import org.springframework.beans.factory.FactoryBean; +import org.springframework.stereotype.Component; + +/** + * 生产自定义bean + * @author spikeCong + * @date 2022/10/25 + **/ +@Component("studentBean") +public class StudentBean implements FactoryBean { + + + //返回工厂中的实例 + @Override + public Object getObject() throws Exception { + + //return new StudentBean(); + //这里返回的不一定是自身的实例,可以是任何对象的实例 + return new TeacherBean(); + } + + //该方法返回的是在IOC容器中getBean所匹配的类型 + @Override + public Class getObjectType() { + return StudentBean.class; + } + + public void study(){ + System.out.println("学生正在学习......"); + } +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo02/TeacherBean.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo02/TeacherBean.java new file mode 100644 index 0000000..74a2014 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring01/demo02/TeacherBean.java @@ -0,0 +1,12 @@ +package com.mashibing.spring01.demo02; + +/** + * @author spikeCong + * @date 2022/10/25 + **/ +public class TeacherBean { + + public void teach(){ + System.out.println("老师正在上课......"); + } +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring_design_pattern/SpringDesignPatternApplication.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring_design_pattern/SpringDesignPatternApplication.java new file mode 100644 index 0000000..13a1757 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/spring_design_pattern/SpringDesignPatternApplication.java @@ -0,0 +1,14 @@ +package com.mashibing.spring_design_pattern; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringDesignPatternApplication { + + public static void main(String[] args) { + + SpringApplication.run(SpringDesignPatternApplication.class, args); + } + +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/controller/CourseController.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/controller/CourseController.java new file mode 100644 index 0000000..6d0638b --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/controller/CourseController.java @@ -0,0 +1,24 @@ +package com.mashibing.test_springioc.controller; + +import com.mashibing.test_springioc.service.CourseService; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +/** + * @author spikeCong + * @date 2022/10/28 + **/ +public class CourseController { + + public static void main(String[] args) throws Exception { + + //1.创建Spring的容器对象 + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); + + //2.从容器对象中获取CourseService对象 + CourseService courseService = context.getBean("courseService", CourseService.class); + + //3.调用UserService的add方法 + courseService.add(); + } +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/controller/CourseController_.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/controller/CourseController_.java new file mode 100644 index 0000000..d5943ca --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/controller/CourseController_.java @@ -0,0 +1,22 @@ +package com.mashibing.test_springioc.controller; + +import com.mashibing.framework.context.support.ClassPathXmlApplicationContext; +import com.mashibing.test_springioc.service.CourseService; + +/** + * @author spikeCong + * @date 2022/10/28 + **/ +public class CourseController_ { + + public static void main(String[] args) { + //1.创建Spring的容器对象 + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); + + //2.从容器对象中获取CourseService对象 + CourseService courseService = context.getBean("courseService", CourseService.class); + + //3.调用UserService的add方法 + courseService.add(); + } +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/dao/CourseDao.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/dao/CourseDao.java new file mode 100644 index 0000000..ab0b299 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/dao/CourseDao.java @@ -0,0 +1,9 @@ +package com.mashibing.test_springioc.dao; + +/** + * @author spikeCong + * @date 2022/10/28 + **/ +public interface CourseDao { + public void add(); +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/dao/impl/CourseDaoImpl.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/dao/impl/CourseDaoImpl.java new file mode 100644 index 0000000..00f2011 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/dao/impl/CourseDaoImpl.java @@ -0,0 +1,31 @@ +package com.mashibing.test_springioc.dao.impl; + + +import com.mashibing.test_springioc.dao.CourseDao; + +/** + * @author spikeCong + * @date 2022/10/28 + **/ +public class CourseDaoImpl implements CourseDao { + + //value注入 + private String courseName; + + public String getCourseName() { + return courseName; + } + + public void setCourseName(String courseName) { + this.courseName = courseName; + } + + public CourseDaoImpl() { + System.out.println("CourseDaoImpl创建了......"); + } + + @Override + public void add() { + System.out.println("CourseDaoImpl的add方法执行了......" + courseName); + } +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/service/CourseService.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/service/CourseService.java new file mode 100644 index 0000000..4d56ab9 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/service/CourseService.java @@ -0,0 +1,10 @@ +package com.mashibing.test_springioc.service; + +/** + * @author spikeCong + * @date 2022/10/28 + **/ +public interface CourseService { + + public void add(); +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/service/impl/CourseServiceImpl.java b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/service/impl/CourseServiceImpl.java new file mode 100644 index 0000000..15ca65a --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/java/com/mashibing/test_springioc/service/impl/CourseServiceImpl.java @@ -0,0 +1,27 @@ +package com.mashibing.test_springioc.service.impl; + +import com.mashibing.test_springioc.dao.CourseDao; +import com.mashibing.test_springioc.service.CourseService; + +/** + * @author spikeCong + * @date 2022/10/28 + **/ +public class CourseServiceImpl implements CourseService { + + public CourseServiceImpl() { + System.out.println("CourseServiceImpl创建了......"); + } + + private CourseDao courseDao; + + public void setCourseDao(CourseDao courseDao) { + this.courseDao = courseDao; + } + + @Override + public void add() { + System.out.println("CourseServiceImpl的add方法执行了......"); + courseDao.add(); + } +} diff --git a/frame_design_pattern/spring_design_pattern/src/main/resources/application.properties b/frame_design_pattern/spring_design_pattern/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/frame_design_pattern/spring_design_pattern/src/main/resources/applicationContext.xml b/frame_design_pattern/spring_design_pattern/src/main/resources/applicationContext.xml new file mode 100644 index 0000000..1e61cbd --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/resources/applicationContext.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/spring_design_pattern/src/main/resources/bean.xml b/frame_design_pattern/spring_design_pattern/src/main/resources/bean.xml new file mode 100644 index 0000000..5f56591 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/main/resources/bean.xml @@ -0,0 +1,30 @@ + + + + + + + + + + "LiLi" + "ANAN" + "xiaoxiao" + + + + \ No newline at end of file diff --git a/frame_design_pattern/spring_design_pattern/src/test/java/com/mashibing/spring_design_pattern/SpringDesignPatternApplicationTests.java b/frame_design_pattern/spring_design_pattern/src/test/java/com/mashibing/spring_design_pattern/SpringDesignPatternApplicationTests.java new file mode 100644 index 0000000..e64fd9e --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/src/test/java/com/mashibing/spring_design_pattern/SpringDesignPatternApplicationTests.java @@ -0,0 +1,13 @@ +package com.mashibing.spring_design_pattern; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringDesignPatternApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/frame_design_pattern/spring_design_pattern/target/classes/META-INF/spring_design_pattern.kotlin_module b/frame_design_pattern/spring_design_pattern/target/classes/META-INF/spring_design_pattern.kotlin_module new file mode 100644 index 0000000..a49347a Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/META-INF/spring_design_pattern.kotlin_module differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/application.properties b/frame_design_pattern/spring_design_pattern/target/classes/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/target/classes/application.properties @@ -0,0 +1 @@ + diff --git a/frame_design_pattern/spring_design_pattern/target/classes/applicationContext.xml b/frame_design_pattern/spring_design_pattern/target/classes/applicationContext.xml new file mode 100644 index 0000000..1e61cbd --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/target/classes/applicationContext.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/spring_design_pattern/target/classes/bean.xml b/frame_design_pattern/spring_design_pattern/target/classes/bean.xml new file mode 100644 index 0000000..5f56591 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/target/classes/bean.xml @@ -0,0 +1,30 @@ + + + + + + + + + + "LiLi" + "ANAN" + "xiaoxiao" + + + + \ No newline at end of file diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo01/Friends.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo01/Friends.class new file mode 100644 index 0000000..4a259af Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo01/Friends.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo01/SpringTest01.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo01/SpringTest01.class new file mode 100644 index 0000000..25f808d Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo01/SpringTest01.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo01/User.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo01/User.class new file mode 100644 index 0000000..c835285 Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo01/User.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo02/AppConfig.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo02/AppConfig.class new file mode 100644 index 0000000..654ca67 Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo02/AppConfig.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo02/SpringTest.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo02/SpringTest.class new file mode 100644 index 0000000..3b70388 Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo02/SpringTest.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo02/StudentBean.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo02/StudentBean.class new file mode 100644 index 0000000..5b58df5 Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo02/StudentBean.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo02/TeacherBean.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo02/TeacherBean.class new file mode 100644 index 0000000..3d56a1e Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring01/demo02/TeacherBean.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring_design_pattern/SpringDesignPatternApplication.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring_design_pattern/SpringDesignPatternApplication.class new file mode 100644 index 0000000..641b5d0 Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/spring_design_pattern/SpringDesignPatternApplication.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/controller/CourseController.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/controller/CourseController.class new file mode 100644 index 0000000..7ac8abf Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/controller/CourseController.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/controller/CourseController_.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/controller/CourseController_.class new file mode 100644 index 0000000..9de7413 Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/controller/CourseController_.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/dao/CourseDao.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/dao/CourseDao.class new file mode 100644 index 0000000..037163e Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/dao/CourseDao.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/dao/impl/CourseDaoImpl.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/dao/impl/CourseDaoImpl.class new file mode 100644 index 0000000..f320114 Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/dao/impl/CourseDaoImpl.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/service/CourseService.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/service/CourseService.class new file mode 100644 index 0000000..5466297 Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/service/CourseService.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/service/impl/CourseServiceImpl.class b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/service/impl/CourseServiceImpl.class new file mode 100644 index 0000000..a4e8f58 Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/classes/com/mashibing/test_springioc/service/impl/CourseServiceImpl.class differ diff --git a/frame_design_pattern/spring_design_pattern/target/maven-archiver/pom.properties b/frame_design_pattern/spring_design_pattern/target/maven-archiver/pom.properties new file mode 100644 index 0000000..4cfca27 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/target/maven-archiver/pom.properties @@ -0,0 +1,3 @@ +artifactId=spring_design_pattern +groupId=com.mashibing +version=0.0.1-SNAPSHOT diff --git a/frame_design_pattern/spring_design_pattern/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/frame_design_pattern/spring_design_pattern/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..5931c5d --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1,13 @@ +com\mashibing\spring01\demo02\StudentBean.class +com\mashibing\test_springioc\dao\CourseDao.class +com\mashibing\test_springioc\dao\impl\CourseDaoImpl.class +com\mashibing\spring01\demo02\AppConfig.class +com\mashibing\spring01\demo02\SpringTest.class +com\mashibing\spring01\demo02\TeacherBean.class +com\mashibing\spring01\demo01\SpringTest01.class +com\mashibing\test_springioc\controller\CourseController.class +com\mashibing\test_springioc\service\CourseService.class +com\mashibing\spring01\demo01\Friends.class +com\mashibing\spring01\demo01\User.class +com\mashibing\test_springioc\service\impl\CourseServiceImpl.class +com\mashibing\spring_design_pattern\SpringDesignPatternApplication.class diff --git a/frame_design_pattern/spring_design_pattern/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/frame_design_pattern/spring_design_pattern/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..57cfe2e --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,13 @@ +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\spring01\demo02\StudentBean.java +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\test_springioc\controller\CourseController.java +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\spring01\demo02\SpringTest.java +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\spring01\demo01\Friends.java +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\test_springioc\dao\impl\CourseDaoImpl.java +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\test_springioc\dao\CourseDao.java +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\spring01\demo01\User.java +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\test_springioc\service\impl\CourseServiceImpl.java +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\spring_design_pattern\SpringDesignPatternApplication.java +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\spring01\demo02\AppConfig.java +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\spring01\demo02\TeacherBean.java +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\test_springioc\service\CourseService.java +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\main\java\com\mashibing\spring01\demo01\SpringTest01.java diff --git a/frame_design_pattern/spring_design_pattern/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst b/frame_design_pattern/spring_design_pattern/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst new file mode 100644 index 0000000..1081480 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst @@ -0,0 +1 @@ +com\mashibing\spring_design_pattern\SpringDesignPatternApplicationTests.class diff --git a/frame_design_pattern/spring_design_pattern/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/frame_design_pattern/spring_design_pattern/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst new file mode 100644 index 0000000..caa668a --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst @@ -0,0 +1 @@ +I:\MSB\msb_work\frame_design_pattern\spring_design_pattern\src\test\java\com\mashibing\spring_design_pattern\SpringDesignPatternApplicationTests.java diff --git a/frame_design_pattern/spring_design_pattern/target/spring_design_pattern-0.0.1-SNAPSHOT.jar b/frame_design_pattern/spring_design_pattern/target/spring_design_pattern-0.0.1-SNAPSHOT.jar new file mode 100644 index 0000000..81e1e6e Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/spring_design_pattern-0.0.1-SNAPSHOT.jar differ diff --git a/frame_design_pattern/spring_design_pattern/target/spring_design_pattern-0.0.1-SNAPSHOT.jar.original b/frame_design_pattern/spring_design_pattern/target/spring_design_pattern-0.0.1-SNAPSHOT.jar.original new file mode 100644 index 0000000..0512056 Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/spring_design_pattern-0.0.1-SNAPSHOT.jar.original differ diff --git a/frame_design_pattern/spring_design_pattern/target/surefire-reports/TEST-com.mashibing.spring_design_pattern.SpringDesignPatternApplicationTests.xml b/frame_design_pattern/spring_design_pattern/target/surefire-reports/TEST-com.mashibing.spring_design_pattern.SpringDesignPatternApplicationTests.xml new file mode 100644 index 0000000..6c8b9d6 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/target/surefire-reports/TEST-com.mashibing.spring_design_pattern.SpringDesignPatternApplicationTests.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/spring_design_pattern/target/surefire-reports/com.mashibing.spring_design_pattern.SpringDesignPatternApplicationTests.txt b/frame_design_pattern/spring_design_pattern/target/surefire-reports/com.mashibing.spring_design_pattern.SpringDesignPatternApplicationTests.txt new file mode 100644 index 0000000..54a6e62 --- /dev/null +++ b/frame_design_pattern/spring_design_pattern/target/surefire-reports/com.mashibing.spring_design_pattern.SpringDesignPatternApplicationTests.txt @@ -0,0 +1,4 @@ +------------------------------------------------------------------------------- +Test set: com.mashibing.spring_design_pattern.SpringDesignPatternApplicationTests +------------------------------------------------------------------------------- +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.77 s - in com.mashibing.spring_design_pattern.SpringDesignPatternApplicationTests diff --git a/frame_design_pattern/spring_design_pattern/target/test-classes/com/mashibing/spring_design_pattern/SpringDesignPatternApplicationTests.class b/frame_design_pattern/spring_design_pattern/target/test-classes/com/mashibing/spring_design_pattern/SpringDesignPatternApplicationTests.class new file mode 100644 index 0000000..f7bddb8 Binary files /dev/null and b/frame_design_pattern/spring_design_pattern/target/test-classes/com/mashibing/spring_design_pattern/SpringDesignPatternApplicationTests.class differ diff --git a/frame_design_pattern/springioc_test/pom.xml b/frame_design_pattern/springioc_test/pom.xml new file mode 100644 index 0000000..d6b1be7 --- /dev/null +++ b/frame_design_pattern/springioc_test/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + com.mashibing + springioc_test + 1.0-SNAPSHOT + + + + com.mashibing + user_defined_springioc + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/frame_design_pattern/springioc_test/springioc_test.iml b/frame_design_pattern/springioc_test/springioc_test.iml new file mode 100644 index 0000000..1c086c6 --- /dev/null +++ b/frame_design_pattern/springioc_test/springioc_test.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/springioc_test/src/main/java/com/mashibing/controller/CourseController.java b/frame_design_pattern/springioc_test/src/main/java/com/mashibing/controller/CourseController.java new file mode 100644 index 0000000..e0e3691 --- /dev/null +++ b/frame_design_pattern/springioc_test/src/main/java/com/mashibing/controller/CourseController.java @@ -0,0 +1,22 @@ +package com.mashibing.controller; + +import com.mashibing.framework.context.support.ClassPathXmlApplicationContext; +import com.mashibing.service.CourseService; + +/** + * @author spikeCong + * @date 2022/10/30 + **/ +public class CourseController { + + public static void main(String[] args) { + //1.创建Spring容器对象 + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); + + //2.从容器中获取CourseService对象 + CourseService courseService = context.getBean("courseService", CourseService.class); + + //3.调用CourseService的add方法 + courseService.add(); + } +} diff --git a/frame_design_pattern/springioc_test/src/main/java/com/mashibing/dao/CourseDao.java b/frame_design_pattern/springioc_test/src/main/java/com/mashibing/dao/CourseDao.java new file mode 100644 index 0000000..728b781 --- /dev/null +++ b/frame_design_pattern/springioc_test/src/main/java/com/mashibing/dao/CourseDao.java @@ -0,0 +1,10 @@ +package com.mashibing.dao; + +/** + * @author spikeCong + * @date 2022/10/30 + **/ +public interface CourseDao { + + public void add(); +} diff --git a/frame_design_pattern/springioc_test/src/main/java/com/mashibing/dao/impl/CourseDaoImpl.java b/frame_design_pattern/springioc_test/src/main/java/com/mashibing/dao/impl/CourseDaoImpl.java new file mode 100644 index 0000000..f81359e --- /dev/null +++ b/frame_design_pattern/springioc_test/src/main/java/com/mashibing/dao/impl/CourseDaoImpl.java @@ -0,0 +1,30 @@ +package com.mashibing.dao.impl; + +import com.mashibing.dao.CourseDao; + +/** + * @author spikeCong + * @date 2022/10/30 + **/ +public class CourseDaoImpl implements CourseDao { + + //value注入 + private String courseName; + + public String getCourseName() { + return courseName; + } + + public void setCourseName(String courseName) { + this.courseName = courseName; + } + + public CourseDaoImpl() { + System.out.println("CourseDaoImpl创建了......"); + } + + @Override + public void add() { + System.out.println("CourseDaoImpl执行了......,课程名: " + courseName); + } +} diff --git a/frame_design_pattern/springioc_test/src/main/java/com/mashibing/service/CourseService.java b/frame_design_pattern/springioc_test/src/main/java/com/mashibing/service/CourseService.java new file mode 100644 index 0000000..1abe7a8 --- /dev/null +++ b/frame_design_pattern/springioc_test/src/main/java/com/mashibing/service/CourseService.java @@ -0,0 +1,10 @@ +package com.mashibing.service; + +/** + * @author spikeCong + * @date 2022/10/30 + **/ +public interface CourseService { + + public void add(); +} diff --git a/frame_design_pattern/springioc_test/src/main/java/com/mashibing/service/impl/CourseServiceImpl.java b/frame_design_pattern/springioc_test/src/main/java/com/mashibing/service/impl/CourseServiceImpl.java new file mode 100644 index 0000000..9373d9b --- /dev/null +++ b/frame_design_pattern/springioc_test/src/main/java/com/mashibing/service/impl/CourseServiceImpl.java @@ -0,0 +1,27 @@ +package com.mashibing.service.impl; + +import com.mashibing.dao.CourseDao; +import com.mashibing.service.CourseService; + +/** + * @author spikeCong + * @date 2022/10/30 + **/ +public class CourseServiceImpl implements CourseService { + + public CourseServiceImpl() { + System.out.println("CourseServiceImpl创建了......"); + } + + private CourseDao courseDao; + + public void setCourseDao(CourseDao courseDao) { + this.courseDao = courseDao; + } + + @Override + public void add() { + System.out.println("CourseServiceImpl执行了......"); + courseDao.add(); + } +} diff --git a/frame_design_pattern/springioc_test/src/main/resources/applicationContext.xml b/frame_design_pattern/springioc_test/src/main/resources/applicationContext.xml new file mode 100644 index 0000000..bee7c33 --- /dev/null +++ b/frame_design_pattern/springioc_test/src/main/resources/applicationContext.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/springioc_test/target/classes/applicationContext.xml b/frame_design_pattern/springioc_test/target/classes/applicationContext.xml new file mode 100644 index 0000000..bee7c33 --- /dev/null +++ b/frame_design_pattern/springioc_test/target/classes/applicationContext.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/springioc_test/target/classes/com/mashibing/controller/CourseController.class b/frame_design_pattern/springioc_test/target/classes/com/mashibing/controller/CourseController.class new file mode 100644 index 0000000..cee1e9d Binary files /dev/null and b/frame_design_pattern/springioc_test/target/classes/com/mashibing/controller/CourseController.class differ diff --git a/frame_design_pattern/springioc_test/target/classes/com/mashibing/dao/CourseDao.class b/frame_design_pattern/springioc_test/target/classes/com/mashibing/dao/CourseDao.class new file mode 100644 index 0000000..3ab4133 Binary files /dev/null and b/frame_design_pattern/springioc_test/target/classes/com/mashibing/dao/CourseDao.class differ diff --git a/frame_design_pattern/springioc_test/target/classes/com/mashibing/dao/impl/CourseDaoImpl.class b/frame_design_pattern/springioc_test/target/classes/com/mashibing/dao/impl/CourseDaoImpl.class new file mode 100644 index 0000000..6dba518 Binary files /dev/null and b/frame_design_pattern/springioc_test/target/classes/com/mashibing/dao/impl/CourseDaoImpl.class differ diff --git a/frame_design_pattern/springioc_test/target/classes/com/mashibing/service/CourseService.class b/frame_design_pattern/springioc_test/target/classes/com/mashibing/service/CourseService.class new file mode 100644 index 0000000..a0b3e59 Binary files /dev/null and b/frame_design_pattern/springioc_test/target/classes/com/mashibing/service/CourseService.class differ diff --git a/frame_design_pattern/springioc_test/target/classes/com/mashibing/service/impl/CourseServiceImpl.class b/frame_design_pattern/springioc_test/target/classes/com/mashibing/service/impl/CourseServiceImpl.class new file mode 100644 index 0000000..719f3ca Binary files /dev/null and b/frame_design_pattern/springioc_test/target/classes/com/mashibing/service/impl/CourseServiceImpl.class differ diff --git a/frame_design_pattern/user_defined_springioc/pom.xml b/frame_design_pattern/user_defined_springioc/pom.xml new file mode 100644 index 0000000..6011c1a --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/pom.xml @@ -0,0 +1,19 @@ + + + 4.0.0 + + com.mashibing + user_defined_springioc + 1.0-SNAPSHOT + + + + org.dom4j + dom4j + 2.1.3 + + + + \ No newline at end of file diff --git a/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/BeanDefinition.java b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/BeanDefinition.java new file mode 100644 index 0000000..cd16b54 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/BeanDefinition.java @@ -0,0 +1,52 @@ +package com.mashibing.framework.beans; + +/** + * 封装bean标签数据的类 包括id和class以及bean的子标签property + * @author spikeCong + * @date 2022/10/27 + **/ +public class BeanDefinition { + + private String id; + + private String className; + + private MutablePropertyValues propertyValues; + + public BeanDefinition() { + propertyValues = new MutablePropertyValues(); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public MutablePropertyValues getPropertyValues() { + return propertyValues; + } + + public void setPropertyValues(MutablePropertyValues propertyValues) { + this.propertyValues = propertyValues; + } + + @Override + public String toString() { + return "BeanDefinition{" + + "id='" + id + '\'' + + ", className='" + className + '\'' + + ", propertyValues=" + propertyValues + + '}'; + } +} diff --git a/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/MutablePropertyValues.java b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/MutablePropertyValues.java new file mode 100644 index 0000000..3c49a45 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/MutablePropertyValues.java @@ -0,0 +1,94 @@ +package com.mashibing.framework.beans; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * 该类用来存储和遍历多个PropertyValue对象 + * @author spikeCong + * @date 2022/10/27 + **/ +public class MutablePropertyValues implements Iterable{ + + //定义一个集合,存储PropertyValue + private final List propertyValueList; + + public MutablePropertyValues() { + this.propertyValueList = new ArrayList(); + } + + //有参构造 接收一个外部传入的list,赋值给propertyValueList属性 + public MutablePropertyValues(List propertyValueList) { + if(propertyValueList == null){ + this.propertyValueList = new ArrayList(); + }else{ + this.propertyValueList = propertyValueList; + } + } + + + //获取当前容器对应的迭代器对象 + @Override + public Iterator iterator() { + //直接获取List集合对应的迭代器 + return propertyValueList.iterator(); + } + + //获取所有的PropertyValue + public PropertyValue[] getPropertyValues(){ + + return propertyValueList.toArray(new PropertyValue[0]); + } + + //根据name属性获取PropertyValue + public PropertyValue getPropertyValue(String propertyName){ + for (PropertyValue propertyValue : propertyValueList) { + if(propertyValue.getName().equals(propertyName)){ + return propertyValue; + } + } + + return null; + } + + //判断集合是否为空 + public boolean isEmpty(){ + return propertyValueList.isEmpty(); + } + + //向集合中添加 + public MutablePropertyValues addPropertyValue(PropertyValue propertyValue){ + //判断集合中存储的propertyValue对象,是否重复,如果重复就覆盖 + for (int i = 0; i < propertyValueList.size(); i++) { + + //获取集合中的propertyValue + PropertyValue currentPv = propertyValueList.get(i); + + //判断当前的pv的name属性 是否与传入的相同,如果相同就覆盖 + if(currentPv.getName().equals(propertyValue.getName())){ + propertyValueList.set(i,propertyValue); + return this; + } + } + + //没有重复,直接添加 + this.propertyValueList.add(propertyValue); + return this; //目的实现链式编程 + } + + //判断是否有指定的name属性值的对象 + public boolean contains(String name){ + + return getPropertyValue(name) != null; + } + + + @Override + public String toString() { + return "MutablePropertyValues{" + + "propertyValueList=" + propertyValueList + + '}'; + } + +} diff --git a/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/PropertyValue.java b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/PropertyValue.java new file mode 100644 index 0000000..1e0317e --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/PropertyValue.java @@ -0,0 +1,60 @@ +package com.mashibing.framework.beans; + +/** + * 该类用来封装Bean标签下的property子标签属性 + * 1.name属性 + * 2.ref属性 + * 3.value属性: 给基本数据类型或者String类型数据赋值的值 + * @author spikeCong + * @date 2022/10/27 + **/ +public class PropertyValue { + + private String name; + + private String ref; + + private String value; + + public PropertyValue() { + } + + public PropertyValue(String name, String ref, String value) { + this.name = name; + this.ref = ref; + this.value = value; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getRef() { + return ref; + } + + public void setRef(String ref) { + this.ref = ref; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public String toString() { + return "PropertyValue{" + + "name='" + name + '\'' + + ", ref='" + ref + '\'' + + ", value='" + value + '\'' + + '}'; + } +} diff --git a/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/BeanFactory.java b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/BeanFactory.java new file mode 100644 index 0000000..29081c6 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/BeanFactory.java @@ -0,0 +1,14 @@ +package com.mashibing.framework.beans.factory; + +/** + * IOC容器的父接口 + * @author spikeCong + * @date 2022/10/28 + **/ +public interface BeanFactory { + + Object getBean(String name) throws Exception; + + //泛型方法 + T getBean(String name,Class clazz); +} diff --git a/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/support/BeanDefinitionReader.java b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/support/BeanDefinitionReader.java new file mode 100644 index 0000000..8c15487 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/support/BeanDefinitionReader.java @@ -0,0 +1,16 @@ +package com.mashibing.framework.beans.factory.support; + +/** + * 该接口定义了配置文件解析的规则 + * @author spikeCong + * @date 2022/10/28 + **/ +public interface BeanDefinitionReader { + + //获取注册表对象 + BeanDefinitionRegistry getRegistry(); + + //加载配置文件并在注册表中进行注册 + void loadBeanDefinitions(String configLocation)throws Exception; + +} diff --git a/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/support/BeanDefinitionRegistry.java b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/support/BeanDefinitionRegistry.java new file mode 100644 index 0000000..f311c55 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/support/BeanDefinitionRegistry.java @@ -0,0 +1,29 @@ +package com.mashibing.framework.beans.factory.support; + +import com.mashibing.framework.beans.BeanDefinition; + +/** + * 注册表对象 + * @author spikeCong + * @date 2022/10/28 + **/ +public interface BeanDefinitionRegistry { + + //注册BeanDefinition对象到注册表 + void registerBeanDefinition(String beanName, BeanDefinition beanDefinition); + + //从注册表删除指定名称的BeanDefinition对象 + void removeBeanDefinition(String beanName); + + //根据名称获取注册表中的对应的BeanDefinition + BeanDefinition getBeanDefinition(String beanName); + + //判断注册表中是否包含指定名称的BeanDefinition对象 + boolean containsBeanDefinition(String beanName); + + //获取注册表中所有的BeanDefinition的对象名称 + String[] getBeanDefinitionNames(); + + //获取注册表中BeanDefinition对象的个数 + int getBeanDefinitionCount(); +} diff --git a/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/support/SimpleBeanDefinitionRegistry.java b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/support/SimpleBeanDefinitionRegistry.java new file mode 100644 index 0000000..7333a8f --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/support/SimpleBeanDefinitionRegistry.java @@ -0,0 +1,46 @@ +package com.mashibing.framework.beans.factory.support; + +import com.mashibing.framework.beans.BeanDefinition; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author spikeCong + * @date 2022/10/28 + **/ +public class SimpleBeanDefinitionRegistry implements BeanDefinitionRegistry { + + //定义一个Map集合 存储BeanDefinition + private Map beanDefinitionMap = new HashMap<>(); + + @Override + public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) { + beanDefinitionMap.put(beanName,beanDefinition); + } + + @Override + public void removeBeanDefinition(String beanName) { + beanDefinitionMap.remove(beanName); + } + + @Override + public BeanDefinition getBeanDefinition(String beanName) { + return beanDefinitionMap.get(beanName); + } + + @Override + public boolean containsBeanDefinition(String beanName) { + return beanDefinitionMap.containsKey(beanName); + } + + @Override + public String[] getBeanDefinitionNames() { + return beanDefinitionMap.keySet().toArray(new String[0]); + } + + @Override + public int getBeanDefinitionCount() { + return beanDefinitionMap.size(); + } +} diff --git a/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/xml/XmlBeanDefinitionReader.java b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/xml/XmlBeanDefinitionReader.java new file mode 100644 index 0000000..7f82181 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/beans/factory/xml/XmlBeanDefinitionReader.java @@ -0,0 +1,99 @@ +package com.mashibing.framework.beans.factory.xml; + +import com.mashibing.framework.beans.BeanDefinition; +import com.mashibing.framework.beans.MutablePropertyValues; +import com.mashibing.framework.beans.PropertyValue; +import com.mashibing.framework.beans.factory.support.BeanDefinitionReader; +import com.mashibing.framework.beans.factory.support.BeanDefinitionRegistry; +import com.mashibing.framework.beans.factory.support.SimpleBeanDefinitionRegistry; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.InputStream; +import java.util.List; + +/** + * 该类是对XML文件进行解析的具体类 + * @author spikeCong + * @date 2022/10/28 + **/ +public class XmlBeanDefinitionReader implements BeanDefinitionReader { + + //声明注册表对象 + private BeanDefinitionRegistry registry; + + public XmlBeanDefinitionReader() { + registry = new SimpleBeanDefinitionRegistry(); + } + + @Override + public BeanDefinitionRegistry getRegistry() { + return registry; + } + + @Override + public void loadBeanDefinitions(String configLocation) throws Exception { + + //使用Dom4j解析XML + SAXReader reader = new SAXReader(); + + //获取指向配置文件的输入流 + InputStream is = XmlBeanDefinitionReader.class.getClassLoader().getResourceAsStream(configLocation); + + //文档对象 + Document document = reader.read(is); + + //获取根标签 + Element rootElement = document.getRootElement(); + + //解析bean标签 + parseBean(rootElement); + } + + private void parseBean(Element rootElement) { + + //获取所有的bean标签 + List elements = rootElement.elements(); + + //遍历每个bean标签 获取id className属性以及property子标签 + for (Element element : elements) { + String id = element.attributeValue("id"); + String className = element.attributeValue("class"); + + //封装数据到BeanDefinition + BeanDefinition beanDefinition = new BeanDefinition(); + beanDefinition.setId(id); + beanDefinition.setClassName(className); + + //获取property + List elementList = element.elements("property"); + + MutablePropertyValues mutablePropertyValues = new MutablePropertyValues(); + + for (Element property : elementList) { + String name = property.attributeValue("name"); + String ref = property.attributeValue("ref"); + String value = property.attributeValue("value"); + PropertyValue propertyValue = new PropertyValue(name, ref, value); + + mutablePropertyValues.addPropertyValue(propertyValue); + } + + //将mutablePropertyValues封装到 BeanDefinition + beanDefinition.setPropertyValues(mutablePropertyValues); + + System.out.println(beanDefinition); + + //将beanDefinition注册到注册表 + registry.registerBeanDefinition(id,beanDefinition); + } + } + + public static void main(String[] args) throws Exception { + + XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(); + + reader.loadBeanDefinitions("bean.xml"); + } +} diff --git a/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/context/ApplicationContext.java b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/context/ApplicationContext.java new file mode 100644 index 0000000..70047c0 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/context/ApplicationContext.java @@ -0,0 +1,14 @@ +package com.mashibing.framework.context; + +import com.mashibing.framework.beans.factory.BeanFactory; + +/** + * 定义非延时加载功能 + * @author spikeCong + * @date 2022/10/28 + **/ +public interface ApplicationContext extends BeanFactory { + + //进行配置文件的加载,并进行对象的创建 + void refresh(); +} diff --git a/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/context/support/AbstractApplicationContext.java b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/context/support/AbstractApplicationContext.java new file mode 100644 index 0000000..bbd5157 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/context/support/AbstractApplicationContext.java @@ -0,0 +1,53 @@ +package com.mashibing.framework.context.support; + +import com.mashibing.framework.beans.factory.support.BeanDefinitionReader; +import com.mashibing.framework.beans.factory.support.BeanDefinitionRegistry; +import com.mashibing.framework.context.ApplicationContext; + +import java.util.HashMap; +import java.util.Map; + +/** + * ApplicationContext接口的子类实现类 + * 创建容器对象时,加载配置文件,对bean进行初始化 + * @author spikeCong + * @date 2022/10/28 + **/ +public abstract class AbstractApplicationContext implements ApplicationContext { + + //声明解析器变量 + protected BeanDefinitionReader beanDefinitionReader; + + //定义一个存储bean对象的Map集合 + protected Map singletonObjects = new HashMap<>(); + + //声明一个配置文件路径变量 + protected String configLocation; + + @Override + public void refresh() { + + //加载BeanDefinition对象 + try { + beanDefinitionReader.loadBeanDefinitions(configLocation); + + //初始化 + finishInitialization(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + //bean初始化 + protected void finishInitialization() throws Exception { + //获取注册表对象 + BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry(); + + //获取BeanDefinition对象 + String[] beanDefinitionNames = registry.getBeanDefinitionNames(); + for (String definitionName : beanDefinitionNames) { + getBean(definitionName); + } + }; +} diff --git a/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/context/support/ClassPathXmlApplicationContext.java b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/context/support/ClassPathXmlApplicationContext.java new file mode 100644 index 0000000..0c14b05 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/context/support/ClassPathXmlApplicationContext.java @@ -0,0 +1,102 @@ +package com.mashibing.framework.context.support; + +import com.mashibing.framework.beans.BeanDefinition; +import com.mashibing.framework.beans.MutablePropertyValues; +import com.mashibing.framework.beans.PropertyValue; +import com.mashibing.framework.beans.factory.support.BeanDefinitionRegistry; +import com.mashibing.framework.beans.factory.xml.XmlBeanDefinitionReader; +import com.mashibing.framework.utils.SpringUtils; + +import java.lang.reflect.Method; + +/** + * IOC容器具体的子实现类,加载XML格式的配置文件 + * @author spikeCong + * @date 2022/10/28 + **/ +public class ClassPathXmlApplicationContext extends AbstractApplicationContext{ + + public ClassPathXmlApplicationContext(String configLocation) { + + this.configLocation = configLocation; + + //构建解析器对象,指定具体解析类为XML + this.beanDefinitionReader = new XmlBeanDefinitionReader(); + + this.refresh(); + } + + //根据bean的对象名称获取bean对象 + @Override + public Object getBean(String name) throws Exception { + + //判断对象容器中是否包含指定名称的bean对象,有就返回,没有则创建 + Object obj = singletonObjects.get(name); + if(obj != null){ + return obj; + } + + //自行创建,获取BeanDefinition对象 + BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry(); + BeanDefinition beanDefinition = registry.getBeanDefinition(name); + + //通过反射创建对象 + String className = beanDefinition.getClassName(); + Class clazz = Class.forName(className); + Object beanObj = clazz.newInstance(); //实例化 + + //需要进行依赖注入 + MutablePropertyValues propertyValues = beanDefinition.getPropertyValues(); + for (PropertyValue propertyValue : propertyValues) { + //获取property标签的属性 + String pName = propertyValue.getName(); + String pRef = propertyValue.getRef(); + String pValue = propertyValue.getValue(); + + if(pRef != null && !"".equals(pRef)){ + //获取依赖的bean对象 + Object bean = getBean(pRef); + //setCourseDao() set + courseDao + String methodName = SpringUtils.getSetterMethod(pName); + + //获取所有方法对象 + Method[] methods = clazz.getMethods(); + for (Method method : methods) { + if(methodName.equals(method.getName())){ + //执行该方法 + method.invoke(beanObj,bean); + } + } + } + + if(pValue != null && !"".equals(pValue)){ + String methodName = SpringUtils.getSetterMethod(pName); + //获取method + Method method = clazz.getMethod(methodName, String.class); + method.invoke(beanObj,pValue); + } + } + + //在返回beanObj之前,需要将对象存储到Map容器中 + this.singletonObjects.put(name,beanObj); + + return beanObj; + } + + @Override + public T getBean(String name, Class clazz) { + + Object bean = null; + + try { + bean = getBean(name); + if(bean == null){ + return null; + } + } catch (Exception e) { + e.printStackTrace(); + } + + return clazz.cast(bean); + } +} diff --git a/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/utils/SpringUtils.java b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/utils/SpringUtils.java new file mode 100644 index 0000000..dd00413 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/java/com/mashibing/framework/utils/SpringUtils.java @@ -0,0 +1,16 @@ +package com.mashibing.framework.utils; + +/** + * @author spikeCong + * @date 2022/10/28 + **/ +public class SpringUtils { + private SpringUtils() { + } + + public static String getSetterMethod(String fieldName){ + //fieldName = courseDao + String methodName = "set" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1); + return methodName; + } +} diff --git a/frame_design_pattern/user_defined_springioc/src/main/resources/bean.xml b/frame_design_pattern/user_defined_springioc/src/main/resources/bean.xml new file mode 100644 index 0000000..6c6bffe --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/src/main/resources/bean.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/user_defined_springioc/target/classes/bean.xml b/frame_design_pattern/user_defined_springioc/target/classes/bean.xml new file mode 100644 index 0000000..6c6bffe --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/target/classes/bean.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/BeanDefinition.class b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/BeanDefinition.class new file mode 100644 index 0000000..77c328e Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/BeanDefinition.class differ diff --git a/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/MutablePropertyValues.class b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/MutablePropertyValues.class new file mode 100644 index 0000000..b0be891 Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/MutablePropertyValues.class differ diff --git a/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/PropertyValue.class b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/PropertyValue.class new file mode 100644 index 0000000..f9cdabb Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/PropertyValue.class differ diff --git a/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/BeanFactory.class b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/BeanFactory.class new file mode 100644 index 0000000..24b2111 Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/BeanFactory.class differ diff --git a/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/support/BeanDefinitionReader.class b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/support/BeanDefinitionReader.class new file mode 100644 index 0000000..936df5e Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/support/BeanDefinitionReader.class differ diff --git a/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/support/BeanDefinitionRegistry.class b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/support/BeanDefinitionRegistry.class new file mode 100644 index 0000000..1ca5df5 Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/support/BeanDefinitionRegistry.class differ diff --git a/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/support/SimpleBeanDefinitionRegistry.class b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/support/SimpleBeanDefinitionRegistry.class new file mode 100644 index 0000000..c46ddb7 Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/support/SimpleBeanDefinitionRegistry.class differ diff --git a/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/xml/XmlBeanDefinitionReader.class b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/xml/XmlBeanDefinitionReader.class new file mode 100644 index 0000000..5c02a96 Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/beans/factory/xml/XmlBeanDefinitionReader.class differ diff --git a/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/context/ApplicationContext.class b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/context/ApplicationContext.class new file mode 100644 index 0000000..58fc1fe Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/context/ApplicationContext.class differ diff --git a/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/context/support/AbstractApplicationContext.class b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/context/support/AbstractApplicationContext.class new file mode 100644 index 0000000..5135941 Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/context/support/AbstractApplicationContext.class differ diff --git a/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/context/support/ClassPathXmlApplicationContext.class b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/context/support/ClassPathXmlApplicationContext.class new file mode 100644 index 0000000..fa7c75a Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/context/support/ClassPathXmlApplicationContext.class differ diff --git a/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/utils/SpringUtils.class b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/utils/SpringUtils.class new file mode 100644 index 0000000..64e1949 Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/classes/com/mashibing/framework/utils/SpringUtils.class differ diff --git a/frame_design_pattern/user_defined_springioc/target/maven-archiver/pom.properties b/frame_design_pattern/user_defined_springioc/target/maven-archiver/pom.properties new file mode 100644 index 0000000..c887424 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/target/maven-archiver/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Fri Oct 28 21:37:44 CST 2022 +groupId=com.mashibing +artifactId=user_defined_springioc +version=1.0-SNAPSHOT diff --git a/frame_design_pattern/user_defined_springioc/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/frame_design_pattern/user_defined_springioc/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..39c936b --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1,12 @@ +com\mashibing\framework\beans\PropertyValue.class +com\mashibing\framework\beans\factory\BeanFactory.class +com\mashibing\framework\beans\factory\support\BeanDefinitionReader.class +com\mashibing\framework\utils\SpringUtils.class +com\mashibing\framework\context\support\AbstractApplicationContext.class +com\mashibing\framework\beans\BeanDefinition.class +com\mashibing\framework\beans\factory\support\SimpleBeanDefinitionRegistry.class +com\mashibing\framework\context\ApplicationContext.class +com\mashibing\framework\beans\factory\support\BeanDefinitionRegistry.class +com\mashibing\framework\beans\MutablePropertyValues.class +com\mashibing\framework\context\support\ClassPathXmlApplicationContext.class +com\mashibing\framework\beans\factory\xml\XmlBeanDefinitionReader.class diff --git a/frame_design_pattern/user_defined_springioc/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/frame_design_pattern/user_defined_springioc/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..6e69ed5 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,12 @@ +I:\MSB\msb_work\frame_design_pattern\user_defined_springioc\src\main\java\com\mashibing\framework\beans\MutablePropertyValues.java +I:\MSB\msb_work\frame_design_pattern\user_defined_springioc\src\main\java\com\mashibing\framework\beans\factory\BeanFactory.java +I:\MSB\msb_work\frame_design_pattern\user_defined_springioc\src\main\java\com\mashibing\framework\beans\factory\support\BeanDefinitionRegistry.java +I:\MSB\msb_work\frame_design_pattern\user_defined_springioc\src\main\java\com\mashibing\framework\beans\factory\support\BeanDefinitionReader.java +I:\MSB\msb_work\frame_design_pattern\user_defined_springioc\src\main\java\com\mashibing\framework\beans\PropertyValue.java +I:\MSB\msb_work\frame_design_pattern\user_defined_springioc\src\main\java\com\mashibing\framework\beans\factory\support\SimpleBeanDefinitionRegistry.java +I:\MSB\msb_work\frame_design_pattern\user_defined_springioc\src\main\java\com\mashibing\framework\utils\SpringUtils.java +I:\MSB\msb_work\frame_design_pattern\user_defined_springioc\src\main\java\com\mashibing\framework\beans\factory\xml\XmlBeanDefinitionReader.java +I:\MSB\msb_work\frame_design_pattern\user_defined_springioc\src\main\java\com\mashibing\framework\context\support\AbstractApplicationContext.java +I:\MSB\msb_work\frame_design_pattern\user_defined_springioc\src\main\java\com\mashibing\framework\context\ApplicationContext.java +I:\MSB\msb_work\frame_design_pattern\user_defined_springioc\src\main\java\com\mashibing\framework\beans\BeanDefinition.java +I:\MSB\msb_work\frame_design_pattern\user_defined_springioc\src\main\java\com\mashibing\framework\context\support\ClassPathXmlApplicationContext.java diff --git a/frame_design_pattern/user_defined_springioc/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/frame_design_pattern/user_defined_springioc/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst new file mode 100644 index 0000000..e69de29 diff --git a/frame_design_pattern/user_defined_springioc/target/user_defined_springioc-1.0-SNAPSHOT.jar b/frame_design_pattern/user_defined_springioc/target/user_defined_springioc-1.0-SNAPSHOT.jar new file mode 100644 index 0000000..bab606b Binary files /dev/null and b/frame_design_pattern/user_defined_springioc/target/user_defined_springioc-1.0-SNAPSHOT.jar differ diff --git a/frame_design_pattern/user_defined_springioc/user_defined_springioc.iml b/frame_design_pattern/user_defined_springioc/user_defined_springioc.iml new file mode 100644 index 0000000..41cf363 --- /dev/null +++ b/frame_design_pattern/user_defined_springioc/user_defined_springioc.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-Iterator-pattern-17/msb-Iterator-pattern-17.iml b/msb-Iterator-pattern-17/msb-Iterator-pattern-17.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-Iterator-pattern-17/msb-Iterator-pattern-17.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-Iterator-pattern-17/pom.xml b/msb-Iterator-pattern-17/pom.xml new file mode 100644 index 0000000..89a8ece --- /dev/null +++ b/msb-Iterator-pattern-17/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.mashibing + msb-Iterator-pattern-17 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example01/ConcreteIterator.java b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example01/ConcreteIterator.java new file mode 100644 index 0000000..a1eb486 --- /dev/null +++ b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example01/ConcreteIterator.java @@ -0,0 +1,39 @@ +package com.mashibing.iterator.example01; + +import java.util.ArrayList; +import java.util.NoSuchElementException; + +/** + * 具体的迭代器 + * @author spikeCong + * @date 2022/10/18 + **/ +public class ConcreteIterator implements Iterator { + + private int cursor; //游标 + + private ArrayList arrayList; //容器 + + public ConcreteIterator(ArrayList arrayList) { + this.cursor = 0; + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + return cursor != arrayList.size(); + } + + @Override + public void next() { + cursor++; + } + + @Override + public E currentItem() { + if(cursor >= arrayList.size()){ + throw new NoSuchElementException(); + } + return arrayList.get(cursor); + } +} diff --git a/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example01/Iterator.java b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example01/Iterator.java new file mode 100644 index 0000000..ee915de --- /dev/null +++ b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example01/Iterator.java @@ -0,0 +1,18 @@ +package com.mashibing.iterator.example01; + +/** + * 迭代器接口 + * @author spikeCong + * @date 2022/10/18 + **/ +public interface Iterator { + + //判断集合中是否有下一个元素 + boolean hasNext(); + + //将有游标后移一位 + void next(); + + //返回当前游标指定的元素 + E currentItem(); +} diff --git a/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example01/Test01.java b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example01/Test01.java new file mode 100644 index 0000000..b9c5628 --- /dev/null +++ b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example01/Test01.java @@ -0,0 +1,29 @@ +package com.mashibing.iterator.example01; + +import java.util.ArrayList; + +/** + * @author spikeCong + * @date 2022/10/18 + **/ +public class Test01 { + + public static void main(String[] args) { + + ArrayList names = new ArrayList<>(); + names.add("lisi"); + names.add("zhangsan"); + names.add("wangwu"); + +// Iterator iterator = new ConcreteIterator<>(names); +// while(iterator.hasNext()){ +// System.out.println(iterator.currentItem()); +// iterator.next(); +// } + + java.util.Iterator iterator1 = names.iterator(); + while(iterator1.hasNext()){ + System.out.println(iterator1.next()); + } + } +} diff --git a/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/Client.java b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/Client.java new file mode 100644 index 0000000..d11ffc7 --- /dev/null +++ b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/Client.java @@ -0,0 +1,24 @@ +package com.mashibing.iterator.example02; + +/** + * @author spikeCong + * @date 2022/10/18 + **/ +public class Client { + + public static void main(String[] args) { + Topic[] topics = new Topic[4]; + topics[0] = new Topic("t1"); + topics[1] = new Topic("t2"); + topics[2] = new Topic("t3"); + topics[3] = new Topic("t4"); + + TopicList topicList = new TopicList(topics); + IteratorIterator iterator = topicList.iterator(); + + while(iterator.hasNext()){ + Topic topic = iterator.next(); + System.out.println(topic.getName()); + } + } +} diff --git a/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/IteratorIterator.java b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/IteratorIterator.java new file mode 100644 index 0000000..4604838 --- /dev/null +++ b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/IteratorIterator.java @@ -0,0 +1,17 @@ +package com.mashibing.iterator.example02; + +/** + * 抽象迭代器 + * @author spikeCong + * @date 2022/10/18 + **/ +public interface IteratorIterator { + + void reset(); //重置为第一个元素 + + E next(); //获取下一个元素 + + E currentItem(); //检索当前元素 + + boolean hasNext(); //判断集合中是否还有下一个元素 +} diff --git a/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/ListList.java b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/ListList.java new file mode 100644 index 0000000..1910841 --- /dev/null +++ b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/ListList.java @@ -0,0 +1,12 @@ +package com.mashibing.iterator.example02; + +/** + * 抽象集合 + * @author spikeCong + * @date 2022/10/18 + **/ +public interface ListList { + + //获取迭代器对象的抽象方法 + IteratorIterator iterator(); +} diff --git a/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/Topic.java b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/Topic.java new file mode 100644 index 0000000..80625dd --- /dev/null +++ b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/Topic.java @@ -0,0 +1,23 @@ +package com.mashibing.iterator.example02; + +/** + * 主题类 + * @author spikeCong + * @date 2022/10/18 + **/ +public class Topic { + + private String name; + + public Topic(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/TopicIterator.java b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/TopicIterator.java new file mode 100644 index 0000000..8a6cf31 --- /dev/null +++ b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/TopicIterator.java @@ -0,0 +1,43 @@ +package com.mashibing.iterator.example02; + +/** + * 具体迭代器 + * @author spikeCong + * @date 2022/10/18 + **/ +public class TopicIterator implements IteratorIterator { + + //Topic数组 + private Topic[] topics; + + //记录存储位置的有游标 + private int position; + + public TopicIterator(Topic[] topics) { + this.topics = topics; + position = 0; + } + + @Override + public void reset() { + position = 0; + } + + @Override + public Topic next() { + return topics[position++]; + } + + @Override + public Topic currentItem() { + return topics[position]; + } + + @Override + public boolean hasNext() { + if(position >= topics.length){ + return false; + } + return true; + } +} diff --git a/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/TopicList.java b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/TopicList.java new file mode 100644 index 0000000..e279675 --- /dev/null +++ b/msb-Iterator-pattern-17/src/main/java/com/mashibing/iterator/example02/TopicList.java @@ -0,0 +1,20 @@ +package com.mashibing.iterator.example02; + +/** + * 具体集合类 + * @author spikeCong + * @date 2022/10/18 + **/ +public class TopicList implements ListList{ + + private Topic[] topics; + + public TopicList(Topic[] topics) { + this.topics = topics; + } + + @Override + public IteratorIterator iterator() { + return new TopicIterator(topics); + } +} diff --git a/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example01/ConcreteIterator.class b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example01/ConcreteIterator.class new file mode 100644 index 0000000..d55d396 Binary files /dev/null and b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example01/ConcreteIterator.class differ diff --git a/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example01/Iterator.class b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example01/Iterator.class new file mode 100644 index 0000000..01cbaba Binary files /dev/null and b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example01/Iterator.class differ diff --git a/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example01/Test01.class b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example01/Test01.class new file mode 100644 index 0000000..a6b0bc1 Binary files /dev/null and b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example01/Test01.class differ diff --git a/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/Client.class b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/Client.class new file mode 100644 index 0000000..14be563 Binary files /dev/null and b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/Client.class differ diff --git a/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/IteratorIterator.class b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/IteratorIterator.class new file mode 100644 index 0000000..6864524 Binary files /dev/null and b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/IteratorIterator.class differ diff --git a/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/ListList.class b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/ListList.class new file mode 100644 index 0000000..930f02d Binary files /dev/null and b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/ListList.class differ diff --git a/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/Topic.class b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/Topic.class new file mode 100644 index 0000000..970a22d Binary files /dev/null and b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/Topic.class differ diff --git a/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/TopicIterator.class b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/TopicIterator.class new file mode 100644 index 0000000..79e2ed8 Binary files /dev/null and b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/TopicIterator.class differ diff --git a/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/TopicList.class b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/TopicList.class new file mode 100644 index 0000000..3a86cf1 Binary files /dev/null and b/msb-Iterator-pattern-17/target/classes/com/mashibing/iterator/example02/TopicList.class differ diff --git a/msb-adapter-pattern-08/msb-adapter-pattern-08.iml b/msb-adapter-pattern-08/msb-adapter-pattern-08.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-adapter-pattern-08/msb-adapter-pattern-08.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-adapter-pattern-08/pom.xml b/msb-adapter-pattern-08/pom.xml new file mode 100644 index 0000000..c842185 --- /dev/null +++ b/msb-adapter-pattern-08/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.mashibing + msb-adapter-pattern-08 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/Client.java b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/Client.java new file mode 100644 index 0000000..64b70d5 --- /dev/null +++ b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/Client.java @@ -0,0 +1,20 @@ +package com.mashibing.adapter.example01; + +/** + * @author spikeCong + * @date 2022/9/28 + **/ +public class Client { + + public static void main(String[] args) { + + Computer computer = new Computer(); + SDCard sdCard = new SDCardImpl(); + String read = computer.read(sdCard); + System.out.println(read); + + System.out.println("====================="); + SDAdapterTF adapterTF = new SDAdapterTF(); + System.out.println(computer.read(adapterTF)); + } +} diff --git a/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/Computer.java b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/Computer.java new file mode 100644 index 0000000..2f1d07e --- /dev/null +++ b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/Computer.java @@ -0,0 +1,15 @@ +package com.mashibing.adapter.example01; + +/** + * 电脑类 + * @author spikeCong + * @date 2022/9/28 + **/ +public class Computer { + + public String read(SDCard sdCard){ + + String data = sdCard.readSD(); + return data; + } +} diff --git a/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/SDAdapterTF.java b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/SDAdapterTF.java new file mode 100644 index 0000000..0365e5b --- /dev/null +++ b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/SDAdapterTF.java @@ -0,0 +1,22 @@ +package com.mashibing.adapter.example01; + +/** + * 适配器类(SD兼容TF) + * @author spikeCong + * @date 2022/9/28 + **/ +public class SDAdapterTF extends TFCardImpl implements SDCard{ + + + @Override + public String readSD() { + System.out.println("adapter read tf card"); + return readTF(); + } + + @Override + public void writeSD(String msg) { + System.out.println("adapter write tf card"); + writeTF(msg); + } +} diff --git a/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/SDCard.java b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/SDCard.java new file mode 100644 index 0000000..b4ada02 --- /dev/null +++ b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/SDCard.java @@ -0,0 +1,15 @@ +package com.mashibing.adapter.example01; + +/** + * SD卡接口 + * @author spikeCong + * @date 2022/9/28 + **/ +public interface SDCard { + + //读取SD卡 + String readSD(); + + //写入SD卡 + void writeSD(String msg); +} diff --git a/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/SDCardImpl.java b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/SDCardImpl.java new file mode 100644 index 0000000..9ce34b2 --- /dev/null +++ b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/SDCardImpl.java @@ -0,0 +1,20 @@ +package com.mashibing.adapter.example01; + +/** + * SD卡实现类 + * @author spikeCong + * @date 2022/9/28 + **/ +public class SDCardImpl implements SDCard { + + @Override + public String readSD() { + String msg = "sd card reading data"; + return msg; + } + + @Override + public void writeSD(String msg) { + System.out.println("sd card write data : " + msg); + } +} diff --git a/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/TFCard.java b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/TFCard.java new file mode 100644 index 0000000..0d773bd --- /dev/null +++ b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/TFCard.java @@ -0,0 +1,15 @@ +package com.mashibing.adapter.example01; + +/** + * TF卡接口 + * @author spikeCong + * @date 2022/9/28 + **/ +public interface TFCard { + + //读取TF卡 + String readTF(); + + //写入TF卡 + void writeTF(String msg); +} diff --git a/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/TFCardImpl.java b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/TFCardImpl.java new file mode 100644 index 0000000..6502583 --- /dev/null +++ b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example01/TFCardImpl.java @@ -0,0 +1,18 @@ +package com.mashibing.adapter.example01; + +/** + * @author spikeCong + * @date 2022/9/28 + **/ +public class TFCardImpl implements TFCard { + @Override + public String readTF() { + String msg = "tf card reading data"; + return msg; + } + + @Override + public void writeTF(String msg) { + System.out.println("tf card write data : " + msg); + } +} diff --git a/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example02/Client.java b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example02/Client.java new file mode 100644 index 0000000..0732561 --- /dev/null +++ b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example02/Client.java @@ -0,0 +1,23 @@ +package com.mashibing.adapter.example02; + +import com.mashibing.adapter.example01.*; + +/** + * @author spikeCong + * @date 2022/9/29 + **/ +public class Client { + + public static void main(String[] args) { + + Computer computer = new Computer(); + SDCard sdCard = new SDCardImpl(); + System.out.println(computer.read(sdCard)); + + System.out.println("======================="); + TFCard tfCard = new TFCardImpl(); + + SDAdapterTF2 adapterTF2 = new SDAdapterTF2(tfCard); + System.out.println(computer.read(adapterTF2)); + } +} diff --git a/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example02/SDAdapterTF2.java b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example02/SDAdapterTF2.java new file mode 100644 index 0000000..2520dde --- /dev/null +++ b/msb-adapter-pattern-08/src/main/java/com/mashibing/adapter/example02/SDAdapterTF2.java @@ -0,0 +1,30 @@ +package com.mashibing.adapter.example02; + +import com.mashibing.adapter.example01.SDCard; +import com.mashibing.adapter.example01.TFCard; + +/** + * 对象适配器-组合形式 + * @author spikeCong + * @date 2022/9/29 + **/ +public class SDAdapterTF2 implements SDCard { + + private TFCard tfCard; + + public SDAdapterTF2(TFCard tfCard) { + this.tfCard = tfCard; + } + + @Override + public String readSD() { + System.out.println("adapter read tf card"); + return tfCard.readTF(); + } + + @Override + public void writeSD(String msg) { + System.out.println("adapter write tf card"); + tfCard.writeTF(msg); + } +} diff --git a/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/Client.class b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/Client.class new file mode 100644 index 0000000..61cd0df Binary files /dev/null and b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/Client.class differ diff --git a/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/Computer.class b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/Computer.class new file mode 100644 index 0000000..30c6291 Binary files /dev/null and b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/Computer.class differ diff --git a/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/SDAdapterTF.class b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/SDAdapterTF.class new file mode 100644 index 0000000..aadbe4f Binary files /dev/null and b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/SDAdapterTF.class differ diff --git a/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/SDCard.class b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/SDCard.class new file mode 100644 index 0000000..4fee873 Binary files /dev/null and b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/SDCard.class differ diff --git a/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/SDCardImpl.class b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/SDCardImpl.class new file mode 100644 index 0000000..4470a11 Binary files /dev/null and b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/SDCardImpl.class differ diff --git a/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/TFCard.class b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/TFCard.class new file mode 100644 index 0000000..59864e9 Binary files /dev/null and b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/TFCard.class differ diff --git a/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/TFCardImpl.class b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/TFCardImpl.class new file mode 100644 index 0000000..eda5e9c Binary files /dev/null and b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example01/TFCardImpl.class differ diff --git a/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example02/Client.class b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example02/Client.class new file mode 100644 index 0000000..610a351 Binary files /dev/null and b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example02/Client.class differ diff --git a/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example02/SDAdapterTF2.class b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example02/SDAdapterTF2.class new file mode 100644 index 0000000..8016a43 Binary files /dev/null and b/msb-adapter-pattern-08/target/classes/com/mashibing/adapter/example02/SDAdapterTF2.class differ diff --git a/msb-bridge-pattern-06/msb-bridge-pattern-06.iml b/msb-bridge-pattern-06/msb-bridge-pattern-06.iml new file mode 100644 index 0000000..292a8c4 --- /dev/null +++ b/msb-bridge-pattern-06/msb-bridge-pattern-06.iml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-bridge-pattern-06/pom.xml b/msb-bridge-pattern-06/pom.xml new file mode 100644 index 0000000..ae0e62d --- /dev/null +++ b/msb-bridge-pattern-06/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + com.mashibing + msb-bridge-pattern-06 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + junit + junit + 4.12 + test + + + org.junit.jupiter + junit-jupiter + 5.4.2 + compile + + + \ No newline at end of file diff --git a/msb-bridge-pattern-06/src/main/java/com/mashibing/Test_Pay.java b/msb-bridge-pattern-06/src/main/java/com/mashibing/Test_Pay.java new file mode 100644 index 0000000..699a0ba --- /dev/null +++ b/msb-bridge-pattern-06/src/main/java/com/mashibing/Test_Pay.java @@ -0,0 +1,42 @@ +package com.mashibing; + +import com.mashibing.example01.PayController; +import com.mashibing.example02.*; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; + +/** + * @author spikeCong + * @date 2022/9/26 + **/ +public class Test_Pay { + + @Test + public void test01(){ + + PayController payController = new PayController(); + System.out.println("测试: 微信支付 --> 人脸支付方式"); + payController.doPay("wx_001","100010000",new BigDecimal(100),1,2); + + System.out.println(); + + System.out.println("测试: 支付宝支付 --> 指纹支付方式"); + payController.doPay("zfb_001","100010000",new BigDecimal(100),2,3); + + } + + @Test + public void test02(){ + + System.out.println("测试场景1: 微信支付,人脸方式"); + Pay wxpay = new WxPay(new PayFaceMode()); + wxpay.transfer("wx_0001001","100090009",new BigDecimal(100)); + + + System.out.println("测试场景2: 支付宝支付,指纹方式"); + Pay zfbPay = new ZfbPay(new PayFingerprintMode()); + wxpay.transfer("zfb_0001001","100090009",new BigDecimal(400)); + + } +} diff --git a/msb-bridge-pattern-06/src/main/java/com/mashibing/example01/PayController.java b/msb-bridge-pattern-06/src/main/java/com/mashibing/example01/PayController.java new file mode 100644 index 0000000..5f46a71 --- /dev/null +++ b/msb-bridge-pattern-06/src/main/java/com/mashibing/example01/PayController.java @@ -0,0 +1,53 @@ +package com.mashibing.example01; + +import java.math.BigDecimal; + +/** + * 支付接口 + * @author spikeCong + * @date 2022/9/26 + **/ +public class PayController { + + + + /** + * 支付功能 + * @param uId 用户id + * @param tradeId 交易id + * @param amount 交易金额 + * @param channelType 渠道类型 1 微信, 2 支付宝 + * @param modeType 支付类型 1 密码,2 人脸,3 指纹 + * @return: boolean + */ + public boolean doPay(String uId, String tradeId, BigDecimal amount,int channelType,int modeType){ + + //微信支付 + if(channelType == 1){ + System.out.println("微信渠道支付开始......"); + if(modeType == 1){ + System.out.println("密码支付"); + }else if(modeType == 2){ + System.out.println("人脸支付"); + }else if(modeType == 3){ + System.out.println("指纹支付"); + } + } + + //支付宝支付 + if(channelType == 2){ + System.out.println("支付宝渠道支付开始......"); + if(modeType == 1){ + System.out.println("密码支付"); + }else if(modeType == 2){ + System.out.println("人脸支付"); + }else if(modeType == 3){ + System.out.println("指纹支付"); + } + } + + return true; + } + + +} diff --git a/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/IPayMode.java b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/IPayMode.java new file mode 100644 index 0000000..3925bc9 --- /dev/null +++ b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/IPayMode.java @@ -0,0 +1,12 @@ +package com.mashibing.example02; + +/** + * 支付模式接口 + * @author spikeCong + * @date 2022/9/26 + **/ +public interface IPayMode { + + //安全校验功能: 对各种支付模式进行风控校验操作 + boolean security(String uId); +} diff --git a/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/Pay.java b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/Pay.java new file mode 100644 index 0000000..ea7ff72 --- /dev/null +++ b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/Pay.java @@ -0,0 +1,21 @@ +package com.mashibing.example02; + +import java.math.BigDecimal; + +/** + * 支付抽象类 + * @author spikeCong + * @date 2022/9/26 + **/ +public abstract class Pay { + + //桥接对象 + protected IPayMode payMode; + + public Pay(IPayMode payMode) { + this.payMode = payMode; + } + + //划账 + public abstract String transfer(String uId, String tradeId, BigDecimal amount); +} diff --git a/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/PayCypher.java b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/PayCypher.java new file mode 100644 index 0000000..b156d93 --- /dev/null +++ b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/PayCypher.java @@ -0,0 +1,15 @@ +package com.mashibing.example02; + +/** + * 密码支付 + * @author spikeCong + * @date 2022/9/26 + **/ +public class PayCypher implements IPayMode { + + @Override + public boolean security(String uId) { + System.out.println("密码支付,风控校验-环境安全"); + return true; + } +} diff --git a/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/PayFaceMode.java b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/PayFaceMode.java new file mode 100644 index 0000000..791aa89 --- /dev/null +++ b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/PayFaceMode.java @@ -0,0 +1,15 @@ +package com.mashibing.example02; + +/** + * 刷脸支付 + * @author spikeCong + * @date 2022/9/26 + **/ +public class PayFaceMode implements IPayMode { + + @Override + public boolean security(String uId) { + System.out.println("人脸支付,风控校验-->脸部识别"); + return true; + } +} diff --git a/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/PayFingerprintMode.java b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/PayFingerprintMode.java new file mode 100644 index 0000000..fe6f361 --- /dev/null +++ b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/PayFingerprintMode.java @@ -0,0 +1,15 @@ +package com.mashibing.example02; + +/** + * 指纹支付 + * @author spikeCong + * @date 2022/9/26 + **/ +public class PayFingerprintMode implements IPayMode{ + + @Override + public boolean security(String uId) { + System.out.println("指纹支付,风控校验->指纹信息"); + return true; + } +} diff --git a/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/WxPay.java b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/WxPay.java new file mode 100644 index 0000000..65fe933 --- /dev/null +++ b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/WxPay.java @@ -0,0 +1,32 @@ +package com.mashibing.example02; + +import java.math.BigDecimal; + +/** + * 支付渠道-微信支付 + * @author spikeCong + * @date 2022/9/26 + **/ +public class WxPay extends Pay{ + + + public WxPay(IPayMode payMode) { + super(payMode); + } + + @Override + public String transfer(String uId, String tradeId, BigDecimal amount) { + + System.out.println("微信渠道支付划账开始......"); + boolean security = payMode.security(uId); + System.out.println("微信渠道支付风险校验: " + uId + "," + tradeId +"," + security); + + if(!security){ + System.out.println("微信渠道支付划账失败! ! "); + return "500"; + } + + System.out.println("微信渠道划账成功! 金额: " + amount); + return "200"; + } +} diff --git a/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/ZfbPay.java b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/ZfbPay.java new file mode 100644 index 0000000..8fa311c --- /dev/null +++ b/msb-bridge-pattern-06/src/main/java/com/mashibing/example02/ZfbPay.java @@ -0,0 +1,30 @@ +package com.mashibing.example02; + +import java.math.BigDecimal; + +/** + * @author spikeCong + * @date 2022/9/26 + **/ +public class ZfbPay extends Pay { + + public ZfbPay(IPayMode payMode) { + super(payMode); + } + + @Override + public String transfer(String uId, String tradeId, BigDecimal amount) { + + System.out.println("支付宝渠道支付划账开始......"); + boolean security = payMode.security(uId); + System.out.println("支付宝渠道支付风险校验: " + uId + "," + tradeId +"," + security); + + if(!security){ + System.out.println("支付宝渠道支付划账失败! ! "); + return "500"; + } + + System.out.println("支付宝渠道划账成功! 金额: " + amount); + return "200"; + } +} diff --git a/msb-bridge-pattern-06/target/classes/com/mashibing/Test_Pay.class b/msb-bridge-pattern-06/target/classes/com/mashibing/Test_Pay.class new file mode 100644 index 0000000..ea9e142 Binary files /dev/null and b/msb-bridge-pattern-06/target/classes/com/mashibing/Test_Pay.class differ diff --git a/msb-bridge-pattern-06/target/classes/com/mashibing/example01/PayController.class b/msb-bridge-pattern-06/target/classes/com/mashibing/example01/PayController.class new file mode 100644 index 0000000..cdf4cc0 Binary files /dev/null and b/msb-bridge-pattern-06/target/classes/com/mashibing/example01/PayController.class differ diff --git a/msb-bridge-pattern-06/target/classes/com/mashibing/example02/IPayMode.class b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/IPayMode.class new file mode 100644 index 0000000..f8aca79 Binary files /dev/null and b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/IPayMode.class differ diff --git a/msb-bridge-pattern-06/target/classes/com/mashibing/example02/Pay.class b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/Pay.class new file mode 100644 index 0000000..b2d6758 Binary files /dev/null and b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/Pay.class differ diff --git a/msb-bridge-pattern-06/target/classes/com/mashibing/example02/PayCypher.class b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/PayCypher.class new file mode 100644 index 0000000..ef3ea8d Binary files /dev/null and b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/PayCypher.class differ diff --git a/msb-bridge-pattern-06/target/classes/com/mashibing/example02/PayFaceMode.class b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/PayFaceMode.class new file mode 100644 index 0000000..ec68ebe Binary files /dev/null and b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/PayFaceMode.class differ diff --git a/msb-bridge-pattern-06/target/classes/com/mashibing/example02/PayFingerprintMode.class b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/PayFingerprintMode.class new file mode 100644 index 0000000..4b3114c Binary files /dev/null and b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/PayFingerprintMode.class differ diff --git a/msb-bridge-pattern-06/target/classes/com/mashibing/example02/WxPay.class b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/WxPay.class new file mode 100644 index 0000000..57d69dc Binary files /dev/null and b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/WxPay.class differ diff --git a/msb-bridge-pattern-06/target/classes/com/mashibing/example02/ZfbPay.class b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/ZfbPay.class new file mode 100644 index 0000000..d0ed14c Binary files /dev/null and b/msb-bridge-pattern-06/target/classes/com/mashibing/example02/ZfbPay.class differ diff --git a/msb-builder-pattern-03/msb-builder-pattern-03.iml b/msb-builder-pattern-03/msb-builder-pattern-03.iml new file mode 100644 index 0000000..2404802 --- /dev/null +++ b/msb-builder-pattern-03/msb-builder-pattern-03.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-builder-pattern-03/pom.xml b/msb-builder-pattern-03/pom.xml new file mode 100644 index 0000000..f66ed8a --- /dev/null +++ b/msb-builder-pattern-03/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + com.mashibing + msb-builder-pattern-03 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + \ No newline at end of file diff --git a/msb-builder-pattern-03/src/main/java/com/mashibing/example01/Client.java b/msb-builder-pattern-03/src/main/java/com/mashibing/example01/Client.java new file mode 100644 index 0000000..643f23f --- /dev/null +++ b/msb-builder-pattern-03/src/main/java/com/mashibing/example01/Client.java @@ -0,0 +1,24 @@ +package com.mashibing.example01; + +import com.mashibing.example01.builder.MobikeBuilder; +import com.mashibing.example01.director.Director; +import com.mashibing.example01.product.Bike; + +/** + * 客户端 + * @author spikeCong + * @date 2022/9/19 + **/ +public class Client { + + public static void main(String[] args) { + + //1.创建指挥者 + Director director = new Director(new MobikeBuilder()); + + //2.获取自行车 + Bike bike = director.construct(); + System.out.println(bike.getFrame() + "," + bike.getSeat()); + } + +} diff --git a/msb-builder-pattern-03/src/main/java/com/mashibing/example01/builder/Builder.java b/msb-builder-pattern-03/src/main/java/com/mashibing/example01/builder/Builder.java new file mode 100644 index 0000000..656de6a --- /dev/null +++ b/msb-builder-pattern-03/src/main/java/com/mashibing/example01/builder/Builder.java @@ -0,0 +1,19 @@ +package com.mashibing.example01.builder; + +import com.mashibing.example01.product.Bike; + +/** + * 抽象建造者类 + * @author spikeCong + * @date 2022/9/19 + **/ +public abstract class Builder { + + protected Bike mBike = new Bike(); + + public abstract void buildFrame(); + + public abstract void buildSeat(); + + public abstract Bike createBike(); +} diff --git a/msb-builder-pattern-03/src/main/java/com/mashibing/example01/builder/HelloBikeBuilder.java b/msb-builder-pattern-03/src/main/java/com/mashibing/example01/builder/HelloBikeBuilder.java new file mode 100644 index 0000000..7e9e952 --- /dev/null +++ b/msb-builder-pattern-03/src/main/java/com/mashibing/example01/builder/HelloBikeBuilder.java @@ -0,0 +1,28 @@ +package com.mashibing.example01.builder; + +import com.mashibing.example01.product.Bike; + +/** + * 哈罗单车建造者 + * @author spikeCong + * @date 2022/9/19 + **/ +public class HelloBikeBuilder extends Builder{ + + @Override + public void buildFrame() { + System.out.println("制作碳纤维车架"); + mBike.setFrame("碳纤维车架"); + } + + @Override + public void buildSeat() { + System.out.println("制作橡胶车座"); + mBike.setFrame("橡胶车座"); + } + + @Override + public Bike createBike() { + return mBike; + } +} diff --git a/msb-builder-pattern-03/src/main/java/com/mashibing/example01/builder/MobikeBuilder.java b/msb-builder-pattern-03/src/main/java/com/mashibing/example01/builder/MobikeBuilder.java new file mode 100644 index 0000000..f2022ff --- /dev/null +++ b/msb-builder-pattern-03/src/main/java/com/mashibing/example01/builder/MobikeBuilder.java @@ -0,0 +1,28 @@ +package com.mashibing.example01.builder; + +import com.mashibing.example01.product.Bike; + +/** + * 摩拜单车建造者 + * @author spikeCong + * @date 2022/9/19 + **/ +public class MobikeBuilder extends Builder { + + @Override + public void buildFrame() { + System.out.println("制作车架!"); + mBike.setFrame("铝合金车架"); + } + + @Override + public void buildSeat() { + System.out.println("制作车座"); + mBike.setSeat("真皮车座"); + } + + @Override + public Bike createBike() { + return mBike; + } +} diff --git a/msb-builder-pattern-03/src/main/java/com/mashibing/example01/director/Director.java b/msb-builder-pattern-03/src/main/java/com/mashibing/example01/director/Director.java new file mode 100644 index 0000000..ab11b26 --- /dev/null +++ b/msb-builder-pattern-03/src/main/java/com/mashibing/example01/director/Director.java @@ -0,0 +1,26 @@ +package com.mashibing.example01.director; + +import com.mashibing.example01.builder.Builder; +import com.mashibing.example01.product.Bike; + +/** + * 指挥者类 + * @author spikeCong + * @date 2022/9/19 + **/ +public class Director { + + private Builder mBuilder; + + public Director(Builder mBuilder) { + this.mBuilder = mBuilder; + } + + + //自行车制作方法 + public Bike construct(){ + mBuilder.buildFrame(); + mBuilder.buildSeat(); + return mBuilder.createBike(); + } +} diff --git a/msb-builder-pattern-03/src/main/java/com/mashibing/example01/product/Bike.java b/msb-builder-pattern-03/src/main/java/com/mashibing/example01/product/Bike.java new file mode 100644 index 0000000..a51643b --- /dev/null +++ b/msb-builder-pattern-03/src/main/java/com/mashibing/example01/product/Bike.java @@ -0,0 +1,29 @@ +package com.mashibing.example01.product; + +/** + * 自行车类 + * @author spikeCong + * @date 2022/9/19 + **/ +public class Bike { + + private String frame; //车架 + + private String seat; //车座 + + public String getFrame() { + return frame; + } + + public void setFrame(String frame) { + this.frame = frame; + } + + public String getSeat() { + return seat; + } + + public void setSeat(String seat) { + this.seat = seat; + } +} diff --git a/msb-builder-pattern-03/src/main/java/com/mashibing/example02/App.java b/msb-builder-pattern-03/src/main/java/com/mashibing/example02/App.java new file mode 100644 index 0000000..26e7a49 --- /dev/null +++ b/msb-builder-pattern-03/src/main/java/com/mashibing/example02/App.java @@ -0,0 +1,17 @@ +package com.mashibing.example02; + +/** + * @author spikeCong + * @date 2022/9/19 + **/ +public class App { + + public static void main(String[] args) { + + //获取连接对象 + RabbitMQClient3 instance = new RabbitMQClient3.Builder().setHost("192.168.52.123"). + setMode(1).setPort(5672).setQueue("test").build(); + + instance.sendMessage("test"); + } +} diff --git a/msb-builder-pattern-03/src/main/java/com/mashibing/example02/RabbitMQClient1.java b/msb-builder-pattern-03/src/main/java/com/mashibing/example02/RabbitMQClient1.java new file mode 100644 index 0000000..8824dd4 --- /dev/null +++ b/msb-builder-pattern-03/src/main/java/com/mashibing/example02/RabbitMQClient1.java @@ -0,0 +1,67 @@ +package com.mashibing.example02; + +/** + * MQ连接客户端 + * @author spikeCong + * @date 2022/9/19 + **/ +public class RabbitMQClient1 { + + private String host = "127.0.0.1"; + + private int port = 5672; + + private int mode; + + private String exchange; + + private String queue; + + private boolean isDurable = true; + + int connectionTimeout = 1000; + + public RabbitMQClient1(String host, int port, int mode, String exchange, String queue, boolean isDurable, int connectionTimeout) { + this.host = host; + this.port = port; + this.mode = mode; + this.exchange = exchange; + this.queue = queue; + this.isDurable = isDurable; + this.connectionTimeout = connectionTimeout; + + if(mode == 1){ //工作队列模式不需要设计交换机,但是队列名称一定要有 + if(exchange != null){ + throw new RuntimeException("工作队列模式无需设计交换机"); + } + if(queue == null || queue.trim().equals("")){ + throw new RuntimeException("工作队列模式名称不能为空"); + } + if(isDurable == false){ + throw new RuntimeException("工作队列模式必须开启持久化"); + } + }else if(mode == 2){ //路由模式必须设计交换机,但是不能设计队列 + if(exchange == null){ + throw new RuntimeException("路由模式下必须设置交换机"); + } + if(queue != null){ + throw new RuntimeException("路由模式无须设计队列名称"); + } + } + + //其他验证方式, + } + + public void sendMessage(String msg){ + + System.out.println("发送消息......"); + } + + public static void main(String[] args) { + //每一种模式,都需要根据不同的情况进行实例化,构造方法会变得过于复杂. + RabbitMQClient1 client1 = new RabbitMQClient1("192.168.52.123",5672, + 2,"sample-exchange",null,true,5000); + + client1.sendMessage("Test-MSG"); + } +} diff --git a/msb-builder-pattern-03/src/main/java/com/mashibing/example02/RabbitMQClient2.java b/msb-builder-pattern-03/src/main/java/com/mashibing/example02/RabbitMQClient2.java new file mode 100644 index 0000000..0127af8 --- /dev/null +++ b/msb-builder-pattern-03/src/main/java/com/mashibing/example02/RabbitMQClient2.java @@ -0,0 +1,147 @@ +package com.mashibing.example02; + +/** + * MQ连接客户端 + * @author spikeCong + * @date 2022/9/19 + **/ +public class RabbitMQClient2 { + + private String host = "127.0.0.1"; + + private int port = 5672; + + private int mode; + + private String exchange; + + private String queue; + + private boolean isDurable = true; + + int connectionTimeout = 1000; + + //私有化构造方法 + private RabbitMQClient2() { + + } + + public String getExchange() { + return exchange; + } + + public void setExchange(String exchange) { + + if(mode == 1){ //工作队列模式不需要设计交换机,但是队列名称一定要有 + if(exchange != null){ + throw new RuntimeException("工作队列模式无需设计交换机"); + } + if(queue == null || queue.trim().equals("")){ + throw new RuntimeException("工作队列模式名称不能为空"); + } + if(isDurable == false){ + throw new RuntimeException("工作队列模式必须开启持久化"); + } + }else if(mode == 2){ //路由模式必须设计交换机,但是不能设计队列 + if(exchange == null){ + throw new RuntimeException("路由模式下必须设置交换机"); + } + if(queue != null){ + throw new RuntimeException("路由模式无须设计队列名称"); + } + } + + //其他验证方式, + + this.exchange = exchange; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public int getMode() { + return mode; + } + + public void setMode(int mode) { + + if(mode == 1){ //工作队列模式不需要设计交换机,但是队列名称一定要有 + if(exchange != null){ + throw new RuntimeException("工作队列模式无需设计交换机"); + } + if(queue == null || queue.trim().equals("")){ + throw new RuntimeException("工作队列模式名称不能为空"); + } + if(isDurable == false){ + throw new RuntimeException("工作队列模式必须开启持久化"); + } + }else if(mode == 2){ //路由模式必须设计交换机,但是不能设计队列 + if(exchange == null){ + throw new RuntimeException("路由模式下必须设置交换机"); + } + if(queue != null){ + throw new RuntimeException("路由模式无须设计队列名称"); + } + } + + this.mode = mode; + } + + public String getQueue() { + return queue; + } + + public void setQueue(String queue) { + this.queue = queue; + } + + public boolean isDurable() { + return isDurable; + } + + public void setDurable(boolean durable) { + isDurable = durable; + } + + public int getConnectionTimeout() { + return connectionTimeout; + } + + public void setConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + } + + public void sendMessage(String msg){ + + System.out.println("发送消息......"); + } + + /** + * set方法的好处是参数的设计更加的灵活,但是通过set方式设置对象属性时,对象有可能存在中间状态(无效状态), + * 并且进行属性校验时有前后顺序约束. + * 破坏了不可变对象的密封性. + * 怎么保证灵活设置参数又不会存在中间状态呢? 答案就是: 使用建造者模式 + */ + public static void main(String[] args) { + + RabbitMQClient2 client2 = new RabbitMQClient2(); + client2.setHost("192.168.52.123"); + client2.setMode(1); + client2.setQueue("queue"); + client2.setDurable(true); + client2.sendMessage("Test-MSG2"); + } +} diff --git a/msb-builder-pattern-03/src/main/java/com/mashibing/example02/RabbitMQClient3.java b/msb-builder-pattern-03/src/main/java/com/mashibing/example02/RabbitMQClient3.java new file mode 100644 index 0000000..a7b3a7d --- /dev/null +++ b/msb-builder-pattern-03/src/main/java/com/mashibing/example02/RabbitMQClient3.java @@ -0,0 +1,98 @@ +package com.mashibing.example02; + +/** + * 建造者模式 + * 1.目标类的构造方法要传入一个Builder对象 + * 2.builder类位于目标类的内部,并且使用static修饰 + * 3.builder类对象提供内置各种set方法,注意: set方法的返回值是builder本身 + * 4.builder类提供一个build() 方法,实现目标对象的创建 + * @author spikeCong + * @date 2022/9/19 + **/ +public class RabbitMQClient3 { + + //私有构造,目标类的构造方法要传入一个Builder对象 + private RabbitMQClient3(Builder builder){ + + } + + //builder类位于目标类的内部,并且使用static修饰 + public static class Builder{ + + //保证不可变对象的属性密闭性 + private String host = "127.0.0.1"; + private int port = 5672; + private int mode; + private String exchange; + private String queue; + private boolean isDurable = true; + int connectionTimeout = 1000; + + //builder类对象提供内置各种set方法,注意: set方法的返回值是builder本身 + public Builder setHost(String host) { + this.host = host; + return this; + } + + public Builder setPort(int port) { + this.port = port; + return this; + } + + public Builder setMode(int mode) { + this.mode = mode; + return this; + } + + public Builder setExchange(String exchange) { + this.exchange = exchange; + return this; + } + + public Builder setQueue(String queue) { + this.queue = queue; + return this; + } + + public Builder setDurable(boolean durable) { + isDurable = durable; + return this; + } + + public Builder setConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + return this; + } + + //builder类提供一个build() 方法,实现目标对象的创建 + public RabbitMQClient3 build(){ + + if(mode == 1){ //工作队列模式不需要设计交换机,但是队列名称一定要有 + if(exchange != null){ + throw new RuntimeException("工作队列模式无需设计交换机"); + } + if(queue == null || queue.trim().equals("")){ + throw new RuntimeException("工作队列模式名称不能为空"); + } + if(isDurable == false){ + throw new RuntimeException("工作队列模式必须开启持久化"); + } + }else if(mode == 2){ //路由模式必须设计交换机,但是不能设计队列 + if(exchange == null){ + throw new RuntimeException("路由模式下必须设置交换机"); + } + if(queue != null){ + throw new RuntimeException("路由模式无须设计队列名称"); + } + } + + return new RabbitMQClient3(this); + } + } + + public void sendMessage(String msg){ + + System.out.println("发送消息......"); + } + +} diff --git a/msb-builder-pattern-03/target/classes/com/mashibing/example01/Client.class b/msb-builder-pattern-03/target/classes/com/mashibing/example01/Client.class new file mode 100644 index 0000000..2f06c46 Binary files /dev/null and b/msb-builder-pattern-03/target/classes/com/mashibing/example01/Client.class differ diff --git a/msb-builder-pattern-03/target/classes/com/mashibing/example01/builder/Builder.class b/msb-builder-pattern-03/target/classes/com/mashibing/example01/builder/Builder.class new file mode 100644 index 0000000..762f210 Binary files /dev/null and b/msb-builder-pattern-03/target/classes/com/mashibing/example01/builder/Builder.class differ diff --git a/msb-builder-pattern-03/target/classes/com/mashibing/example01/builder/HelloBikeBuilder.class b/msb-builder-pattern-03/target/classes/com/mashibing/example01/builder/HelloBikeBuilder.class new file mode 100644 index 0000000..b168346 Binary files /dev/null and b/msb-builder-pattern-03/target/classes/com/mashibing/example01/builder/HelloBikeBuilder.class differ diff --git a/msb-builder-pattern-03/target/classes/com/mashibing/example01/builder/MobikeBuilder.class b/msb-builder-pattern-03/target/classes/com/mashibing/example01/builder/MobikeBuilder.class new file mode 100644 index 0000000..6fafe0e Binary files /dev/null and b/msb-builder-pattern-03/target/classes/com/mashibing/example01/builder/MobikeBuilder.class differ diff --git a/msb-builder-pattern-03/target/classes/com/mashibing/example01/director/Director.class b/msb-builder-pattern-03/target/classes/com/mashibing/example01/director/Director.class new file mode 100644 index 0000000..c51841f Binary files /dev/null and b/msb-builder-pattern-03/target/classes/com/mashibing/example01/director/Director.class differ diff --git a/msb-builder-pattern-03/target/classes/com/mashibing/example01/product/Bike.class b/msb-builder-pattern-03/target/classes/com/mashibing/example01/product/Bike.class new file mode 100644 index 0000000..022e5d8 Binary files /dev/null and b/msb-builder-pattern-03/target/classes/com/mashibing/example01/product/Bike.class differ diff --git a/msb-builder-pattern-03/target/classes/com/mashibing/example02/App.class b/msb-builder-pattern-03/target/classes/com/mashibing/example02/App.class new file mode 100644 index 0000000..d90a346 Binary files /dev/null and b/msb-builder-pattern-03/target/classes/com/mashibing/example02/App.class differ diff --git a/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient1.class b/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient1.class new file mode 100644 index 0000000..d45f818 Binary files /dev/null and b/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient1.class differ diff --git a/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient2.class b/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient2.class new file mode 100644 index 0000000..eef60ac Binary files /dev/null and b/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient2.class differ diff --git a/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient3$1.class b/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient3$1.class new file mode 100644 index 0000000..fec0cd5 Binary files /dev/null and b/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient3$1.class differ diff --git a/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient3$Builder.class b/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient3$Builder.class new file mode 100644 index 0000000..77bd983 Binary files /dev/null and b/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient3$Builder.class differ diff --git a/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient3.class b/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient3.class new file mode 100644 index 0000000..0eb4623 Binary files /dev/null and b/msb-builder-pattern-03/target/classes/com/mashibing/example02/RabbitMQClient3.class differ diff --git a/msb-chain of responsibility-pattern-15/msb-chain of responsibility-pattern-15.iml b/msb-chain of responsibility-pattern-15/msb-chain of responsibility-pattern-15.iml new file mode 100644 index 0000000..e13fd2e --- /dev/null +++ b/msb-chain of responsibility-pattern-15/msb-chain of responsibility-pattern-15.iml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-chain of responsibility-pattern-15/pom.xml b/msb-chain of responsibility-pattern-15/pom.xml new file mode 100644 index 0000000..7859122 --- /dev/null +++ b/msb-chain of responsibility-pattern-15/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + com.mashibing + msb-chain of responsibility-pattern-15 + 1.0-SNAPSHOT + + + + + com.alibaba + fastjson + 1.2.62 + + + junit + junit + 4.12 + test + + + + org.slf4j + slf4j-api + 1.7.5 + + + org.slf4j + jcl-over-slf4j + 1.7.5 + + + ch.qos.logback + logback-classic + 1.0.9 + + + slf4j-api + org.slf4j + + + + + org.junit.jupiter + junit-jupiter + RELEASE + compile + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/Client.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/Client.java new file mode 100644 index 0000000..af8b77a --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/Client.java @@ -0,0 +1,22 @@ +package com.mashibing.chain.example01; + +/** + * @author spikeCong + * @date 2022/10/14 + **/ +public class Client { + + public static void main(String[] args) { + Handler h1 = new HandlerA(); + Handler h2 = new HandlerB(); + Handler h3 = new HandlerC(); + + //创建处理链 + h1.setSuccessor(h2); + h2.setSuccessor(h3); + + RequestData requestData = new RequestData("请求数据: ABCD"); + //调用处理链头部的方法 + h1.handle(requestData); + } +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/Handler.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/Handler.java new file mode 100644 index 0000000..1d2ed0b --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/Handler.java @@ -0,0 +1,18 @@ +package com.mashibing.chain.example01; + +/** + * 抽象的处理者类 + * @author spikeCong + * @date 2022/10/14 + **/ +public abstract class Handler { + + //后继处理者的引用 + protected Handler successor; + + public void setSuccessor(Handler successor) { + this.successor = successor; + } + + public abstract void handle(RequestData requestData); +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/HandlerA.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/HandlerA.java new file mode 100644 index 0000000..72cbc68 --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/HandlerA.java @@ -0,0 +1,21 @@ +package com.mashibing.chain.example01; + +/** + * @author spikeCong + * @date 2022/10/14 + **/ +public class HandlerA extends Handler { + + @Override + public void handle(RequestData requestData) { + + System.out.println("HandlerA 执行代码逻辑! 处理: " + requestData.getData()); + requestData.setData(requestData.getData().replace("A","")); + //判断时候继续向后调用处理器 + if(successor != null){ + successor.handle(requestData); + }else{ + System.out.println("执行中止"); + } + } +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/HandlerB.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/HandlerB.java new file mode 100644 index 0000000..5471424 --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/HandlerB.java @@ -0,0 +1,21 @@ +package com.mashibing.chain.example01; + +/** + * @author spikeCong + * @date 2022/10/14 + **/ +public class HandlerB extends Handler { + + @Override + public void handle(RequestData requestData) { + + System.out.println("HandlerB 执行代码逻辑! 处理: " + requestData.getData()); + requestData.setData(requestData.getData().replace("B","")); + //判断时候继续向后调用处理器 + if(successor != null){ + successor.handle(requestData); + }else{ + System.out.println("执行中止"); + } + } +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/HandlerC.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/HandlerC.java new file mode 100644 index 0000000..e0bd78f --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/HandlerC.java @@ -0,0 +1,21 @@ +package com.mashibing.chain.example01; + +/** + * @author spikeCong + * @date 2022/10/14 + **/ +public class HandlerC extends Handler { + + @Override + public void handle(RequestData requestData) { + + System.out.println("HandlerC 执行代码逻辑! 处理: " + requestData.getData()); + requestData.setData(requestData.getData().replace("C","")); + //判断时候继续向后调用处理器 + if(successor != null){ + successor.handle(requestData); + }else{ + System.out.println("执行中止"); + } + } +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/RequestData.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/RequestData.java new file mode 100644 index 0000000..21eaac4 --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example01/RequestData.java @@ -0,0 +1,23 @@ +package com.mashibing.chain.example01; + +/** + * 请求封装 + * @author spikeCong + * @date 2022/10/14 + **/ +public class RequestData { + + private String data; + + public RequestData(String data) { + this.data = data; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example02/AuthController.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example02/AuthController.java new file mode 100644 index 0000000..5418482 --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example02/AuthController.java @@ -0,0 +1,68 @@ +package com.mashibing.chain.example02; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * 审核申请接口 + * @author spikeCong + * @date 2022/10/16 + **/ +public class AuthController { + + /** + * 审核方法 + * @param name 申请人姓名 + * @param orderId 申请单ID + * @param authDate 申请时间 + * @return: AuthInfo + */ + public AuthInfo doAuth(String name, String orderId, Date authDate) throws ParseException { + + //三级审批 + Date date = null; + + //查询是否存在审核信息,虚拟三级审核人ID: 1000013 + date = AuthService.queryAuthInfo("1000013", orderId); + if(date == null){ + return new AuthInfo("0001","单号: " + orderId , + "状态: 等待三级审批负责人进行审批"); + } + + + /** + * 二级审批 + * 查询是否存在审核信息,虚拟二级审核人ID: 1000012 + * 二级审核人审核申请单的时间范围为: 11-01 ~ 11-10 + */ + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + if(authDate.after(sdf.parse("2022-10-31 00:00:00")) && authDate.before(sdf.parse("2022-11-11 00:00:00")) ){ + //条件成立,查询二级审核信息 + date = AuthService.queryAuthInfo("1000012",orderId); + + if(date == null){ + return new AuthInfo("0001","单号: " + orderId , + "状态: 等待二级审批负责人进行审批"); + } + } + + /** + * 一级审批 + * 查询是否存在审核信息,虚拟二级审核人ID: 1000012 + * 二级审核人审核申请单的时间范围为: 11-11 ~ 11-31 + */ + if(authDate.after(sdf.parse("2022-11-10 00:00:00")) && authDate.before(sdf.parse("2022-11-31 00:00:00")) ){ + //条件成立,查询二级审核信息 + date = AuthService.queryAuthInfo("1000011",orderId); + + if(date == null){ + return new AuthInfo("0001","单号: " + orderId , + "状态: 等待一级审批负责人进行审批"); + } + } + + return new AuthInfo("0001","单号: " + orderId ,"申请人: " + name," 状态:审批完成!"); + } + +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example02/AuthInfo.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example02/AuthInfo.java new file mode 100644 index 0000000..25162dc --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example02/AuthInfo.java @@ -0,0 +1,44 @@ +package com.mashibing.chain.example02; + +/** + * 封装审核信息 + * @author spikeCong + * @date 2022/10/16 + **/ +public class AuthInfo { + + private String code; //状态码 + + private String info = ""; //审核相关信息 + + public AuthInfo(String code, String... infos) { + this.code = code; + for (String str : infos) { + this.info = info.concat(str+ " "); + } + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getInfo() { + return info; + } + + public void setInfo(String info) { + this.info = info; + } + + @Override + public String toString() { + return "AuthInfo{" + + "code='" + code + '\'' + + ", info='" + info + '\'' + + '}'; + } +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example02/AuthService.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example02/AuthService.java new file mode 100644 index 0000000..6bc39a8 --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example02/AuthService.java @@ -0,0 +1,37 @@ +package com.mashibing.chain.example02; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * 模拟审核服务 + * @author spikeCong + * @date 2022/10/16 + **/ +public class AuthService { + + //审核信息容器 key:审批人Id+审批单Id , value:审批时间 + private static Map authMap = new HashMap<>(); + + + /** + * 审核方法 + * @param uId 审核人id + * @param orderId 审核单id + */ + public static void auth(String uId , String orderId){ + System.out.println("进入审批流程,审批人ID: " + uId); + authMap.put(uId.concat(orderId),new Date()); + } + + /** + * 查询审核结果 + * @param uId + * @param orderId + * @return: java.util.Date + */ + public static Date queryAuthInfo(String uId , String orderId){ + return authMap.get(uId.concat(orderId)); + } +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example02/Client.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example02/Client.java new file mode 100644 index 0000000..5368ddb --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example02/Client.java @@ -0,0 +1,44 @@ +package com.mashibing.chain.example02; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * @author spikeCong + * @date 2022/10/16 + **/ +public class Client { + + public static void main(String[] args) throws ParseException { + + AuthController controller = new AuthController(); + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date date = sdf.parse("2022-11-06 00:00:00"); + + //模拟审核请求及审批操作 + AuthInfo info1 = controller.doAuth("研发小周", "100001000010000", date); + System.out.println("当前审核状态: " + info1.getInfo()); + + AuthService.auth("1000013","100001000010000"); + System.out.println("三级审批负责人审批完成,审批人: 王工"); + + System.out.println("================================================="); + + AuthInfo info2 = controller.doAuth("研发小周", "100001000010000", date); + System.out.println("当前审核状态: " + info2.getInfo()); + + AuthService.auth("1000012","100001000010000"); + System.out.println("二级审批负责人审批完成,审批人: 周经理"); + + + System.out.println("================================================="); + + AuthInfo info3 = controller.doAuth("研发小周", "100001000010000", date); + System.out.println("当前审核状态: " + info3.getInfo()); + + AuthService.auth("1000012","100001000010000"); + System.out.println("一级审批负责人审批完成,审批人: 罗总"); + } +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/AuthLink.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/AuthLink.java new file mode 100644 index 0000000..47e4b43 --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/AuthLink.java @@ -0,0 +1,47 @@ +package com.mashibing.chain.example03; + +import com.mashibing.chain.example02.AuthInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.xml.crypto.dsig.SignatureMethod; +import java.text.SimpleDateFormat; +import java.util.Date; + + +/** + * 抽象审核链类 + * @author spikeCong + * @date 2022/10/16 + **/ +public abstract class AuthLink { + + protected Logger logger = (Logger) LoggerFactory.getLogger(AuthLink.class); + + protected SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + protected String levelUserId; //审核人id + + protected String levelUserName; //审核人姓名 + + protected AuthLink next; //表示持有下一个处理对象的引用 + + public AuthLink(String levelUserId, String levelUserName) { + this.levelUserId = levelUserId; + this.levelUserName = levelUserName; + } + + //获取下一个处理器 + public AuthLink getNext() { + return next; + } + + //向责任链中添加处理器 + public AuthLink appendNext(AuthLink next){ + this.next = next; + return this; + } + + //抽象审核方法 + public abstract AuthInfo doAuth(String uId, String orderId, Date authDate); +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/Client.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/Client.java new file mode 100644 index 0000000..6d73fce --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/Client.java @@ -0,0 +1,50 @@ +package com.mashibing.chain.example03; + +import com.alibaba.fastjson.JSON; +import com.mashibing.chain.example02.AuthService; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * @author spikeCong + * @date 2022/10/16 + **/ +public class Client { + + private Logger logger = LoggerFactory.getLogger(Client.class); + + @Test + public void test_Auth() throws ParseException { + + //定义责任链 + AuthLink authLink = new Level3AuthLink("1000013", "李工") + .appendNext(new Level2AuthLink("1000012", "王经理") + .appendNext(new Level1AuthLink("1000011", "罗总"))); + + SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date currentDate = f.parse("2022-11-18 23:49:46"); + + logger.info("测试结果:{}", JSON.toJSONString(authLink.doAuth("研发牛马", "1000998004813441", currentDate))); + +// // 模拟三级负责人审批 + AuthService.auth("1000013", "1000998004813441"); + logger.info("测试结果:{}", "模拟三级负责人审批,王工"); + logger.info("测试结果:{}", JSON.toJSONString(authLink.doAuth("研发牛马", "1000998004813441", currentDate))); + +// // 模拟二级负责人审批 + AuthService.auth("1000012", "1000998004813441"); + logger.info("测试结果:{}", "模拟二级负责人审批,张经理"); + logger.info("测试结果:{}", JSON.toJSONString(authLink.doAuth("研发牛马", "1000998004813441", currentDate))); + +// // 模拟一级负责人审批 + AuthService.auth("1000011", "1000998004813441"); + logger.info("测试结果:{}", "模拟一级负责人审批,段总"); + logger.info("测试结果:{}", JSON.toJSONString(authLink.doAuth("研发牛马", "1000998004813441", currentDate))); + + } +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/Level1AuthLink.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/Level1AuthLink.java new file mode 100644 index 0000000..6ca2926 --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/Level1AuthLink.java @@ -0,0 +1,46 @@ +package com.mashibing.chain.example03; + +import com.mashibing.chain.example02.AuthInfo; +import com.mashibing.chain.example02.AuthService; + +import java.text.ParseException; +import java.util.Date; + +/** + * @author spikeCong + * @date 2022/10/16 + **/ +public class Level1AuthLink extends AuthLink { + + private Date beginDate = sdf.parse("2022-11-11 00:00:00"); + private Date endDate = sdf.parse("2022-11-31 00:00:00"); + + public Level1AuthLink(String levelUserId, String levelUserName) throws ParseException { + super(levelUserId, levelUserName); + } + + @Override + public AuthInfo doAuth(String uId, String orderId, Date authDate) { + + Date date = AuthService.queryAuthInfo(levelUserId, orderId); + + if(null == date){ + return new AuthInfo("0001","单号: "+ + orderId," 状态: 待一级审核人审批", levelUserName); + } + + AuthLink next = super.getNext(); + + if(next == null){ + return new AuthInfo("0001","单号: "+ + orderId," 状态: 一级审批完成", "审批人: " + levelUserName); + } + + if(authDate.before(beginDate) || authDate.after(endDate)){ + return new AuthInfo("0001","单号: "+ + orderId," 状态: 一级审批完成", "审批人: " + levelUserName); + } + + return next.doAuth(uId,orderId,authDate); + } +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/Level2AuthLink.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/Level2AuthLink.java new file mode 100644 index 0000000..b2ebcee --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/Level2AuthLink.java @@ -0,0 +1,46 @@ +package com.mashibing.chain.example03; + +import com.mashibing.chain.example02.AuthInfo; +import com.mashibing.chain.example02.AuthService; + +import java.text.ParseException; +import java.util.Date; + +/** + * @author spikeCong + * @date 2022/10/16 + **/ +public class Level2AuthLink extends AuthLink { + + private Date beginDate = sdf.parse("2022-11-11 00:00:00"); + private Date endDate = sdf.parse("2022-11-31 00:00:00"); + + public Level2AuthLink(String levelUserId, String levelUserName) throws ParseException { + super(levelUserId, levelUserName); + } + + @Override + public AuthInfo doAuth(String uId, String orderId, Date authDate) { + + Date date = AuthService.queryAuthInfo(levelUserId, orderId); + + if(null == date){ + return new AuthInfo("0001","单号: "+ + orderId," 状态: 待二级审核人审批", levelUserName); + } + + AuthLink next = super.getNext(); + + if(next == null){ + return new AuthInfo("0000","单号: "+ + orderId," 状态: 二级审批完成", "审批人: " + levelUserName); + } + + if(authDate.before(beginDate) || authDate.after(endDate)){ + return new AuthInfo("0000","单号: "+ + orderId," 状态: 二级审批完成", "审批人: " + levelUserName); + } + + return next.doAuth(uId,orderId,authDate); + } +} diff --git a/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/Level3AuthLink.java b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/Level3AuthLink.java new file mode 100644 index 0000000..b616f2c --- /dev/null +++ b/msb-chain of responsibility-pattern-15/src/main/java/com/mashibing/chain/example03/Level3AuthLink.java @@ -0,0 +1,38 @@ +package com.mashibing.chain.example03; + +import com.mashibing.chain.example02.AuthInfo; +import com.mashibing.chain.example02.AuthService; + +import java.text.ParseException; +import java.util.Date; + +/** + * @author spikeCong + * @date 2022/10/16 + **/ +public class Level3AuthLink extends AuthLink { + + public Level3AuthLink(String levelUserId, String levelUserName) throws ParseException { + super(levelUserId, levelUserName); + } + + @Override + public AuthInfo doAuth(String uId, String orderId, Date authDate) { + + Date date = AuthService.queryAuthInfo(levelUserId, orderId); + + if(null == date){ + return new AuthInfo("0001","单号: "+ + orderId," 状态: 待三级审核人审批", levelUserName); + } + + AuthLink next = super.getNext(); + + if(next == null){ + return new AuthInfo("0000","单号: "+ + orderId," 状态: 三级审批完成", "审批人: " + levelUserName); + } + + return next.doAuth(uId,orderId,authDate); + } +} diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/Client.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/Client.class new file mode 100644 index 0000000..e2432c8 Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/Client.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/Handler.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/Handler.class new file mode 100644 index 0000000..9f09a47 Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/Handler.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/HandlerA.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/HandlerA.class new file mode 100644 index 0000000..c593891 Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/HandlerA.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/HandlerB.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/HandlerB.class new file mode 100644 index 0000000..3646028 Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/HandlerB.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/HandlerC.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/HandlerC.class new file mode 100644 index 0000000..abd4cba Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/HandlerC.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/RequestData.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/RequestData.class new file mode 100644 index 0000000..2a3b367 Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example01/RequestData.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example02/AuthController.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example02/AuthController.class new file mode 100644 index 0000000..268cee7 Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example02/AuthController.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example02/AuthInfo.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example02/AuthInfo.class new file mode 100644 index 0000000..bf765a0 Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example02/AuthInfo.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example02/AuthService.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example02/AuthService.class new file mode 100644 index 0000000..8434dcc Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example02/AuthService.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example02/Client.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example02/Client.class new file mode 100644 index 0000000..9834a06 Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example02/Client.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/AuthLink.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/AuthLink.class new file mode 100644 index 0000000..835fd39 Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/AuthLink.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/Client.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/Client.class new file mode 100644 index 0000000..2c9a470 Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/Client.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/Level1AuthLink.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/Level1AuthLink.class new file mode 100644 index 0000000..e72a486 Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/Level1AuthLink.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/Level2AuthLink.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/Level2AuthLink.class new file mode 100644 index 0000000..e1a157c Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/Level2AuthLink.class differ diff --git a/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/Level3AuthLink.class b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/Level3AuthLink.class new file mode 100644 index 0000000..b5eba45 Binary files /dev/null and b/msb-chain of responsibility-pattern-15/target/classes/com/mashibing/chain/example03/Level3AuthLink.class differ diff --git a/msb-command-pattern-20/msb-command-pattern-20.iml b/msb-command-pattern-20/msb-command-pattern-20.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-command-pattern-20/msb-command-pattern-20.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-command-pattern-20/pom.xml b/msb-command-pattern-20/pom.xml new file mode 100644 index 0000000..b15916d --- /dev/null +++ b/msb-command-pattern-20/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + com.mashibing + msb-command-pattern-20 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + \ No newline at end of file diff --git a/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Chef.java b/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Chef.java new file mode 100644 index 0000000..6154829 --- /dev/null +++ b/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Chef.java @@ -0,0 +1,13 @@ +package com.mashibing.command.example01; + +/** + * 厨师类 -> Receiver接收者角色 + * @author spikeCong + * @date 2022/10/20 + **/ +public class Chef { + + public void makeFood(int num , String foodName){ + System.out.println(num + "份, " + foodName); + } +} diff --git a/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Client.java b/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Client.java new file mode 100644 index 0000000..3ba2461 --- /dev/null +++ b/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Client.java @@ -0,0 +1,36 @@ +package com.mashibing.command.example01; + +/** + * @author spikeCong + * @date 2022/10/20 + **/ +public class Client { + + public static void main(String[] args) { + + Order order1 = new Order(); + order1.setDiningTable(10); + order1.getFoodMenu().put("鲍鱼炒饭",1); + order1.getFoodMenu().put("海参炒面",1); + + Order order2 = new Order(); + order2.setDiningTable(15); + order2.getFoodMenu().put("回锅肉盖饭",1); + order2.getFoodMenu().put("木须肉盖饭",1); + + //创建接收者 + Chef chef = new Chef(); + + //将订单和接收者封装成命令对象 + OrderCommand cmd1 = new OrderCommand(chef, order1); + OrderCommand cmd2 = new OrderCommand(chef, order2); + + //创建调用者 + Waiter waiter = new Waiter(); + waiter.setCommand(cmd1); + waiter.setCommand(cmd2); + + //将订单发送给厨师 上菜 + waiter.orderUp(); + } +} diff --git a/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Command.java b/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Command.java new file mode 100644 index 0000000..bde5d52 --- /dev/null +++ b/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Command.java @@ -0,0 +1,11 @@ +package com.mashibing.command.example01; + +/** + * 抽象命令接口 + * @author spikeCong + * @date 2022/10/20 + **/ +public interface Command { + + void execute(); //统一的执行方法 +} diff --git a/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Order.java b/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Order.java new file mode 100644 index 0000000..e070239 --- /dev/null +++ b/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Order.java @@ -0,0 +1,32 @@ +package com.mashibing.command.example01; + +import java.util.HashMap; +import java.util.Map; + +/** + * 订单类 + * @author spikeCong + * @date 2022/10/20 + **/ +public class Order { + + private int diningTable; //餐桌号码 + + private Map foodMenu = new HashMap<>(); //存储菜名和份数 + + public int getDiningTable() { + return diningTable; + } + + public void setDiningTable(int diningTable) { + this.diningTable = diningTable; + } + + public Map getFoodMenu() { + return foodMenu; + } + + public void setFoodMenu(Map foodMenu) { + this.foodMenu = foodMenu; + } +} diff --git a/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/OrderCommand.java b/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/OrderCommand.java new file mode 100644 index 0000000..3ff83a0 --- /dev/null +++ b/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/OrderCommand.java @@ -0,0 +1,39 @@ +package com.mashibing.command.example01; + +import java.util.Set; + +/** + * 具体命令 + * @author spikeCong + * @date 2022/10/20 + **/ +public class OrderCommand implements Command { + + //接收者对象的引用 + private Chef receiver; + + private Order order; + + public OrderCommand(Chef receiver, Order order) { + this.receiver = receiver; + this.order = order; + } + + @Override + public void execute() { + System.out.println(order.getDiningTable() + "桌的订单: "); + Set keySet = order.getFoodMenu().keySet(); + for (String key : keySet) { + receiver.makeFood(order.getFoodMenu().get(key),key); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + System.out.println(order.getDiningTable() + "桌的菜品已经上齐!"); + } + +} diff --git a/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Waiter.java b/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Waiter.java new file mode 100644 index 0000000..6f5e032 --- /dev/null +++ b/msb-command-pattern-20/src/main/java/com/mashibing/command/example01/Waiter.java @@ -0,0 +1,36 @@ +package com.mashibing.command.example01; + +import java.util.ArrayList; + +/** + * 服务员 -> Invoker 调用者 + * @author spikeCong + * @date 2022/10/20 + **/ +public class Waiter { + + //可以持有多个命令对象 + private ArrayList commands; + + public Waiter() { + this.commands = new ArrayList<>(); + } + + public Waiter(ArrayList commands) { + this.commands = commands; + } + + public void setCommand(Command command) { + this.commands.add(command); + } + + //发出指令 + public void orderUp(){ + System.out.println("叮咚! 服务员: 有新的订单,请师傅开始制作......"); + for (Command command : commands) { + if(command != null){ + command.execute(); + } + } + } +} diff --git a/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Chef.class b/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Chef.class new file mode 100644 index 0000000..1b1d108 Binary files /dev/null and b/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Chef.class differ diff --git a/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Client.class b/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Client.class new file mode 100644 index 0000000..94a7de6 Binary files /dev/null and b/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Client.class differ diff --git a/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Command.class b/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Command.class new file mode 100644 index 0000000..ef542d4 Binary files /dev/null and b/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Command.class differ diff --git a/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Order.class b/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Order.class new file mode 100644 index 0000000..9610e74 Binary files /dev/null and b/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Order.class differ diff --git a/msb-command-pattern-20/target/classes/com/mashibing/command/example01/OrderCommand.class b/msb-command-pattern-20/target/classes/com/mashibing/command/example01/OrderCommand.class new file mode 100644 index 0000000..432c934 Binary files /dev/null and b/msb-command-pattern-20/target/classes/com/mashibing/command/example01/OrderCommand.class differ diff --git a/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Waiter.class b/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Waiter.class new file mode 100644 index 0000000..ce921a7 Binary files /dev/null and b/msb-command-pattern-20/target/classes/com/mashibing/command/example01/Waiter.class differ diff --git a/msb-composite-pattern-10/msb-composite-pattern-10.iml b/msb-composite-pattern-10/msb-composite-pattern-10.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-composite-pattern-10/msb-composite-pattern-10.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-composite-pattern-10/pom.xml b/msb-composite-pattern-10/pom.xml new file mode 100644 index 0000000..bd63a7a --- /dev/null +++ b/msb-composite-pattern-10/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.mashibing + msb-composite-pattern-10 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example01/Component.java b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example01/Component.java new file mode 100644 index 0000000..6871ad1 --- /dev/null +++ b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example01/Component.java @@ -0,0 +1,15 @@ +package com.mashibing.composite.example01; + +/** + * 抽象根节点角色 + * 对客户端而言,只需要针对抽象编程,无需关心具体子类是树枝节点还是叶子节点 + * @author spikeCong + * @date 2022/10/7 + **/ +public abstract class Component { + + public abstract void add(Component c); //增加节点 + public abstract void remove(Component c); //删除节点 + public abstract Component getChild(int i); //获取节点 + public abstract void operation(); //业务方法 +} diff --git a/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example01/Composite.java b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example01/Composite.java new file mode 100644 index 0000000..22b3875 --- /dev/null +++ b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example01/Composite.java @@ -0,0 +1,39 @@ +package com.mashibing.composite.example01; + +import java.util.ArrayList; + +/** + * 树枝节点 + * 树枝节点类是一个容器对象,它既可以包含树枝节点也可以包含叶子节点 + * @author spikeCong + * @date 2022/10/7 + **/ +public class Composite extends Component { + + //定义集合属性,保存子节点的数据 + private ArrayList list = new ArrayList<>(); + + @Override + public void add(Component c) { + list.add(c); + } + + @Override + public void remove(Component c) { + list.remove(c); + } + + @Override + public Component getChild(int i) { + return list.get(i); + } + + //具体业务方法 + @Override + public void operation() { + //在循环中,递归调用其他节点中的operation() 方法 + for (Component component : list) { + component.operation(); + } + } +} diff --git a/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example01/Leaf.java b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example01/Leaf.java new file mode 100644 index 0000000..defcc2a --- /dev/null +++ b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example01/Leaf.java @@ -0,0 +1,29 @@ +package com.mashibing.composite.example01; + +/** + * 叶子节点 + * 叶子节点中不能包含子节点 + * @author spikeCong + * @date 2022/10/7 + **/ +public class Leaf extends Component { + @Override + public void add(Component c) { + + } + + @Override + public void remove(Component c) { + + } + + @Override + public Component getChild(int i) { + return null; + } + + @Override + public void operation() { + //叶子节点中的具体方法 + } +} diff --git a/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example02/Client.java b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example02/Client.java new file mode 100644 index 0000000..0e72d1c --- /dev/null +++ b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example02/Client.java @@ -0,0 +1,35 @@ +package com.mashibing.composite.example02; + +/** + * @author spikeCong + * @date 2022/10/7 + **/ +public class Client { + + public static void main(String[] args) { + + //创建根节点 + Directory rootDir = new Directory("root"); + + //创建树枝节点 + Directory binDir = new Directory("bin"); + //向bin目录添加叶子节点 + binDir.add(new File("vi",10000)); + binDir.add(new File("test",20000)); + + Directory tmpDir = new Directory("tmp"); + + Directory usrDir = new Directory("usr"); + Directory mysqlDir = new Directory("mysql"); + mysqlDir.add(new File("my.cnf",30)); + mysqlDir.add(new File("test.db",25000)); + usrDir.add(mysqlDir); + + //将所有子文件夹封装到根节点 + rootDir.add(binDir); + rootDir.add(tmpDir); + rootDir.add(usrDir); + + rootDir.printList(""); + } +} diff --git a/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example02/Directory.java b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example02/Directory.java new file mode 100644 index 0000000..77d206c --- /dev/null +++ b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example02/Directory.java @@ -0,0 +1,59 @@ +package com.mashibing.composite.example02; + +import java.util.ArrayList; + +/** + * Directory 容器对象,表示文件夹 + * @author spikeCong + * @date 2022/10/7 + **/ +public class Directory extends Entry { + + //文件的名字 + private String name; + + //文件夹和文件的集合 + private ArrayList directory = new ArrayList<>(); + + public Directory(String name) { + this.name = name; + } + + @Override + public String getName() { + return this.name; + } + + + /** + * 获取文件大小 + * 1.如果entry对象是file类型,则调用getSize方法获取文件大小 + * 2.如果entry对象是Directory类型,会继续调用子文件夹的getSize()方法,形成递归调用 + */ + @Override + public int getSize() { + + int size = 0; + + //遍历获取文件大小 + for (Entry entry : directory) { + size += entry.getSize(); + } + + return size; + } + + @Override + public Entry add(Entry entry) { + directory.add(entry); + return this; + } + + @Override + public void printList(String prefix) { + System.out.println("/" + this); + for (Entry entry : directory) { + entry.printList("/" + name); + } + } +} diff --git a/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example02/Entry.java b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example02/Entry.java new file mode 100644 index 0000000..c6c61b1 --- /dev/null +++ b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example02/Entry.java @@ -0,0 +1,25 @@ +package com.mashibing.composite.example02; + +/** + * Entry抽象类 (文件夹+文件) + * @author spikeCong + * @date 2022/10/7 + **/ +public abstract class Entry { + + public abstract String getName(); //获取文件名 + + public abstract int getSize(); //获取文件大小 + + //添加文件或者文件夹方法 + public abstract Entry add(Entry entry); + + //显示指定目录下的所有文件的信息 + public abstract void printList(String prefix); + + @Override + public String toString() { + + return getName() +"(" + getSize() +")"; + } +} diff --git a/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example02/File.java b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example02/File.java new file mode 100644 index 0000000..27dff26 --- /dev/null +++ b/msb-composite-pattern-10/src/main/java/com/mashibing/composite/example02/File.java @@ -0,0 +1,38 @@ +package com.mashibing.composite.example02; + +/** + * File类,表示文件 + * @author spikeCong + * @date 2022/10/7 + **/ +public class File extends Entry{ + + private String name; //文件名 + private int size; //文件大小 + + public File(String name, int size) { + this.name = name; + this.size = size; + } + + @Override + public String getName() { + return this.name; + } + + @Override + public int getSize() { + return this.size; + } + + @Override + public Entry add(Entry entry) { + return null; + } + + @Override + public void printList(String prefix) { + + System.out.println(prefix + "/" + this); + } +} diff --git a/msb-composite-pattern-10/target/classes/com/mashibing/composite/example01/Component.class b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example01/Component.class new file mode 100644 index 0000000..c1f99df Binary files /dev/null and b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example01/Component.class differ diff --git a/msb-composite-pattern-10/target/classes/com/mashibing/composite/example01/Composite.class b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example01/Composite.class new file mode 100644 index 0000000..ec71cf6 Binary files /dev/null and b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example01/Composite.class differ diff --git a/msb-composite-pattern-10/target/classes/com/mashibing/composite/example01/Leaf.class b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example01/Leaf.class new file mode 100644 index 0000000..94fd3c4 Binary files /dev/null and b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example01/Leaf.class differ diff --git a/msb-composite-pattern-10/target/classes/com/mashibing/composite/example02/Client.class b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example02/Client.class new file mode 100644 index 0000000..2cb080f Binary files /dev/null and b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example02/Client.class differ diff --git a/msb-composite-pattern-10/target/classes/com/mashibing/composite/example02/Directory.class b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example02/Directory.class new file mode 100644 index 0000000..62daccc Binary files /dev/null and b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example02/Directory.class differ diff --git a/msb-composite-pattern-10/target/classes/com/mashibing/composite/example02/Entry.class b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example02/Entry.class new file mode 100644 index 0000000..5e7fec3 Binary files /dev/null and b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example02/Entry.class differ diff --git a/msb-composite-pattern-10/target/classes/com/mashibing/composite/example02/File.class b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example02/File.class new file mode 100644 index 0000000..62fee8c Binary files /dev/null and b/msb-composite-pattern-10/target/classes/com/mashibing/composite/example02/File.class differ diff --git a/msb-decorator-pattern-07/msb-decorator-pattern-07.iml b/msb-decorator-pattern-07/msb-decorator-pattern-07.iml new file mode 100644 index 0000000..5d36bf1 --- /dev/null +++ b/msb-decorator-pattern-07/msb-decorator-pattern-07.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-decorator-pattern-07/pom.xml b/msb-decorator-pattern-07/pom.xml new file mode 100644 index 0000000..d4b8ce3 --- /dev/null +++ b/msb-decorator-pattern-07/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + com.mashibing + msb-decorator-pattern-07 + 1.0-SNAPSHOT + + + + + commons-io + commons-io + 2.6 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example01/Component.java b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example01/Component.java new file mode 100644 index 0000000..37bc244 --- /dev/null +++ b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example01/Component.java @@ -0,0 +1,12 @@ +package com.maishibing.decorator.example01; + +/** + * 抽象构建类 + * @author spikeCong + * @date 2022/9/27 + **/ +public abstract class Component { + + //抽象方法 + public abstract void operation(); +} diff --git a/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example01/ConcreteComponent.java b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example01/ConcreteComponent.java new file mode 100644 index 0000000..fb27ad8 --- /dev/null +++ b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example01/ConcreteComponent.java @@ -0,0 +1,13 @@ +package com.maishibing.decorator.example01; + +/** + * 具体构建类(被装饰类) + * @author spikeCong + * @date 2022/9/27 + **/ +public class ConcreteComponent extends Component{ + + public void operation() { + //基础功能的实现(一些复杂功能通过装饰类进行扩展) + } +} diff --git a/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example01/ConcreteDecorator.java b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example01/ConcreteDecorator.java new file mode 100644 index 0000000..caefd5b --- /dev/null +++ b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example01/ConcreteDecorator.java @@ -0,0 +1,24 @@ +package com.maishibing.decorator.example01; + +/** + * 具体装饰类 + * @author spikeCong + * @date 2022/9/27 + **/ +public class ConcreteDecorator extends Decorator { + + public ConcreteDecorator(Component component) { + super(component); + } + + @Override + public void operation() { + super.operation(); //调用原有的业务方法 + add(); //调用新增的方法 + } + + //新增业务方法 + public void add(){ + //...... + } +} diff --git a/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example01/Decorator.java b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example01/Decorator.java new file mode 100644 index 0000000..567638e --- /dev/null +++ b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example01/Decorator.java @@ -0,0 +1,22 @@ +package com.maishibing.decorator.example01; + +/** + * 抽象装饰类-装饰者模式的核心 + * @author spikeCong + * @date 2022/9/27 + **/ +public class Decorator extends Component{ + + //维持一个对抽象构件对象的引用 + private Component component; + + //通过构造注入一个抽象构件类型的对象 + public Decorator(Component component) { + this.component = component; + } + + public void operation() { + //调用原有的业务方法,并没有真正的进行装饰,而是提供了一个统一的接口,将装饰的过程交给子类完成 + component.operation(); + } +} diff --git a/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/BaseFileDataLoader.java b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/BaseFileDataLoader.java new file mode 100644 index 0000000..b85a5df --- /dev/null +++ b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/BaseFileDataLoader.java @@ -0,0 +1,40 @@ +package com.maishibing.decorator.example02; + +import org.apache.commons.io.FileUtils; + +import java.io.File; +import java.io.IOException; + +/** + * 具体组件: 抽象文件读取接口的实现类 + * @author spikeCong + * @date 2022/9/27 + **/ +public class BaseFileDataLoader implements DataLoader{ + + private String filePath; + + public BaseFileDataLoader(String filePath) { + this.filePath = filePath; + } + + //读 + public String read() { + try { + String result = FileUtils.readFileToString(new File(filePath), "utf-8"); + return result; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + //写 + public void write(String data) { + try { + FileUtils.writeStringToFile(new File(filePath),data,"utf-8"); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/Client.java b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/Client.java new file mode 100644 index 0000000..db2e986 --- /dev/null +++ b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/Client.java @@ -0,0 +1,23 @@ +package com.maishibing.decorator.example02; + +/** + * @author spikeCong + * @date 2022/9/27 + **/ +public class Client { + + public static void main(String[] args) { + + String info = "name:tom,age:30"; + +// DataLoader loader = new BaseFileDataLoader("demo.txt"); +// loader.write(info); +// +// String read = loader.read(); +// System.out.println(read); + DataLoaderDecorator decorator = new EncryptionDataDecorator(new BaseFileDataLoader("demo2.txt")); + //decorator.write(info); + String read = decorator.read(); + System.out.println(read); + } +} diff --git a/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/DataLoader.java b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/DataLoader.java new file mode 100644 index 0000000..cba462b --- /dev/null +++ b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/DataLoader.java @@ -0,0 +1,13 @@ +package com.maishibing.decorator.example02; + +/** + * 抽象的文件读取接口 + * @author spikeCong + * @date 2022/9/27 + **/ +public interface DataLoader { + + String read(); + + void write(String data); +} diff --git a/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/DataLoaderDecorator.java b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/DataLoaderDecorator.java new file mode 100644 index 0000000..a241b58 --- /dev/null +++ b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/DataLoaderDecorator.java @@ -0,0 +1,23 @@ +package com.maishibing.decorator.example02; + +/** + * 抽象装饰者类 + * @author spikeCong + * @date 2022/9/27 + **/ +public class DataLoaderDecorator implements DataLoader{ + + private DataLoader dataLoader; + + public DataLoaderDecorator(DataLoader dataLoader) { + this.dataLoader = dataLoader; + } + + public String read() { + return dataLoader.read(); + } + + public void write(String data) { + dataLoader.write(data); + } +} diff --git a/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/EncryptionDataDecorator.java b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/EncryptionDataDecorator.java new file mode 100644 index 0000000..27d001c --- /dev/null +++ b/msb-decorator-pattern-07/src/main/java/com/maishibing/decorator/example02/EncryptionDataDecorator.java @@ -0,0 +1,59 @@ +package com.maishibing.decorator.example02; + + +import java.io.UnsupportedEncodingException; +import java.util.Base64; + +/** + * 具体装饰者类-对文件内容进行加密和解密 + * @author spikeCong + * @date 2022/9/27 + **/ +public class EncryptionDataDecorator extends DataLoaderDecorator { + + public EncryptionDataDecorator(DataLoader dataLoader) { + super(dataLoader); + } + + @Override + public String read() { + return decode(super.read()); + } + + @Override + public void write(String data) { + super.write(encode(data)); + } + + //加密操作 + public String encode(String data){ + + try { + Base64.Encoder encoder = Base64.getEncoder(); + byte[] bytes = data.getBytes("utf-8"); + + String result = encoder.encodeToString(bytes); + return result; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + //解密操作 + public String decode(String data){ + try { + Base64.Decoder decode = Base64.getDecoder(); + String result = new String(decode.decode(data),"utf-8"); + + return result; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + + + +} diff --git a/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example01/Component.class b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example01/Component.class new file mode 100644 index 0000000..cdbeafd Binary files /dev/null and b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example01/Component.class differ diff --git a/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example01/ConcreteComponent.class b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example01/ConcreteComponent.class new file mode 100644 index 0000000..808d865 Binary files /dev/null and b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example01/ConcreteComponent.class differ diff --git a/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example01/ConcreteDecorator.class b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example01/ConcreteDecorator.class new file mode 100644 index 0000000..a36ac1a Binary files /dev/null and b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example01/ConcreteDecorator.class differ diff --git a/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example01/Decorator.class b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example01/Decorator.class new file mode 100644 index 0000000..cbd5f32 Binary files /dev/null and b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example01/Decorator.class differ diff --git a/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/BaseFileDataLoader.class b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/BaseFileDataLoader.class new file mode 100644 index 0000000..4db1bb8 Binary files /dev/null and b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/BaseFileDataLoader.class differ diff --git a/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/Client.class b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/Client.class new file mode 100644 index 0000000..6f5369b Binary files /dev/null and b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/Client.class differ diff --git a/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/DataLoader.class b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/DataLoader.class new file mode 100644 index 0000000..c329a1e Binary files /dev/null and b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/DataLoader.class differ diff --git a/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/DataLoaderDecorator.class b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/DataLoaderDecorator.class new file mode 100644 index 0000000..98c9a72 Binary files /dev/null and b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/DataLoaderDecorator.class differ diff --git a/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/EncryptionDataDecorator.class b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/EncryptionDataDecorator.class new file mode 100644 index 0000000..65e3078 Binary files /dev/null and b/msb-decorator-pattern-07/target/classes/com/maishibing/decorator/example02/EncryptionDataDecorator.class differ diff --git a/msb-facade-pattern-09/msb-facade-pattern-09.iml b/msb-facade-pattern-09/msb-facade-pattern-09.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-facade-pattern-09/msb-facade-pattern-09.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-facade-pattern-09/pom.xml b/msb-facade-pattern-09/pom.xml new file mode 100644 index 0000000..b032666 --- /dev/null +++ b/msb-facade-pattern-09/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.mashibing + msb-facade-pattern-09 + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + \ No newline at end of file diff --git a/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/Client.java b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/Client.java new file mode 100644 index 0000000..a86e4fc --- /dev/null +++ b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/Client.java @@ -0,0 +1,14 @@ +package com.mashibing.facade.example01; + +/** + * @author spikeCong + * @date 2022/10/5 + **/ +public class Client { + + public static void main(String[] args) { + + Facade facade = new Facade(); + facade.method(); + } +} diff --git a/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/Facade.java b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/Facade.java new file mode 100644 index 0000000..8f584e4 --- /dev/null +++ b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/Facade.java @@ -0,0 +1,20 @@ +package com.mashibing.facade.example01; + +/** + * @author spikeCong + * @date 2022/10/5 + **/ +public class Facade { + + private SubSystemA obj1 = new SubSystemA(); + private SubSystemB obj2 = new SubSystemB(); + private SubSystemC obj3 = new SubSystemC(); + + public void method(){ + + obj1.methodA(); + obj2.methodB(); + obj3.methodC(); + } + +} diff --git a/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/SubSystemA.java b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/SubSystemA.java new file mode 100644 index 0000000..d561369 --- /dev/null +++ b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/SubSystemA.java @@ -0,0 +1,12 @@ +package com.mashibing.facade.example01; + +/** + * @author spikeCong + * @date 2022/10/5 + **/ +public class SubSystemA { + + public void methodA(){ + //业务代码 + } +} diff --git a/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/SubSystemB.java b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/SubSystemB.java new file mode 100644 index 0000000..c031157 --- /dev/null +++ b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/SubSystemB.java @@ -0,0 +1,12 @@ +package com.mashibing.facade.example01; + +/** + * @author spikeCong + * @date 2022/10/5 + **/ +public class SubSystemB { + + public void methodB(){ + + } +} diff --git a/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/SubSystemC.java b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/SubSystemC.java new file mode 100644 index 0000000..e513e76 --- /dev/null +++ b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example01/SubSystemC.java @@ -0,0 +1,12 @@ +package com.mashibing.facade.example01; + +/** + * @author spikeCong + * @date 2022/10/5 + **/ +public class SubSystemC { + + public void methodC(){ + + } +} diff --git a/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/AirCondition.java b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/AirCondition.java new file mode 100644 index 0000000..ea014c4 --- /dev/null +++ b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/AirCondition.java @@ -0,0 +1,19 @@ +package com.mashibing.facade.example02; + +/** + * 空调类 + * @author spikeCong + * @date 2022/10/5 + **/ +public class AirCondition { + + public void on(){ + + System.out.println("打开空调......"); + } + + public void off(){ + + System.out.println("关闭空调......"); + } +} diff --git a/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/Client.java b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/Client.java new file mode 100644 index 0000000..6d04dd8 --- /dev/null +++ b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/Client.java @@ -0,0 +1,17 @@ +package com.mashibing.facade.example02; + +/** + * @author spikeCong + * @date 2022/10/5 + **/ +public class Client { + + public static void main(String[] args) { + + //创建外观对象 + SmartAppliancesFacade facade = new SmartAppliancesFacade(); + + facade.say("打开家电"); + facade.say("关闭家电"); + } +} diff --git a/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/Light.java b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/Light.java new file mode 100644 index 0000000..c1b665d --- /dev/null +++ b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/Light.java @@ -0,0 +1,19 @@ +package com.mashibing.facade.example02; + +/** + * 电灯类 + * @author spikeCong + * @date 2022/10/5 + **/ +public class Light { + + public void on(){ + + System.out.println("打开灯......"); + } + + public void off(){ + + System.out.println("关闭灯......"); + } +} diff --git a/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/SmartAppliancesFacade.java b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/SmartAppliancesFacade.java new file mode 100644 index 0000000..83c58fb --- /dev/null +++ b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/SmartAppliancesFacade.java @@ -0,0 +1,49 @@ +package com.mashibing.facade.example02; + +/** + * 智能音箱外观类 + * @author spikeCong + * @date 2022/10/5 + **/ +public class SmartAppliancesFacade { + + private Light light; + + private TV tv; + + private AirCondition airCondition; + + public SmartAppliancesFacade() { + this.light =new Light(); + this.tv = new TV(); + this.airCondition = new AirCondition(); + } + + public void say(String message){ + if(message.contains("打开")){ + on(); + }else if(message.contains("关闭")){ + off(); + }else{ + System.out.println("对不起没有听清楚您说什么! 请重新再说一遍"); + } + + } + + + //起床后 语音开启 电灯 电视 空调 + private void on() { + System.out.println("起床了!"); + light.on(); + tv.on(); + airCondition.on(); + } + + //睡觉前 语音关闭 电灯 电视 空调 + private void off() { + System.out.println("睡觉了!"); + light.off(); + tv.off(); + airCondition.off(); + } +} diff --git a/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/TV.java b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/TV.java new file mode 100644 index 0000000..b16c5aa --- /dev/null +++ b/msb-facade-pattern-09/src/main/java/com/mashibing/facade/example02/TV.java @@ -0,0 +1,19 @@ +package com.mashibing.facade.example02; + +/** + * 电灯类 + * @author spikeCong + * @date 2022/10/5 + **/ +public class TV { + + public void on(){ + + System.out.println("打开电视......"); + } + + public void off(){ + + System.out.println("关闭电视......"); + } +} diff --git a/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/Client.class b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/Client.class new file mode 100644 index 0000000..049726c Binary files /dev/null and b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/Client.class differ diff --git a/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/Facade.class b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/Facade.class new file mode 100644 index 0000000..15d9d5c Binary files /dev/null and b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/Facade.class differ diff --git a/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/SubSystemA.class b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/SubSystemA.class new file mode 100644 index 0000000..894faa6 Binary files /dev/null and b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/SubSystemA.class differ diff --git a/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/SubSystemB.class b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/SubSystemB.class new file mode 100644 index 0000000..28df8b7 Binary files /dev/null and b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/SubSystemB.class differ diff --git a/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/SubSystemC.class b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/SubSystemC.class new file mode 100644 index 0000000..e56ad0f Binary files /dev/null and b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example01/SubSystemC.class differ diff --git a/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/AirCondition.class b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/AirCondition.class new file mode 100644 index 0000000..1ee377f Binary files /dev/null and b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/AirCondition.class differ diff --git a/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/Client.class b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/Client.class new file mode 100644 index 0000000..32b56e5 Binary files /dev/null and b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/Client.class differ diff --git a/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/Light.class b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/Light.class new file mode 100644 index 0000000..b798d76 Binary files /dev/null and b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/Light.class differ diff --git a/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/SmartAppliancesFacade.class b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/SmartAppliancesFacade.class new file mode 100644 index 0000000..da4e751 Binary files /dev/null and b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/SmartAppliancesFacade.class differ diff --git a/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/TV.class b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/TV.class new file mode 100644 index 0000000..5d1f7ee Binary files /dev/null and b/msb-facade-pattern-09/target/classes/com/mashibing/facade/example02/TV.class differ diff --git a/msb-factory-pattern-02/msb-factory-pattern-02.iml b/msb-factory-pattern-02/msb-factory-pattern-02.iml new file mode 100644 index 0000000..d0cdb84 --- /dev/null +++ b/msb-factory-pattern-02/msb-factory-pattern-02.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-factory-pattern-02/pom.xml b/msb-factory-pattern-02/pom.xml new file mode 100644 index 0000000..61f719f --- /dev/null +++ b/msb-factory-pattern-02/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + com.mashibing + msb-factory-pattern-02 + 1.0-SNAPSHOT + + + + com.alibaba + fastjson + 1.2.62 + + + junit + junit + 4.12 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/Client.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/Client.java new file mode 100644 index 0000000..2329a5b --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/Client.java @@ -0,0 +1,51 @@ +package com.mashibing.factory.abstract_factory; + +import com.mashibing.factory.abstract_factory.factory.AppliancesFactory; +import com.mashibing.factory.abstract_factory.factory.HisenseFactory; +import com.mashibing.factory.abstract_factory.product.AbstractFreezer; +import com.mashibing.factory.abstract_factory.product.AbstractTV; + +/** + * 客户端 + * @author spikeCong + * @date 2022/9/15 + **/ +public class Client { + + private AbstractTV tv; + + private AbstractFreezer freezer; + + public Client(AppliancesFactory factory){ + + //在客户端看来就是使用抽象工厂来生产家电 + this.tv = factory.createTV(); + this.freezer = factory.createFreezer(); + } + + public AbstractTV getTv() { + return tv; + } + + public void setTv(AbstractTV tv) { + this.tv = tv; + } + + public AbstractFreezer getFreezer() { + return freezer; + } + + public void setFreezer(AbstractFreezer freezer) { + this.freezer = freezer; + } + + public static void main(String[] args) { + + Client client = new Client(new HisenseFactory()); + AbstractTV tv = client.getTv(); + System.out.println(tv); + + AbstractFreezer freezer = client.getFreezer(); + System.out.println(freezer); + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/factory/AppliancesFactory.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/factory/AppliancesFactory.java new file mode 100644 index 0000000..9b1e28a --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/factory/AppliancesFactory.java @@ -0,0 +1,17 @@ +package com.mashibing.factory.abstract_factory.factory; + +import com.mashibing.factory.abstract_factory.product.AbstractFreezer; +import com.mashibing.factory.abstract_factory.product.AbstractTV; + +/** + * 抽象工厂: 在一个抽象工厂中可以声明多个工厂方法,用于创建不同类型的产品 + * @author spikeCong + * @date 2022/9/15 + **/ +public interface AppliancesFactory { + + AbstractTV createTV(); + + AbstractFreezer createFreezer(); + +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/factory/HairFactory.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/factory/HairFactory.java new file mode 100644 index 0000000..2c3fd07 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/factory/HairFactory.java @@ -0,0 +1,24 @@ +package com.mashibing.factory.abstract_factory.factory; + +import com.mashibing.factory.abstract_factory.product.AbstractFreezer; +import com.mashibing.factory.abstract_factory.product.AbstractTV; +import com.mashibing.factory.abstract_factory.product.HairFreezer; +import com.mashibing.factory.abstract_factory.product.HairTV; + +/** + * 具体工厂 + * @author spikeCong + * @date 2022/9/15 + **/ +public class HairFactory implements AppliancesFactory { + + @Override + public AbstractTV createTV() { + return new HairTV(); + } + + @Override + public AbstractFreezer createFreezer() { + return new HairFreezer(); + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/factory/HisenseFactory.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/factory/HisenseFactory.java new file mode 100644 index 0000000..5be8a45 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/factory/HisenseFactory.java @@ -0,0 +1,23 @@ +package com.mashibing.factory.abstract_factory.factory; + +import com.mashibing.factory.abstract_factory.product.AbstractFreezer; +import com.mashibing.factory.abstract_factory.product.AbstractTV; +import com.mashibing.factory.abstract_factory.product.HisenseFreezer; +import com.mashibing.factory.abstract_factory.product.HisenseTV; + +/** + * @author spikeCong + * @date 2022/9/15 + **/ +public class HisenseFactory implements AppliancesFactory { + + @Override + public AbstractTV createTV() { + return new HisenseTV(); + } + + @Override + public AbstractFreezer createFreezer() { + return new HisenseFreezer(); + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/AbstractFreezer.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/AbstractFreezer.java new file mode 100644 index 0000000..7a799ff --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/AbstractFreezer.java @@ -0,0 +1,9 @@ +package com.mashibing.factory.abstract_factory.product; + +/** + * 抽象产品 + * @author spikeCong + * @date 2022/9/15 + **/ +public interface AbstractFreezer { +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/AbstractTV.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/AbstractTV.java new file mode 100644 index 0000000..7b56719 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/AbstractTV.java @@ -0,0 +1,9 @@ +package com.mashibing.factory.abstract_factory.product; + +/** + * 抽象产品: 定义产品规范,描述了产品的主要的特征和功能 + * @author spikeCong + * @date 2022/9/15 + **/ +public interface AbstractTV { +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/HairFreezer.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/HairFreezer.java new file mode 100644 index 0000000..893910c --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/HairFreezer.java @@ -0,0 +1,8 @@ +package com.mashibing.factory.abstract_factory.product; + +/** + * @author spikeCong + * @date 2022/9/15 + **/ +public class HairFreezer implements AbstractFreezer { +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/HairTV.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/HairTV.java new file mode 100644 index 0000000..517c433 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/HairTV.java @@ -0,0 +1,9 @@ +package com.mashibing.factory.abstract_factory.product; + +/** + * 具体产品 + * @author spikeCong + * @date 2022/9/15 + **/ +public class HairTV implements AbstractTV { +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/HisenseFreezer.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/HisenseFreezer.java new file mode 100644 index 0000000..6b4fe57 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/HisenseFreezer.java @@ -0,0 +1,8 @@ +package com.mashibing.factory.abstract_factory.product; + +/** + * @author spikeCong + * @date 2022/9/15 + **/ +public class HisenseFreezer implements AbstractFreezer { +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/HisenseTV.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/HisenseTV.java new file mode 100644 index 0000000..e49700e --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/abstract_factory/product/HisenseTV.java @@ -0,0 +1,8 @@ +package com.mashibing.factory.abstract_factory.product; + +/** + * @author spikeCong + * @date 2022/9/15 + **/ +public class HisenseTV implements AbstractTV { +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/controller/DeliverController.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/controller/DeliverController.java new file mode 100644 index 0000000..4c9a717 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/controller/DeliverController.java @@ -0,0 +1,32 @@ +package com.mashibing.factory.factory_method.controller; + +import com.mashibing.factory.factory_method.factory.FreeGoodsFactoryMap; +import com.mashibing.factory.factory_method.factory.impl.DiscountFreeGoodsFactory; +import com.mashibing.factory.factory_method.factory.FreeGoodsFactory; +import com.mashibing.factory.factory_method.factory.impl.SmallGiftFreeGoodsFactory; +import com.mashibing.factory.simple_factory.entity.AwardInfo; +import com.mashibing.factory.simple_factory.entity.ResponseResult; +import com.mashibing.factory.simple_factory.service.IFreeGoods; + + +/** + * 发放奖品接口 + * @author spikeCong + * @date 2022/9/9 + **/ +public class DeliverController { + + //发放奖品 + public ResponseResult awardToUser(AwardInfo awardInfo){ + + //根据类型获取具体工厂 + FreeGoodsFactory goodsFactory = FreeGoodsFactoryMap.getParserFactory(awardInfo.getAwardTypes()); + + //从工厂类中获取对应实例 + IFreeGoods iFreeGoods = goodsFactory.getInstance(); + + System.out.println("==========工厂方法模式============="); + ResponseResult responseResult = iFreeGoods.sendFreeGoods(awardInfo); + return responseResult; + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/factory/FreeGoodsFactory.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/factory/FreeGoodsFactory.java new file mode 100644 index 0000000..d6e0d9c --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/factory/FreeGoodsFactory.java @@ -0,0 +1,13 @@ +package com.mashibing.factory.factory_method.factory; + +import com.mashibing.factory.simple_factory.service.IFreeGoods; + +/** + * 抽象工厂 + * @author spikeCong + * @date 2022/9/9 + **/ +public interface FreeGoodsFactory { + + IFreeGoods getInstance(); +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/factory/FreeGoodsFactoryMap.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/factory/FreeGoodsFactoryMap.java new file mode 100644 index 0000000..e5101bb --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/factory/FreeGoodsFactoryMap.java @@ -0,0 +1,37 @@ +package com.mashibing.factory.factory_method.factory; + +import com.mashibing.factory.factory_method.factory.impl.DiscountFreeGoodsFactory; +import com.mashibing.factory.factory_method.factory.impl.SmallGiftFreeGoodsFactory; + +import java.util.HashMap; +import java.util.Map; + +/** + * 工厂的工厂,用来创建工厂类对象. + * @author spikeCong + * @date 2022/9/9 + **/ +public class FreeGoodsFactoryMap { + + //创建map集合,保存工厂对象 + private static final Map cachedFactories = new HashMap<>(); + + static{ + cachedFactories.put(1,new DiscountFreeGoodsFactory()); + cachedFactories.put(2,new SmallGiftFreeGoodsFactory()); + } + + public static FreeGoodsFactory getParserFactory(Integer type){ + if(type == 1){ + + FreeGoodsFactory freeGoodsFactory = cachedFactories.get(1); + return freeGoodsFactory; + }else if(type == 2){ + + FreeGoodsFactory freeGoodsFactory = cachedFactories.get(2); + return freeGoodsFactory; + } + + return null; + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/factory/impl/DiscountFreeGoodsFactory.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/factory/impl/DiscountFreeGoodsFactory.java new file mode 100644 index 0000000..0f20d95 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/factory/impl/DiscountFreeGoodsFactory.java @@ -0,0 +1,19 @@ +package com.mashibing.factory.factory_method.factory.impl; + +import com.mashibing.factory.factory_method.factory.FreeGoodsFactory; +import com.mashibing.factory.simple_factory.service.IFreeGoods; +import com.mashibing.factory.simple_factory.service.impl.DiscountFreeGoods; + +/** + * 生产优惠券发放接口-具体工厂 + * @author spikeCong + * @date 2022/9/9 + **/ +public class DiscountFreeGoodsFactory implements FreeGoodsFactory { + + @Override + public IFreeGoods getInstance() { + //返回的是具体产品 + return new DiscountFreeGoods(); + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/factory/impl/SmallGiftFreeGoodsFactory.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/factory/impl/SmallGiftFreeGoodsFactory.java new file mode 100644 index 0000000..9760670 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/factory_method/factory/impl/SmallGiftFreeGoodsFactory.java @@ -0,0 +1,18 @@ +package com.mashibing.factory.factory_method.factory.impl; + +import com.mashibing.factory.factory_method.factory.FreeGoodsFactory; +import com.mashibing.factory.simple_factory.service.IFreeGoods; +import com.mashibing.factory.simple_factory.service.impl.SmallGiftFreeGoods; + +/** + * 生产小礼品发放接口-具体工厂 + * @author spikeCong + * @date 2022/9/9 + **/ +public class SmallGiftFreeGoodsFactory implements FreeGoodsFactory { + + @Override + public IFreeGoods getInstance() { + return new SmallGiftFreeGoods(); + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/controller/DeliverController.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/controller/DeliverController.java new file mode 100644 index 0000000..c9f7841 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/controller/DeliverController.java @@ -0,0 +1,56 @@ +package com.mashibing.factory.schemes01.controller; + +import com.mashibing.factory.schemes01.entity.AwardInfo; +import com.mashibing.factory.schemes01.entity.DiscountResult; +import com.mashibing.factory.schemes01.entity.SmallGiftInfo; +import com.mashibing.factory.schemes01.service.DiscountService; +import com.mashibing.factory.schemes01.service.SmallGiftService; +import com.mashibing.factory.schemes01.service.YouKuMemberService; + +import java.util.UUID; + +/** + * 发放奖品接口 + * @author spikeCong + * @date 2022/9/8 + **/ +public class DeliverController { + + /** + * 按照类型的不同发放奖品 + * 奖品类型: 1 打折券 ,2 优酷会员 , 3 小礼品, 4 优惠券 + * @param awardInfo + */ + public void awardToUser(AwardInfo awardInfo){ + + if(awardInfo.getAwardTypes() == 1){ //打折券 + DiscountService discountService = new DiscountService(); + DiscountResult discountResult = discountService.sendDiscount(awardInfo.getUid(), awardInfo.getAwardNumber()); + System.out.println("打折券发放成功!" + discountResult ); + + }else if(awardInfo.getAwardTypes() == 2){//优酷会员 + + String phone = awardInfo.getExtMap().get("phone"); + + YouKuMemberService youKuMemberService = new YouKuMemberService(); + youKuMemberService.openMember(phone,awardInfo.getAwardNumber()); + System.out.println("优酷会员发放成功!"); + + } else if(awardInfo.getAwardTypes() == 3){//小礼品 + //封装收获人信息 + SmallGiftInfo info = new SmallGiftInfo(); + info.setUserPhone(awardInfo.getExtMap().get("phone")); + info.setUserName(awardInfo.getExtMap().get("username")); + info.setAddress(awardInfo.getExtMap().get("address")); + info.setOrderId(UUID.randomUUID().toString()); + + SmallGiftService smallGiftService = new SmallGiftService(); + Boolean aBoolean = smallGiftService.giveSmallGift(info); + if(aBoolean){ + System.out.println("小礼品发放成功!"); + } + } + + } + +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/AwardInfo.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/AwardInfo.java new file mode 100644 index 0000000..ecf0ecc --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/AwardInfo.java @@ -0,0 +1,64 @@ +package com.mashibing.factory.schemes01.entity; + +import java.util.Map; + +/** + * 获奖信息实体类 + * @author spikeCong + * @date 2022/9/8 + **/ +public class AwardInfo { + + private String uid; //用户id + + private Integer awardTypes; //奖品类型: 1 打折券 ,2 优酷会员 , 3 小礼品 + + private String awardNumber; //奖品编号 + + private Map extMap; //额外信息 + + public AwardInfo() { + } + + @Override + public String toString() { + return "AwardInfo{" + + "uid='" + uid + '\'' + + ", awardTypes=" + awardTypes + + ", awardNumber='" + awardNumber + '\'' + + ", extMap=" + extMap + + '}'; + } + + public String getUid() { + return uid; + } + + public void setUid(String uid) { + this.uid = uid; + } + + public Integer getAwardTypes() { + return awardTypes; + } + + public void setAwardTypes(Integer awardTypes) { + this.awardTypes = awardTypes; + } + + public String getAwardNumber() { + return awardNumber; + } + + public void setAwardNumber(String awardNumber) { + this.awardNumber = awardNumber; + } + + public Map getExtMap() { + return extMap; + } + + public void setExtMap(Map extMap) { + this.extMap = extMap; + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/DiscountInfo.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/DiscountInfo.java new file mode 100644 index 0000000..1d8c99c --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/DiscountInfo.java @@ -0,0 +1,11 @@ +package com.mashibing.factory.schemes01.entity; + +/** + * 打折券信息实体类 + * @author spikeCong + * @date 2022/9/8 + **/ +public class DiscountInfo { + + //属性信息省略...... +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/DiscountResult.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/DiscountResult.java new file mode 100644 index 0000000..557804d --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/DiscountResult.java @@ -0,0 +1,35 @@ +package com.mashibing.factory.schemes01.entity; + +/** + * 打折券响应信息封装实体类 + * @author spikeCong + * @date 2022/9/8 + **/ +public class DiscountResult { + + private String status; //状态码 + + private String message; //信息 + + + public DiscountResult(String status, String message) { + this.status = status; + this.message = message; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/SmallGiftInfo.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/SmallGiftInfo.java new file mode 100644 index 0000000..678a4b0 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/SmallGiftInfo.java @@ -0,0 +1,59 @@ +package com.mashibing.factory.schemes01.entity; + +/** + * 小礼品实体类 + * @author spikeCong + * @date 2022/9/8 + **/ +public class SmallGiftInfo { + + private String userName; + + private String userPhone; + + private String orderId; + + private String address; + + @Override + public String toString() { + return "SmallGiftInfo{" + + "userName='" + userName + '\'' + + ", userPhone='" + userPhone + '\'' + + ", orderId='" + orderId + '\'' + + ", address='" + address + '\'' + + '}'; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getUserPhone() { + return userPhone; + } + + public void setUserPhone(String userPhone) { + this.userPhone = userPhone; + } + + public String getOrderId() { + return orderId; + } + + public void setOrderId(String orderId) { + this.orderId = orderId; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/YouKuMember.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/YouKuMember.java new file mode 100644 index 0000000..3ce0023 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/entity/YouKuMember.java @@ -0,0 +1,12 @@ +package com.mashibing.factory.schemes01.entity; + +/** + * 优酷会员实体类 + * @author spikeCong + * @date 2022/9/8 + **/ +public class YouKuMember { + + //属性信息省略 + +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/service/DiscountService.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/service/DiscountService.java new file mode 100644 index 0000000..cca069e --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/service/DiscountService.java @@ -0,0 +1,18 @@ +package com.mashibing.factory.schemes01.service; + +import com.mashibing.factory.schemes01.entity.DiscountResult; + +/** + * 打折券服务 + * @author spikeCong + * @date 2022/9/8 + **/ +public class DiscountService { + + public DiscountResult sendDiscount(String uid, String awardNumber){ + + System.out.println("向用户发送一张打折券: " + uid + " , " + awardNumber); + return new DiscountResult("200","发放打折券成功!"); + } + +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/service/SmallGiftService.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/service/SmallGiftService.java new file mode 100644 index 0000000..64a582c --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/service/SmallGiftService.java @@ -0,0 +1,18 @@ +package com.mashibing.factory.schemes01.service; + +import com.alibaba.fastjson.JSON; +import com.mashibing.factory.schemes01.entity.SmallGiftInfo; + +/** + * 小礼品服务 + * @author spikeCong + * @date 2022/9/8 + **/ +public class SmallGiftService { + + public Boolean giveSmallGift(SmallGiftInfo smallGiftInfo){ + + System.out.println("小礼品已发送,获奖用户注意查收! " + JSON.toJSON(smallGiftInfo)); + return true; + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/service/YouKuMemberService.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/service/YouKuMemberService.java new file mode 100644 index 0000000..38685b7 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/schemes01/service/YouKuMemberService.java @@ -0,0 +1,15 @@ +package com.mashibing.factory.schemes01.service; + +/** + * 优酷会员 + * @author spikeCong + * @date 2022/9/8 + **/ +public class YouKuMemberService { + + public void openMember(String bindMobile, String awardNumber){ + + System.out.println("发放优酷会员: " + bindMobile + " , " + awardNumber); + } + +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/controller/DeliverController.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/controller/DeliverController.java new file mode 100644 index 0000000..ecf85c4 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/controller/DeliverController.java @@ -0,0 +1,28 @@ +package com.mashibing.factory.simple_factory.controller; + +import com.mashibing.factory.simple_factory.entity.AwardInfo; +import com.mashibing.factory.simple_factory.entity.ResponseResult; +import com.mashibing.factory.simple_factory.factory.FreeGoodsFactory; +import com.mashibing.factory.simple_factory.service.IFreeGoods; + +/** + * 发放奖品接口 + * @author spikeCong + * @date 2022/9/9 + **/ +public class DeliverController { + + //发放奖品 + public ResponseResult awardToUser(AwardInfo awardInfo){ + + try { + IFreeGoods freeGoods = FreeGoodsFactory.getInstance(awardInfo.getAwardTypes()); + ResponseResult responseResult = freeGoods.sendFreeGoods(awardInfo); + return responseResult; + } catch (Exception e) { + e.printStackTrace(); + return new ResponseResult("201","奖品发放失败!"); + } + } + +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/entity/AwardInfo.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/entity/AwardInfo.java new file mode 100644 index 0000000..851d07c --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/entity/AwardInfo.java @@ -0,0 +1,64 @@ +package com.mashibing.factory.simple_factory.entity; + +import java.util.Map; + +/** + * 获奖信息实体类 + * @author spikeCong + * @date 2022/9/8 + **/ +public class AwardInfo { + + private String uid; //用户id + + private Integer awardTypes; //奖品类型: 1 打折券 ,2 优酷会员 , 3 小礼品 + + private String awardNumber; //奖品编号 + + private Map extMap; //额外信息 + + public AwardInfo() { + } + + @Override + public String toString() { + return "AwardInfo{" + + "uid='" + uid + '\'' + + ", awardTypes=" + awardTypes + + ", awardNumber='" + awardNumber + '\'' + + ", extMap=" + extMap + + '}'; + } + + public String getUid() { + return uid; + } + + public void setUid(String uid) { + this.uid = uid; + } + + public Integer getAwardTypes() { + return awardTypes; + } + + public void setAwardTypes(Integer awardTypes) { + this.awardTypes = awardTypes; + } + + public String getAwardNumber() { + return awardNumber; + } + + public void setAwardNumber(String awardNumber) { + this.awardNumber = awardNumber; + } + + public Map getExtMap() { + return extMap; + } + + public void setExtMap(Map extMap) { + this.extMap = extMap; + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/entity/ResponseResult.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/entity/ResponseResult.java new file mode 100644 index 0000000..799566e --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/entity/ResponseResult.java @@ -0,0 +1,59 @@ +package com.mashibing.factory.simple_factory.entity; + +/** + * 响应信息封装实体类 + * @author spikeCong + * @date 2022/9/8 + **/ +public class ResponseResult { + + private String status; //状态码 + + private String message; //信息 + + private Object data; //数据 + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public ResponseResult(String status, String message, Object data) { + this.status = status; + this.message = message; + this.data = data; + } + + public ResponseResult(String status, String message) { + this.status = status; + this.message = message; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public String toString() { + return "ResponseResult{" + + "status='" + status + '\'' + + ", message='" + message + '\'' + + ", data=" + data + + '}'; + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/entity/SmallGiftInfo.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/entity/SmallGiftInfo.java new file mode 100644 index 0000000..3fd87d4 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/entity/SmallGiftInfo.java @@ -0,0 +1,59 @@ +package com.mashibing.factory.simple_factory.entity; + +/** + * 小礼品实体类 + * @author spikeCong + * @date 2022/9/8 + **/ +public class SmallGiftInfo { + + private String userName; + + private String userPhone; + + private String orderId; + + private String address; + + @Override + public String toString() { + return "SmallGiftInfo{" + + "userName='" + userName + '\'' + + ", userPhone='" + userPhone + '\'' + + ", orderId='" + orderId + '\'' + + ", address='" + address + '\'' + + '}'; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getUserPhone() { + return userPhone; + } + + public void setUserPhone(String userPhone) { + this.userPhone = userPhone; + } + + public String getOrderId() { + return orderId; + } + + public void setOrderId(String orderId) { + this.orderId = orderId; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/factory/FreeGoodsFactory.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/factory/FreeGoodsFactory.java new file mode 100644 index 0000000..b60d35c --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/factory/FreeGoodsFactory.java @@ -0,0 +1,32 @@ +package com.mashibing.factory.simple_factory.factory; + +import com.mashibing.factory.simple_factory.service.IFreeGoods; +import com.mashibing.factory.simple_factory.service.impl.DiscountFreeGoods; +import com.mashibing.factory.simple_factory.service.impl.SmallGiftFreeGoods; +import com.mashibing.factory.simple_factory.service.impl.YouKuMemberFreeGoods; + +/** + * 具体工厂: 生成免费商品 + * @author spikeCong + * @date 2022/9/9 + **/ +public class FreeGoodsFactory { + + public static IFreeGoods getInstance(Integer awardType){ + + IFreeGoods iFreeGoods = null; + + if(awardType == 1){ //打折券 + + iFreeGoods = new DiscountFreeGoods(); + }else if(awardType == 2){ //优酷会员 + + iFreeGoods = new YouKuMemberFreeGoods(); + }else if(awardType == 3){ //小礼品 + + iFreeGoods = new SmallGiftFreeGoods(); + } + + return iFreeGoods; + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/service/IFreeGoods.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/service/IFreeGoods.java new file mode 100644 index 0000000..bf9e4ac --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/service/IFreeGoods.java @@ -0,0 +1,15 @@ +package com.mashibing.factory.simple_factory.service; + +import com.mashibing.factory.simple_factory.entity.AwardInfo; +import com.mashibing.factory.simple_factory.entity.ResponseResult; + +/** + * 免费商品发放接口 + * @author spikeCong + * @date 2022/9/8 + **/ +public interface IFreeGoods { + + ResponseResult sendFreeGoods(AwardInfo awardInfo); + +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/service/impl/DiscountFreeGoods.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/service/impl/DiscountFreeGoods.java new file mode 100644 index 0000000..4207587 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/service/impl/DiscountFreeGoods.java @@ -0,0 +1,20 @@ +package com.mashibing.factory.simple_factory.service.impl; + +import com.mashibing.factory.simple_factory.entity.AwardInfo; +import com.mashibing.factory.simple_factory.entity.ResponseResult; +import com.mashibing.factory.simple_factory.service.IFreeGoods; + +/** + * 模拟打折券服务 + * @author spikeCong + * @date 2022/9/8 + **/ +public class DiscountFreeGoods implements IFreeGoods { + + @Override + public ResponseResult sendFreeGoods(AwardInfo awardInfo) { + + System.out.println("向用户发放一张打折券: " + awardInfo.getUid() + " , " + awardInfo.getAwardNumber()); + return new ResponseResult("200","打折券发放成功!"); + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/service/impl/SmallGiftFreeGoods.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/service/impl/SmallGiftFreeGoods.java new file mode 100644 index 0000000..152b18e --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/service/impl/SmallGiftFreeGoods.java @@ -0,0 +1,30 @@ +package com.mashibing.factory.simple_factory.service.impl; + +import com.alibaba.fastjson.JSON; +import com.mashibing.factory.simple_factory.entity.AwardInfo; +import com.mashibing.factory.simple_factory.entity.ResponseResult; +import com.mashibing.factory.simple_factory.entity.SmallGiftInfo; +import com.mashibing.factory.simple_factory.service.IFreeGoods; + +import java.util.UUID; + +/** + * 小礼品发放服务 + * @author spikeCong + * @date 2022/9/8 + **/ +public class SmallGiftFreeGoods implements IFreeGoods { + + @Override + public ResponseResult sendFreeGoods(AwardInfo awardInfo) { + + SmallGiftInfo smallGiftInfo = new SmallGiftInfo(); + smallGiftInfo.setUserPhone(awardInfo.getExtMap().get("phone")); + smallGiftInfo.setUserName(awardInfo.getExtMap().get("username")); + smallGiftInfo.setAddress(awardInfo.getExtMap().get("address")); + smallGiftInfo.setOrderId(UUID.randomUUID().toString()); + + System.out.println("小礼品发放成,请注意查收: " + JSON.toJSON(smallGiftInfo)); + return new ResponseResult("200","小礼品发送成功",smallGiftInfo); + } +} diff --git a/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/service/impl/YouKuMemberFreeGoods.java b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/service/impl/YouKuMemberFreeGoods.java new file mode 100644 index 0000000..1e46f55 --- /dev/null +++ b/msb-factory-pattern-02/src/main/java/com/mashibing/factory/simple_factory/service/impl/YouKuMemberFreeGoods.java @@ -0,0 +1,21 @@ +package com.mashibing.factory.simple_factory.service.impl; + +import com.mashibing.factory.simple_factory.entity.AwardInfo; +import com.mashibing.factory.simple_factory.entity.ResponseResult; +import com.mashibing.factory.simple_factory.service.IFreeGoods; + +/** + * 优酷 会员服务 + * @author spikeCong + * @date 2022/9/8 + **/ +public class YouKuMemberFreeGoods implements IFreeGoods { + + @Override + public ResponseResult sendFreeGoods(AwardInfo awardInfo) { + + String phone = awardInfo.getExtMap().get("phone"); + System.out.println("发放优酷会员成功,绑定手机号: " + phone); + return new ResponseResult("200","优酷会员发放成功!"); + } +} diff --git a/msb-factory-pattern-02/src/test/java/com/mashibing/factory/TestApi01.java b/msb-factory-pattern-02/src/test/java/com/mashibing/factory/TestApi01.java new file mode 100644 index 0000000..4e1b7f4 --- /dev/null +++ b/msb-factory-pattern-02/src/test/java/com/mashibing/factory/TestApi01.java @@ -0,0 +1,62 @@ +package com.mashibing.factory; + + +import com.mashibing.factory.schemes01.controller.DeliverController; +import com.mashibing.factory.schemes01.entity.AwardInfo; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author spikeCong + * @date 2022/9/8 + **/ +public class TestApi01 { + + DeliverController deliverController = new DeliverController(); + + //测试发放奖品接口 + @Test + public void test01(){ + + //1. 发放打折券优惠 + AwardInfo info1 = new AwardInfo(); + info1.setUid("1001"); + info1.setAwardTypes(1); + info1.setAwardNumber("DEL12345"); + + deliverController.awardToUser(info1); + + } + + @Test + public void test02(){ + //2. 发放优酷会员 + AwardInfo info2 = new AwardInfo(); + info2.setUid("1002"); + info2.setAwardTypes(2); + info2.setAwardNumber("DW12345"); + Map map = new HashMap<>(); + map.put("phone","13512341234"); + info2.setExtMap(map); + + deliverController.awardToUser(info2); + } + + @Test + public void test03(){ + //2. 发放小礼品 + AwardInfo info3 = new AwardInfo(); + info3.setUid("1003"); + info3.setAwardTypes(3); + info3.setAwardNumber("SM12345"); + Map map2 = new HashMap<>(); + map2.put("username","大远"); + map2.put("phone","13512341234"); + map2.put("address","北京天安门"); + info3.setExtMap(map2); + + deliverController.awardToUser(info3); + } +} diff --git a/msb-factory-pattern-02/src/test/java/com/mashibing/factory/TestApi02.java b/msb-factory-pattern-02/src/test/java/com/mashibing/factory/TestApi02.java new file mode 100644 index 0000000..12e989c --- /dev/null +++ b/msb-factory-pattern-02/src/test/java/com/mashibing/factory/TestApi02.java @@ -0,0 +1,67 @@ +package com.mashibing.factory; + +import com.mashibing.factory.simple_factory.controller.DeliverController; +import com.mashibing.factory.simple_factory.entity.AwardInfo; +import com.mashibing.factory.simple_factory.entity.ResponseResult; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author spikeCong + * @date 2022/9/9 + **/ +public class TestApi02 { + + DeliverController deliverController = new DeliverController(); + + @Test + public void test01(){ + + //1. 发放打折券优惠 + AwardInfo info1 = new AwardInfo(); + info1.setUid("1001"); + info1.setAwardTypes(1); + info1.setAwardNumber("DEL12345"); + + ResponseResult result = deliverController.awardToUser(info1); + System.out.println(result); + + } + + @Test + public void test02(){ + + //2. 发放优酷会员 + AwardInfo info2 = new AwardInfo(); + info2.setUid("1002"); + info2.setAwardTypes(2); + info2.setAwardNumber("DW12345"); + Map map = new HashMap<>(); + map.put("phone","13512341234"); + info2.setExtMap(map); + + ResponseResult result1 = deliverController.awardToUser(info2); + System.out.println(result1); + + } + + @Test + public void test03(){ + + //3. 发放小礼品 + AwardInfo info3 = new AwardInfo(); + info3.setUid("1003"); + info3.setAwardTypes(3); + info3.setAwardNumber("SM12345"); + Map map2 = new HashMap<>(); + map2.put("username","大远"); + map2.put("phone","13512341234"); + map2.put("address","北京天安门"); + info3.setExtMap(map2); + + ResponseResult result2 = deliverController.awardToUser(info3); + System.out.println(result2); + } +} diff --git a/msb-factory-pattern-02/src/test/java/com/mashibing/factory/TestApi03.java b/msb-factory-pattern-02/src/test/java/com/mashibing/factory/TestApi03.java new file mode 100644 index 0000000..9debc81 --- /dev/null +++ b/msb-factory-pattern-02/src/test/java/com/mashibing/factory/TestApi03.java @@ -0,0 +1,32 @@ +package com.mashibing.factory; + +import com.mashibing.factory.factory_method.controller.DeliverController; +import com.mashibing.factory.simple_factory.entity.AwardInfo; +import com.mashibing.factory.simple_factory.entity.ResponseResult; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author spikeCong + * @date 2022/9/9 + **/ +public class TestApi03 { + + DeliverController deliverController = new DeliverController(); + + @Test + public void test01(){ + + //1. 发放打折券优惠 + AwardInfo info1 = new AwardInfo(); + info1.setUid("1001"); + info1.setAwardTypes(1); + info1.setAwardNumber("DEL12345"); + + ResponseResult result = deliverController.awardToUser(info1); + System.out.println(result); + } + +} diff --git a/msb-factory-pattern-02/target/classes/META-INF/msb-factory-pattern-02.kotlin_module b/msb-factory-pattern-02/target/classes/META-INF/msb-factory-pattern-02.kotlin_module new file mode 100644 index 0000000..a49347a Binary files /dev/null and b/msb-factory-pattern-02/target/classes/META-INF/msb-factory-pattern-02.kotlin_module differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/Client.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/Client.class new file mode 100644 index 0000000..343c303 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/Client.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/factory/AppliancesFactory.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/factory/AppliancesFactory.class new file mode 100644 index 0000000..a2a0654 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/factory/AppliancesFactory.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/factory/HairFactory.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/factory/HairFactory.class new file mode 100644 index 0000000..b6dde89 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/factory/HairFactory.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/factory/HisenseFactory.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/factory/HisenseFactory.class new file mode 100644 index 0000000..482a3fa Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/factory/HisenseFactory.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/AbstractFreezer.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/AbstractFreezer.class new file mode 100644 index 0000000..9ba3337 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/AbstractFreezer.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/AbstractTV.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/AbstractTV.class new file mode 100644 index 0000000..3d9ebac Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/AbstractTV.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/HairFreezer.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/HairFreezer.class new file mode 100644 index 0000000..c7677bd Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/HairFreezer.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/HairTV.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/HairTV.class new file mode 100644 index 0000000..efc62bc Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/HairTV.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/HisenseFreezer.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/HisenseFreezer.class new file mode 100644 index 0000000..beac9c6 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/HisenseFreezer.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/HisenseTV.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/HisenseTV.class new file mode 100644 index 0000000..d9f2626 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/abstract_factory/product/HisenseTV.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/controller/DeliverController.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/controller/DeliverController.class new file mode 100644 index 0000000..51bb3a6 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/controller/DeliverController.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/factory/FreeGoodsFactory.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/factory/FreeGoodsFactory.class new file mode 100644 index 0000000..4d132bc Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/factory/FreeGoodsFactory.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/factory/FreeGoodsFactoryMap.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/factory/FreeGoodsFactoryMap.class new file mode 100644 index 0000000..a80d2ba Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/factory/FreeGoodsFactoryMap.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/factory/impl/DiscountFreeGoodsFactory.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/factory/impl/DiscountFreeGoodsFactory.class new file mode 100644 index 0000000..74f8628 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/factory/impl/DiscountFreeGoodsFactory.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/factory/impl/SmallGiftFreeGoodsFactory.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/factory/impl/SmallGiftFreeGoodsFactory.class new file mode 100644 index 0000000..a0d207d Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/factory_method/factory/impl/SmallGiftFreeGoodsFactory.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/controller/DeliverController.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/controller/DeliverController.class new file mode 100644 index 0000000..53a6660 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/controller/DeliverController.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/AwardInfo.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/AwardInfo.class new file mode 100644 index 0000000..e008cc7 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/AwardInfo.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/DiscountInfo.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/DiscountInfo.class new file mode 100644 index 0000000..a56bceb Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/DiscountInfo.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/DiscountResult.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/DiscountResult.class new file mode 100644 index 0000000..b533a70 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/DiscountResult.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/SmallGiftInfo.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/SmallGiftInfo.class new file mode 100644 index 0000000..32b5476 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/SmallGiftInfo.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/YouKuMember.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/YouKuMember.class new file mode 100644 index 0000000..bb20376 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/entity/YouKuMember.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/service/DiscountService.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/service/DiscountService.class new file mode 100644 index 0000000..4509d37 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/service/DiscountService.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/service/SmallGiftService.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/service/SmallGiftService.class new file mode 100644 index 0000000..5526ca6 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/service/SmallGiftService.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/service/YouKuMemberService.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/service/YouKuMemberService.class new file mode 100644 index 0000000..83821fd Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/schemes01/service/YouKuMemberService.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/controller/DeliverController.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/controller/DeliverController.class new file mode 100644 index 0000000..ae55705 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/controller/DeliverController.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/entity/AwardInfo.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/entity/AwardInfo.class new file mode 100644 index 0000000..00f52d7 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/entity/AwardInfo.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/entity/ResponseResult.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/entity/ResponseResult.class new file mode 100644 index 0000000..be638ff Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/entity/ResponseResult.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/entity/SmallGiftInfo.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/entity/SmallGiftInfo.class new file mode 100644 index 0000000..73cd986 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/entity/SmallGiftInfo.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/factory/FreeGoodsFactory.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/factory/FreeGoodsFactory.class new file mode 100644 index 0000000..ec653f4 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/factory/FreeGoodsFactory.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/service/IFreeGoods.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/service/IFreeGoods.class new file mode 100644 index 0000000..fea504b Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/service/IFreeGoods.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/service/impl/DiscountFreeGoods.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/service/impl/DiscountFreeGoods.class new file mode 100644 index 0000000..c49aa15 Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/service/impl/DiscountFreeGoods.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/service/impl/SmallGiftFreeGoods.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/service/impl/SmallGiftFreeGoods.class new file mode 100644 index 0000000..c33f16d Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/service/impl/SmallGiftFreeGoods.class differ diff --git a/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/service/impl/YouKuMemberFreeGoods.class b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/service/impl/YouKuMemberFreeGoods.class new file mode 100644 index 0000000..33c561a Binary files /dev/null and b/msb-factory-pattern-02/target/classes/com/mashibing/factory/simple_factory/service/impl/YouKuMemberFreeGoods.class differ diff --git a/msb-factory-pattern-02/target/test-classes/com/mashibing/factory/TestApi01.class b/msb-factory-pattern-02/target/test-classes/com/mashibing/factory/TestApi01.class new file mode 100644 index 0000000..0c3f3a0 Binary files /dev/null and b/msb-factory-pattern-02/target/test-classes/com/mashibing/factory/TestApi01.class differ diff --git a/msb-factory-pattern-02/target/test-classes/com/mashibing/factory/TestApi02.class b/msb-factory-pattern-02/target/test-classes/com/mashibing/factory/TestApi02.class new file mode 100644 index 0000000..d5fd779 Binary files /dev/null and b/msb-factory-pattern-02/target/test-classes/com/mashibing/factory/TestApi02.class differ diff --git a/msb-factory-pattern-02/target/test-classes/com/mashibing/factory/TestApi03.class b/msb-factory-pattern-02/target/test-classes/com/mashibing/factory/TestApi03.class new file mode 100644 index 0000000..36e722e Binary files /dev/null and b/msb-factory-pattern-02/target/test-classes/com/mashibing/factory/TestApi03.class differ diff --git a/msb-flyweight-pattern-11/msb-flyweight-pattern-11.iml b/msb-flyweight-pattern-11/msb-flyweight-pattern-11.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-flyweight-pattern-11/msb-flyweight-pattern-11.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-flyweight-pattern-11/pom.xml b/msb-flyweight-pattern-11/pom.xml new file mode 100644 index 0000000..7c3cc10 --- /dev/null +++ b/msb-flyweight-pattern-11/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.mashibing + msb-flyweight-pattern-11 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/Test1.java b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/Test1.java new file mode 100644 index 0000000..bb8b5f4 --- /dev/null +++ b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/Test1.java @@ -0,0 +1,21 @@ +package com.mashibing.flyweight; + +/** + * @author spikeCong + * @date 2022/10/10 + **/ +public class Test1 { + + public static void main(String[] args) { + Integer i1 = 127; + Integer i2 = 127; + + System.out.println("i1和i2对象是否是同一个对象?" + (i1 == i2)); + + Integer i3 = 128; + Integer i4 = 128; + + System.out.println("i3和i4对象是否是同一个对象?" + (i3 == i4)); + } + +} diff --git a/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/Client.java b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/Client.java new file mode 100644 index 0000000..1a65a2d --- /dev/null +++ b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/Client.java @@ -0,0 +1,27 @@ +package com.mashibing.flyweight.example01; + +/** + * @author spikeCong + * @date 2022/10/10 + **/ +public class Client { + + public static void main(String[] args) { + + //获取工厂对象 + FlyweightFactory factory = new FlyweightFactory(); + + //通过工厂对象获取共享的享元对象 + Flyweight a1 = factory.getFlyweight("A"); + a1.operation("a1ExState"); + + Flyweight a2 = factory.getFlyweight("A"); + a2.operation("a2ExState"); + System.out.println(a1 == a2); + + //获取非共享的享元对象 + UnsharedFlyweight u1 = new UnsharedFlyweight("A"); + UnsharedFlyweight u2 = new UnsharedFlyweight("A"); + System.out.println(u1 == u2); + } +} diff --git a/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/ConcreteFlyweight.java b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/ConcreteFlyweight.java new file mode 100644 index 0000000..50e949c --- /dev/null +++ b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/ConcreteFlyweight.java @@ -0,0 +1,28 @@ +package com.mashibing.flyweight.example01; + +/** + * 可共享的-具体享元类 + * 注意: 在具体享元类中,需要将内部状态和外部状态分开处理. + * @author spikeCong + * @date 2022/10/10 + **/ +public class ConcreteFlyweight extends Flyweight{ + + //内部状态 : inState作为一个成员变量,同一个享元对象的内部状态是一致的. + private String inState; + + public ConcreteFlyweight(String inState) { + this.inState = inState; + } + + + /** + * 外部状态在使用的时候,通常是有外部设置,不保存在享元对象中,即使是同一个对象 + * @param state + */ + @Override + public void operation(String state) { + + System.out.println("=== 享元对象的内部状态: " + inState + ",外部状态: " + state ); + } +} diff --git a/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/Flyweight.java b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/Flyweight.java new file mode 100644 index 0000000..becd639 --- /dev/null +++ b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/Flyweight.java @@ -0,0 +1,11 @@ +package com.mashibing.flyweight.example01; + +/** + * 抽象享元类 + * @author spikeCong + * @date 2022/10/10 + **/ +public abstract class Flyweight { + + public abstract void operation(String state); +} diff --git a/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/FlyweightFactory.java b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/FlyweightFactory.java new file mode 100644 index 0000000..4545214 --- /dev/null +++ b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/FlyweightFactory.java @@ -0,0 +1,42 @@ +package com.mashibing.flyweight.example01; + +import java.util.HashMap; +import java.util.Map; + +/** + * 享元工厂类 + * 作用: 作为存储享元对象的享元池.用户获取享元对象时先从享元池中获取,有则返回,没有创建新的返回给用户, + * 并且在享元池中保存 + * @author spikeCong + * @date 2022/10/10 + **/ +public class FlyweightFactory { + + //定义一个Map集合用于存储享元对象,实现享元池 + private Map pool = new HashMap<>(); + + //实现享元对象之间的状态传递 + public FlyweightFactory() { + //添加对应的内部状态 + pool.put("A",new ConcreteFlyweight("A")); + pool.put("B",new ConcreteFlyweight("B")); + pool.put("C",new ConcreteFlyweight("C")); + } + + //根据内部状态进行查找 + public Flyweight getFlyweight(String key){ + + //对象是否存在 + if(pool.containsKey(key)){ + System.out.println("=== 享元池中存在,直接复用,key : " + key); + return pool.get(key); + }else{ + //如果对象不存在,就创建一个添加到享元池,然后返回 + System.out.println("=== 享元池中不存在,创建并复用,key: " + key); + Flyweight fw = new ConcreteFlyweight(key); + pool.put(key,fw); + return fw; + } + } + +} diff --git a/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/UnsharedFlyweight.java b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/UnsharedFlyweight.java new file mode 100644 index 0000000..4b627f1 --- /dev/null +++ b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example01/UnsharedFlyweight.java @@ -0,0 +1,20 @@ +package com.mashibing.flyweight.example01; + +/** + * 非共享的具体享元类 + * @author gongspikeCong + * @date 2022/10/10 + **/ +public class UnsharedFlyweight extends Flyweight{ + + private String inState; + + public UnsharedFlyweight(String inState) { + this.inState = inState; + } + + @Override + public void operation(String state) { + System.out.println("=== 使用不共享对象,内部状态: " + inState + ",外部状态: " + state ); + } +} diff --git a/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/BlackGobang.java b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/BlackGobang.java new file mode 100644 index 0000000..bc977a0 --- /dev/null +++ b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/BlackGobang.java @@ -0,0 +1,15 @@ +package com.mashibing.flyweight.example02; + +/** + * 共享享元类-黑色棋子 + * @author spikeCong + * @date 2022/10/10 + **/ +public class BlackGobang extends GobangFlyweight { + + + @Override + public String getColor() { + return "黑色"; + } +} diff --git a/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/Client.java b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/Client.java new file mode 100644 index 0000000..be110e3 --- /dev/null +++ b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/Client.java @@ -0,0 +1,30 @@ +package com.mashibing.flyweight.example02; + +/** + * @author spikeCong + * @date 2022/10/10 + **/ +public class Client { + + public static void main(String[] args) { + + GobangFactory instance = GobangFactory.getInstance(); + + //获取3颗黑子 + GobangFlyweight b1 = instance.getGobang("b"); + GobangFlyweight b2 = instance.getGobang("b"); + GobangFlyweight b3 = instance.getGobang("b"); + System.out.println("判断黑子是否是同一对象: " + (b1 == b2)); + + GobangFlyweight w1 = instance.getGobang("w"); + GobangFlyweight w2 = instance.getGobang("w"); + System.out.println("判断白子是否是同一对象: " + (w1 == w2)); + + //显示棋子 + b1.display(); + b2.display(); + b3.display(); + w1.display(); + w2.display(); + } +} diff --git a/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/GobangFactory.java b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/GobangFactory.java new file mode 100644 index 0000000..1fd2776 --- /dev/null +++ b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/GobangFactory.java @@ -0,0 +1,43 @@ +package com.mashibing.flyweight.example02; + +import java.util.HashMap; +import java.util.Map; + +/** + * 享元工厂类-生产五子棋棋子 + * @author spikeCong + * @date 2022/10/10 + **/ +public class GobangFactory { + + + //享元池 + private static Map pool; + + //创建共享享元对象,设置对象的内部状态 + private GobangFactory() { + pool = new HashMap<>(); + GobangFlyweight black = new BlackGobang(); //黑子 + GobangFlyweight write = new WriteGobang(); //白子 + + pool.put("b",black); + pool.put("w",write); + } + + + //获取唯一享元工厂对象的方法 + public static GobangFactory getInstance(){ + return SingletonHolder.INSTANCE; + } + + //静态内部类 + private static class SingletonHolder{ + private static final GobangFactory INSTANCE = new GobangFactory(); + } + + //通过key获取集合中的享元对象 + public GobangFlyweight getGobang(String key){ + return pool.get(key); + } + +} diff --git a/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/GobangFlyweight.java b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/GobangFlyweight.java new file mode 100644 index 0000000..d32aaf6 --- /dev/null +++ b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/GobangFlyweight.java @@ -0,0 +1,16 @@ +package com.mashibing.flyweight.example02; + +/** + * 抽象享元类: 五子棋 + * @author spikeCong + * @date 2022/10/10 + **/ +public abstract class GobangFlyweight { + + public abstract String getColor(); + + public void display(){ + System.out.println("棋子颜色: " + this.getColor()); + } + +} diff --git a/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/WriteGobang.java b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/WriteGobang.java new file mode 100644 index 0000000..48993e8 --- /dev/null +++ b/msb-flyweight-pattern-11/src/main/java/com/mashibing/flyweight/example02/WriteGobang.java @@ -0,0 +1,15 @@ +package com.mashibing.flyweight.example02; + +/** + * 共享享元类-白色棋子 + * @author spikeCong + * @date 2022/10/10 + **/ +public class WriteGobang extends GobangFlyweight { + + + @Override + public String getColor() { + return "白色"; + } +} diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/Test1.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/Test1.class new file mode 100644 index 0000000..fa95c66 Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/Test1.class differ diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/Client.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/Client.class new file mode 100644 index 0000000..3947d7e Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/Client.class differ diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/ConcreteFlyweight.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/ConcreteFlyweight.class new file mode 100644 index 0000000..0d3787b Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/ConcreteFlyweight.class differ diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/Flyweight.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/Flyweight.class new file mode 100644 index 0000000..d42c9d0 Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/Flyweight.class differ diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/FlyweightFactory.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/FlyweightFactory.class new file mode 100644 index 0000000..ce38d32 Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/FlyweightFactory.class differ diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/UnsharedFlyweight.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/UnsharedFlyweight.class new file mode 100644 index 0000000..1ac9fb6 Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example01/UnsharedFlyweight.class differ diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/BlackGobang.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/BlackGobang.class new file mode 100644 index 0000000..c0ddb16 Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/BlackGobang.class differ diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/Client.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/Client.class new file mode 100644 index 0000000..1451b9b Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/Client.class differ diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/GobangFactory$1.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/GobangFactory$1.class new file mode 100644 index 0000000..bc2d557 Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/GobangFactory$1.class differ diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/GobangFactory$SingletonHolder.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/GobangFactory$SingletonHolder.class new file mode 100644 index 0000000..66811ab Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/GobangFactory$SingletonHolder.class differ diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/GobangFactory.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/GobangFactory.class new file mode 100644 index 0000000..eaa31ee Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/GobangFactory.class differ diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/GobangFlyweight.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/GobangFlyweight.class new file mode 100644 index 0000000..21dc975 Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/GobangFlyweight.class differ diff --git a/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/WriteGobang.class b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/WriteGobang.class new file mode 100644 index 0000000..9c5527e Binary files /dev/null and b/msb-flyweight-pattern-11/target/classes/com/mashibing/flyweight/example02/WriteGobang.class differ diff --git a/msb-interperter-pattern-21/target/classes/META-INF/msb-interperter-pattern-21.kotlin_module b/msb-interperter-pattern-21/target/classes/META-INF/msb-interperter-pattern-21.kotlin_module new file mode 100644 index 0000000..a49347a Binary files /dev/null and b/msb-interperter-pattern-21/target/classes/META-INF/msb-interperter-pattern-21.kotlin_module differ diff --git a/msb-interpreter-pattern-21/msb-interpreter-pattern-21.iml b/msb-interpreter-pattern-21/msb-interpreter-pattern-21.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-interpreter-pattern-21/msb-interpreter-pattern-21.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-interpreter-pattern-21/pom.xml b/msb-interpreter-pattern-21/pom.xml new file mode 100644 index 0000000..d6c80d3 --- /dev/null +++ b/msb-interpreter-pattern-21/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.mashibing + msb-interpreter-pattern-21 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example01/Client.java b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example01/Client.java new file mode 100644 index 0000000..8a5fee5 --- /dev/null +++ b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example01/Client.java @@ -0,0 +1,15 @@ +package com.mashibing.interpreter.example01; + +/** + * @author spikeCong + * @date 2022/10/21 + **/ +public class Client { + + public static void main(String[] args) { + + ExpressionInterpreter interpreter = new ExpressionInterpreter(); + long result = interpreter.interpret("6 8 3 2 4 + - + *"); + System.out.println("6 8 3 2 4 + - + * 表达式的结果为: "+result); + } +} diff --git a/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example01/ExpressionInterpreter.java b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example01/ExpressionInterpreter.java new file mode 100644 index 0000000..7684823 --- /dev/null +++ b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example01/ExpressionInterpreter.java @@ -0,0 +1,68 @@ +package com.mashibing.interpreter.example01; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * 表达式解释器 + * @author spikeCong + * @date 2022/10/21 + **/ +public class ExpressionInterpreter { + + //Deque双向队列 ,可以从队列两端增加或删除元素 + private Deque numbers = new LinkedList(); + + //接收表达式进行解析 + public long interpret(String expression){ + + //9 5 7 3 - + * + String[] elements = expression.split(" "); + + int length = elements.length; + + //获取表达式中的数字 + for (int i = 0; i < (length+1) / 2; ++i) { + //向集合的尾部添加元素 + numbers.addLast(Long.parseLong(elements[i])); + } + + //获取表达式中的符号,进行计算 + for (int i = (length+1) / 2; i < length; ++i) { + String operator = elements[i]; + //符号必须是 + - * / ,否则抛出异常 + boolean isValid = "+".equals(operator) || "-".equals(operator) || + "*".equals(operator) || "/".equals(operator); + + if(!isValid){ + throw new RuntimeException("无效表达式!" + expression); + } + + //获取集合中的数字,移除集合中的第一个元素,并且返回被移除的值 + long number1 = numbers.pollFirst(); //数字 + long number2 = numbers.pollFirst(); //数字 + + long result = 0; + if("+".equals(operator)){ + result = number1 + number2; + }else if("-".equals(operator)){ + result = number1 - number2; + }else if("*".equals(operator)){ + result = number1 * number2; + }else if("/".equals(operator)){ + result = number1 / number2; + } + + //将运算结果添加到集合头部 + numbers.addFirst(result); + } + + //运算的最终结果是被保存在集合中的 + if(numbers.size() != 1){ + throw new RuntimeException("无效表达式!" + expression); + } + + //移除集合中的唯一的结果,并返回 + return numbers.pop(); + } +} diff --git a/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/Client.java b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/Client.java new file mode 100644 index 0000000..6bac5d2 --- /dev/null +++ b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/Client.java @@ -0,0 +1,15 @@ +package com.mashibing.interpreter.example02; + +/** + * @author spikeCong + * @date 2022/10/21 + **/ +public class Client { + + public static void main(String[] args) { + + ExpressionInterpreter e = new ExpressionInterpreter(); + long result = e.interpret("10 2 5 5 5 / - + *"); + System.out.println(result); + } +} diff --git a/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/DivExpression.java b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/DivExpression.java new file mode 100644 index 0000000..2791b4d --- /dev/null +++ b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/DivExpression.java @@ -0,0 +1,23 @@ +package com.mashibing.interpreter.example02; + +/** + * 除法法运算 + * @author spikeCong + * @date 2022/10/21 + **/ +public class DivExpression implements Expression { + + private Expression exp1; + + private Expression exp2; + + public DivExpression(Expression exp1, Expression exp2) { + this.exp1 = exp1; + this.exp2 = exp2; + } + + @Override + public long interpret() { + return exp1.interpret() / exp2.interpret(); + } +} diff --git a/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/Expression.java b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/Expression.java new file mode 100644 index 0000000..0d8e38f --- /dev/null +++ b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/Expression.java @@ -0,0 +1,11 @@ +package com.mashibing.interpreter.example02; + +/** + * 表达式接口 + * @author spikeCong + * @date 2022/10/21 + **/ +public interface Expression { + + long interpret(); +} diff --git a/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/ExpressionInterpreter.java b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/ExpressionInterpreter.java new file mode 100644 index 0000000..859f286 --- /dev/null +++ b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/ExpressionInterpreter.java @@ -0,0 +1,62 @@ +package com.mashibing.interpreter.example02; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * 表达式解释器类 + * @author spikeCong + * @date 2022/10/21 + **/ +public class ExpressionInterpreter { + + private Deque numbers = new LinkedList<>(); + + public long interpret(String expression){ + + String[] elements = expression.split(" "); + + int length = elements.length; + + for (int i = 0; i < (length + 1) /2; ++i) { + numbers.addLast(new NumExpression(elements[i])); + } + + for (int i = (length + 1) / 2; i < length; ++i) { + + String operator = elements[i]; + + //符号必须是 + - * / ,否则抛出异常 + boolean isValid = "+".equals(operator) || "-".equals(operator) || + "*".equals(operator) || "/".equals(operator); + + if(!isValid){ + throw new RuntimeException("无效表达式!" + expression); + } + + Expression exp1 = numbers.pollFirst(); + Expression exp2 = numbers.pollFirst(); + + Expression result = null; + if("+".equals(operator)){ + result = new PluExpression(exp1,exp2); + }else if("-".equals(operator)){ + result = new SubExpression(exp1,exp2); + }else if("*".equals(operator)){ + result = new MulExpression(exp1,exp2); + }else if("/".equals(operator)){ + result = new DivExpression(exp1,exp2); + } + + long num = result.interpret(); + numbers.addFirst(new NumExpression(num)); + } + + if(numbers.size() != 1){ + throw new RuntimeException("无效的表达式!" + expression); + } + + return numbers.pop().interpret(); + } + +} diff --git a/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/MulExpression.java b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/MulExpression.java new file mode 100644 index 0000000..1ea4226 --- /dev/null +++ b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/MulExpression.java @@ -0,0 +1,23 @@ +package com.mashibing.interpreter.example02; + +/** + * 乘法法运算 + * @author spikeCong + * @date 2022/10/21 + **/ +public class MulExpression implements Expression { + + private Expression exp1; + + private Expression exp2; + + public MulExpression(Expression exp1, Expression exp2) { + this.exp1 = exp1; + this.exp2 = exp2; + } + + @Override + public long interpret() { + return exp1.interpret() * exp2.interpret(); + } +} diff --git a/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/NumExpression.java b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/NumExpression.java new file mode 100644 index 0000000..05ea397 --- /dev/null +++ b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/NumExpression.java @@ -0,0 +1,24 @@ +package com.mashibing.interpreter.example02; + +/** + * 数字表达式 + * @author spikeCong + * @date 2022/10/21 + **/ +public class NumExpression implements Expression{ + + private long number; + + public NumExpression(long number) { + this.number = number; + } + + public NumExpression(String number) { + this.number = Long.parseLong(number); + } + + @Override + public long interpret() { + return this.number; + } +} diff --git a/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/PluExpression.java b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/PluExpression.java new file mode 100644 index 0000000..022db0f --- /dev/null +++ b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/PluExpression.java @@ -0,0 +1,23 @@ +package com.mashibing.interpreter.example02; + +/** + * 加法运算 + * @author spikeCong + * @date 2022/10/21 + **/ +public class PluExpression implements Expression { + + private Expression exp1; + + private Expression exp2; + + public PluExpression(Expression exp1, Expression exp2) { + this.exp1 = exp1; + this.exp2 = exp2; + } + + @Override + public long interpret() { + return exp1.interpret() + exp2.interpret(); + } +} diff --git a/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/SubExpression.java b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/SubExpression.java new file mode 100644 index 0000000..46f6b3e --- /dev/null +++ b/msb-interpreter-pattern-21/src/main/java/com/mashibing/interpreter/example02/SubExpression.java @@ -0,0 +1,23 @@ +package com.mashibing.interpreter.example02; + +/** + * 减法运算 + * @author spikeCong + * @date 2022/10/21 + **/ +public class SubExpression implements Expression { + + private Expression exp1; + + private Expression exp2; + + public SubExpression(Expression exp1, Expression exp2) { + this.exp1 = exp1; + this.exp2 = exp2; + } + + @Override + public long interpret() { + return exp1.interpret() - exp2.interpret(); + } +} diff --git a/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example01/Client.class b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example01/Client.class new file mode 100644 index 0000000..6fb1a4b Binary files /dev/null and b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example01/Client.class differ diff --git a/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example01/ExpressionInterpreter.class b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example01/ExpressionInterpreter.class new file mode 100644 index 0000000..35083f3 Binary files /dev/null and b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example01/ExpressionInterpreter.class differ diff --git a/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/Client.class b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/Client.class new file mode 100644 index 0000000..6d282ac Binary files /dev/null and b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/Client.class differ diff --git a/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/DivExpression.class b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/DivExpression.class new file mode 100644 index 0000000..4b7645a Binary files /dev/null and b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/DivExpression.class differ diff --git a/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/Expression.class b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/Expression.class new file mode 100644 index 0000000..03431c4 Binary files /dev/null and b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/Expression.class differ diff --git a/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/ExpressionInterpreter.class b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/ExpressionInterpreter.class new file mode 100644 index 0000000..d60bb5a Binary files /dev/null and b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/ExpressionInterpreter.class differ diff --git a/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/MulExpression.class b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/MulExpression.class new file mode 100644 index 0000000..3d2c784 Binary files /dev/null and b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/MulExpression.class differ diff --git a/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/NumExpression.class b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/NumExpression.class new file mode 100644 index 0000000..5b37b4a Binary files /dev/null and b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/NumExpression.class differ diff --git a/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/PluExpression.class b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/PluExpression.class new file mode 100644 index 0000000..d2f5f17 Binary files /dev/null and b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/PluExpression.class differ diff --git a/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/SubExpression.class b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/SubExpression.class new file mode 100644 index 0000000..5abf51f Binary files /dev/null and b/msb-interpreter-pattern-21/target/classes/com/mashibing/interpreter/example02/SubExpression.class differ diff --git a/msb-mediator-pattern-22/msb-mediator-pattern-22.iml b/msb-mediator-pattern-22/msb-mediator-pattern-22.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-mediator-pattern-22/msb-mediator-pattern-22.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-mediator-pattern-22/pom.xml b/msb-mediator-pattern-22/pom.xml new file mode 100644 index 0000000..b0b5e55 --- /dev/null +++ b/msb-mediator-pattern-22/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.mashibing + msb-mediator-pattern-22 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/Client.java b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/Client.java new file mode 100644 index 0000000..ef2dfc1 --- /dev/null +++ b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/Client.java @@ -0,0 +1,21 @@ +package com.mashibing.mediator.example01; + +/** + * @author spikeCong + * @date 2022/10/21 + **/ +public class Client { + + public static void main(String[] args) { + + //创建中介者 + Mediator mediator = new MediatorImpl(); + + //创建同事对象 + Colleague c1 = new ConcreteColleagueA(mediator); + c1.exec("key-A"); + + Colleague c2 = new ConcreteColleagueB(mediator); + c2.exec("key-B"); + } +} diff --git a/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/Colleague.java b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/Colleague.java new file mode 100644 index 0000000..875ee68 --- /dev/null +++ b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/Colleague.java @@ -0,0 +1,22 @@ +package com.mashibing.mediator.example01; + +/** + * 抽象同事类 + * @author spikeCong + * @date 2022/10/21 + **/ +public abstract class Colleague { + + private Mediator mediator; + + public Colleague(Mediator mediator) { + this.mediator = mediator; + } + + public Mediator getMediator() { + return mediator; + } + + //同事间进行交互的抽象方法 + public abstract void exec(String key); +} diff --git a/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/ConcreteColleagueA.java b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/ConcreteColleagueA.java new file mode 100644 index 0000000..01a37c3 --- /dev/null +++ b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/ConcreteColleagueA.java @@ -0,0 +1,18 @@ +package com.mashibing.mediator.example01; + +/** + * 具体同事类 + * @author spikeCong + * @date 2022/10/21 + **/ +public class ConcreteColleagueA extends Colleague{ + public ConcreteColleagueA(Mediator mediator) { + super(mediator); + } + + @Override + public void exec(String key) { + System.out.println("=====在A同事中,通过中介者执行!"); + getMediator().apply(key); + } +} diff --git a/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/ConcreteColleagueB.java b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/ConcreteColleagueB.java new file mode 100644 index 0000000..680e69f --- /dev/null +++ b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/ConcreteColleagueB.java @@ -0,0 +1,18 @@ +package com.mashibing.mediator.example01; + +/** + * 具体同事类 + * @author spikeCong + * @date 2022/10/21 + **/ +public class ConcreteColleagueB extends Colleague{ + public ConcreteColleagueB(Mediator mediator) { + super(mediator); + } + + @Override + public void exec(String key) { + System.out.println("=====在B同事中,通过中介者执行!"); + getMediator().apply(key); + } +} diff --git a/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/Mediator.java b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/Mediator.java new file mode 100644 index 0000000..be5c42d --- /dev/null +++ b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/Mediator.java @@ -0,0 +1,13 @@ +package com.mashibing.mediator.example01; + +/** + * 抽象中介者 + * + * @author spikeCong + * @date 2022/10/21 + **/ +public interface Mediator { + + //处理同事对象注册与转发同事对象信息的方法 + void apply(String key); +} diff --git a/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/MediatorImpl.java b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/MediatorImpl.java new file mode 100644 index 0000000..ea1c096 --- /dev/null +++ b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example01/MediatorImpl.java @@ -0,0 +1,14 @@ +package com.mashibing.mediator.example01; + +/** + * 具体中介者 + * @author spikeCong + * @date 2022/10/21 + **/ +public class MediatorImpl implements Mediator { + + @Override + public void apply(String key) { + System.out.println("最终中介者执行的操作,key为" + key); + } +} diff --git a/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/Client.java b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/Client.java new file mode 100644 index 0000000..fa4bc8b --- /dev/null +++ b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/Client.java @@ -0,0 +1,30 @@ +package com.mashibing.mediator.example02; + +/** + * @author spikeCong + * @date 2022/10/21 + **/ +public class Client { + + public static void main(String[] args) { + + //中介机构 + MediatorStructure mediator = new MediatorStructure(); + + //房主 + HouseOwner houseOwner = new HouseOwner("张三", mediator); + + //租房者 + Tenant tenant = new Tenant("李四", mediator); + + //中介收集房主及租房者信息 + mediator.setHouseOwner(houseOwner); + mediator.setTenant(tenant); + + //租房人的需求 + tenant.contact("需要在天通苑附近找一个,两室一厅的房子一家人住,房租在5000~6000之间"); + + //房主的需求 + houseOwner.contact("出租一套天通苑地跌站附近的两室一厅,房租6000,可谈"); + } +} diff --git a/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/HouseOwner.java b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/HouseOwner.java new file mode 100644 index 0000000..6d962f5 --- /dev/null +++ b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/HouseOwner.java @@ -0,0 +1,23 @@ +package com.mashibing.mediator.example02; + +/** + * 具体同事类-房租拥有者 + * @author spikeCong + * @date 2022/10/21 + **/ +public class HouseOwner extends Person { + + public HouseOwner(String name, Mediator mediator) { + super(name, mediator); + } + + //与中介联系 + public void contact(String message){ + mediator.contact(message,this); + } + + //获取信息 + public void getMessage(String message){ + System.out.println("房主: " + name +",获取到的信息: " + message); + } +} diff --git a/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/Mediator.java b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/Mediator.java new file mode 100644 index 0000000..f8cf3fb --- /dev/null +++ b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/Mediator.java @@ -0,0 +1,12 @@ +package com.mashibing.mediator.example02; + +/** + * 抽象中介者 + * @author spikeCong + * @date 2022/10/21 + **/ +public abstract class Mediator { + + //创建联络方法 + public abstract void contact(String message, Person person); +} diff --git a/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/MediatorStructure.java b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/MediatorStructure.java new file mode 100644 index 0000000..2addd7a --- /dev/null +++ b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/MediatorStructure.java @@ -0,0 +1,43 @@ +package com.mashibing.mediator.example02; + +/** + * 具体的中介者-中介机构 + * @author spikeCong + * @date 2022/10/21 + **/ +public class MediatorStructure extends Mediator { + + //中介知晓 房租出租人和承租人的信息 + private HouseOwner houseOwner; //房主 + + private Tenant tenant; //租房者 + + public Tenant getTenant() { + return tenant; + } + + public void setTenant(Tenant tenant) { + this.tenant = tenant; + } + + public HouseOwner getHouseOwner() { + return houseOwner; + } + + public void setHouseOwner(HouseOwner houseOwner) { + this.houseOwner = houseOwner; + } + + + + @Override + public void contact(String message, Person person) { + if(person == houseOwner){ + //如果是房主,则租房者获得信息 + tenant.getMessage(message); + }else{ + //如果是租房者,则房租获得信息 + houseOwner.getMessage(message); + } + } +} diff --git a/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/Person.java b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/Person.java new file mode 100644 index 0000000..6e0c3f2 --- /dev/null +++ b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/Person.java @@ -0,0 +1,19 @@ +package com.mashibing.mediator.example02; + +/** + * 抽象同事类 + * @author spikeCong + * @date 2022/10/21 + **/ +public abstract class Person { + + protected String name; + + //持有中介者的引用 + protected Mediator mediator; + + public Person(String name, Mediator mediator) { + this.name = name; + this.mediator = mediator; + } +} diff --git a/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/Tenant.java b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/Tenant.java new file mode 100644 index 0000000..47b9783 --- /dev/null +++ b/msb-mediator-pattern-22/src/main/java/com/mashibing/mediator/example02/Tenant.java @@ -0,0 +1,21 @@ +package com.mashibing.mediator.example02; + +/** + * 具体同事类-承租人 + * @author spikeCong + * @date 2022/10/21 + **/ +public class Tenant extends Person { + + public Tenant(String name, Mediator mediator) { + super(name, mediator); + } + + public void contact(String message){ + mediator.contact(message,this); + } + + public void getMessage(String message){ + System.out.println("租房者"+name+",获取到的信息: " + message); + } +} diff --git a/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/Client.class b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/Client.class new file mode 100644 index 0000000..6db283d Binary files /dev/null and b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/Client.class differ diff --git a/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/Colleague.class b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/Colleague.class new file mode 100644 index 0000000..5f99d18 Binary files /dev/null and b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/Colleague.class differ diff --git a/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/ConcreteColleagueA.class b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/ConcreteColleagueA.class new file mode 100644 index 0000000..87b61ff Binary files /dev/null and b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/ConcreteColleagueA.class differ diff --git a/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/ConcreteColleagueB.class b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/ConcreteColleagueB.class new file mode 100644 index 0000000..081d2e8 Binary files /dev/null and b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/ConcreteColleagueB.class differ diff --git a/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/Mediator.class b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/Mediator.class new file mode 100644 index 0000000..389c11d Binary files /dev/null and b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/Mediator.class differ diff --git a/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/MediatorImpl.class b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/MediatorImpl.class new file mode 100644 index 0000000..00280d8 Binary files /dev/null and b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example01/MediatorImpl.class differ diff --git a/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/Client.class b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/Client.class new file mode 100644 index 0000000..c900ead Binary files /dev/null and b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/Client.class differ diff --git a/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/HouseOwner.class b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/HouseOwner.class new file mode 100644 index 0000000..d59c6b6 Binary files /dev/null and b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/HouseOwner.class differ diff --git a/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/Mediator.class b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/Mediator.class new file mode 100644 index 0000000..90fee30 Binary files /dev/null and b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/Mediator.class differ diff --git a/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/MediatorStructure.class b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/MediatorStructure.class new file mode 100644 index 0000000..0a88fbb Binary files /dev/null and b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/MediatorStructure.class differ diff --git a/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/Person.class b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/Person.class new file mode 100644 index 0000000..36156ec Binary files /dev/null and b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/Person.class differ diff --git a/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/Tenant.class b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/Tenant.class new file mode 100644 index 0000000..d98c7ff Binary files /dev/null and b/msb-mediator-pattern-22/target/classes/com/mashibing/mediator/example02/Tenant.class differ diff --git a/msb-memento-pattern-19/msb-memento-pattern-19.iml b/msb-memento-pattern-19/msb-memento-pattern-19.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-memento-pattern-19/msb-memento-pattern-19.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-memento-pattern-19/pom.xml b/msb-memento-pattern-19/pom.xml new file mode 100644 index 0000000..5096409 --- /dev/null +++ b/msb-memento-pattern-19/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.mashibing + msb-memento-pattern-19 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example01/Caretaker.java b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example01/Caretaker.java new file mode 100644 index 0000000..875185a --- /dev/null +++ b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example01/Caretaker.java @@ -0,0 +1,19 @@ +package com.mashibing.memento.example01; + +/** + * 负责人类-获取和保存备忘录对象 + * @author spikeCong + * @date 2022/10/19 + **/ +public class Caretaker { + + private Memento memento; + + public Memento getMemento() { + return memento; + } + + public void setMemento(Memento memento) { + this.memento = memento; + } +} diff --git a/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example01/Client.java b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example01/Client.java new file mode 100644 index 0000000..7f41df7 --- /dev/null +++ b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example01/Client.java @@ -0,0 +1,30 @@ +package com.mashibing.memento.example01; + +/** + * @author spikeCong + * @date 2022/10/19 + **/ +public class Client { + + public static void main(String[] args) { + + //创建发起人对象 + Originator o1 = new Originator(); + o1.setId("1"); + o1.setName("spike"); + o1.setPhone("13522777722"); + System.out.println("========"+o1); + + //创建负责人对象 + Caretaker caretaker = new Caretaker(); + caretaker.setMemento(o1.createMemento()); + + //修改 + o1.setName("update"); + System.out.println("========" + o1); + + //从负责人对象中获取备忘录对象,实现恢复操作 + o1.restoreMemento(caretaker.getMemento()); + System.out.println("========" + o1); + } +} diff --git a/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example01/Memento.java b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example01/Memento.java new file mode 100644 index 0000000..58d9848 --- /dev/null +++ b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example01/Memento.java @@ -0,0 +1,69 @@ +package com.mashibing.memento.example01; + +/** + * 备忘录角色 + * 访问权限为: 默认,在同包下可见(尽量保证只有发起者类可以访问备忘录类) + * @author spikeCong + * @date 2022/10/19 + **/ +class Memento { + + private String state = "从备份对象恢复原始对象"; + + private String id; + + private String name; + + private String phone; + + public Memento() { + } + + public Memento(String id, String name, String phone) { + this.id = id; + this.name = name; + this.phone = phone; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + @Override + public String toString() { + return "Memento{" + + "state='" + state + '\'' + + ", id='" + id + '\'' + + ", name='" + name + '\'' + + ", phone='" + phone + '\'' + + '}'; + } +} diff --git a/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example01/Originator.java b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example01/Originator.java new file mode 100644 index 0000000..46842bd --- /dev/null +++ b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example01/Originator.java @@ -0,0 +1,76 @@ +package com.mashibing.memento.example01; + +/** + * 发起人角色 + * @author spikeCong + * @date 2022/10/19 + **/ +public class Originator { + + private String state = "原始对象"; + + private String id; + + private String name; + + private String phone; + + public Originator() { + } + + //创建备忘录对象 + public Memento createMemento(){ + return new Memento(id,name,phone); + } + + //恢复对象 + public void restoreMemento(Memento m){ + + this.state = m.getState(); + this.id = m.getId(); + this.name = m.getName(); + this.phone = m.getPhone(); + } + + @Override + public String toString() { + return "Originator{" + + "state='" + state + '\'' + + ", id='" + id + '\'' + + ", name='" + name + '\'' + + ", phone='" + phone + '\'' + + '}'; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example02/MainApp.java b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example02/MainApp.java new file mode 100644 index 0000000..d5426c1 --- /dev/null +++ b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example02/MainApp.java @@ -0,0 +1,41 @@ +package com.mashibing.memento.example02; + +/** + * @author spikeCong + * @date 2022/10/19 + **/ +public class MainApp { + + public static void main(String[] args) throws InterruptedException { + + //创建玩家类,设置初始金币 + Player player = new Player(100); + + //创建备忘录对象 + Memento memento = player.createMemento(); + + for (int i = 0; i < 100; i++) { + + //显示扔骰子的次数 + System.out.println("第" + i+"次投掷!"); + + //显示当前玩家状态 + System.out.println("当前状态: " + player); + + //开启游戏 + player.yacht(); + System.out.println("玩家所持有的金币: " + player.getMoney() + " 元"); + + //复活操作 + if(player.getMoney() > memento.getMoney()){ + System.out.println("赚到金币,保存当前状态,继续游戏!"); + memento = player.createMemento(); //更新快照 + }else if(player.getMoney() < memento.getMoney() / 2){ + System.out.println("所持金币不多,将游戏恢复到初始状态!"); + player.restoreMemento(memento); + } + + Thread.sleep(1000); + } + } +} diff --git a/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example02/Memento.java b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example02/Memento.java new file mode 100644 index 0000000..9cc0b2b --- /dev/null +++ b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example02/Memento.java @@ -0,0 +1,37 @@ +package com.mashibing.memento.example02; + +import java.util.ArrayList; +import java.util.List; + +/** + * 备份玩家的状态 + * @author spikeCong + * @date 2022/10/19 + **/ +class Memento { + + private int money; //玩家获取的金币 + + ArrayList fruits; //玩家获取的水果 + + public Memento(int money) { + this.money = money; + this.fruits = new ArrayList(); + } + + //获取当前玩家的金币 + public int getMoney(){ + return money; + } + + //获取当前玩家的水果 + List getFruits(){ + return (List) fruits.clone(); + } + + //添加水果 + void addFruit(String fruit){ + fruits.add(fruit); + } + +} diff --git a/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example02/Player.java b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example02/Player.java new file mode 100644 index 0000000..707521f --- /dev/null +++ b/msb-memento-pattern-19/src/main/java/com/mashibing/memento/example02/Player.java @@ -0,0 +1,95 @@ +package com.mashibing.memento.example02; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +/** + * 玩家类 + * @author spikeCong + * @date 2022/10/19 + **/ +public class Player { + + private int money; //金币 + + private List fruits = new ArrayList<>(); //玩家获得的水果 + + private static String[] fruitsName ={ //表示水果种类的数组 + "苹果","葡萄","香蕉","橘子" + }; + + Random random = new Random(); + + public Player(int money) { + this.money = money; + } + + //获取当前所有金币 + public int getMoney(){ + return money; + } + + //获取一个水果 + public String getFruit(){ + String prefix = ""; + if(random.nextBoolean()){ + prefix = "好吃的"; + } + + //从数组中拿一个水果 + String f = fruitsName[random.nextInt(fruitsName.length)]; + return prefix + f; + } + + //掷骰子方法 + public void yacht(){ + + int dice = random.nextInt(6) + 1; //掷骰子 + + if(dice == 1){ + money += 100; + System.out.println("所持有的金币增加了..."); + }else if(dice == 2){ + money /= 2; + System.out.println("所持有的金币减少一半"); + }else if(dice == 6){ //获取水果 + String fruit = getFruit(); + System.out.println("获取了水果: " + fruit); + fruits.add(fruit); + }else{ + //其他结果 + System.out.println("无效数字,继续投掷!"); + } + } + + //拍摄快照 + public Memento createMemento(){ + Memento memento = new Memento(money); + for (String fruit : fruits) { + + //判断: 只保存 '好吃的'水果 + if(fruit.startsWith("好吃的")){ + memento.addFruit(fruit); + } + } + + return memento; + } + + + //撤销方法 + public void restoreMemento(Memento memento){ + this.money = memento.getMoney(); + this.fruits = memento.getFruits(); + } + + + @Override + public String toString() { + return "Player{" + + "money=" + money + + ", fruits=" + fruits + + '}'; + } +} diff --git a/msb-memento-pattern-19/target/classes/com/mashibing/memento/example01/Caretaker.class b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example01/Caretaker.class new file mode 100644 index 0000000..9b686c0 Binary files /dev/null and b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example01/Caretaker.class differ diff --git a/msb-memento-pattern-19/target/classes/com/mashibing/memento/example01/Client.class b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example01/Client.class new file mode 100644 index 0000000..dcc1ce6 Binary files /dev/null and b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example01/Client.class differ diff --git a/msb-memento-pattern-19/target/classes/com/mashibing/memento/example01/Memento.class b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example01/Memento.class new file mode 100644 index 0000000..c869cb8 Binary files /dev/null and b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example01/Memento.class differ diff --git a/msb-memento-pattern-19/target/classes/com/mashibing/memento/example01/Originator.class b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example01/Originator.class new file mode 100644 index 0000000..ecf96bc Binary files /dev/null and b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example01/Originator.class differ diff --git a/msb-memento-pattern-19/target/classes/com/mashibing/memento/example02/MainApp.class b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example02/MainApp.class new file mode 100644 index 0000000..73aff19 Binary files /dev/null and b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example02/MainApp.class differ diff --git a/msb-memento-pattern-19/target/classes/com/mashibing/memento/example02/Memento.class b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example02/Memento.class new file mode 100644 index 0000000..229563b Binary files /dev/null and b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example02/Memento.class differ diff --git a/msb-memento-pattern-19/target/classes/com/mashibing/memento/example02/Player.class b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example02/Player.class new file mode 100644 index 0000000..0031b5c Binary files /dev/null and b/msb-memento-pattern-19/target/classes/com/mashibing/memento/example02/Player.class differ diff --git a/msb-observer-pattern-12/msb-observer-pattern-12.iml b/msb-observer-pattern-12/msb-observer-pattern-12.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-observer-pattern-12/msb-observer-pattern-12.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-observer-pattern-12/pom.xml b/msb-observer-pattern-12/pom.xml new file mode 100644 index 0000000..eb59186 --- /dev/null +++ b/msb-observer-pattern-12/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.mashibing.observer + msb-observer-pattern-12 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/TestApp.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/TestApp.java new file mode 100644 index 0000000..5ed2ae0 --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/TestApp.java @@ -0,0 +1,22 @@ +package com.mashibing.observer; + +import com.mashibing.observer.example02.LotteryResult; +import com.mashibing.observer.example03.LotteryService; +import com.mashibing.observer.example03.LotteryServiceImpl; + +/** + * @author spikeCong + * @date 2022/10/12 + **/ +public class TestApp { + + public static void main(String[] args) { + +// LotteryService lotteryService = new LotteryServiceImpl(); +// LotteryResult result = lotteryService.lottery("13579246810101010"); +// System.out.println(result); + LotteryService ls = new LotteryServiceImpl(); + LotteryResult lotteryResult = ls.lotteryAndMsg("215673512673512736125763"); + System.out.println(lotteryResult); + } +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/Client.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/Client.java new file mode 100644 index 0000000..f720f7e --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/Client.java @@ -0,0 +1,25 @@ +package com.mashibing.observer.example01; + +import java.util.Observer; + +/** + * @author spikeCong + * @date 2022/10/11 + **/ +public class Client { + + public static void main(String[] args) { + + + + //创建目标类 + Subject subject = new ConcreteSubject(); + + //注册观察者,注册多个 + subject.attach(new ConcreteObserver1()); + subject.attach(new ConcreteObserver2()); + + //具体的主题内部发生改变,给所有注册的观察者发送通知 + subject.notifyObservers(); + } +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/ConcreteObserver1.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/ConcreteObserver1.java new file mode 100644 index 0000000..2763494 --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/ConcreteObserver1.java @@ -0,0 +1,14 @@ +package com.mashibing.observer.example01; + +/** + * 具体的观察者 + * @author spikeCong + * @date 2022/10/11 + **/ +public class ConcreteObserver1 implements Observer { + + @Override + public void update() { + System.out.println("ConcreteObserver1 得到通知,更新状态! !"); + } +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/ConcreteObserver2.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/ConcreteObserver2.java new file mode 100644 index 0000000..39ceedb --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/ConcreteObserver2.java @@ -0,0 +1,14 @@ +package com.mashibing.observer.example01; + +/** + * @author spikeCong + * @date 2022/10/11 + **/ +public class ConcreteObserver2 implements Observer{ + + + @Override + public void update() { + System.out.println("ConcreteObserver2 得到通知,更新状态! !"); + } +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/ConcreteSubject.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/ConcreteSubject.java new file mode 100644 index 0000000..45ae90e --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/ConcreteSubject.java @@ -0,0 +1,36 @@ +package com.mashibing.observer.example01; + +import java.util.ArrayList; + +/** + * 具体目标类 + * @author spikeCong + * @date 2022/10/11 + **/ +public class ConcreteSubject implements Subject { + + //定义集合,存储所有的观察者对象 + private ArrayList observers = new ArrayList<>(); + + + //注册方法,向观察者集合增加一个观察者 + @Override + public void attach(Observer observer) { + observers.add(observer); + } + + //注销方法,用于从观察者集合中移除一个观察者 + @Override + public void detach(Observer observer) { + observers.remove(observer); + } + + //通知方法 + @Override + public void notifyObservers() { + //遍历集合,调用每一个观察者的响应方法 + for (Observer observer : observers) { + observer.update(); + } + } +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/Observer.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/Observer.java new file mode 100644 index 0000000..566253d --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/Observer.java @@ -0,0 +1,12 @@ +package com.mashibing.observer.example01; + +/** + * 抽象观察者 + * @author spikeCong + * @date 2022/10/11 + **/ +public interface Observer { + + //update方法: 为不同的观察者更新行为定义一个相同的接口,不同的观察者对该接口有不同的实现 + public void update(); +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/Subject.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/Subject.java new file mode 100644 index 0000000..3d129a3 --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example01/Subject.java @@ -0,0 +1,13 @@ +package com.mashibing.observer.example01; + +/** + * 抽象目标类 + * @author spikeCong + * @date 2022/10/11 + **/ +public interface Subject { + + void attach(Observer observer); + void detach(Observer observer); + void notifyObservers(); +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example02/DrawHouseService.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example02/DrawHouseService.java new file mode 100644 index 0000000..ae5dea5 --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example02/DrawHouseService.java @@ -0,0 +1,18 @@ +package com.mashibing.observer.example02; + +/** + * 模拟买房摇号服务 + * @author spikeCong + * @date 2022/10/12 + **/ +public class DrawHouseService { + + //摇号抽签 + public String lots(String uId){ + if(uId.hashCode() % 2 == 0){ + return "恭喜ID为: " + uId + " 的用户,在本次摇号中中签!" ; + }else{ + return "很遗憾,ID为: " + uId + "的用户,您本次未中签!" ; + } + } +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example02/LotteryResult.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example02/LotteryResult.java new file mode 100644 index 0000000..ebdbf2e --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example02/LotteryResult.java @@ -0,0 +1,56 @@ +package com.mashibing.observer.example02; + +import java.util.Date; + +/** + * 开奖结果封装类 + * @author spikeCong + * @date 2022/10/12 + **/ +public class LotteryResult { + + private String uId; //用户id + + private String msg; //摇号信息 + + private Date dateTime; //业务时间 + + public LotteryResult(String uId, String msg, Date dateTime) { + this.uId = uId; + this.msg = msg; + this.dateTime = dateTime; + } + + @Override + public String toString() { + return "LotteryResult{" + + "uId='" + uId + '\'' + + ", msg='" + msg + '\'' + + ", dateTime=" + dateTime + + '}'; + } + + public String getuId() { + return uId; + } + + public void setuId(String uId) { + this.uId = uId; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public Date getDateTime() { + return dateTime; + } + + public void setDateTime(Date dateTime) { + this.dateTime = dateTime; + } +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example02/LotteryService.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example02/LotteryService.java new file mode 100644 index 0000000..77baf7e --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example02/LotteryService.java @@ -0,0 +1,13 @@ +package com.mashibing.observer.example02; + +/** + * 开奖服务接口 + * @author spikeCong + * @date 2022/10/12 + **/ +public interface LotteryService { + + //开奖之后的业务操作 + public LotteryResult lottery(String uId); + +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example02/LotteryServiceImpl.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example02/LotteryServiceImpl.java new file mode 100644 index 0000000..2adb97c --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example02/LotteryServiceImpl.java @@ -0,0 +1,27 @@ +package com.mashibing.observer.example02; + +import java.util.Date; + +/** + * @author spikeCong + * @date 2022/10/12 + **/ +public class LotteryServiceImpl implements LotteryService { + + //注入摇号服务 + private DrawHouseService houseService = new DrawHouseService(); + + @Override + public LotteryResult lottery(String uId) { + //1.摇号 + String result = houseService.lots(uId); + + //2.发短信 + System.out.println("发送短信通知用户,ID为: " + uId +",您的摇号结果如下: " + result); + + //3.发送MQ信息 + System.out.println("记录用户摇号结果到MQ,ID为: " + uId +",摇号结果: " + result); + + return new LotteryResult(uId,result,new Date()); + } +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/EventListener.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/EventListener.java new file mode 100644 index 0000000..e57d76e --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/EventListener.java @@ -0,0 +1,13 @@ +package com.mashibing.observer.example03; + +import com.mashibing.observer.example02.LotteryResult; + +/** + * 事件监听接口 + * @author spikeCong + * @date 2022/10/12 + **/ +public interface EventListener { + + void doEvent(LotteryResult result); +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/EventManager.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/EventManager.java new file mode 100644 index 0000000..ea149bc --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/EventManager.java @@ -0,0 +1,61 @@ +package com.mashibing.observer.example03; + +import com.mashibing.observer.example02.LotteryResult; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 事件处理类 + * @author spikeCong + * @date 2022/10/12 + **/ +public class EventManager { + + public enum EventType{ + MQ,Message + } + + //监听器集合 + Map, List> listeners = new HashMap<>(); + + public EventManager(Enum... operations) { + for (Enum operation : operations) { + this.listeners.put(operation,new ArrayList<>()); + } + } + + /** + * 订阅 + * @param eventType 事件类型 + * @param listener 监听对象 + */ + public void subscribe(Enum eventType,EventListener listener){ + List users = listeners.get(eventType); + users.add(listener); + } + + /** + * 取消订阅 + * @param eventType 事件类型 + * @param listener 监听对象 + */ + public void unsubscribe(Enum eventType,EventListener listener){ + List users = listeners.get(eventType); + users.remove(listener); + } + + /** + * 通知 + * @param eventType + * @param result + */ + public void notify(Enum eventType, LotteryResult result){ + List users = listeners.get(eventType); + for (EventListener listener : users) { + listener.doEvent(result); + } + } +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/LotteryService.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/LotteryService.java new file mode 100644 index 0000000..2c3c086 --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/LotteryService.java @@ -0,0 +1,32 @@ +package com.mashibing.observer.example03; + +import com.mashibing.observer.example02.LotteryResult; + +/** + * 开奖服务接口 + * @author spikeCong + * @date 2022/10/12 + **/ +public abstract class LotteryService { + + private EventManager eventManager; + + public LotteryService() { + //设置事件的类型 + eventManager = new EventManager(EventManager.EventType.MQ,EventManager.EventType.Message); + //订阅 + eventManager.subscribe(EventManager.EventType.Message,new MessageEventListener()); + eventManager.subscribe(EventManager.EventType.MQ,new MQEventListener()); + } + + public LotteryResult lotteryAndMsg(String uId){ + LotteryResult lottery = lottery(uId); + //发送通知 + eventManager.notify(EventManager.EventType.Message,lottery); + eventManager.notify(EventManager.EventType.MQ,lottery); + + return lottery; + } + + public abstract LotteryResult lottery(String uId); +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/LotteryServiceImpl.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/LotteryServiceImpl.java new file mode 100644 index 0000000..99d191d --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/LotteryServiceImpl.java @@ -0,0 +1,23 @@ +package com.mashibing.observer.example03; + +import com.mashibing.observer.example02.DrawHouseService; +import com.mashibing.observer.example02.LotteryResult; + +import java.util.Date; + +/** + * @author spikeCong + * @date 2022/10/12 + **/ +public class LotteryServiceImpl extends LotteryService { + + //注入摇号服务 + private DrawHouseService houseService = new DrawHouseService(); + + @Override + public LotteryResult lottery(String uId) { + //1.摇号 + String result = houseService.lots(uId); + return new LotteryResult(uId,result,new Date()); + } +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/MQEventListener.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/MQEventListener.java new file mode 100644 index 0000000..4886a00 --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/MQEventListener.java @@ -0,0 +1,17 @@ +package com.mashibing.observer.example03; + +import com.mashibing.observer.example02.LotteryResult; + +/** + * MQ消息发送事件监听 + * @author spikeCong + * @date 2022/10/12 + **/ +public class MQEventListener implements EventListener{ + + @Override + public void doEvent(LotteryResult result) { + System.out.println("记录用户的摇号结果(MQ),用户ID: " + result.getuId() + + ",摇号结果: " + result.getMsg()); + } +} diff --git a/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/MessageEventListener.java b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/MessageEventListener.java new file mode 100644 index 0000000..46d2a15 --- /dev/null +++ b/msb-observer-pattern-12/src/main/java/com/mashibing/observer/example03/MessageEventListener.java @@ -0,0 +1,17 @@ +package com.mashibing.observer.example03; + +import com.mashibing.observer.example02.LotteryResult; + +/** + * 短信发送事件监听类 + * @author spikeCong + * @date 2022/10/12 + **/ +public class MessageEventListener implements EventListener { + + @Override + public void doEvent(LotteryResult result) { + System.out.println("发送短信通知,用户ID: " + result.getuId() + +",您的摇号结果为: " + result.getMsg()); + } +} diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/TestApp.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/TestApp.class new file mode 100644 index 0000000..9c40680 Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/TestApp.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/Client.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/Client.class new file mode 100644 index 0000000..d0668aa Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/Client.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/ConcreteObserver1.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/ConcreteObserver1.class new file mode 100644 index 0000000..d1aa8e2 Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/ConcreteObserver1.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/ConcreteObserver2.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/ConcreteObserver2.class new file mode 100644 index 0000000..0066099 Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/ConcreteObserver2.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/ConcreteSubject.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/ConcreteSubject.class new file mode 100644 index 0000000..bf64035 Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/ConcreteSubject.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/Observer.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/Observer.class new file mode 100644 index 0000000..77560a4 Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/Observer.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/Subject.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/Subject.class new file mode 100644 index 0000000..e6da5da Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example01/Subject.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example02/DrawHouseService.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example02/DrawHouseService.class new file mode 100644 index 0000000..9b1b1b5 Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example02/DrawHouseService.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example02/LotteryResult.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example02/LotteryResult.class new file mode 100644 index 0000000..be15534 Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example02/LotteryResult.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example02/LotteryService.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example02/LotteryService.class new file mode 100644 index 0000000..1cc5ddd Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example02/LotteryService.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example02/LotteryServiceImpl.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example02/LotteryServiceImpl.class new file mode 100644 index 0000000..fffad97 Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example02/LotteryServiceImpl.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/EventListener.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/EventListener.class new file mode 100644 index 0000000..22ec84f Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/EventListener.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/EventManager$EventType.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/EventManager$EventType.class new file mode 100644 index 0000000..1303f35 Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/EventManager$EventType.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/EventManager.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/EventManager.class new file mode 100644 index 0000000..cb1a73a Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/EventManager.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/LotteryService.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/LotteryService.class new file mode 100644 index 0000000..3d64406 Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/LotteryService.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/LotteryServiceImpl.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/LotteryServiceImpl.class new file mode 100644 index 0000000..1dcf35e Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/LotteryServiceImpl.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/MQEventListener.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/MQEventListener.class new file mode 100644 index 0000000..68e397a Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/MQEventListener.class differ diff --git a/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/MessageEventListener.class b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/MessageEventListener.class new file mode 100644 index 0000000..9e9244c Binary files /dev/null and b/msb-observer-pattern-12/target/classes/com/mashibing/observer/example03/MessageEventListener.class differ diff --git a/msb-prototype-pattern-04/a.txt b/msb-prototype-pattern-04/a.txt new file mode 100644 index 0000000..ff3ee4d Binary files /dev/null and b/msb-prototype-pattern-04/a.txt differ diff --git a/msb-prototype-pattern-04/msb-prototype-pattern-04.iml b/msb-prototype-pattern-04/msb-prototype-pattern-04.iml new file mode 100644 index 0000000..319f82b --- /dev/null +++ b/msb-prototype-pattern-04/msb-prototype-pattern-04.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-prototype-pattern-04/pom.xml b/msb-prototype-pattern-04/pom.xml new file mode 100644 index 0000000..73f6c8f --- /dev/null +++ b/msb-prototype-pattern-04/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + com.mashibing + msb-prototype-pattern-04 + 1.0-SNAPSHOT + + + junit + junit + 4.12 + compile + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-prototype-pattern-04/src/main/java/com/mashibing/example01/ConcretePrototype.java b/msb-prototype-pattern-04/src/main/java/com/mashibing/example01/ConcretePrototype.java new file mode 100644 index 0000000..00253ca --- /dev/null +++ b/msb-prototype-pattern-04/src/main/java/com/mashibing/example01/ConcretePrototype.java @@ -0,0 +1,36 @@ +package com.mashibing.example01; + +import java.io.Serializable; + +/** + * 具体原型类 + * 实现Cloneable标识接口,表示当前类对象可复制 + * @author spikeCong + * @date 2022/9/21 + **/ +public class ConcretePrototype implements Cloneable, Serializable { + + private Person person; + + public Person getPerson() { + return person; + } + + public void setPerson(Person person) { + this.person = person; + } + + public void show(){ + System.out.println("嫌疑人姓名: " + person.getName()); + } + + public ConcretePrototype() { + System.out.println("具体原型对象创建成功!"); + } + + @Override + protected ConcretePrototype clone() throws CloneNotSupportedException { + System.out.println("克隆对象复制成功!"); + return (ConcretePrototype) super.clone(); + } +} diff --git a/msb-prototype-pattern-04/src/main/java/com/mashibing/example01/Person.java b/msb-prototype-pattern-04/src/main/java/com/mashibing/example01/Person.java new file mode 100644 index 0000000..78b820c --- /dev/null +++ b/msb-prototype-pattern-04/src/main/java/com/mashibing/example01/Person.java @@ -0,0 +1,27 @@ +package com.mashibing.example01; + +import java.io.Serializable; + +/** + * @author spikeCong + * @date 2022/9/21 + **/ +public class Person implements Serializable { + + private String name; + + public Person() { + } + + public Person(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/msb-prototype-pattern-04/src/main/java/com/mashibing/example01/TestPrototype.java b/msb-prototype-pattern-04/src/main/java/com/mashibing/example01/TestPrototype.java new file mode 100644 index 0000000..b79a929 --- /dev/null +++ b/msb-prototype-pattern-04/src/main/java/com/mashibing/example01/TestPrototype.java @@ -0,0 +1,67 @@ +package com.mashibing.example01; + +import org.junit.Test; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +/** + * @author spikeCong + * @date 2022/9/21 + **/ +public class TestPrototype { + + @Test + public void test01() throws CloneNotSupportedException { + + ConcretePrototype c1 = new ConcretePrototype(); + ConcretePrototype c2 = c1.clone(); + + System.out.println("对象c1和对象c2是同一个对象吗?" + (c1 == c2)); + } + + @Test + public void test02() throws CloneNotSupportedException { + + ConcretePrototype c1 = new ConcretePrototype(); + Person p1 = new Person("峰哥"); + c1.setPerson(p1); + + //复制c1 + ConcretePrototype c2 = c1.clone(); + Person p2 = c2.getPerson(); + p2.setName("凡哥"); + + c1.show(); + c2.show(); + System.out.println("对象p1和对象p2是同一个对象吗?" + (p1 == p2)); + } + + @Test + public void test03() throws Exception { + + ConcretePrototype c1 = new ConcretePrototype(); + Person p1 = new Person("峰哥"); + c1.setPerson(p1); + + //创建对象序列化输出流 + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt")); + + //将c1对象写到文件 + oos.writeObject(c1); + oos.close(); + + //创建对象序列化输入流 + ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt")); + //读取对象 + ConcretePrototype c2 = (ConcretePrototype) ois.readObject(); + Person p2 = c2.getPerson(); + p2.setName("凡哥"); + + c1.show(); + c2.show(); + System.out.println("对象p1和对象p2是同一个对象吗?" + (p1 == p2)); + } +} diff --git a/msb-prototype-pattern-04/src/main/java/com/mashibing/example02/AdvTemplate.java b/msb-prototype-pattern-04/src/main/java/com/mashibing/example02/AdvTemplate.java new file mode 100644 index 0000000..6114ab1 --- /dev/null +++ b/msb-prototype-pattern-04/src/main/java/com/mashibing/example02/AdvTemplate.java @@ -0,0 +1,31 @@ +package com.mashibing.example02; + +/** + * 广告模板 + * @author spikeCong + * @date 2022/9/21 + **/ +public class AdvTemplate { + + //广告信名称 + private String advSubject = "xx银行本月还款达标,可抽iPhone 13等好礼!"; + + //广告信内容 + private String advContext = "达标用户请在2022年3月1日到2022年3月30日参与抽奖......"; + + public String getAdvSubject() { + return advSubject; + } + + public void setAdvSubject(String advSubject) { + this.advSubject = advSubject; + } + + public String getAdvContext() { + return advContext; + } + + public void setAdvContext(String advContext) { + this.advContext = advContext; + } +} diff --git a/msb-prototype-pattern-04/src/main/java/com/mashibing/example02/Client.java b/msb-prototype-pattern-04/src/main/java/com/mashibing/example02/Client.java new file mode 100644 index 0000000..e315b95 --- /dev/null +++ b/msb-prototype-pattern-04/src/main/java/com/mashibing/example02/Client.java @@ -0,0 +1,41 @@ +package com.mashibing.example02; + +import java.util.Random; + +/** + * 业务场景 + * @author spikeCong + * @date 2022/9/21 + **/ +public class Client { + + //发送邮件的数量 + private static int MAX_COUNT = 6; + + //发送邮件 + public static void sendMail(Mail mail){ + System.out.println("标题: " + mail.getSubject() + "\t 收件人: " + mail.getReceiver() + + "\t ...发送成功!"); + } + + public static void main(String[] args) { + + int i = 0; + + //定义模板 + Mail mail = new Mail(new AdvTemplate()); + mail.setTail("xxx银行版权所有"); + while(i < MAX_COUNT){ + //每封邮件不同的信息 + mail.setAppellation(" 先生 (女士)"); + Random random = new Random(); + int num = random.nextInt(999999999); + mail.setReceiver(num + "@"+"mashibing.com"); + + //发送邮件 + sendMail(mail); + i++; + } + } + +} diff --git a/msb-prototype-pattern-04/src/main/java/com/mashibing/example02/Mail.java b/msb-prototype-pattern-04/src/main/java/com/mashibing/example02/Mail.java new file mode 100644 index 0000000..7a30a6e --- /dev/null +++ b/msb-prototype-pattern-04/src/main/java/com/mashibing/example02/Mail.java @@ -0,0 +1,64 @@ +package com.mashibing.example02; + +/** + * 邮件类 + * @author spikeCong + * @date 2022/9/21 + **/ +public class Mail { + + private String receiver; //收件人 + + private String subject; //邮件名称 + + private String appellation; //称呼 + + private String context; //邮件内容 + + private String tail; //邮件尾部 "xx版权所有" + + public Mail(AdvTemplate advTemplate) { + this.subject = advTemplate.getAdvSubject(); + this.context = advTemplate.getAdvContext(); + } + + public String getReceiver() { + return receiver; + } + + public void setReceiver(String receiver) { + this.receiver = receiver; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getAppellation() { + return appellation; + } + + public void setAppellation(String appellation) { + this.appellation = appellation; + } + + public String getContext() { + return context; + } + + public void setContext(String context) { + this.context = context; + } + + public String getTail() { + return tail; + } + + public void setTail(String tail) { + this.tail = tail; + } +} diff --git a/msb-prototype-pattern-04/src/main/java/com/mashibing/example03/Client.java b/msb-prototype-pattern-04/src/main/java/com/mashibing/example03/Client.java new file mode 100644 index 0000000..9715393 --- /dev/null +++ b/msb-prototype-pattern-04/src/main/java/com/mashibing/example03/Client.java @@ -0,0 +1,47 @@ +package com.mashibing.example03; + +import com.mashibing.example02.AdvTemplate; +import com.mashibing.example03.Mail; + +import java.util.Random; + +/** + * 业务场景 + * @author spikeCong + * @date 2022/9/21 + **/ +public class Client { + + //发送邮件的数量 + private static int MAX_COUNT = 6; + + //发送邮件 + public static void sendMail(Mail mail){ + System.out.println("标题: " + mail.getSubject() + "\t 收件人: " + mail.getReceiver() + + "\t ...发送成功!"); + } + + public static void main(String[] args) { + + int i = 0; + + //定义模板 + Mail mail = new Mail(new AdvTemplate()); + mail.setTail("xxx银行版权所有"); + + while(i < MAX_COUNT){ + + //每封邮件不同的信息 + Mail cloneMail = mail.clone(); + cloneMail.setAppellation(" 先生 (女士)"); + Random random = new Random(); + int num = random.nextInt(999999999); + cloneMail.setReceiver(num + "@"+"mashibing.com"); + + //发送邮件 + sendMail(cloneMail); + i++; + } + } + +} diff --git a/msb-prototype-pattern-04/src/main/java/com/mashibing/example03/Mail.java b/msb-prototype-pattern-04/src/main/java/com/mashibing/example03/Mail.java new file mode 100644 index 0000000..9f61821 --- /dev/null +++ b/msb-prototype-pattern-04/src/main/java/com/mashibing/example03/Mail.java @@ -0,0 +1,80 @@ +package com.mashibing.example03; + +import com.mashibing.example02.AdvTemplate; + +/** + * 邮件类-原型类 + * @author spikeCong + * @date 2022/9/21 + **/ +public class Mail implements Cloneable{ + + private String receiver; //收件人 + + private String subject; //邮件名称 + + private String appellation; //称呼 + + private String context; //邮件内容 + + private String tail; //邮件尾部 "xx版权所有" + + public Mail(AdvTemplate advTemplate) { + this.subject = advTemplate.getAdvSubject(); + this.context = advTemplate.getAdvContext(); + } + + public String getReceiver() { + return receiver; + } + + public void setReceiver(String receiver) { + this.receiver = receiver; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getAppellation() { + return appellation; + } + + public void setAppellation(String appellation) { + this.appellation = appellation; + } + + public String getContext() { + return context; + } + + public void setContext(String context) { + this.context = context; + } + + public String getTail() { + return tail; + } + + public void setTail(String tail) { + this.tail = tail; + } + + @Override + protected Mail clone(){ + + Mail mail = null; + + try { + + return mail = (Mail) super.clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + return mail; + } + } +} diff --git a/msb-prototype-pattern-04/target/classes/com/mashibing/example01/ConcretePrototype.class b/msb-prototype-pattern-04/target/classes/com/mashibing/example01/ConcretePrototype.class new file mode 100644 index 0000000..e05925d Binary files /dev/null and b/msb-prototype-pattern-04/target/classes/com/mashibing/example01/ConcretePrototype.class differ diff --git a/msb-prototype-pattern-04/target/classes/com/mashibing/example01/Person.class b/msb-prototype-pattern-04/target/classes/com/mashibing/example01/Person.class new file mode 100644 index 0000000..091a64f Binary files /dev/null and b/msb-prototype-pattern-04/target/classes/com/mashibing/example01/Person.class differ diff --git a/msb-prototype-pattern-04/target/classes/com/mashibing/example01/TestPrototype.class b/msb-prototype-pattern-04/target/classes/com/mashibing/example01/TestPrototype.class new file mode 100644 index 0000000..cfac1d1 Binary files /dev/null and b/msb-prototype-pattern-04/target/classes/com/mashibing/example01/TestPrototype.class differ diff --git a/msb-prototype-pattern-04/target/classes/com/mashibing/example02/AdvTemplate.class b/msb-prototype-pattern-04/target/classes/com/mashibing/example02/AdvTemplate.class new file mode 100644 index 0000000..3f6bec3 Binary files /dev/null and b/msb-prototype-pattern-04/target/classes/com/mashibing/example02/AdvTemplate.class differ diff --git a/msb-prototype-pattern-04/target/classes/com/mashibing/example02/Client.class b/msb-prototype-pattern-04/target/classes/com/mashibing/example02/Client.class new file mode 100644 index 0000000..6ef919d Binary files /dev/null and b/msb-prototype-pattern-04/target/classes/com/mashibing/example02/Client.class differ diff --git a/msb-prototype-pattern-04/target/classes/com/mashibing/example02/Mail.class b/msb-prototype-pattern-04/target/classes/com/mashibing/example02/Mail.class new file mode 100644 index 0000000..7062dd7 Binary files /dev/null and b/msb-prototype-pattern-04/target/classes/com/mashibing/example02/Mail.class differ diff --git a/msb-prototype-pattern-04/target/classes/com/mashibing/example03/Client.class b/msb-prototype-pattern-04/target/classes/com/mashibing/example03/Client.class new file mode 100644 index 0000000..e35dc5a Binary files /dev/null and b/msb-prototype-pattern-04/target/classes/com/mashibing/example03/Client.class differ diff --git a/msb-prototype-pattern-04/target/classes/com/mashibing/example03/Mail.class b/msb-prototype-pattern-04/target/classes/com/mashibing/example03/Mail.class new file mode 100644 index 0000000..a2ee0c3 Binary files /dev/null and b/msb-prototype-pattern-04/target/classes/com/mashibing/example03/Mail.class differ diff --git a/msb-proxy-pattern-05/msb-proxy-pattern-05.iml b/msb-proxy-pattern-05/msb-proxy-pattern-05.iml new file mode 100644 index 0000000..9b58323 --- /dev/null +++ b/msb-proxy-pattern-05/msb-proxy-pattern-05.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-proxy-pattern-05/pom.xml b/msb-proxy-pattern-05/pom.xml new file mode 100644 index 0000000..f7db2fc --- /dev/null +++ b/msb-proxy-pattern-05/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + com.mashibing + msb-proxy-pattern-05 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + junit + junit + 4.12 + compile + + + + cglib + cglib + 3.2.5 + + + \ No newline at end of file diff --git a/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/TestProxy.java b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/TestProxy.java new file mode 100644 index 0000000..c14ce0d --- /dev/null +++ b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/TestProxy.java @@ -0,0 +1,66 @@ +package com.mashibing.proxy; + +import com.mashibing.proxy.example01.IUserDao; +import com.mashibing.proxy.example01.UserDaoImpl; +import com.mashibing.proxy.example01.UserDaoProxy; +import com.mashibing.proxy.example02.ProxyFactory; +import com.mashibing.proxy.example03.User; +import com.mashibing.proxy.example03.UserLogProxy; +import com.mashibing.proxy.example03.UserServiceImpl; +import org.junit.Test; + +import java.util.List; + +/** + * @author spikeCong + * @date 2022/9/22 + **/ +public class TestProxy { + + + /** + * 静态代理 + * 优点: 可以在不修改目标类的前提下,扩展目标类的功能 + * 缺点: + * 1.冗余.由于代理对象要实现和目标对象一致的接口,会产生很多的代理. + * 2.不易维护.一旦接口中增加方法,目标对象和代理对象都要进行修改. + */ + @Test + public void testStaticProxy(){ + + //目标类 + IUserDao dao = new UserDaoImpl(); + + //代理对象 + UserDaoProxy proxy = new UserDaoProxy(dao); + proxy.save(); + } + + public static void main(String[] args) { + + IUserDao userDao = new UserDaoImpl(); + System.out.println(userDao.getClass()); //目标对象的信息 + + IUserDao proxy = (IUserDao) new ProxyFactory(userDao).getProxyInstance();//获取代理对象 + System.out.println(proxy.getClass()); + proxy.save();//代理方法 + + while (true){} + } + + @Test + public void testCglibProxy(){ + + //目标对象 + UserServiceImpl userService = new UserServiceImpl(); + System.out.println(userService.getClass()); + + //代理对象 + UserServiceImpl proxy = (UserServiceImpl) new UserLogProxy().getLogProxy(userService); + System.out.println(proxy.getClass()); + + List list = proxy.findUserList(); + System.out.println("用户信息: " +list); + } + +} diff --git a/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example01/IUserDao.java b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example01/IUserDao.java new file mode 100644 index 0000000..759887a --- /dev/null +++ b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example01/IUserDao.java @@ -0,0 +1,11 @@ +package com.mashibing.proxy.example01; + +/** + * DAO接口 + * @author spikeCong + * @date 2022/9/22 + **/ +public interface IUserDao { + + void save(); +} diff --git a/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example01/UserDaoImpl.java b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example01/UserDaoImpl.java new file mode 100644 index 0000000..4d3b394 --- /dev/null +++ b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example01/UserDaoImpl.java @@ -0,0 +1,14 @@ +package com.mashibing.proxy.example01; + +/** + * 目标类 + * @author spikeCong + * @date 2022/9/22 + **/ +public class UserDaoImpl implements IUserDao { + + @Override + public void save() { + System.out.println("保存数据"); + } +} diff --git a/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example01/UserDaoProxy.java b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example01/UserDaoProxy.java new file mode 100644 index 0000000..bd04f13 --- /dev/null +++ b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example01/UserDaoProxy.java @@ -0,0 +1,22 @@ +package com.mashibing.proxy.example01; + +/** + * 代理类 + * @author spikeCong + * @date 2022/9/22 + **/ +public class UserDaoProxy implements IUserDao { + + private IUserDao target; + + public UserDaoProxy(IUserDao target) { + this.target = target; + } + + @Override + public void save() { + System.out.println("开启事务"); //扩展额外的功能 + target.save(); + System.out.println("提交事务"); + } +} diff --git a/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example02/ProxyFactory.java b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example02/ProxyFactory.java new file mode 100644 index 0000000..c2e77d1 --- /dev/null +++ b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example02/ProxyFactory.java @@ -0,0 +1,43 @@ +package com.mashibing.proxy.example02; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +/** + * 代理工厂类-动态的生成代理对象 + * @author spikeCong + * @date 2022/9/22 + **/ +public class ProxyFactory { + + //维护一个目标对象 + private Object target; + + public ProxyFactory(Object target) { + this.target = target; + } + + //为目标对象生成代理对象 + public Object getProxyInstance(){ + + return Proxy.newProxyInstance( + //目标类使用的类加载器 + target.getClass().getClassLoader(), + //目标对象实现的接口类型 + target.getClass().getInterfaces(), + new InvocationHandler() { //事件处理器 + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + + System.out.println("开启事务"); + method.invoke(target,args); + System.out.println("提交事务"); + return null; + } + } + ); + } + +} diff --git a/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example03/User.java b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example03/User.java new file mode 100644 index 0000000..cacdc0f --- /dev/null +++ b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example03/User.java @@ -0,0 +1,41 @@ +package com.mashibing.proxy.example03; + +/** + * @author spikeCong + * @date 2022/9/23 + **/ +public class User { + + private String name; + + private int age; + + public User(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public String toString() { + return "User{" + + "name='" + name + '\'' + + ", age=" + age + + '}'; + } +} diff --git a/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example03/UserLogProxy.java b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example03/UserLogProxy.java new file mode 100644 index 0000000..d6660f6 --- /dev/null +++ b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example03/UserLogProxy.java @@ -0,0 +1,56 @@ +package com.mashibing.proxy.example03; + +import net.sf.cglib.proxy.Enhancer; +import net.sf.cglib.proxy.MethodInterceptor; +import net.sf.cglib.proxy.MethodProxy; + +import java.lang.reflect.Method; +import java.text.SimpleDateFormat; +import java.util.Calendar; + +/** + * @author spikeCong + * @date 2022/9/23 + **/ +public class UserLogProxy implements MethodInterceptor { + + + /** + * 生成CGLIB动态代理类方法 + * @param target 需要被代理的目标类 + * @return: java.lang.Object 代理类对象 + */ + public Object getLogProxy(Object target){ + + //增强器类,用来创建动态代理类 + Enhancer enhancer = new Enhancer(); + + //设置代理类的父类字节码对象 + enhancer.setSuperclass(target.getClass()); + + //设置回调 + enhancer.setCallback(this); + + //创建动态代理对象,并返回 + return enhancer.create(); + } + + /** + * 实现回调方法 + * @param o 代理对象 + * @param method 目标对象中的方法的Method实例 + * @param args 实际参数 + * @param methodProxy 代理类对象中的方法的Method实例 + * @return: java.lang.Object + */ + @Override + public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { + + Calendar instance = Calendar.getInstance(); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + System.out.println(format.format(instance.getTime()) + "[ " +method.getName() +"] 查询用户信息..."); + Object result = methodProxy.invokeSuper(o, args); + return null; + } +} diff --git a/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example03/UserServiceImpl.java b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example03/UserServiceImpl.java new file mode 100644 index 0000000..cb545bb --- /dev/null +++ b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example03/UserServiceImpl.java @@ -0,0 +1,18 @@ +package com.mashibing.proxy.example03; + +import java.util.Collections; +import java.util.List; + +/** + * 目标类 + * @author spikeCong + * @date 2022/9/23 + **/ +public class UserServiceImpl { + + //查询功能 + public List findUserList(){ + + return Collections.singletonList(new User("tom",23)); + } +} diff --git a/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example04/Client.java b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example04/Client.java new file mode 100644 index 0000000..79ff4da --- /dev/null +++ b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example04/Client.java @@ -0,0 +1,17 @@ +package com.mashibing.proxy.example04; + +/** + * @author spikeCong + * @date 2022/11/20 + **/ +public class Client { + + public static void main(String[] args) { + RealSubject realSubject = new RealSubject(); + System.out.println(realSubject); // 目标对象信息 + Subject proxy = (Subject) new ProxyFactory(realSubject).getProxyInstance(); + System.out.println(proxy); + proxy.request(); + } + +} diff --git a/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example04/ProxyFactory.java b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example04/ProxyFactory.java new file mode 100644 index 0000000..c7d7cb9 --- /dev/null +++ b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example04/ProxyFactory.java @@ -0,0 +1,49 @@ +package com.mashibing.proxy.example04; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +/** + * @author spikeCong + * @date 2022/11/20 + **/ +public class ProxyFactory { + // 代理对象 + private Object target; + + public ProxyFactory(Object t) { + target = t; + } + + // 为目标对象生成代理对象 + public Object getProxyInstance() { + return Proxy.newProxyInstance( + target.getClass().getClassLoader(), // 目标类使用的类加载器 + target.getClass().getInterfaces(), // 目标类实现的接口 + new InvocationHandler() { // 事件处理器匿名实现类 + /** + * @param proxy 代理对象 + * @param method 代理对象的方法 + * @param args 方法参数 + * @return 代理对象方法的返回值,没有就返回null + */ + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + before(); + Object result = method.invoke(target, args); + after(); + return result; + } + } + ); + } + + private void before() { + System.out.println("before method..."); + } + private void after() { + System.out.println("after method..."); + } + +} diff --git a/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example04/RealSubject.java b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example04/RealSubject.java new file mode 100644 index 0000000..bb79527 --- /dev/null +++ b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example04/RealSubject.java @@ -0,0 +1,12 @@ +package com.mashibing.proxy.example04; + +/** + * @author spikeCong + * @date 2022/11/20 + **/ +public class RealSubject implements Subject { + @Override + public void request() { + System.out.println("执行了被代理角色。。。"); + } +} diff --git a/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example04/Subject.java b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example04/Subject.java new file mode 100644 index 0000000..86d9acb --- /dev/null +++ b/msb-proxy-pattern-05/src/main/java/com/mashibing/proxy/example04/Subject.java @@ -0,0 +1,10 @@ +package com.mashibing.proxy.example04; + +/** + * @author spikeCong + * @date 2022/11/20 + **/ +public interface Subject { + + void request(); +} diff --git a/msb-proxy-pattern-05/target/classes/META-INF/msb-proxy-pattern-05.kotlin_module b/msb-proxy-pattern-05/target/classes/META-INF/msb-proxy-pattern-05.kotlin_module new file mode 100644 index 0000000..a49347a Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/META-INF/msb-proxy-pattern-05.kotlin_module differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/TestProxy.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/TestProxy.class new file mode 100644 index 0000000..5abc9d3 Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/TestProxy.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example01/IUserDao.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example01/IUserDao.class new file mode 100644 index 0000000..53690cd Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example01/IUserDao.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example01/UserDaoImpl.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example01/UserDaoImpl.class new file mode 100644 index 0000000..da795a0 Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example01/UserDaoImpl.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example01/UserDaoProxy.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example01/UserDaoProxy.class new file mode 100644 index 0000000..9be7af7 Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example01/UserDaoProxy.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example02/ProxyFactory$1.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example02/ProxyFactory$1.class new file mode 100644 index 0000000..c30780a Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example02/ProxyFactory$1.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example02/ProxyFactory.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example02/ProxyFactory.class new file mode 100644 index 0000000..e365bb6 Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example02/ProxyFactory.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example03/User.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example03/User.class new file mode 100644 index 0000000..11a260e Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example03/User.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example03/UserLogProxy.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example03/UserLogProxy.class new file mode 100644 index 0000000..c603e26 Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example03/UserLogProxy.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example03/UserServiceImpl.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example03/UserServiceImpl.class new file mode 100644 index 0000000..2b8f131 Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example03/UserServiceImpl.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/Client.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/Client.class new file mode 100644 index 0000000..fd27d1e Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/Client.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/ProxyFactory$1.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/ProxyFactory$1.class new file mode 100644 index 0000000..caea676 Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/ProxyFactory$1.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/ProxyFactory.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/ProxyFactory.class new file mode 100644 index 0000000..06066d5 Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/ProxyFactory.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/RealSubject.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/RealSubject.class new file mode 100644 index 0000000..230e4ca Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/RealSubject.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/Subject.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/Subject.class new file mode 100644 index 0000000..dd6e77e Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/example04/Subject.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/testoverflow/CglibProxyTest.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/testoverflow/CglibProxyTest.class new file mode 100644 index 0000000..692fd27 Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/testoverflow/CglibProxyTest.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/testoverflow/Human.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/testoverflow/Human.class new file mode 100644 index 0000000..f6db51f Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/testoverflow/Human.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/testoverflow/MyMethodInterceptor.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/testoverflow/MyMethodInterceptor.class new file mode 100644 index 0000000..8d598e5 Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/testoverflow/MyMethodInterceptor.class differ diff --git a/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/testoverflow/ProxyWithCglib.class b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/testoverflow/ProxyWithCglib.class new file mode 100644 index 0000000..1f965e4 Binary files /dev/null and b/msb-proxy-pattern-05/target/classes/com/mashibing/proxy/testoverflow/ProxyWithCglib.class differ diff --git a/msb-singleton-pattern-01/msb-singleton-pattern-01.iml b/msb-singleton-pattern-01/msb-singleton-pattern-01.iml new file mode 100644 index 0000000..975d5da --- /dev/null +++ b/msb-singleton-pattern-01/msb-singleton-pattern-01.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-singleton-pattern-01/pom.xml b/msb-singleton-pattern-01/pom.xml new file mode 100644 index 0000000..0c53ecc --- /dev/null +++ b/msb-singleton-pattern-01/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + com.mashibing + msb-singleton-pattern-01 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + junit + junit + 4.12 + test + + + \ No newline at end of file diff --git a/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo01/Singleton_01.java b/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo01/Singleton_01.java new file mode 100644 index 0000000..59f2e31 --- /dev/null +++ b/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo01/Singleton_01.java @@ -0,0 +1,27 @@ +package com.mashibing.singleton.demo01; + +/** + * 单例模式-饿汉式 + * 在类加载期间初始化私有的静态实例,保证instance实例创建过程是线程安全的. + * 特点: 不支持延时加载,获取实例对象的速度比较快,但是如果对象比较大,而且一直没有使用就会造成内存的浪费. + * @author spikeCong + * @date 2022/9/5 + **/ +public class Singleton_01 { + + //1. 私有构造方法 + private Singleton_01(){ + + } + + //2. 在本类中创建私有静态的全局对象 + private static Singleton_01 instance = new Singleton_01(); + + + //3. 提供一个全局访问点,供外部获取单例对象 + public static Singleton_01 getInstance(){ + + return instance; + } + +} diff --git a/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo02/Singleton_02.java b/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo02/Singleton_02.java new file mode 100644 index 0000000..0c50bc8 --- /dev/null +++ b/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo02/Singleton_02.java @@ -0,0 +1,32 @@ +package com.mashibing.singleton.demo02; + +import com.mashibing.singleton.demo01.Singleton_01; + +/** + * 单例模式-懒汉式 + * 特点: 支持延时加载,只有调用getInstance方法时,才会创建对象. + * @author spikeCong + * @date 2022/9/5 + **/ +public class Singleton_02 { + + //1. 私有构造方法 + private Singleton_02(){ + + } + + //2. 在本类中创建私有静态的全局对象 + private static Singleton_02 instance; + + + //3. 通过判断对象是否被初始化,来选择是否创建对象 + public static Singleton_02 getInstance(){ + + if(instance == null){ + + instance = new Singleton_02(); + } + return instance; + } + +} diff --git a/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo03/Singleton_03.java b/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo03/Singleton_03.java new file mode 100644 index 0000000..671a4ab --- /dev/null +++ b/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo03/Singleton_03.java @@ -0,0 +1,32 @@ +package com.mashibing.singleton.demo03; + +import com.mashibing.singleton.demo02.Singleton_02; + +/** + * 单例模式-懒汉式(线程安全) + * 使用synchronized锁 锁住创建单例对象的方法,防止多个线程同时调用. + * 缺点: 因为对getInstance() 方法加了锁,导致这个函数的并发度很低. + * @author spikeCong + * @date 2022/9/5 + **/ +public class Singleton_03 { + + //1. 私有构造方法 + private Singleton_03(){ + + } + + //2. 在本类中创建私有静态的全局对象 + private static Singleton_03 instance; + + + //3. 通过添加synchronize,保证多线程模式下的单例对象的唯一性 + public static synchronized Singleton_03 getInstance(){ + + if(instance == null){ + instance = new Singleton_03(); + } + return instance; + } + +} diff --git a/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo04/Singleton_04.java b/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo04/Singleton_04.java new file mode 100644 index 0000000..c759e61 --- /dev/null +++ b/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo04/Singleton_04.java @@ -0,0 +1,45 @@ +package com.mashibing.singleton.demo04; + +import com.mashibing.singleton.demo03.Singleton_03; + +/** + * 单例模式-双重校验 + * @author spikeCong + * @date 2022/9/5 + **/ +public class Singleton_04 { + + //1. 私有构造方法 + private Singleton_04(){ + + } + + //2. 在本类中创建私有静态的全局对象 + // 使用 volatile保证变量可见性,屏蔽指令重排序 + private volatile static Singleton_04 instance; + + //3. 获取单例对象的静态方法 + public static Singleton_04 getInstance(){ + + //第一次判断,如果instance不为null,不进入抢锁阶段,直接返回实例 + if(instance == null){ + synchronized (Singleton_04.class){ + //第二次判断,抢到锁之后再次进行判断,判断是否为null + if(instance == null){ + + instance = new Singleton_04(); + /** + * 上面的创建对象的代码,在JVM中被分为三步: + * 1.分配内存空间 + * 2.初始化对象 + * 3.将instance指向分配好的内存空间 + */ + + } + } + } + + return instance; + } + +} diff --git a/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo05/Singleton_05.java b/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo05/Singleton_05.java new file mode 100644 index 0000000..5c74e27 --- /dev/null +++ b/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo05/Singleton_05.java @@ -0,0 +1,31 @@ +package com.mashibing.singleton.demo05; + +/** + * 单例模式-静态内部类(懒加载) + * 根据静态内部类的特性,同时解决了 延时加载 线程安全的问题,并且代码更加简洁 + * @author spikeCong + * @date 2022/9/5 + **/ +public class Singleton_05 { + + private Singleton_05(){ + + if(SingletonHandler.instance != null){ + throw new RuntimeException("不允许非法访问"); + } + } + + //创建静态内部类 + private static class SingletonHandler{ + + //在静态内部类中创建单例,在装载内部类的时候,才会创建单例对象 + private static Singleton_05 instance = new Singleton_05(); + } + + + public static Singleton_05 getInstance(){ + + return SingletonHandler.instance; + } + +} diff --git a/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo06/Singleton_06.java b/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo06/Singleton_06.java new file mode 100644 index 0000000..e78d2da --- /dev/null +++ b/msb-singleton-pattern-01/src/main/java/com/mashibing/singleton/demo06/Singleton_06.java @@ -0,0 +1,29 @@ +package com.mashibing.singleton.demo06; + +/** + * 单例模式-枚举 + * 阻止反射的破坏: 在反射方法中不允许使用反射创建枚举对象 + * 阻止序列化的破坏: 在序列化的时候仅仅是将枚举对象的name属性输出到了结果中,反序列化的时候,就会通过 + * Enum的 valueOf方法 来根据名字去查找对应枚举对象. + * @author spikeCong + * @date 2022/9/6 + **/ +public enum Singleton_06 { + + INSTANCE; + + private Object data; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public static Singleton_06 getInstance(){ + + return INSTANCE; + } +} diff --git a/msb-singleton-pattern-01/src/test/java/com/mashibing/singleton/test01/Test_Singleton.java b/msb-singleton-pattern-01/src/test/java/com/mashibing/singleton/test01/Test_Singleton.java new file mode 100644 index 0000000..b14e12b --- /dev/null +++ b/msb-singleton-pattern-01/src/test/java/com/mashibing/singleton/test01/Test_Singleton.java @@ -0,0 +1,47 @@ +package com.mashibing.singleton.test01; + +import com.mashibing.singleton.demo01.Singleton_01; +import com.mashibing.singleton.demo02.Singleton_02; +import com.mashibing.singleton.demo03.Singleton_03; +import com.mashibing.singleton.demo04.Singleton_04; +import com.mashibing.singleton.demo05.Singleton_05; +import org.junit.Test; + +/** + * @author spikeCong + * @date 2022/9/5 + **/ +public class Test_Singleton { + + //测试饿汉式 + @Test + public void test01(){ + + Singleton_01 instance = Singleton_01.getInstance(); + + Singleton_01 instance1 = Singleton_01.getInstance(); + + System.out.println(instance == instance1); + } + + //测试-懒汉式 + @Test + public void test02(){ + + for (int i = 0; i < 500; i++) { + new Thread(() -> { +// Singleton_02 instance = Singleton_02.getInstance(); +// System.out.println(Thread.currentThread().getName() + "------" + instance); + + Singleton_04 instance = Singleton_04.getInstance(); + System.out.println(Thread.currentThread().getName() + "------" + instance); + }).start(); + } + } + + @Test + public void test03(){ + + } + +} diff --git a/msb-singleton-pattern-01/src/test/java/com/mashibing/singleton/test02/Test_Reflect.java b/msb-singleton-pattern-01/src/test/java/com/mashibing/singleton/test02/Test_Reflect.java new file mode 100644 index 0000000..50195a2 --- /dev/null +++ b/msb-singleton-pattern-01/src/test/java/com/mashibing/singleton/test02/Test_Reflect.java @@ -0,0 +1,27 @@ +package com.mashibing.singleton.test02; + +import com.mashibing.singleton.demo05.Singleton_05; +import com.mashibing.singleton.demo06.Singleton_06; + +import java.lang.reflect.Constructor; + +/** + * 反射对于单例的破坏 + * @author spikeCong + * @date 2022/9/6 + **/ +public class Test_Reflect { + + public static void main(String[] args) throws Exception { + + Class clazz = Singleton_05.class; + + Constructor constructor = clazz.getDeclaredConstructor(); + constructor.setAccessible(true); //设置为true后,就可以对类中的私有成员进行操作 + +// Singleton_06 instance = constructor.newInstance(); + + Singleton_05 instance = constructor.newInstance(); + System.out.println(instance); + } +} diff --git a/msb-singleton-pattern-01/src/test/java/com/mashibing/singleton/test02/Test_Serializable.java b/msb-singleton-pattern-01/src/test/java/com/mashibing/singleton/test02/Test_Serializable.java new file mode 100644 index 0000000..e8f9499 --- /dev/null +++ b/msb-singleton-pattern-01/src/test/java/com/mashibing/singleton/test02/Test_Serializable.java @@ -0,0 +1,67 @@ +package com.mashibing.singleton.test02; + +import com.mashibing.singleton.demo06.Singleton_06; +import org.junit.Test; + +import java.io.*; + +/** + * 序列化对单例的破坏 + * @author spikeCong + * @date 2022/9/6 + **/ +public class Test_Serializable { + + @Test + public void test() throws Exception{ + + //序列化对象输出流 + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("tempFile.obj")); + oos.writeObject(Singleton_06.getInstance()); + + + //序列化对象输入流 + File file = new File("tempFile.obj"); + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); + Singleton_06 singleton06 = (Singleton_06) ois.readObject(); + + + System.out.println(singleton06); + System.out.println(Singleton_06.getInstance()); + + System.out.println(Singleton_06.getInstance() == singleton06);//false + } +} + + +/** + * 单例类实现序列化接口 + */ +class Singleton implements Serializable { + + private volatile static Singleton singleton; + + private Singleton() { + + } + + public static Singleton getInstance() { + if (singleton == null) { + synchronized (Singleton.class) { + if (singleton == null) { + singleton = new Singleton(); + } + } + } + return singleton; + } + + /** + * 只需要在单例类中定义readResolve方法,就可以解决序列化对于单例的破坏 + * 程序会判断是否有readResolve方法,如果有就执行该方法,如果不存在---就会创建一个新的对象 + */ + private Object readResolve(){ + + return singleton; + } +} \ No newline at end of file diff --git a/msb-singleton-pattern-01/target/classes/META-INF/msb-singleton-pattern-01.kotlin_module b/msb-singleton-pattern-01/target/classes/META-INF/msb-singleton-pattern-01.kotlin_module new file mode 100644 index 0000000..a49347a Binary files /dev/null and b/msb-singleton-pattern-01/target/classes/META-INF/msb-singleton-pattern-01.kotlin_module differ diff --git a/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo01/Singleton_01.class b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo01/Singleton_01.class new file mode 100644 index 0000000..8fd0955 Binary files /dev/null and b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo01/Singleton_01.class differ diff --git a/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo02/Singleton_02.class b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo02/Singleton_02.class new file mode 100644 index 0000000..dc6e756 Binary files /dev/null and b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo02/Singleton_02.class differ diff --git a/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo03/Singleton_03.class b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo03/Singleton_03.class new file mode 100644 index 0000000..f88f544 Binary files /dev/null and b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo03/Singleton_03.class differ diff --git a/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo04/Singleton_04.class b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo04/Singleton_04.class new file mode 100644 index 0000000..fc1d072 Binary files /dev/null and b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo04/Singleton_04.class differ diff --git a/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo05/Singleton_05$1.class b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo05/Singleton_05$1.class new file mode 100644 index 0000000..e3f6a31 Binary files /dev/null and b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo05/Singleton_05$1.class differ diff --git a/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo05/Singleton_05$SingletonHandler.class b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo05/Singleton_05$SingletonHandler.class new file mode 100644 index 0000000..cf75262 Binary files /dev/null and b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo05/Singleton_05$SingletonHandler.class differ diff --git a/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo05/Singleton_05.class b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo05/Singleton_05.class new file mode 100644 index 0000000..3306da4 Binary files /dev/null and b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo05/Singleton_05.class differ diff --git a/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo06/Singleton_06.class b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo06/Singleton_06.class new file mode 100644 index 0000000..3d3366c Binary files /dev/null and b/msb-singleton-pattern-01/target/classes/com/mashibing/singleton/demo06/Singleton_06.class differ diff --git a/msb-singleton-pattern-01/target/test-classes/com/mashibing/singleton/test01/Test_Singleton.class b/msb-singleton-pattern-01/target/test-classes/com/mashibing/singleton/test01/Test_Singleton.class new file mode 100644 index 0000000..fcdae44 Binary files /dev/null and b/msb-singleton-pattern-01/target/test-classes/com/mashibing/singleton/test01/Test_Singleton.class differ diff --git a/msb-singleton-pattern-01/target/test-classes/com/mashibing/singleton/test02/Singleton.class b/msb-singleton-pattern-01/target/test-classes/com/mashibing/singleton/test02/Singleton.class new file mode 100644 index 0000000..48ffde0 Binary files /dev/null and b/msb-singleton-pattern-01/target/test-classes/com/mashibing/singleton/test02/Singleton.class differ diff --git a/msb-singleton-pattern-01/target/test-classes/com/mashibing/singleton/test02/Test_Reflect.class b/msb-singleton-pattern-01/target/test-classes/com/mashibing/singleton/test02/Test_Reflect.class new file mode 100644 index 0000000..f0f24f6 Binary files /dev/null and b/msb-singleton-pattern-01/target/test-classes/com/mashibing/singleton/test02/Test_Reflect.class differ diff --git a/msb-singleton-pattern-01/target/test-classes/com/mashibing/singleton/test02/Test_Serializable.class b/msb-singleton-pattern-01/target/test-classes/com/mashibing/singleton/test02/Test_Serializable.class new file mode 100644 index 0000000..700ad72 Binary files /dev/null and b/msb-singleton-pattern-01/target/test-classes/com/mashibing/singleton/test02/Test_Serializable.class differ diff --git a/msb-singleton-pattern-01/tempFile.obj b/msb-singleton-pattern-01/tempFile.obj new file mode 100644 index 0000000..eae6fb3 Binary files /dev/null and b/msb-singleton-pattern-01/tempFile.obj differ diff --git a/msb-state-pattern-16/msb-state-pattern-16.iml b/msb-state-pattern-16/msb-state-pattern-16.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-state-pattern-16/msb-state-pattern-16.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-state-pattern-16/pom.xml b/msb-state-pattern-16/pom.xml new file mode 100644 index 0000000..4f47076 --- /dev/null +++ b/msb-state-pattern-16/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.mashibing + msb-state-pattern-16 + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + \ No newline at end of file diff --git a/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/Client.java b/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/Client.java new file mode 100644 index 0000000..61cd1b4 --- /dev/null +++ b/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/Client.java @@ -0,0 +1,22 @@ +package com.mashibing.state.example01; + +/** + * @author spikeCong + * @date 2022/10/17 + **/ +public class Client { + + public static void main(String[] args) { + + Context context = new Context(); + + State state1 = new ConcreteStateA(); + state1.handle(context); + System.out.println(context.getCurrentState().toString()); + + System.out.println("================================="); + State state2 = new ConcreteStateB(); + state2.handle(context); + System.out.println(context.getCurrentState().toString()); + } +} diff --git a/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/ConcreteStateA.java b/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/ConcreteStateA.java new file mode 100644 index 0000000..8d1df29 --- /dev/null +++ b/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/ConcreteStateA.java @@ -0,0 +1,19 @@ +package com.mashibing.state.example01; + +/** + * @author spikeCong + * @date 2022/10/17 + **/ +public class ConcreteStateA implements State { + + @Override + public void handle(Context context) { + System.out.println("进入到状态模式A......"); + context.setCurrentState(this); + } + + @Override + public String toString() { + return "当前状态: ConcreteStateA"; + } +} diff --git a/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/ConcreteStateB.java b/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/ConcreteStateB.java new file mode 100644 index 0000000..6bd5aea --- /dev/null +++ b/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/ConcreteStateB.java @@ -0,0 +1,19 @@ +package com.mashibing.state.example01; + +/** + * @author spikeCong + * @date 2022/10/17 + **/ +public class ConcreteStateB implements State { + + @Override + public void handle(Context context) { + System.out.println("进入到状态模式B......"); + context.setCurrentState(this); + } + + @Override + public String toString() { + return "当前状态: ConcreteStateB"; + } +} diff --git a/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/Context.java b/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/Context.java new file mode 100644 index 0000000..e3832e8 --- /dev/null +++ b/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/Context.java @@ -0,0 +1,35 @@ +package com.mashibing.state.example01; + +/** + * 上下文类 + * @author spikeCong + * @date 2022/10/17 + **/ +public class Context { + + //维持一个对状态对象的有引用 + private State currentState; + + public Context() { + this.currentState = null; + } + + public Context(State currentState) { + this.currentState = currentState; + } + + public State getCurrentState() { + return currentState; + } + + public void setCurrentState(State currentState) { + this.currentState = currentState; + } + + @Override + public String toString() { + return "Context{" + + "currentState=" + currentState + + '}'; + } +} diff --git a/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/State.java b/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/State.java new file mode 100644 index 0000000..64d7334 --- /dev/null +++ b/msb-state-pattern-16/src/main/java/com/mashibing/state/example01/State.java @@ -0,0 +1,12 @@ +package com.mashibing.state.example01; + +/** + * 抽象状态接口 + * @author spikeCong + * @date 2022/10/17 + **/ +public interface State { + + //声明抽象方法,不同具体状态类可以有不同的实现 + void handle(Context context); +} diff --git a/msb-state-pattern-16/src/main/java/com/mashibing/state/example02/TrafficLight.java b/msb-state-pattern-16/src/main/java/com/mashibing/state/example02/TrafficLight.java new file mode 100644 index 0000000..7ac6879 --- /dev/null +++ b/msb-state-pattern-16/src/main/java/com/mashibing/state/example02/TrafficLight.java @@ -0,0 +1,48 @@ +package com.mashibing.state.example02; + +/** + * 交通灯类有三种状态 + * 红灯(禁行) ,黄灯(警示),绿灯(同行) + * @author spikeCong + * @date 2022/10/17 + **/ +public class TrafficLight { + + //初始化状态 + private String state = "红色"; + + //切换为绿灯,通行状态 + public void switchToGreen(){ + if("绿".equals(state)){ //当前是绿灯 + System.out.println("当前为绿灯,无需切换!"); + }else if("红".equals(state)){ + System.out.println("红灯不能切换为绿灯"); + }else if("黄".equals(state)){ + state = "绿"; + System.out.println("绿灯亮起...时长: 60秒"); + } + } + + //切换为黄灯,警示状态 + public void switchToYellow() { + if ("黄".equals(state)) { //当前是黄灯 + System.out.println("当前为黄灯,无需切换!"); + } else if ("红".equals(state) || "绿".equals(state)) { + System.out.println("红灯不能切换为绿灯"); + state = "黄"; + System.out.println("黄灯亮起...时长:10秒"); + } + } + + //切换为红灯,禁止状态 + public void switchToRed(){ + if("红".equals(state)){ //当前是红灯 + System.out.println("当前为红灯,无需切换!"); + }else if("绿".equals(state)){ + System.out.println("绿灯不能切换为红灯"); + }else if("黄".equals(state)){ + state = "红"; + System.out.println("红灯亮起...时长: 90秒"); + } + } +} diff --git a/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/Client.java b/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/Client.java new file mode 100644 index 0000000..fd50306 --- /dev/null +++ b/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/Client.java @@ -0,0 +1,16 @@ +package com.mashibing.state.example03; + +/** + * @author spikeCong + * @date 2022/10/17 + **/ +public class Client { + + public static void main(String[] args) { + + TrafficLight trafficLight = new TrafficLight(); + trafficLight.switchToRed(); + trafficLight.switchToGreen(); + trafficLight.switchToYellow(); + } +} diff --git a/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/GreenState.java b/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/GreenState.java new file mode 100644 index 0000000..3dbdb20 --- /dev/null +++ b/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/GreenState.java @@ -0,0 +1,23 @@ +package com.mashibing.state.example03; + +/** + * @author spikeCong + * @date 2022/10/17 + **/ +public class GreenState implements State { + + @Override + public void switchToGreen(TrafficLight trafficLight) { + System.out.println("当前是绿灯,无需切换!"); + } + + @Override + public void switchToYellow(TrafficLight trafficLight) { + System.out.println("黄灯亮起...时长: 10秒"); + } + + @Override + public void switchToRed(TrafficLight trafficLight) { + System.out.println("绿灯不能够切换为红灯!"); + } +} diff --git a/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/RedState.java b/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/RedState.java new file mode 100644 index 0000000..3af4872 --- /dev/null +++ b/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/RedState.java @@ -0,0 +1,23 @@ +package com.mashibing.state.example03; + +/** + * @author spikeCong + * @date 2022/10/17 + **/ +public class RedState implements State { + + @Override + public void switchToGreen(TrafficLight trafficLight) { + System.out.println("红灯不能切换为绿灯!"); + } + + @Override + public void switchToYellow(TrafficLight trafficLight) { + System.out.println("黄灯亮起...时长: 10秒"); + } + + @Override + public void switchToRed(TrafficLight trafficLight) { + System.out.println("当前为红灯,无需切换!"); + } +} diff --git a/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/State.java b/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/State.java new file mode 100644 index 0000000..0a5b609 --- /dev/null +++ b/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/State.java @@ -0,0 +1,17 @@ +package com.mashibing.state.example03; + +import com.mashibing.state.example03.TrafficLight; + +/** + * 交通灯状态接口 + * @author spikeCong + * @date 2022/10/17 + **/ +public interface State { + + void switchToGreen(TrafficLight trafficLight); //切换为绿灯 + + void switchToYellow(TrafficLight trafficLight); //切换为黄灯 + + void switchToRed(TrafficLight trafficLight); //切换为红灯 +} diff --git a/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/TrafficLight.java b/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/TrafficLight.java new file mode 100644 index 0000000..6ac5357 --- /dev/null +++ b/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/TrafficLight.java @@ -0,0 +1,32 @@ +package com.mashibing.state.example03; + +/** + * 交通灯类 + * @author spikeCong + * @date 2022/10/17 + **/ +public class TrafficLight { + + //初始化-红灯 + State state = new RedState(); + + public void setState(State state) { + this.state = state; + } + + //切换为绿灯,通行状态 + public void switchToGreen(){ + state.switchToGreen(this); + } + + //切换为黄灯,警示状态 + public void switchToYellow() { + state.switchToYellow(this); + } + + //切换为红灯,禁止状态 + public void switchToRed(){ + state.switchToRed(this); + } + +} diff --git a/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/YellowState.java b/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/YellowState.java new file mode 100644 index 0000000..382ba58 --- /dev/null +++ b/msb-state-pattern-16/src/main/java/com/mashibing/state/example03/YellowState.java @@ -0,0 +1,23 @@ +package com.mashibing.state.example03; + +/** + * @author spikeCong + * @date 2022/10/17 + **/ +public class YellowState implements State { + + @Override + public void switchToGreen(TrafficLight trafficLight) { + System.out.println("绿灯亮起...时长:60秒!"); + } + + @Override + public void switchToYellow(TrafficLight trafficLight) { + System.out.println("当前是黄灯,无需切换!"); + } + + @Override + public void switchToRed(TrafficLight trafficLight) { + System.out.println("红灯亮起...时长:90秒!"); + } +} diff --git a/msb-state-pattern-16/target/classes/com/mashibing/state/example01/Client.class b/msb-state-pattern-16/target/classes/com/mashibing/state/example01/Client.class new file mode 100644 index 0000000..41d637a Binary files /dev/null and b/msb-state-pattern-16/target/classes/com/mashibing/state/example01/Client.class differ diff --git a/msb-state-pattern-16/target/classes/com/mashibing/state/example01/ConcreteStateA.class b/msb-state-pattern-16/target/classes/com/mashibing/state/example01/ConcreteStateA.class new file mode 100644 index 0000000..363503e Binary files /dev/null and b/msb-state-pattern-16/target/classes/com/mashibing/state/example01/ConcreteStateA.class differ diff --git a/msb-state-pattern-16/target/classes/com/mashibing/state/example01/ConcreteStateB.class b/msb-state-pattern-16/target/classes/com/mashibing/state/example01/ConcreteStateB.class new file mode 100644 index 0000000..d71686e Binary files /dev/null and b/msb-state-pattern-16/target/classes/com/mashibing/state/example01/ConcreteStateB.class differ diff --git a/msb-state-pattern-16/target/classes/com/mashibing/state/example01/Context.class b/msb-state-pattern-16/target/classes/com/mashibing/state/example01/Context.class new file mode 100644 index 0000000..390b1c6 Binary files /dev/null and b/msb-state-pattern-16/target/classes/com/mashibing/state/example01/Context.class differ diff --git a/msb-state-pattern-16/target/classes/com/mashibing/state/example01/State.class b/msb-state-pattern-16/target/classes/com/mashibing/state/example01/State.class new file mode 100644 index 0000000..8fe6781 Binary files /dev/null and b/msb-state-pattern-16/target/classes/com/mashibing/state/example01/State.class differ diff --git a/msb-state-pattern-16/target/classes/com/mashibing/state/example02/TrafficLight.class b/msb-state-pattern-16/target/classes/com/mashibing/state/example02/TrafficLight.class new file mode 100644 index 0000000..91c483f Binary files /dev/null and b/msb-state-pattern-16/target/classes/com/mashibing/state/example02/TrafficLight.class differ diff --git a/msb-state-pattern-16/target/classes/com/mashibing/state/example03/Client.class b/msb-state-pattern-16/target/classes/com/mashibing/state/example03/Client.class new file mode 100644 index 0000000..cd54d4a Binary files /dev/null and b/msb-state-pattern-16/target/classes/com/mashibing/state/example03/Client.class differ diff --git a/msb-state-pattern-16/target/classes/com/mashibing/state/example03/GreenState.class b/msb-state-pattern-16/target/classes/com/mashibing/state/example03/GreenState.class new file mode 100644 index 0000000..a98836c Binary files /dev/null and b/msb-state-pattern-16/target/classes/com/mashibing/state/example03/GreenState.class differ diff --git a/msb-state-pattern-16/target/classes/com/mashibing/state/example03/RedState.class b/msb-state-pattern-16/target/classes/com/mashibing/state/example03/RedState.class new file mode 100644 index 0000000..f267ec5 Binary files /dev/null and b/msb-state-pattern-16/target/classes/com/mashibing/state/example03/RedState.class differ diff --git a/msb-state-pattern-16/target/classes/com/mashibing/state/example03/State.class b/msb-state-pattern-16/target/classes/com/mashibing/state/example03/State.class new file mode 100644 index 0000000..ff2c1b4 Binary files /dev/null and b/msb-state-pattern-16/target/classes/com/mashibing/state/example03/State.class differ diff --git a/msb-state-pattern-16/target/classes/com/mashibing/state/example03/TrafficLight.class b/msb-state-pattern-16/target/classes/com/mashibing/state/example03/TrafficLight.class new file mode 100644 index 0000000..115e216 Binary files /dev/null and b/msb-state-pattern-16/target/classes/com/mashibing/state/example03/TrafficLight.class differ diff --git a/msb-state-pattern-16/target/classes/com/mashibing/state/example03/YellowState.class b/msb-state-pattern-16/target/classes/com/mashibing/state/example03/YellowState.class new file mode 100644 index 0000000..5ced425 Binary files /dev/null and b/msb-state-pattern-16/target/classes/com/mashibing/state/example03/YellowState.class differ diff --git a/msb-strategy-pattern-14/.idea/easyCodeTableSetting.xml b/msb-strategy-pattern-14/.idea/easyCodeTableSetting.xml new file mode 100644 index 0000000..dac3281 --- /dev/null +++ b/msb-strategy-pattern-14/.idea/easyCodeTableSetting.xml @@ -0,0 +1,166 @@ + + + + + + \ No newline at end of file diff --git a/msb-strategy-pattern-14/.idea/misc.xml b/msb-strategy-pattern-14/.idea/misc.xml new file mode 100644 index 0000000..29af3ee --- /dev/null +++ b/msb-strategy-pattern-14/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/msb-strategy-pattern-14/.idea/modules.xml b/msb-strategy-pattern-14/.idea/modules.xml new file mode 100644 index 0000000..e50aa93 --- /dev/null +++ b/msb-strategy-pattern-14/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/msb-strategy-pattern-14/.idea/workspace.xml b/msb-strategy-pattern-14/.idea/workspace.xml new file mode 100644 index 0000000..42768b0 --- /dev/null +++ b/msb-strategy-pattern-14/.idea/workspace.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + 1665655310598 + + + + \ No newline at end of file diff --git a/msb-strategy-pattern-14/msb-strategy-pattern-14.iml b/msb-strategy-pattern-14/msb-strategy-pattern-14.iml new file mode 100644 index 0000000..812d406 --- /dev/null +++ b/msb-strategy-pattern-14/msb-strategy-pattern-14.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-strategy-pattern-14/pom.xml b/msb-strategy-pattern-14/pom.xml new file mode 100644 index 0000000..181774b --- /dev/null +++ b/msb-strategy-pattern-14/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + com.mashibing + msb-strategy-pattern-14 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + org.dom4j + dom4j + 2.1.3 + + + jaxen + jaxen + 1.1.6 + + + \ No newline at end of file diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/Client.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/Client.java new file mode 100644 index 0000000..3152e34 --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/Client.java @@ -0,0 +1,16 @@ +package com.mashibing.strategy.example01; + +/** + * @author spikeCong + * @date 2022/10/13 + **/ +public class Client { + + public static void main(String[] args) { + + Strategy strategyB= new ConcreteStrategyB(); + Context context = new Context(strategyB); + + context.algorithm(); + } +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/ConcreteStrategyA.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/ConcreteStrategyA.java new file mode 100644 index 0000000..d5376d3 --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/ConcreteStrategyA.java @@ -0,0 +1,14 @@ +package com.mashibing.strategy.example01; + +/** + * 具体策略类 + * @author spikeCong + * @date 2022/10/13 + **/ +public class ConcreteStrategyA implements Strategy { + + @Override + public void algorithm() { + System.out.println("执行策略A"); + } +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/ConcreteStrategyB.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/ConcreteStrategyB.java new file mode 100644 index 0000000..dec62de --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/ConcreteStrategyB.java @@ -0,0 +1,13 @@ +package com.mashibing.strategy.example01; + +/** + * @author spikeCong + * @date 2022/10/13 + **/ +public class ConcreteStrategyB implements Strategy { + + @Override + public void algorithm() { + System.out.println("执行策略B"); + } +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/Context.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/Context.java new file mode 100644 index 0000000..6ee7f07 --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/Context.java @@ -0,0 +1,22 @@ +package com.mashibing.strategy.example01; + +/** + * 上下文类: 策略模式的本质就是通过Context类作为控制单元,对不同的策略进行调度分配 + * @author spikeCong + * @date 2022/10/13 + **/ +public class Context { + + //维持一个抽象策略的引用 + private Strategy strategy; + + public Context(Strategy strategy) { + this.strategy = strategy; + } + + + //调用策略类中的算法 + public void algorithm(){ + strategy.algorithm(); + } +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/Strategy.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/Strategy.java new file mode 100644 index 0000000..98c5a85 --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example01/Strategy.java @@ -0,0 +1,11 @@ +package com.mashibing.strategy.example01; + +/** + * 抽象策略类 + * @author spikeCong + * @date 2022/10/13 + **/ +public interface Strategy { + + void algorithm(); +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example02/Client.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example02/Client.java new file mode 100644 index 0000000..98d7f4e --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example02/Client.java @@ -0,0 +1,38 @@ +package com.mashibing.strategy.example02; + +import java.util.List; + +/** + * @author spikeCong + * @date 2022/10/13 + **/ +public class Client { + + public static void main(String[] args) { + + List receiptList = ReceiptBuilder.getReceiptList(); + + //回执类型: MT1101、MT2101、MT4101、MT8104 + for (Receipt receipt : receiptList) { + if("MT1011".equals(receipt.getType())){ + System.out.println("接收到MT1011的回执信息"); + System.out.println("解析回执内容"); + System.out.println("执行业务逻辑A......"); + }else if("MT2101".equals(receipt.getType())){ + System.out.println("接收到MT2101的回执信息"); + System.out.println("解析回执内容"); + System.out.println("执行业务逻辑B......"); + }else if("MT4101".equals(receipt.getType())){ + System.out.println("接收到MT4101的回执信息"); + System.out.println("解析回执内容"); + System.out.println("执行业务逻辑C......"); + }else if("MT8104".equals(receipt.getType())){ + System.out.println("接收到MT8104的回执信息"); + System.out.println("解析回执内容"); + System.out.println("执行业务逻辑D......"); + } + + //......... + } + } +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example02/Receipt.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example02/Receipt.java new file mode 100644 index 0000000..8c756d3 --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example02/Receipt.java @@ -0,0 +1,37 @@ +package com.mashibing.strategy.example02; + +/** + * 回执信息 + * @author spikeCong + * @date 2022/10/13 + **/ +public class Receipt { + + private String message; //回执内容 + + private String type; //回执类型: MT1101、MT2101、MT4101、MT8104 + + public Receipt() { + } + + public Receipt(String message, String type) { + this.message = message; + this.type = type; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example02/ReceiptBuilder.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example02/ReceiptBuilder.java new file mode 100644 index 0000000..c60b436 --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example02/ReceiptBuilder.java @@ -0,0 +1,27 @@ +package com.mashibing.strategy.example02; + +import java.util.ArrayList; +import java.util.List; + +/** + * 回执信息生成类 + * @author spikeCong + * @date 2022/10/13 + **/ +public class ReceiptBuilder { + + public static List getReceiptList(){ + + //模拟回执信息 + List list = new ArrayList<>(); + //MT1101、MT2101、MT4101、MT8104 + list.add(new Receipt("MT1101回执信息","MT1101")); +// list.add(new Receipt("MT2101回执信息","MT2101")); +// list.add(new Receipt("MT4101回执信息","MT4101")); +// list.add(new Receipt("MT8104回执信息","MT8104")); + + //...... + return list; + } + +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/Client.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/Client.java new file mode 100644 index 0000000..46c0fe6 --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/Client.java @@ -0,0 +1,34 @@ +package com.mashibing.strategy.example03; + +import com.mashibing.strategy.example02.Receipt; +import com.mashibing.strategy.example02.ReceiptBuilder; + +import java.util.List; + +/** + * @author spikeCong + * @date 2022/10/13 + **/ +public class Client { + + public static void main(String[] args) { + + //模拟回执 + List receiptList = ReceiptBuilder.getReceiptList(); + + //策略上下文 + ReceiptStrategyContext context = new ReceiptStrategyContext(); + + //策略模式最主要的工作: 将策略的 定义, 创建, 使用这三部分进行了解耦. + for (Receipt receipt : receiptList) { + //获取策略 + ReceiptHandleStrategyFactory.init(); + ReceiptHandleStrategy strategy = ReceiptHandleStrategyFactory.getStrategy(receipt.getType()); + //设置策略 + context.setReceiptHandleStrategy(strategy); + //执行策略 + context.handleReceipt(receipt); + } + + } +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/MT1101ReceiptHandleStrategy.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/MT1101ReceiptHandleStrategy.java new file mode 100644 index 0000000..4a06558 --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/MT1101ReceiptHandleStrategy.java @@ -0,0 +1,16 @@ +package com.mashibing.strategy.example03; + +import com.mashibing.strategy.example02.Receipt; + +/** + * 具体策略类 + * @author spikeCong + * @date 2022/10/13 + **/ +public class MT1101ReceiptHandleStrategy implements ReceiptHandleStrategy { + + @Override + public void handleReceipt(Receipt receipt) { + System.out.println("解析报文MT1101: " + receipt.getMessage()); + } +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/MT2101ReceiptHandleStrategy.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/MT2101ReceiptHandleStrategy.java new file mode 100644 index 0000000..6193d7e --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/MT2101ReceiptHandleStrategy.java @@ -0,0 +1,15 @@ +package com.mashibing.strategy.example03; + +import com.mashibing.strategy.example02.Receipt; + +/** + * @author spikeCong + * @date 2022/10/13 + **/ +public class MT2101ReceiptHandleStrategy implements ReceiptHandleStrategy { + + @Override + public void handleReceipt(Receipt receipt) { + System.out.println("解析报文 MT2101: " + receipt.getMessage()); + } +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/ReceiptHandleStrategy.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/ReceiptHandleStrategy.java new file mode 100644 index 0000000..f05c5a6 --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/ReceiptHandleStrategy.java @@ -0,0 +1,13 @@ +package com.mashibing.strategy.example03; + +import com.mashibing.strategy.example02.Receipt; + +/** + * 回执处理策略接口 + * @author spikeCong + * @date 2022/10/13 + **/ +public interface ReceiptHandleStrategy { + + void handleReceipt(Receipt receipt); +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/ReceiptHandleStrategyFactory.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/ReceiptHandleStrategyFactory.java new file mode 100644 index 0000000..ca74778 --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/ReceiptHandleStrategyFactory.java @@ -0,0 +1,51 @@ +package com.mashibing.strategy.example03; + +import com.mashibing.strategy.example02.Receipt; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Node; +import org.dom4j.io.SAXReader; + +import java.util.HashMap; +import java.util.Map; + +/** + * 策略工厂类 + * @author spikeCong + * @date 2022/10/13 + **/ +public class ReceiptHandleStrategyFactory { + + public ReceiptHandleStrategyFactory() { + } + + //使用Map集合存储策略信息,彻底的消除if...else + private static Map strategyMap; + + //初始化具体策略,保存到map集合 + public static void init(){ + strategyMap = new HashMap<>(); +// strategyMap.put("MT1101",new MT1101ReceiptHandleStrategy()); +// strategyMap.put("MT2101",new MT2101ReceiptHandleStrategy()); + try { + SAXReader reader = new SAXReader(); + String file = "I:\\MSB\\msb_work\\designpattern\\msb-strategy-pattern-14\\src\\main\\resources\\config.xml"; + + Document document = reader.read(file); + Node node = document.selectSingleNode("/confing/className"); + String className = node.getText(); + Class clazz = Class.forName(className); + ReceiptHandleStrategy strategy = (ReceiptHandleStrategy) clazz.newInstance(); + strategyMap.put("MT1101",strategy); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + + //根据回执类型,获取对应的策略对象 + public static ReceiptHandleStrategy getStrategy(String receiptType){ + return strategyMap.get(receiptType); + } +} diff --git a/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/ReceiptStrategyContext.java b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/ReceiptStrategyContext.java new file mode 100644 index 0000000..6693506 --- /dev/null +++ b/msb-strategy-pattern-14/src/main/java/com/mashibing/strategy/example03/ReceiptStrategyContext.java @@ -0,0 +1,24 @@ +package com.mashibing.strategy.example03; + +import com.mashibing.strategy.example02.Receipt; + +/** + * 上下文类, 持有策略接口,决定执行哪一个具体的策略类 + * @author spikeCong + * @date 2022/10/13 + **/ +public class ReceiptStrategyContext { + + private ReceiptHandleStrategy receiptHandleStrategy; + + public void setReceiptHandleStrategy(ReceiptHandleStrategy receiptHandleStrategy) { + this.receiptHandleStrategy = receiptHandleStrategy; + } + + //调用策略类中方法 + public void handleReceipt(Receipt receipt){ + if(receipt != null){ + receiptHandleStrategy.handleReceipt(receipt); + } + } +} diff --git a/msb-strategy-pattern-14/src/main/resources/config.xml b/msb-strategy-pattern-14/src/main/resources/config.xml new file mode 100644 index 0000000..357e0f7 --- /dev/null +++ b/msb-strategy-pattern-14/src/main/resources/config.xml @@ -0,0 +1,4 @@ + + + com.mashibing.strategy.example03.MT1101ReceiptHandleStrategy + \ No newline at end of file diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/Client.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/Client.class new file mode 100644 index 0000000..44d11ae Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/Client.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/ConcreteStrategyA.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/ConcreteStrategyA.class new file mode 100644 index 0000000..c277a30 Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/ConcreteStrategyA.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/ConcreteStrategyB.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/ConcreteStrategyB.class new file mode 100644 index 0000000..8c4ea02 Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/ConcreteStrategyB.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/Context.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/Context.class new file mode 100644 index 0000000..373f8aa Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/Context.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/Strategy.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/Strategy.class new file mode 100644 index 0000000..0878f6e Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example01/Strategy.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example02/Client.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example02/Client.class new file mode 100644 index 0000000..44ea2a2 Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example02/Client.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example02/Receipt.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example02/Receipt.class new file mode 100644 index 0000000..d8e37c9 Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example02/Receipt.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example02/ReceiptBuilder.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example02/ReceiptBuilder.class new file mode 100644 index 0000000..edf5b56 Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example02/ReceiptBuilder.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/Client.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/Client.class new file mode 100644 index 0000000..a4a4128 Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/Client.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/MT1101ReceiptHandleStrategy.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/MT1101ReceiptHandleStrategy.class new file mode 100644 index 0000000..39c3f95 Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/MT1101ReceiptHandleStrategy.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/MT2101ReceiptHandleStrategy.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/MT2101ReceiptHandleStrategy.class new file mode 100644 index 0000000..8f5ad67 Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/MT2101ReceiptHandleStrategy.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/ReceiptHandleStrategy.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/ReceiptHandleStrategy.class new file mode 100644 index 0000000..827b1e4 Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/ReceiptHandleStrategy.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/ReceiptHandleStrategyFactory.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/ReceiptHandleStrategyFactory.class new file mode 100644 index 0000000..d46c2c1 Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/ReceiptHandleStrategyFactory.class differ diff --git a/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/ReceiptStrategyContext.class b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/ReceiptStrategyContext.class new file mode 100644 index 0000000..9e04c2f Binary files /dev/null and b/msb-strategy-pattern-14/target/classes/com/mashibing/strategy/example03/ReceiptStrategyContext.class differ diff --git a/msb-strategy-pattern-14/target/classes/config.xml b/msb-strategy-pattern-14/target/classes/config.xml new file mode 100644 index 0000000..357e0f7 --- /dev/null +++ b/msb-strategy-pattern-14/target/classes/config.xml @@ -0,0 +1,4 @@ + + + com.mashibing.strategy.example03.MT1101ReceiptHandleStrategy + \ No newline at end of file diff --git a/msb-templatemethod-pattern-13/msb-templatemethod-pattern-13.iml b/msb-templatemethod-pattern-13/msb-templatemethod-pattern-13.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-templatemethod-pattern-13/msb-templatemethod-pattern-13.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-templatemethod-pattern-13/pom.xml b/msb-templatemethod-pattern-13/pom.xml new file mode 100644 index 0000000..fee4731 --- /dev/null +++ b/msb-templatemethod-pattern-13/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.mashibing + msb-templatemethod-pattern-13 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example01/AbstractClassTemplate.java b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example01/AbstractClassTemplate.java new file mode 100644 index 0000000..da22574 --- /dev/null +++ b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example01/AbstractClassTemplate.java @@ -0,0 +1,40 @@ +package com.mashibing.templtemethod.example01; + +/** + * 抽象父类 + * @author spikeCong + * @date 2022/10/12 + **/ +public abstract class AbstractClassTemplate { + + void step1(String key){ + System.out.println("在模板类中 -> 执行步骤1"); + if(step2(key)){ + step3(); + }else{ + step4(); + } + + step5(); + } + + boolean step2(String key){ + System.out.println("在模板类中 -> 执行步骤2"); + if("x".equals(key)){ + return true; + } + return false; + } + + abstract void step3(); + + abstract void step4(); + + void step5(){ + System.out.println("在模板类中 -> 执行步骤5"); + } + + void run(String key){ + step1(key); + } +} diff --git a/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example01/ConcreteClassA.java b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example01/ConcreteClassA.java new file mode 100644 index 0000000..de7e842 --- /dev/null +++ b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example01/ConcreteClassA.java @@ -0,0 +1,18 @@ +package com.mashibing.templtemethod.example01; + +/** + * @author spikeCong + * @date 2022/10/12 + **/ +public class ConcreteClassA extends AbstractClassTemplate { + + @Override + void step3() { + System.out.println("在子类A中 -> 执行步骤3"); + } + + @Override + void step4() { + System.out.println("在子类A中 -> 执行步骤4"); + } +} diff --git a/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example01/ConcreteClassB.java b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example01/ConcreteClassB.java new file mode 100644 index 0000000..4651108 --- /dev/null +++ b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example01/ConcreteClassB.java @@ -0,0 +1,18 @@ +package com.mashibing.templtemethod.example01; + +/** + * @author spikeCong + * @date 2022/10/12 + **/ +public class ConcreteClassB extends AbstractClassTemplate { + + @Override + void step3() { + System.out.println("在子类B中 -> 执行步骤3"); + } + + @Override + void step4() { + System.out.println("在子类B中 -> 执行步骤4"); + } +} diff --git a/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example01/Test01.java b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example01/Test01.java new file mode 100644 index 0000000..27acb9f --- /dev/null +++ b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example01/Test01.java @@ -0,0 +1,21 @@ +package com.mashibing.templtemethod.example01; + +/** + * @author spikeCong + * @date 2022/10/12 + **/ +public class Test01 { + + public static void main(String[] args) { + + AbstractClassTemplate concreteClassA = new ConcreteClassA(); + concreteClassA.run("x"); + + System.out.println("================"); + + AbstractClassTemplate concreteClassB = new ConcreteClassB(); + concreteClassB.run(""); + + + } +} diff --git a/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example02/Account.java b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example02/Account.java new file mode 100644 index 0000000..97e7763 --- /dev/null +++ b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example02/Account.java @@ -0,0 +1,38 @@ +package com.mashibing.templtemethod.example02; + +/** + * 账户抽象类 + * @author spikeCong + * @date 2022/10/12 + **/ +public abstract class Account { + + //step1 具体方法-验证用户信息是否正确 + public boolean validate(String account,String password){ + System.out.println("账号: " + account + ",密码: " + password); + if(account.equalsIgnoreCase("tom") && password.equalsIgnoreCase("123456")){ + return true; + }else{ + return false; + } + } + + //step2 抽象方法-计算利息 + public abstract void calculate(); + + //step3 具体方法-显示利息 + public void display(){ + System.out.println("显示利息!"); + } + + //模板方法 + public void handle(String account,String password){ + if(!validate(account,password)){ + System.out.println("账户或者密码错误!"); + return; + } + + calculate(); + display(); + } +} diff --git a/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example02/Client.java b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example02/Client.java new file mode 100644 index 0000000..38a005c --- /dev/null +++ b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example02/Client.java @@ -0,0 +1,18 @@ +package com.mashibing.templtemethod.example02; + +/** + * @author spikeCong + * @date 2022/10/12 + **/ +public class Client { + + public static void main(String[] args) { + + Account a1 = new LoanSevenDays(); + a1.handle("tom","123456"); + + System.out.println("==================="); + Account a2 = new LoanOneMonth(); + a2.handle("tom","123456"); + } +} diff --git a/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example02/LoanOneMonth.java b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example02/LoanOneMonth.java new file mode 100644 index 0000000..5fa99a2 --- /dev/null +++ b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example02/LoanOneMonth.java @@ -0,0 +1,14 @@ +package com.mashibing.templtemethod.example02; + +/** + * 借款有一个月 + * @author spikeCong + * @date 2022/10/12 + **/ +public class LoanOneMonth extends Account { + + @Override + public void calculate() { + System.out.println("借款周期30天,利息为借款总额的10%"); + } +} diff --git a/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example02/LoanSevenDays.java b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example02/LoanSevenDays.java new file mode 100644 index 0000000..ce0ef4d --- /dev/null +++ b/msb-templatemethod-pattern-13/src/main/java/com/mashibing/templtemethod/example02/LoanSevenDays.java @@ -0,0 +1,19 @@ +package com.mashibing.templtemethod.example02; + +/** + * 借款七天 + * @author spikeCong + * @date 2022/10/12 + **/ +public class LoanSevenDays extends Account{ + + @Override + public void calculate() { + System.out.println("借款周期7天,无利息! 仅收取贷款金额的1%的服务费"); + } + + @Override + public void display() { + System.out.println("七日借款内无利息!"); + } +} diff --git a/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example01/AbstractClassTemplate.class b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example01/AbstractClassTemplate.class new file mode 100644 index 0000000..962f5d3 Binary files /dev/null and b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example01/AbstractClassTemplate.class differ diff --git a/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example01/ConcreteClassA.class b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example01/ConcreteClassA.class new file mode 100644 index 0000000..41696fc Binary files /dev/null and b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example01/ConcreteClassA.class differ diff --git a/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example01/ConcreteClassB.class b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example01/ConcreteClassB.class new file mode 100644 index 0000000..da17b63 Binary files /dev/null and b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example01/ConcreteClassB.class differ diff --git a/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example01/Test01.class b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example01/Test01.class new file mode 100644 index 0000000..27f85ce Binary files /dev/null and b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example01/Test01.class differ diff --git a/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example02/Account.class b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example02/Account.class new file mode 100644 index 0000000..c4d69be Binary files /dev/null and b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example02/Account.class differ diff --git a/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example02/Client.class b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example02/Client.class new file mode 100644 index 0000000..438d4af Binary files /dev/null and b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example02/Client.class differ diff --git a/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example02/LoanOneMonth.class b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example02/LoanOneMonth.class new file mode 100644 index 0000000..bb42075 Binary files /dev/null and b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example02/LoanOneMonth.class differ diff --git a/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example02/LoanSevenDays.class b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example02/LoanSevenDays.class new file mode 100644 index 0000000..9861cc8 Binary files /dev/null and b/msb-templatemethod-pattern-13/target/classes/com/mashibing/templtemethod/example02/LoanSevenDays.class differ diff --git a/msb-visitor-pattern-18/msb-visitor-pattern-18.iml b/msb-visitor-pattern-18/msb-visitor-pattern-18.iml new file mode 100644 index 0000000..c035f0b --- /dev/null +++ b/msb-visitor-pattern-18/msb-visitor-pattern-18.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/msb-visitor-pattern-18/pom.xml b/msb-visitor-pattern-18/pom.xml new file mode 100644 index 0000000..360e53a --- /dev/null +++ b/msb-visitor-pattern-18/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.mashibing.visitor + msb-visitor-pattern-18 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + \ No newline at end of file diff --git a/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Acceptable.java b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Acceptable.java new file mode 100644 index 0000000..d4e773b --- /dev/null +++ b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Acceptable.java @@ -0,0 +1,12 @@ +package com.mashibing.visitor.example01; + +/** + * 接待者这接口 (抽象元素角色) + * @author spikeCong + * @date 2022/10/18 + **/ +public interface Acceptable { + + //接收所有的Visitor访问者的子类 + public void accept(Visitor visitor); +} diff --git a/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Candy.java b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Candy.java new file mode 100644 index 0000000..ff23709 --- /dev/null +++ b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Candy.java @@ -0,0 +1,21 @@ +package com.mashibing.visitor.example01; + +import java.time.LocalDate; + +/** + * 糖果类 + * @author spikeCong + * @date 2022/10/18 + **/ +public class Candy extends Product implements Acceptable{ + + public Candy(String name, LocalDate produceDate, double price) { + super(name, produceDate, price); + } + + @Override + public void accept(Visitor visitor) { + //在accept方法中调用访问者, 并将自己 this 传递回去. + visitor.visit(this); + } +} diff --git a/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Client.java b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Client.java new file mode 100644 index 0000000..baf1283 --- /dev/null +++ b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Client.java @@ -0,0 +1,45 @@ +package com.mashibing.visitor.example01; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @author spikeCong + * @date 2022/10/18 + **/ +public class Client { + + public static void main(String[] args) { + +// Candy candy = new Candy("德芙巧克力", LocalDate.of(2022, 1, 1), 10.0); +// +// Visitor visitor = new DiscountVisitor(LocalDate.of(2022,10,5)); +// visitor.visit(candy); + + //将3件商品加入购物车 +// List products = Arrays.asList( +// new Candy("金丝猴奶糖",LocalDate.of(2022,10,1),10), +// new Wine("郎酒",LocalDate.of(2022,10,1),1000), +// new Fruit("草莓",LocalDate.of(2022,10,8),50,1) +// ); + +// Visitor visitor = new DiscountVisitor(LocalDate.of(2022,10,5)); +// for (Product product : products) { +// //visitor.visit(); +// } + + //模拟添加多个商品 + List list = Arrays.asList( + new Candy("金丝猴奶糖",LocalDate.of(2022,10,1),10), + new Wine("郎酒",LocalDate.of(2022,10,1),1000), + new Fruit("草莓",LocalDate.of(2022,10,8),50,1) + ); + + Visitor visitor = new DiscountVisitor(LocalDate.of(2022,10,11)); + for (Acceptable product : list) { + product.accept(visitor); + } + } +} diff --git a/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/DiscountVisitor.java b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/DiscountVisitor.java new file mode 100644 index 0000000..3304e73 --- /dev/null +++ b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/DiscountVisitor.java @@ -0,0 +1,63 @@ +package com.mashibing.visitor.example01; + + +import java.text.NumberFormat; +import java.time.LocalDate; + +/** + * 折扣计价访问者类 + * @author spikeCong + * @date 2022/10/18 + **/ +public class DiscountVisitor implements Visitor { + + private LocalDate billDate; + + public DiscountVisitor(LocalDate billDate) { + this.billDate = billDate; + System.out.println("结算日期: " + billDate); + } + + @Override + public void visit(Candy candy) { + System.out.println("糖果: " + candy.getName()); + + //糖果大于180天,禁止售卖,否则糖果一律九折 + long days = billDate.toEpochDay() - candy.getProduceDate().toEpochDay(); + + if(days > 180){ + System.out.println("超过半年的糖果,请勿食用!"); + }else{ + double realPrice = candy.getPrice() * 0.9; + System.out.println("糖果打折后的价格为: " + + NumberFormat.getCurrencyInstance().format(realPrice)); + } + } + + @Override + public void visit(Wine wine) { + System.out.println("酒类: " + wine.getName() +",无折扣价格!"); + System.out.println("原价售卖: " + + NumberFormat.getCurrencyInstance().format(wine.getPrice())); + } + + @Override + public void visit(Fruit fruit) { + System.out.println("水果: " + fruit.getName()); + + long days = billDate.toEpochDay() - fruit.getProduceDate().toEpochDay(); + + double rate = 0; + if(days > 7){ + System.out.println("超过七天的水果,请勿食用!"); + }else if(days > 3){ + rate = 0.5; + }else{ + rate = 1; + } + + double realPrice = fruit.getPrice() * fruit.getWeight() * rate; + System.out.println("水果价格为: " + + NumberFormat.getCurrencyInstance().format(realPrice)); + } +} diff --git a/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Fruit.java b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Fruit.java new file mode 100644 index 0000000..205e36d --- /dev/null +++ b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Fruit.java @@ -0,0 +1,31 @@ +package com.mashibing.visitor.example01; + +import java.time.LocalDate; + +/** + * 水果类 + * @author spikeCong + * @date 2022/10/18 + **/ +public class Fruit extends Product implements Acceptable{ + + private double weight; //重量 + + public Fruit(String name, LocalDate produceDate, double price, double weight) { + super(name, produceDate, price); + this.weight = weight; + } + + public double getWeight() { + return weight; + } + + public void setWeight(double weight) { + this.weight = weight; + } + + @Override + public void accept(Visitor visitor) { + visitor.visit(this); + } +} diff --git a/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Product.java b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Product.java new file mode 100644 index 0000000..ea63ed1 --- /dev/null +++ b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Product.java @@ -0,0 +1,47 @@ +package com.mashibing.visitor.example01; + +import java.time.LocalDate; + +/** + * 抽象商品父类 + * @author spikeCong + * @date 2022/10/18 + **/ +public abstract class Product { + + private String name; //商品名 + + private LocalDate produceDate; //生产日期 + + private double price; //商品价格 + + public Product(String name, LocalDate produceDate, double price) { + this.name = name; + this.produceDate = produceDate; + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public LocalDate getProduceDate() { + return produceDate; + } + + public void setProduceDate(LocalDate produceDate) { + this.produceDate = produceDate; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } +} diff --git a/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Visitor.java b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Visitor.java new file mode 100644 index 0000000..187b922 --- /dev/null +++ b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Visitor.java @@ -0,0 +1,15 @@ +package com.mashibing.visitor.example01; + +/** + * 访问者接口 - 根据入参的不同调用对应的重载方法 + * @author spikeCong + * @date 2022/10/18 + **/ +public interface Visitor { + + public void visit(Candy candy); //糖果重载方法 + + public void visit(Wine wine); //酒类重载方法 + + public void visit(Fruit fruit); //水果重载方法 +} diff --git a/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Wine.java b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Wine.java new file mode 100644 index 0000000..70eebea --- /dev/null +++ b/msb-visitor-pattern-18/src/main/java/com/mashibing/visitor/example01/Wine.java @@ -0,0 +1,20 @@ +package com.mashibing.visitor.example01; + +import java.time.LocalDate; + +/** + * 酒水类 + * @author spikeCong + * @date 2022/10/18 + **/ +public class Wine extends Product implements Acceptable{ + + public Wine(String name, LocalDate produceDate, double price) { + super(name, produceDate, price); + } + + @Override + public void accept(Visitor visitor) { + visitor.visit(this); + } +} diff --git a/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Acceptable.class b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Acceptable.class new file mode 100644 index 0000000..f10f987 Binary files /dev/null and b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Acceptable.class differ diff --git a/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Candy.class b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Candy.class new file mode 100644 index 0000000..46de9d4 Binary files /dev/null and b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Candy.class differ diff --git a/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Client.class b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Client.class new file mode 100644 index 0000000..e0cf1fc Binary files /dev/null and b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Client.class differ diff --git a/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/DiscountVisitor.class b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/DiscountVisitor.class new file mode 100644 index 0000000..4b6c0ae Binary files /dev/null and b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/DiscountVisitor.class differ diff --git a/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Fruit.class b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Fruit.class new file mode 100644 index 0000000..7893b9e Binary files /dev/null and b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Fruit.class differ diff --git a/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Product.class b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Product.class new file mode 100644 index 0000000..6a3fc3b Binary files /dev/null and b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Product.class differ diff --git a/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Visitor.class b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Visitor.class new file mode 100644 index 0000000..12d7360 Binary files /dev/null and b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Visitor.class differ diff --git a/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Wine.class b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Wine.class new file mode 100644 index 0000000..fd182a6 Binary files /dev/null and b/msb-visitor-pattern-18/target/classes/com/mashibing/visitor/example01/Wine.class differ