工厂模式和依赖项

时间:2018-02-16 12:24:48

标签: java oop design-patterns factory-pattern

工厂方法用于避免违反开闭原则。 而不是通过继承创建对象:

date=["16.02.2018", "blah"]

with open('indata.csv', 'w+') as CSVreport:
    wr = csv.writer(CSVreport)
    wr.writerow(date)

我们使用工厂"界面"与亲戚具体工厂(实现工厂并覆盖其方法的类):

Product myProd = new ConcreteProduct1; // concreteProduct extends abstract product
myProd.doSomething();

我读了很多关于工厂方法的文章;我明白,使用工厂方法,可以排除开闭原理的变形。但我仍然不明白:

  1. 使用此设计模式,我们仍然依赖于类Product(myProd)(1)
  2. 此外..我们与Concrete-Factories有一个依赖关系(客户需要实例化一个特定的通缉混凝土工厂,客户必须知道一个女巫)(2)
  3. 坦克你做任何澄清。

2 个答案:

答案 0 :(得分:1)

  1. 我们确实依赖Product,但重点是消除对ConcreteProduct的依赖。

  2. 不,我们不依赖于ConcreteFactory,因为我们将工厂作为参数传递。

    class MassProduction {
        ProductFactory factory;
        public MyClass(ProductFactory fact) {
           factory = fac;
        }
        public List<Product> produce(int amount) {
            ArrayList<Product> result = new ArrayList<>(amount);
            for (int i = 0; i < amount; i++) { 
                result.add(factory.createProduct());
            }
            return result;
        }
    }
    
  3. 我们决不依赖ConcreteProductConcreteFactory

答案 1 :(得分:1)

您可能并不完全处于需要使用整个概念的位置。

这是一个用例:

// define the interfaces
public interface Product {}
public interface ProductFactory {
    public Product createProduct();
}
// create some implementors
public class Sweatshirt implements Product {
    static final ProductFactory FACTORY = new ProductFactory() {
        public Product createProduct() {
            return new Sweatshirt();
        }
    }
}
public class Pants implements Product {
    static final ProductFactory FACTORY = new ProductFactory() {
        public Product createProduct() {
            return new Pants();
        }
    }
}
public class Hat implements Product {
    static final ProductFactory FACTORY = new ProductFactory() {
        public Product createProduct() {
            return new Hat();
        }
    }
}

// create a client class
class Stock {
    private List<? extends Product> stock = new ArrayList<>();
    public void add(int amount, ProductFactory fac) {
        for (int i = 0; i < amount; i++) {
            stock.add(fac.createProduct());
        }
    }
    public void printAll() {
        stock.forEach(p -> System.out.println(p.getClass()));
    }
}

// driver class that allows the user to enter amounts and product type
// and adds them to the stock until any entry is invalid
class InventoryManagement {
    public static void main(String[] args) {
        Stock stock = new Stock();
        try (Scanner sc = new Scanner(System.in)) {
            while (true) {
                // read amount from console input
                int amount = sc.nextInt();
                // read type from console input
                String value = sc.next();
                ProductFactory factory = null;
                switch(value) {
                    case "s":
                         factory = Sweatshirt.FACTORY;
                         break;
                    case "p":
                         factory = Pants.FACTORY;
                         break;
                    case "h":
                         factory = Hat.FACTORY;
                         break;
                 }
                 if (factory != null) {
                     stock.add(amount, factory);
                 } else {
                     break;
                 }
            }
        } catch (Exception e) {} 
        stock.printAll();
    }
}

如您所见,客户端类(Stock)没有对任何具体ProductFactory的引用,但它使用给定的Factory创建Product s。它是连接两者(客户端类Stock和具体产品)的驱动程序类。