这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
package com.bolingcavalry.decorator;
public interface Coffee {
/**
* 咖啡名称
* @return
*/
String name();
/**
* 当前咖啡的价格
* @return
*/
int getPrice();
}
package com.bolingcavalry.decorator.impl;
import com.bolingcavalry.decorator.Coffee;
import javax.enterprise.context.ApplicationScoped;
/**
* 意式浓缩咖啡,价格3美元
*/
@ApplicationScoped
public class Espresso implements Coffee {
@Override
public String name() {
return "Espresso";
}
@Override
public int getPrice() {
return 3;
}
}
package com.bolingcavalry.decorator.impl;
import com.bolingcavalry.decorator.Coffee;
import io.quarkus.arc.Priority;
import io.quarkus.logging.Log;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.inject.Inject;
@Decorator
@Priority(11)
public class Latte implements Coffee {
/**
* 牛奶价格:2美元
*/
private static final int MILK_PRICE = 2;
@Delegate
@Inject
Coffee delegate;
@Override
public String name() {
return "Latte";
}
@Override
public int getPrice() {
// 将Latte的代理类打印出来,看quarkus注入的是否正确
Log.info("Latte's delegate type : " + this.delegate.name());
return delegate.getPrice() + MILK_PRICE;
}
}
package com.bolingcavalry.decorator.impl;
import com.bolingcavalry.decorator.Coffee;
import io.quarkus.arc.Priority;
import io.quarkus.logging.Log;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.inject.Inject;
/**
* 焦糖玛奇朵:拿铁+焦糖
*/
@Decorator
@Priority(10)
public class CaramelMacchiato implements Coffee {
/**
* 焦糖价格:1美元
*/
private static final int CARAMEL_PRICE = 1;
@Delegate
@Inject
Coffee delegate;
@Override
public String name() {
return "CaramelMacchiato";
}
@Override
public int getPrice() {
// 将CaramelMacchiato的代理类打印出来,看quarkus注入的是否正确
Log.infov("CaramelMacchiato's delegate type : " + this.delegate.name());
return delegate.getPrice() + CARAMEL_PRICE;
}
}
@Delegate
@Inject
Coffee delegate;
package com.bolingcavalry;
import com.bolingcavalry.decorator.Coffee;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import javax.inject.Inject;
@QuarkusTest
public class DecoratorTest {
@Inject
Coffee coffee;
@Test
public void testDecoratorPrice() {
Assertions.assertEquals(6, coffee.getPrice());
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://xinchen.blog.csdn.net/article/details/124234802
内容来源于网络,如有侵权,请联系作者删除!