假设我们正在开发一个电商网站,我们需要实现一个购物车系统。为了满足客户的需求,我们决定使用单例模式来确保购物车只有一个实例。我们还使用工厂模式来创建商品对象,并使用装饰器模式来添加额外的商品属性,如颜色、尺寸和材质等。 为了实现购物车的实时更新,我们使用观察者模式来通知购物车中的商品变化。当客户选择一种支付方式时,我们使用策略模式来选择相应的付款策略,例如信用卡、支付宝或微信支付等。的具体实现代码java
时间: 2024-03-25 13:35:42 浏览: 25
好的,以下是一个简单的 Java 实现购物车系统的示例代码,包括单例模式、工厂模式、装饰器模式、观察者模式和策略模式。请注意,这只是一个示例,实际情况下实现可能更为复杂:
```
// 购物车类,使用单例模式确保只有一个实例
public class ShoppingCart {
private static ShoppingCart instance;
private List<Item> items;
private ShoppingCart() {
items = new ArrayList<>();
}
public static synchronized ShoppingCart getInstance() {
if (instance == null) {
instance = new ShoppingCart();
}
return instance;
}
public void addItem(Item item) {
items.add(item);
notifyObservers();
}
public void removeItem(Item item) {
items.remove(item);
notifyObservers();
}
public List<Item> getItems() {
return items;
}
// 观察者模式,通知购物车中的商品变化
private List<Observer> observers = new ArrayList<>();
public void addObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void notifyObservers() {
for (Observer observer : observers) {
observer.update();
}
}
// 策略模式,选择相应的付款策略
private PaymentStrategy paymentStrategy;
public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
this.paymentStrategy = paymentStrategy;
}
public void pay() {
if (paymentStrategy != null) {
paymentStrategy.pay(getTotalCost());
}
}
public double getTotalCost() {
double total = 0;
for (Item item : items) {
total += item.getPrice();
}
return total;
}
}
// 商品类,使用工厂模式创建对象
public abstract class Item {
private String name;
private double price;
public Item(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
public class Clothing extends Item {
public Clothing(String name, double price) {
super(name, price);
}
}
public class Electronic extends Item {
public Electronic(String name, double price) {
super(name, price);
}
}
public class ItemFactory {
public static Item createItem(String type, String name, double price) {
switch (type) {
case "clothing":
return new Clothing(name, price);
case "electronic":
return new Electronic(name, price);
default:
throw new IllegalArgumentException("Invalid item type: " + type);
}
}
}
// 装饰器模式,添加额外的商品属性
public abstract class ItemDecorator extends Item {
private Item item;
public ItemDecorator(Item item) {
super(item.getName(), item.getPrice());
this.item = item;
}
public Item getItem() {
return item;
}
}
public class ColorDecorator extends ItemDecorator {
private String color;
public ColorDecorator(Item item, String color) {
super(item);
this.color = color;
}
public String getColor() {
return color;
}
}
public class SizeDecorator extends ItemDecorator {
private String size;
public SizeDecorator(Item item, String size) {
super(item);
this.size = size;
}
public String getSize() {
return size;
}
}
public class MaterialDecorator extends ItemDecorator {
private String material;
public MaterialDecorator(Item item, String material) {
super(item);
this.material = material;
}
public String getMaterial() {
return material;
}
}
// 策略模式,定义付款策略接口
public interface PaymentStrategy {
void pay(double amount);
}
public class CreditCardStrategy implements PaymentStrategy {
private String cardNumber;
private String cvv;
private String expirationDate;
public CreditCardStrategy(String cardNumber, String cvv, String expirationDate) {
this.cardNumber = cardNumber;
this.cvv = cvv;
this.expirationDate = expirationDate;
}
@Override
public void pay(double amount) {
// 实现信用卡付款逻辑
}
}
public class AlipayStrategy implements PaymentStrategy {
private String username;
private String password;
public AlipayStrategy(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public void pay(double amount) {
// 实现支付宝付款逻辑
}
}
public class WechatPayStrategy implements PaymentStrategy {
private String openid;
public WechatPayStrategy(String openid) {
this.openid = openid;
}
@Override
public void pay(double amount) {
// 实现微信支付逻辑
}
}
```
请注意,这只是一个简单的示例代码,实际情况下可能会更为复杂。希望能对你有所帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)