Compare commits

...

No commits in common. 'master' and 'main' have entirely different histories.
master ... main

1
1

@ -1 +0,0 @@
Subproject commit 43e557cf39f84ccbf9ca62957def1729424b6502

1
2

@ -1 +0,0 @@
Subproject commit f5f32aff1bd200132cf6f9ca8e55f34bf564f923

@ -0,0 +1,2 @@
# ruanjian

@ -1,6 +0,0 @@
public class ConcreteProductA implements Product {
@Override
public void operation() {
System.out.println("ConcreteProductA operation");
}
}

@ -1,6 +0,0 @@
public class ConcreteProductB implements Product {
@Override
public void operation() {
System.out.println("ConcreteProductB operation");
}
}

@ -1,13 +0,0 @@
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;
}
}

@ -1,17 +0,0 @@
public class FactoryTest {
public static void main(String[] args) {
// 测试创建ConcreteProductA
Product productA = Factory.createProduct("A");
System.out.println("创建产品A");
productA.operation();
// 测试创建ConcreteProductB
Product productB = Factory.createProduct("B");
System.out.println("\n创建产品B");
productB.operation();
// 测试无效类型
Product invalidProduct = Factory.createProduct("C");
System.out.println("\n创建无效产品" + (invalidProduct == null ? "返回null" : "失败"));
}
}

@ -1,3 +0,0 @@
public interface Product {
void operation();
}
Loading…
Cancel
Save