java语言使用组合模式描述商场的服装店,专卖店,衣服,裤子的关系,设计代码
时间: 2023-06-03 09:01:29 浏览: 52
Sure, I can try to help you with that! Here's some example Java code for the Composite pattern applied to a clothing store:
```
public interface StoreComponent {
public double getPrice();
}
public class SpecificStore implements StoreComponent {
private double price;
public SpecificStore(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
}
public class StoreComposite implements StoreComponent {
private List<StoreComponent> components;
public StoreComposite() {
components = new ArrayList<StoreComponent>();
}
public void addComponent(StoreComponent component) {
components.add(component);
}
public void removeComponent(StoreComponent component) {
components.remove(component);
}
public double getPrice() {
double total = 0;
for (StoreComponent component : components) {
total += component.getPrice();
}
return total;
}
}
public class ClothingStore extends StoreComposite {
public ClothingStore() {
addComponent(new SpecificStore(50)); // price for clothing store
}
}
public class SpecialtyStore extends StoreComposite {
public SpecialtyStore() {
addComponent(new SpecificStore(100)); // price for specialty store
}
}
public class Shirt implements StoreComponent {
private double price;
public Shirt(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
}
public class Pants implements StoreComponent {
private double price;
public Pants(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
}
```
With this code, you can create various stores and clothing items and add them together to calculate the total price. For example:
```
ClothingStore clothingStore = new ClothingStore();
SpecialtyStore specialtyStore = new SpecialtyStore();
Shirt shirt1 = new Shirt(20);
Shirt shirt2 = new Shirt(25);
Pants pants1 = new Pants(30);
clothingStore.addComponent(shirt1);
clothingStore.addComponent(shirt2);
specialtyStore.addComponent(pants1);
StoreComposite mainStore = new StoreComposite();
mainStore.addComponent(clothingStore);
mainStore.addComponent(specialtyStore);
System.out.println("Total price: $" + mainStore.getPrice());
```
Output: `Total price: $225.0`
I hope that helps! Let me know if you have any more questions.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![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_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)