java实现常见的三种工厂模式
工厂模式用于创建对象的过程进行封装,通过工厂类来统一创建对象的逻辑。它可以隐藏对象的创建细节,提供一种灵活的方式来创建对象,降低了代码的耦合性。常见的工厂模式包括简单工厂模式、工厂方法模式和抽象工厂模式。
① 抽象产品接口
interface Product {
void operation();
}
②定义具体产品A B
class ProductA implements Product {
public void operation() {
System.out.println("ProductA operation");
}
}
class ProductB implements Product {
public void operation() {
System.out.println("ProductB operation");
}
}
③创建简单工厂类
class SimpleFactory {
public static Product createProduct(String type) {
if (type.equals("A")) {
return new ProductA();
} else if (type.equals("B")) {
return new ProductB();
}
throw new IllegalArgumentException("Invalid product type.");
}
}
④示例使用
public class Main {
public static void main(String[] args) {
Product productA = SimpleFactory.createProduct("A");
productA.operation(); // Output: ProductA operation
Product productB = SimpleFactory.createProduct("B");
productB.operation(); // Output: ProductB operation
}
}
①抽象工厂类
interface Factory {
Product createProduct();
}
②定义具体工厂A B
class FactoryA implements Factory {
public Product createProduct() {
return new ProductA();
}
}
class FactoryB implements Factory {
public Product createProduct() {
return new ProductB();
}
}
③示例使用
public class Main {
public static void main(String[] args) {
Factory factoryA = new FactoryA();
Product productA = factoryA.createProduct();
productA.operation(); // Output: ProductA operation
Factory factoryB = new FactoryB();
Product productB = factoryB.createProduct();
productB.operation(); // Output: ProductB operation
}
}
①抽象产品A B
interface AbstractProductA {
void operationA();
}
interface AbstractProductB {
void operationB();
}
②定义具体产品A1 A2 B1 B2
class ProductA1 implements AbstractProductA {
public void operationA() {
System.out.println("ProductA1 operationA");
}
}
class ProductA2 implements AbstractProductA {
public void operationA() {
System.out.println("ProductA2 operationA");
}
}
class ProductB1 implements AbstractProductB {
public void operationB() {
System.out.println("ProductB1 operationB");
}
}
class ProductB2 implements AbstractProductB {
public void operationB() {
System.out.println("ProductB2 operationB");
}
}
③抽象工厂
interface AbstractFactory {
AbstractProductA createProductA();
AbstractProductB createProductB();
}
④定义具体工厂1 2
class Factory1 implements AbstractFactory {
public AbstractProductA createProductA() {
return new ProductA1();
}
public AbstractProductB createProductB() {
return new ProductB1();
}
}
class Factory2 implements AbstractFactory {
public AbstractProductA createProductA() {
return new ProductA2();
}
public AbstractProductB createProductB() {
return new ProductB2();
}
}
⑤示例使用
public class Main {
public static void main(String[] args) {
AbstractFactory factory1 = new Factory1();
AbstractProductA productA1 = factory1.createProductA();
AbstractProductB productB1 = factory1.createProductB();
productA1.operationA(); // Output: ProductA1 operationA
productB1.operationB(); // Output: ProductB1 operationB
AbstractFactory factory2 = new Factory2();
AbstractProductA productA2 = factory2.createProductA();
AbstractProductB productB2 = factory2.createProductB();
productA2.operationA(); // Output: ProductA2 operationA
productB2.operationB(); // Output: ProductB2 operationB
}
}
每种模式都有不同的应用场景和特点,可以根据具体需求选择适合的工厂模式来创建对象。