commit
a8569e29bd
@ -0,0 +1,6 @@
|
||||
public class ConcreteProductA implements Product {
|
||||
@Override
|
||||
public void operation() {
|
||||
System.out.println("ConcreteProductA operation");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
public class ConcreteProductB implements Product {
|
||||
@Override
|
||||
public void operation() {
|
||||
System.out.println("ConcreteProductB operation");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
public class Factory {
|
||||
public static Product createProduct(String type) {
|
||||
if (type == null) {
|
||||
return null;
|
||||
}
|
||||
if (type.equalsIgnoreCase("A")) {
|
||||
return new ConcreteProductA();
|
||||
} else if (type.equalsIgnoreCase("B")) {
|
||||
return new ConcreteProductB();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 添加测试方法
|
||||
public static void main(String[] args) {
|
||||
// 创建产品A
|
||||
Product productA = Factory.createProduct("A");
|
||||
productA.operation();
|
||||
|
||||
// 创建产品B
|
||||
Product productB = Factory.createProduct("B");
|
||||
productB.operation();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
public interface Product {
|
||||
void operation();
|
||||
}
|
||||
Loading…
Reference in new issue