用抽象工厂模式实现服装工厂类。Java
时间: 2024-05-22 21:12:15 浏览: 15
public interface Clothes {
void produce();
}
public class Shirt implements Clothes {
@Override
public void produce() {
System.out.println("生产衬衫");
}
}
public class Trousers implements Clothes {
@Override
public void produce() {
System.out.println("生产裤子");
}
}
public interface ClothesFactory {
Clothes createClothes();
}
public class ShirtFactory implements ClothesFactory {
@Override
public Clothes createClothes() {
return new Shirt();
}
}
public class TrousersFactory implements ClothesFactory {
@Override
public Clothes createClothes() {
return new Trousers();
}
}
public class ClothesProducer {
public static void main(String[] args) {
ClothesFactory shirtFactory = new ShirtFactory();
Clothes shirt = shirtFactory.createClothes();
shirt.produce();
ClothesFactory trousersFactory = new TrousersFactory();
Clothes trousers = trousersFactory.createClothes();
trousers.produce();
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)