前言
自始至终觉得编程是一门技术,更是一门艺术,不能只满足于写完代码运行后的结果完事,还应该让后人能读懂,容易修改,容易扩展和复用。又由于自己经常写出屎山代码,所以准备苦学设计模式,尽力写出更加优雅的代码。
正文
问题引入
定义一个商场收银软件,营业员根据客户所购买的商品的数目和单价来计算出费用,其中有原价购买,打折购买和满多少返现多少。要求写出容易扩展,代码可复用的程序。
简单来说就是创建一个工厂的类,用户需要什么就给他什么类型,通过多态来实现
1.定义一个超类,方便后续生成其他的子类来实现商场打折。
package com.ffyc.designpattern.day01easyfactory;
/**
- Created with IntelliJ IDEA.
* - @Author: Coderfine
- @Date: 2023/07/24/23:25
@Description:定义首付款的超类
*/
public abstract class CacheSuper {public abstract double acceptCash(double money);
}
2 定义一个工厂类,通过主函数传递的参数来判断生产哪一个具体的实现类。
package com.ffyc.designpattern.day01easyfactory;
/**
- Created with IntelliJ IDEA.
* - @Author: Coderfine
- @Date: 2023/07/24/23:40
- @Description:定义收取现金的工厂类
*/
public class CashFactory {
public static CacheSuper createCashFactory(String type) {
}//声明顶级父类 CacheSuper cs = null; switch (type) { case "正常收费": cs = new NormalCash(); break; case "满300减100": cs = new CashReturn(300d,100d); break; case "打8折" : cs = new CashRebate(0.8); break; default: cs = new NormalCash(); } return cs;
}
3.主函数的入口
package com.ffyc.designpattern.day01easyfactory;
import java.util.Scanner;
/**
- Created with IntelliJ IDEA.
* - @Author: Coderfine
- @Date: 2023/07/24/23:22
- @Description:定义启动类
@Recommand:定义一个商场收银软件,营业员根据客户所购买的商品的数目和单价来计算出费用,
*/
public class main {
public static void main(String[] args) {int num = 6; int price = 100; double total = 0; meau(); String type = ""; Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); switch (choice) { case 1: type = "正常收费"; break; case 2: type = "满300减100"; break; case 3: type = "打8折"; break; } //返回一个超类 CacheSuper cacheSuper = CashFactory.createCashFactory(type); //超类实现方法 total = cacheSuper.acceptCash((double) price * num); System.out.println(total);
}
public static void meau() {
System.out.println("1.正常收费"); System.out.println("2.满300减100"); System.out.println("3.打8折");
}
}
4定义正常原价购买的实现类
package com.ffyc.designpattern.day01easyfactory;
/**
- Created with IntelliJ IDEA.
* - @Author: Coderfine
- @Date: 2023/07/24/23:27
@Description:这是一个正常收费的类,来集成我的超类
*/
public class NormalCash extends CacheSuper{//因为是正常收费所以直接返回
@Override
public double acceptCash(double money) {return money;
}
}
5.定义打折消费的实现类
package com.ffyc.designpattern.day01easyfactory;
/**
- Created with IntelliJ IDEA.
* - @Author: Coderfine
- @Date: 2023/07/24/23:28
@Description:定义打折类
*/
public class CashRebate extends CacheSuper{private double Rebate = 1d;
//初始化的时候传入打折力度
public CashRebate (double Rebate){this.Rebate = Rebate;
}
@Override
public double acceptCash(double money) {return money*this.Rebate;
}
}
6.定义满多少就返现的实现类
package com.ffyc.designpattern.day01easyfactory;
/**
- Created with IntelliJ IDEA.
* - @Author: Coderfine
- @Date: 2023/07/24/23:33
@Description:满多少前就返现的类
*/
public class CashReturn extends CacheSuper {
private double moneyCondition = 0.0d;
private double moneyReturn = 0.0d;public CashReturn(double moneyCondition, double moneyReturn) {
this.moneyCondition = moneyCondition; this.moneyReturn = moneyReturn;
}
@Override
public double acceptCash(double money) {double result = money; if(money >= moneyCondition) { result = money-(money/moneyCondition)*moneyReturn; } return result;
}
}这样就完成了一个简单的工厂模式,使得代码更容易扩展,打折,满多少返现都非常简单,甚至再添加一种新的促销方式,也只是再添加一个Cash****的类,Factory类再加一个分支就可以了,算得上优雅的代码。 但是每次扩张都要维护工厂,以至于代码需要重新进行部署,也是很麻烦的,命运的齿轮开始转动,设计模式的学习之路开始了。