开闭原则

This commit is contained in:
2018-09-18 17:02:00 +08:00
commit 6a72751bd4
14 changed files with 366 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
package top.fjy8018;
/**
* 设计模式实践引导类
*/
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

View File

@@ -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();
}

View File

@@ -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) +
'}';
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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));
}
}