Skip to content

Commit cdef777

Browse files
committed
外观模式
1 parent 4c90cc9 commit cdef777

File tree

8 files changed

+70
-0
lines changed

8 files changed

+70
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.anxpp.designpattern.facade;
2+
//计算优惠
3+
public class Discount {
4+
int getDiscount(String discountCode){
5+
return Math.abs(discountCode.hashCode())%3;
6+
}
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.anxpp.designpattern.facade;
2+
//计费子系统
3+
public class FinalPrice {
4+
ProductPrice productPrice;
5+
Postage postage;
6+
Discount discount;
7+
public FinalPrice(){
8+
productPrice = new ProductPrice();
9+
postage = new Postage();
10+
discount = new Discount();
11+
}
12+
int getFinalPrice(String product,String addr,String discountCode){
13+
return productPrice.getPrice(product)+postage.getPostage(addr)-discount.getDiscount(discountCode);
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.anxpp.designpattern.facade;
2+
//计算邮费
3+
public class Postage {
4+
int getPostage(String addr){
5+
return Math.abs(addr.hashCode())%20+6;//模拟邮费计算
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.anxpp.designpattern.facade;
2+
//获取商品价格
3+
public class ProductPrice {
4+
int getPrice(String product){
5+
return Math.abs(product.hashCode());//模拟获取商品价格
6+
}
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.anxpp.designpattern.facade;
2+
//外观
3+
public enum ProductSalesman {
4+
instance;
5+
Stock stock = new Stock();
6+
FinalPrice finalPrice = new FinalPrice();
7+
Object buySomething(String product,String addr,String discountCode){
8+
if(!stock.hasStock(product))
9+
return "库存不足";
10+
int price = finalPrice.getFinalPrice(product, addr, discountCode);
11+
return "订单信息:" + product + "-" + addr + "-" + discountCode + "-" + price;
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.anxpp.designpattern.facade;
2+
3+
import java.util.Random;
4+
//库存子系统
5+
public class Stock {
6+
boolean hasStock(String product){
7+
return new Random().nextInt(Math.abs(product.hashCode()))>0;//模拟是否还有库存
8+
}
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.anxpp.designpattern.facade;
2+
public class TestUse {
3+
public static void main(String args[]){
4+
Object info = ProductSalesman.instance.buySomething("银河飞船", "地球", "K1234523");
5+
System.out.println(info);
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @author Administrator
3+
* 外观模式
4+
*/
5+
package com.anxpp.designpattern.facade;

0 commit comments

Comments
 (0)