full模式和List模式
时间: 2023-05-17 10:05:07 浏览: 76
在什么情况下使用?
Full模式和List模式是指在Lua中使用的两种不同的迭代器模式。Full模式会返回所有的值,而List模式则只返回第一个值。在使用迭代器时,应根据具体情况选择使用哪种模式。如果需要获取所有的值,则应使用Full模式;如果只需要获取第一个值,则应使用List模式。
相关问题
策略模式 电商 java示例
策略模式(Strategy Pattern)是一种行为型设计模式,它允许在运行时选择算法的行为。在该模式中,算法被封装在独立的策略类中,客户端代码可以通过组合和委托来实现不同的行为。
在电商系统中,可以使用策略模式来实现不同的促销策略,如满减、折扣、赠品等。下面是一个简单的Java示例:
首先,我们定义一个促销策略接口:
```
public interface PromotionStrategy {
double applyDiscount(double price);
}
```
然后,我们实现不同的促销策略:
```
public class DiscountPromotionStrategy implements PromotionStrategy {
private double discount;
public DiscountPromotionStrategy(double discount) {
this.discount = discount;
}
@Override
public double applyDiscount(double price) {
return price * discount;
}
}
public class FullReductionPromotionStrategy implements PromotionStrategy {
private double full;
private double reduction;
public FullReductionPromotionStrategy(double full, double reduction) {
this.full = full;
this.reduction = reduction;
}
@Override
public double applyDiscount(double price) {
if (price >= full) {
return price - reduction;
}
return price;
}
}
public class GiftPromotionStrategy implements PromotionStrategy {
private String gift;
public GiftPromotionStrategy(String gift) {
this.gift = gift;
}
@Override
public double applyDiscount(double price) {
System.out.println("送您一份礼物:" + gift);
return price;
}
}
```
最后,我们在购物车类中使用策略模式来选择不同的促销策略:
```
public class ShoppingCart {
private List<Double> items = new ArrayList<>();
private PromotionStrategy promotionStrategy;
public void addItem(double price) {
items.add(price);
}
public void setPromotionStrategy(PromotionStrategy promotionStrategy) {
this.promotionStrategy = promotionStrategy;
}
public double calculateTotal() {
double total = items.stream().mapToDouble(Double::doubleValue).sum();
if (promotionStrategy != null) {
total = promotionStrategy.applyDiscount(total);
}
return total;
}
}
```
使用示例:
```
ShoppingCart cart = new ShoppingCart();
cart.addItem(100);
cart.addItem(200);
cart.addItem(300);
// 使用折扣策略
cart.setPromotionStrategy(new DiscountPromotionStrategy(0.8));
double total1 = cart.calculateTotal();
System.out.println("折扣后总价:" + total1); // 折扣后总价:480.0
// 使用满减策略
cart.setPromotionStrategy(new FullReductionPromotionStrategy(500, 100));
double total2 = cart.calculateTotal();
System.out.println("满减后总价:" + total2); // 满减后总价:500.0
// 使用赠品策略
cart.setPromotionStrategy(new GiftPromotionStrategy("小熊玩具"));
double total3 = cart.calculateTotal();
System.out.println("赠品后总价:" + total3); // 赠品后总价:600.0
```
这样,我们就可以根据需求灵活地选择不同的促销策略,而不需要修改购物车类的代码。
java手写生产者和消费者模式代码实现
好的,下面是 Java 手写生产者和消费者模式代码实现:
```
public class ProducerConsumerExample {
public static void main(String[] args) {
List<Integer> buffer = new ArrayList<>();
int maxSize = 5;
Thread producerThread = new Thread(new Producer(buffer, maxSize), "Producer");
Thread consumerThread = new Thread(new Consumer(buffer), "Consumer");
producerThread.start();
consumerThread.start();
}
}
class Producer implements Runnable {
private List<Integer> buffer;
private int maxSize;
public Producer(List<Integer> buffer, int maxSize) {
this.buffer = buffer;
this.maxSize = maxSize;
}
public void run() {
for (int i = 0; i < 10; i++) {
try {
produce(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void produce(int i) throws InterruptedException {
synchronized (buffer) {
while (buffer.size() == maxSize) {
System.out.println("Buffer is full. Waiting for consumer to consume...");
buffer.wait();
}
buffer.add(i);
System.out.println("Produced : " + i);
buffer.notifyAll();
}
}
}
class Consumer implements Runnable {
private List<Integer> buffer;
public Consumer(List<Integer> buffer) {
this.buffer = buffer;
}
public void run() {
while (true) {
try {
consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void consume() throws InterruptedException {
synchronized (buffer) {
while (buffer.isEmpty()) {
System.out.println("Buffer is empty. Waiting for producer to produce...");
buffer.wait();
}
int val = buffer.remove(0);
System.out.println("Consumed : " + val);
buffer.notifyAll();
}
}
}
```
希望可以帮到你,若有需要可以继续提问。接下来请问你想问什么?