提交 aaa05319 编写于 作者: E ex_kongxiang

feat(桥接模式):新增

上级 60c0e777
*.class
*.iml
.idea
out/
target/
\ No newline at end of file
package com.kongxiang.raindrop.dp.type.structure.bridge;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public abstract class AbstractDiscount implements Discount {
protected PriceRange priceRange;
public AbstractDiscount(PriceRange priceRange) {
this.priceRange = priceRange;
}
}
package com.kongxiang.raindrop.dp.type.structure.bridge;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public class Client {
public static void main(String[] args) {
double totalMoney = 10000.0;
// 实现了 6种 实现
AbstractDiscount d1 = new FiveDisCount(new PriceRange1000());
AbstractDiscount d2 = new FiveDisCount(new PriceRange1000To5000());
AbstractDiscount d3 = new FiveDisCount(new PriceRange5000());
AbstractDiscount d4 = new NoDisCount(new PriceRange1000());
AbstractDiscount d5 = new NoDisCount(new PriceRange1000To5000());
AbstractDiscount d6 = new NoDisCount(new PriceRange5000());
d1.calculatePrice(totalMoney);
d2.calculatePrice(totalMoney);
d3.calculatePrice(totalMoney);
d4.calculatePrice(totalMoney);
d5.calculatePrice(totalMoney);
d6.calculatePrice(totalMoney);
}
}
package com.kongxiang.raindrop.dp.type.structure.bridge;
/**
* 折扣维度
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public interface Discount {
/**
* 计算折扣价
* @param totalMoney 总价
* @return 打折后的价格
*/
public double calculatePrice(double totalMoney);
}
package com.kongxiang.raindrop.dp.type.structure.bridge;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public class FiveDisCount extends AbstractDiscount {
public FiveDisCount(PriceRange priceRange) {
super(priceRange);
}
@Override
public double calculatePrice(double totalMoney) {
boolean inRange = super.priceRange.isInRange(totalMoney);
if (inRange){
totalMoney = totalMoney - super.priceRange.returnCase();
System.out.println("符合售价区间返现规则:返现价格" + super.priceRange.returnCase() + "返现后价格 :" + totalMoney);
}else {
System.out.println("不符合返现规则:价格" + totalMoney);
}
System.out.println("计算价格 :" + totalMoney +" x 折扣" + 0.5 +" = " + (totalMoney * 0.5));
return (totalMoney * 0.5);
}
}
package com.kongxiang.raindrop.dp.type.structure.bridge;
/**
* 不打折
*
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public class NoDisCount extends AbstractDiscount {
public NoDisCount(PriceRange priceRange) {
super(priceRange);
}
@Override
public double calculatePrice(double totalMoney) {
boolean inRange = super.priceRange.isInRange(totalMoney);
if (inRange) {
totalMoney = totalMoney - super.priceRange.returnCase();
System.out.println("符合售价区间返现规则:返现价格" + super.priceRange.returnCase() + "返现后价格 :" + totalMoney);
} else {
System.out.println("不符合返现规则:价格" + totalMoney);
}
System.out.println("计算价格 :" + totalMoney + " x 折扣" + 1 + " = " + (totalMoney * 1));
return (totalMoney * 1);
}
}
package com.kongxiang.raindrop.dp.type.structure.bridge;
/**
* 售价区间
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public interface PriceRange {
/**
* 判断是否是当前区间
* @param price 商品售价
* @return 所属区间
*/
public boolean isInRange(double price);
/**
* 根据区间返现
* @return 返现额度
*/
public double returnCase();
}
package com.kongxiang.raindrop.dp.type.structure.bridge;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public class PriceRange1000 implements PriceRange{
@Override
public boolean isInRange(double price) {
return price<1000;
}
@Override
public double returnCase() {
return 5;
}
}
package com.kongxiang.raindrop.dp.type.structure.bridge;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public class PriceRange1000To5000 implements PriceRange{
@Override
public boolean isInRange(double price) {
return price>1000 && price<5000;
}
@Override
public double returnCase() {
return 100;
}
}
package com.kongxiang.raindrop.dp.type.structure.bridge;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public class PriceRange5000 implements PriceRange{
@Override
public boolean isInRange(double price) {
return price>5000;
}
@Override
public double returnCase() {
return 500;
}
}
package com.kongxiang.raindrop.dp.type.structure.bridge.twodimensional;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
@Data
@AllArgsConstructor
public class Point {
private int x;
private int y;
}
package com.kongxiang.raindrop.dp.type.structure.bridge.twodimensional;
import com.kongxiang.raindrop.dp.type.structure.bridge.twodimensional.XAdd1;
import com.kongxiang.raindrop.dp.type.structure.bridge.twodimensional.Y;
import com.kongxiang.raindrop.dp.type.structure.bridge.twodimensional.Y2x;
import lombok.extern.slf4j.Slf4j;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public class Student {
public static void main(String[] args) {
// 练习题
// 创建试卷练习题
//试题: 偏移函数 dx+1 ,y函数 x*2 ,当给定x轴上的点是8的时候,求空间上的点
Y exercise = new Y2x(new XAdd1());
answerQuest(exercise);
exercise = new Y2x(new XMov2());
answerQuest(exercise);
exercise = new Ylog(new XMov2());
answerQuest(exercise);
exercise = new Ylog(new XAdd1());
answerQuest(exercise);
// 假设 x 有 m种扩展算法, y种有n种算法
// 现在将2维拓展到3维,新增一个维度的化,每增加一个三维z的抽象实例
// 就可以新增 mxn 的不同算法题。
// 如果z 有 o 种算法。那么 在三维空间种的算法表达就有 m x n x o 种 具体算法题。
//Z exercise = new Zlog(new Ylog(new XAdd1()));
}
private static void answerQuest(Y exercise) {
Point fxResult = exercise.getFxResult(8);
System.out.println("x " + fxResult.getX() + " : Y " + fxResult.getY());
}
}
package com.kongxiang.raindrop.dp.type.structure.bridge.twodimensional;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public interface X {
/**
* x轴函数
* @param dx
* @return
*/
int offsetFun(int dx);
}
package com.kongxiang.raindrop.dp.type.structure.bridge.twodimensional;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public class XAdd1 implements X {
/**
* x坐标偏移函数
* f(dx)=dx+1
* @param dx
* @return
*/
@Override
public int offsetFun(int dx) {
return dx + 1;
}
}
package com.kongxiang.raindrop.dp.type.structure.bridge.twodimensional;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public class XMov2 implements X {
/**
* x坐标偏移函数
* f(dx)=dx/2
* @param dx
* @return
*/
@Override
public int offsetFun(int dx) {
return dx / 2 ;
}
}
package com.kongxiang.raindrop.dp.type.structure.bridge.twodimensional;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public abstract class Y {
private X fdx;
public Y(X x){
this.fdx = x;
}
private int calcuY(int dx) {
int realX = fdx.offsetFun(dx);
return fx(realX);
}
public Point getFxResult(int x){
return new Point(fdx.offsetFun(x),calcuY(x));
}
/**
* 计算 y=f(x) 的 函数
* @param realX 实际坐标值
* @return 实际y轴坐标
*/
protected abstract int fx(int realX);
}
package com.kongxiang.raindrop.dp.type.structure.bridge.twodimensional;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public class Y2x extends Y{
public Y2x(X x) {
super(x);
}
/**
* f(x) = 2 * x
* @param realX 实际坐标值
* @return
*/
@Override
protected int fx(int realX) {
return realX * 2 ;
}
}
package com.kongxiang.raindrop.dp.type.structure.bridge.twodimensional;
/**
* @author 孔翔
* @since 2023-10-16
* copyright for author : 孔翔 at 2023-10-16
* dp
*/
public class Ylog extends Y{
public Ylog(X x) {
super(x);
}
/**
* f(x)=log(x)
* @param realX 实际坐标值
* @return
*/
@Override
protected int fx(int realX) {
return (int) Math.log(realX *1.0 );
}
}
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="SonarLintModuleSettings">
<option name="uniqueId" value="c0754f24-9258-4617-85c1-cf884e30c2e1" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$/..">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/../test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/../test/java" isTestSource="false" />
</content>
</component>
<component name="SonarLintModuleSettings">
<option name="uniqueId" value="963d936e-8979-414a-98ac-724c2c4fc949" />
</component>
</module>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册