From 6a72751bd4a5559502964dca2aa031a842fdd5bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=E5=98=89=E9=98=B3?= Date: Tue, 18 Sep 2018 17:02:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E9=97=AD=E5=8E=9F=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.name | 1 + .idea/compiler.xml | 13 +++ .idea/encodings.xml | 6 ++ .idea/inspectionProfiles/Project_Default.xml | 36 ++++++++ .idea/misc.xml | 14 +++ .idea/vcs.xml | 6 ++ Design-pattern.iml | 2 + pom.xml | 86 +++++++++++++++++++ src/main/java/top/fjy8018/App.java | 10 +++ .../principle/openclose/Course.java | 35 ++++++++ .../principle/openclose/JavaCourse.java | 47 ++++++++++ .../openclose/JavaDiscountCourse.java | 53 ++++++++++++ src/test/java/top/fjy8018/AppTest.java | 18 ++++ .../principle/openclose/JavaCourseTest.java | 39 +++++++++ 14 files changed, 366 insertions(+) create mode 100644 .idea/.name create mode 100644 .idea/compiler.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 Design-pattern.iml create mode 100644 pom.xml create mode 100644 src/main/java/top/fjy8018/App.java create mode 100644 src/main/java/top/fjy8018/designpattern/principle/openclose/Course.java create mode 100644 src/main/java/top/fjy8018/designpattern/principle/openclose/JavaCourse.java create mode 100644 src/main/java/top/fjy8018/designpattern/principle/openclose/JavaDiscountCourse.java create mode 100644 src/test/java/top/fjy8018/AppTest.java create mode 100644 src/test/java/top/fjy8018/designpattern/principle/openclose/JavaCourseTest.java diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..1b623a0 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +designpattern \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..bd82cea --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..b26911b --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..6560a98 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4b661a5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Design-pattern.iml b/Design-pattern.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/Design-pattern.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d6dc414 --- /dev/null +++ b/pom.xml @@ -0,0 +1,86 @@ + + + + 4.0.0 + + top.fjy8018 + designpattern + 1.0-SNAPSHOT + + designpattern + + + UTF-8 + 1.7 + 1.7 + + + + + + org.junit.jupiter + junit-jupiter-api + 5.3.1 + test + + + + + org.projectlombok + lombok + 1.18.2 + + + + + ch.qos.logback + logback-classic + 1.2.3 + + + + + org.apache.logging.log4j + log4j-core + 2.11.0 + + + + + + + + + maven-clean-plugin + 3.0.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.7.0 + + + maven-surefire-plugin + 2.20.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + + diff --git a/src/main/java/top/fjy8018/App.java b/src/main/java/top/fjy8018/App.java new file mode 100644 index 0000000..535393c --- /dev/null +++ b/src/main/java/top/fjy8018/App.java @@ -0,0 +1,10 @@ +package top.fjy8018; + +/** + * 设计模式实践引导类 + */ +public class App { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/main/java/top/fjy8018/designpattern/principle/openclose/Course.java b/src/main/java/top/fjy8018/designpattern/principle/openclose/Course.java new file mode 100644 index 0000000..62de72d --- /dev/null +++ b/src/main/java/top/fjy8018/designpattern/principle/openclose/Course.java @@ -0,0 +1,35 @@ +package top.fjy8018.designpattern.principle.openclose; + +import java.math.BigDecimal; + +/** + * 课程接口 + * 对于面向接口编程,接口应当是稳定的 + * 根据开闭原则,在此体现为对接口的增加和修改时关闭的,因为会导致所有实现类的变化 + * 而对课程类的扩展(extend)是开放的 + * + * @author F嘉阳 + * @date 2018-09-18 16:28 + */ +public interface Course { + /** + * 获取课程ID + * + * @return + */ + Integer getId(); + + /** + * 获取课程名称 + * + * @return + */ + String getName(); + + /** + * 获取课程价格 + * + * @return + */ + BigDecimal getPrice(); +} diff --git a/src/main/java/top/fjy8018/designpattern/principle/openclose/JavaCourse.java b/src/main/java/top/fjy8018/designpattern/principle/openclose/JavaCourse.java new file mode 100644 index 0000000..53fa470 --- /dev/null +++ b/src/main/java/top/fjy8018/designpattern/principle/openclose/JavaCourse.java @@ -0,0 +1,47 @@ +package top.fjy8018.designpattern.principle.openclose; + +import java.math.BigDecimal; + +/** + * 课程实现类 + * + * @author F嘉阳 + * @date 2018-09-18 16:30 + */ +public class JavaCourse implements Course { + private Integer id; + + private String name; + + private BigDecimal price; + + public JavaCourse(Integer id, String name, BigDecimal price) { + this.id = id; + this.name = name; + this.price = price; + } + + @Override + public Integer getId() { + return this.id; + } + + @Override + public String getName() { + return this.name; + } + + @Override + public BigDecimal getPrice() { + return this.price; + } + + @Override + public String toString() { + return "JavaCourse{" + + "id=" + id + + ", name='" + name + '\'' + + ", price=" + price.setScale(2, BigDecimal.ROUND_HALF_UP) + + '}'; + } +} diff --git a/src/main/java/top/fjy8018/designpattern/principle/openclose/JavaDiscountCourse.java b/src/main/java/top/fjy8018/designpattern/principle/openclose/JavaDiscountCourse.java new file mode 100644 index 0000000..d0b1f27 --- /dev/null +++ b/src/main/java/top/fjy8018/designpattern/principle/openclose/JavaDiscountCourse.java @@ -0,0 +1,53 @@ +package top.fjy8018.designpattern.principle.openclose; + +import java.math.BigDecimal; + +/** + * Java课程促销 + * 根据开闭原则:对于扩展是开放的,对于修改是关闭的 + * 故通过继承方式实现 + * + * @author F嘉阳 + * @date 2018-09-18 16:43 + */ +public class JavaDiscountCourse extends JavaCourse { + + public JavaDiscountCourse(Integer id, String name, BigDecimal price) { + super(id, name, price); + } + + @Override + public Integer getId() { + return super.getId(); + } + + @Override + public String getName() { + return super.getName(); + } + + /** + * 获取原价 + * + * @return + */ + public BigDecimal getOriginPrice() { + return super.getPrice(); + } + + /** + * 实现打折价格逻辑 + * + * @return + */ + @Override + public BigDecimal getPrice() { + // BigDecimal推荐使用String构造器,防止精度转换问题 + return super.getPrice().multiply(new BigDecimal("0.8")); + } + + @Override + public String toString() { + return super.toString(); + } +} diff --git a/src/test/java/top/fjy8018/AppTest.java b/src/test/java/top/fjy8018/AppTest.java new file mode 100644 index 0000000..9921fd3 --- /dev/null +++ b/src/test/java/top/fjy8018/AppTest.java @@ -0,0 +1,18 @@ +package top.fjy8018; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +/** + * Unit test for simple App. + */ +public class AppTest { + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() { + assertTrue(true); + } +} diff --git a/src/test/java/top/fjy8018/designpattern/principle/openclose/JavaCourseTest.java b/src/test/java/top/fjy8018/designpattern/principle/openclose/JavaCourseTest.java new file mode 100644 index 0000000..ac92524 --- /dev/null +++ b/src/test/java/top/fjy8018/designpattern/principle/openclose/JavaCourseTest.java @@ -0,0 +1,39 @@ +package top.fjy8018.designpattern.principle.openclose; + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.math.BigDecimal; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * 开闭原则单元测试 + */ +@Slf4j +class JavaCourseTest { + + /** + * 普通情况 + */ + @Test + public void normal() { + Course javaCourse = new JavaCourse(1, "Java课程", new BigDecimal("99")); + log.info("课程Id:{},课程名称:{},价格:{}", javaCourse.getId(), javaCourse.getName(), javaCourse.getPrice()); + } + + /** + * 促销打折情况 + */ + @Test + public void discount() { + Course course = new JavaDiscountCourse(1, "Java课程", new BigDecimal("99")); + JavaDiscountCourse javaCourse = (JavaDiscountCourse) course; + log.info("课程Id:{},课程名称:{},原价:{},折后价格:{}", + javaCourse.getId(), javaCourse.getName(), javaCourse.getOriginPrice(), + javaCourse.getPrice().setScale(2, BigDecimal.ROUND_HALF_UP)); + } + +} \ No newline at end of file