为什么我们需要Abstract工厂设计模式?[英] Why do we need Abstract factory design pattern?

本文是小编为大家收集整理的关于为什么我们需要Abstract工厂设计模式?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

大多数定义说:

抽象工厂提供 创建家庭的界面 相关对象未指定 他们的具体课程

抽象出厂模式的用途是什么,因为我们可以通过创建混凝土类本身来实现任务.为什么我们有一种工厂方法来创建混凝土类的对象?

请给我任何现实生活中的示例,我必须在其中实施抽象的模式?

推荐答案

抽象工厂是依赖注入(DI)的非常中心的设计模式.这是堆栈溢出问题的列表,其中抽象出厂的应用已被接受为解决方案.

就我的理解,这些问题代表了人们遇到的真正问题或问题,因此应该让您开始一些现实生活中的例子:

其他推荐答案

使用抽象出厂模式使用的现实生活示例正在为两个不同的数据源提供数据访问.假设您的应用程序支持不同的数据存储. (例如,一个SQL数据库和XML文件).您有两个不同的数据访问接口,例如IReadableStore和IWritableStore定义应用程序期望的通用方法,无论使用的数据源类型如何.

应使用哪种类型的数据源不应更改客户端代码检索其数据访问类的方式.您的AbstractDataAccessFactory知道哪种类型的数据源已配置,并为客户端代码提供了 混凝土工厂 ,即SqlDataAccessFactory或XmlDataAccessFactory.这些具体的工厂可以创建具体的实现,例如SqlReadableStore和SqlWriteableStore.

dbproviderfactordor"> dbproviderfactory "> dbproviderfactory in .NET框架是此模式的一个示例.

其他推荐答案

如果我理解您的理解正确 - 问题是,为什么我们同时拥有工厂方法和抽象的工厂模式. 当不同的多态类具有不同的实例化过程时,您需要抽象工厂.而且您希望一些模块创建实例并使用它们,而不知道对象初始化的任何细节. 例如 - 您想创建一些计算的Java对象.但是其中一些是应用程序的一部分,而其他字节则应从数据库中读取. 另一方面 - 为什么我们需要工厂方法?同意,抽象工厂重叠.但是在某些情况下 - 编写代码要少得多,具有更少的类和接口使系统更易于理解.

本文地址:https://www.itbaoku.cn/post/627470.html

问题描述

Most of the definition says:

An abstract factory provides an interface for creating families of related objects without specifying their concrete classes

What is the use of Abstract Factory Pattern as we can achieve the task via creating object of concrete class itself. Why do we have a factory method that creates object of Concrete class?

Please provide me any real life example where I must implement abstractFactory pattern?

推荐答案

Abstract Factory is a very central design pattern for Dependency Injection (DI). Here's a list of Stack Overflow questions where application of Abstract Factory has been accepted as the solution.

To the best of my understanding, these questions represent real concerns or problems that people had, so that should get you started with some real-life examples:

其他推荐答案

A real life example for the use of the Abstract Factory pattern is providing data access to two different data sources. Assume your application supports different data stores. (e.g. a SQL Database and an XML file). You have two different data access interfaces e.g. an IReadableStoreand IWritableStore defining the common methods expected by your application regardless of the type of data source used.

Which type of data source shall be used shouldn't change the way client code retrieves it's data access classes. Your AbstractDataAccessFactory knows which type of data source is configured and provides a concrete Factory for the client code, i.e. SqlDataAccessFactory or XmlDataAccessFactory. These concrete factories can create the concrete implementations, e.g. SqlReadableStore and SqlWriteableStore.

The DbProviderFactory in .NET Framework is an example of this pattern.

其他推荐答案

If I understand you right - the question is, why do we have both the Factory method and the abstract factory patterns. You need abstract factory when different polymorphic classes has different instantiation procedure. And you want some module to create instances and use them, without knowing any details of object initialization. For example - you want to create Java objects doing some calculations. But some of them are part of the application, while other's bytecode should be read from the DB. In the other hand - why do we need factory method? Agree, that abstract factory overlaps it. But in some cases - it is much less code to write, having less classes and interfaces makes system easier to comprehend.