我正在尝试学习Java中的责任链模式,如果后续代码不存在,则在下面的代码中,它给出// Products.
const input = [
{
'name': 'Triblend Android T-Shirt',
'id': '12345',
'price': '15.00',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Black',
'quantity': 1,
},
{
'name': 'Triblend Android T-Shirt',
'id': '12345',
'price': '15.00',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Black',
'quantity': 1
},
{
'name': 'Triblend Android T-Shirt',
'id': '12345',
'price': '15.00',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Black',
'quantity': 1
}
]
// Condense.
const condense = (products) => Object.values(products.reduce((total, product) => {
const {id} = product
const existing = total[id]
if (existing) {
const quantity = parseFloat(existing.quantity) + parseFloat(product.quantity)
const price = (quantity * parseFloat(product.price)).toFixed(2)
return {
...total,
[id]: {
...existing,
quantity,
price
}
}
}
return {...total, [id]: product}
}, {}))
// Proof.
const output = condense(input)
console.log(output)
。我想检查后继是否像这样存在
NullPointerException
我该怎么做?
if(this.sucessor exists) {
this.successor.check(Home)
}
答案 0 :(得分:1)
难道不是很明显吗?
public void next(HomeStatus Home){
if(this.successor != null){
this.successor.check(Home);
}
}