用抽象工厂模式实现服装工厂类。Java
时间: 2024-05-22 21:12:15 浏览: 156
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();
}
}
阅读全文