标签: factory-pattern

设置/拆卸会损害测试可维护性吗?

这似乎引发了另一个问题的一些对话,我认为值得转入自己的问题.

DRY原则似乎是我们解决维护问题的首选武器,但是测试代码的维护又如何呢?是否适用相同的经验法则?

开发人员测试社区中的一些强烈声音认为设置和拆卸是有害的,应该避免......仅举几例:

实际上,由于这个原因,xUnit.net已经完全从框架中删除了它们(虽然有办法解决这种自我限制).

你有什么经验吗?设置/拆卸是否会受损或有助于测试可维护性?

更新:像JUnit4或TestNG(@ BeforeClass,@ BeforeGroups等)中提供的更细粒度的构造会有所作为吗?

maintainability unit-testing dry fixtures factory-pattern

15
推荐指数
1
解决办法
4040
查看次数

工厂设计模式 - 不使用静态方法,因为单元测试是一个问题

我知道这个问题已在stackoverflow中被多次询问,但不知何故仍然在找出解决方案时遇到一些麻烦.我认为以下示例是一个使用静态方法的好例子

public class ConnectionFactory
{
     public static Connection createConnection(ConnectionType connectionType, String ipAddr, Integer port)
    {
           //Some error checking
         switch(connectionType)
         {   
             case TCP:
                  return createTcpConnection(ipAddr, port);
             case UDP:
                  return createUdpConnection(ipAddr, port);
             case RTP:
                  return createRtpConnection(ipAddr, port);
             case SCTP:
                  return createRtpConnection(ipAddr, port);
             default:
                  break;
         }
    }

    // TcpConnection, RtpConnection, SctpConnection and UdpConnection implement interface Connection
    public Connection createTcpConnection()
    {
        Connection connection = new TcpConnection();
         .....
         .....
         return connection;
    }

    public Connection createUdpConnection()
    {
        Connection connection = new UdpConnection();
        .....
        .....
        return connection; …
Run Code Online (Sandbox Code Playgroud)

java junit design-patterns factory-pattern

15
推荐指数
1
解决办法
1万
查看次数

如何在Factory方法模式和抽象工厂模式之间进行选择

我知道之前有过类似的问题.在过去的几天里,我一直在阅读很多相关内容,我想我现在可以理解设计和代码流程方面的差异了.困扰我的是,似乎两种模式都可以解决同一组问题而没有真正的理由来选择一种或另一种.当我试图自己解决这个问题的时候,我试图实现一个小例子(从我在"Head First:Design patterns"一书中找到的那个开始).在这个例子中,我试图解决相同的问题两次:一次只使用"工厂方法模式"而另一次使用"抽象工厂模式".我会告诉你代码然后我会发表一些评论和问题.

通用接口和类

public interface IDough { }
public interface ISauce { }
public class NYDough : IDough { }
public class NYSauce : ISauce { }
public class KNDough : IDough { }
public class KNSauce : ISauce { }
Run Code Online (Sandbox Code Playgroud)

纯工厂方法模式

// pure Factory method pattern
public abstract class Pizza
{
    protected IDough Dough { get; set; }
    protected ISauce Sauce { get; set; }
    protected abstract IDough CreateDough();
    protected abstract ISauce CreateSauce();
    public void Prepare()
    {
        Dough …
Run Code Online (Sandbox Code Playgroud)

c# design-patterns factory-pattern

15
推荐指数
1
解决办法
5615
查看次数

弹簧动态注塑,工厂般的图案

依赖注入的延续,延迟注射实践.我有Main课程:

package test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component
public class Main {
    @Autowired
    private StringValidator stringValidator;

    @Autowired
    private StringService stringService;

    @Autowired
    private ValidationService validationService;

    public void main() {
        scanKeyboardCreateLists();

        stringValidator.validate();

        final List<String> validatedList = stringValidator.getValidatedList();
        for (String currentValid : validatedList) {
            System.out.println(currentValid);
        }
    }

    private void scanKeyboardCreateLists() {
        //Let's presume the user interacts with the GUI, dynamically changing the object graph...
        //Needless to say, this is past container …
Run Code Online (Sandbox Code Playgroud)

java spring factory dependency-injection factory-pattern

15
推荐指数
1
解决办法
2万
查看次数

基于注释的ServiceLocatorFactoryBean?

我想在我的项目中实现Factory模式.我已经浏览了在线资源,我开始知道应该实现spring ServiceLocatorFactoryBean而不是普通的java工厂模式....

我已经按照这个链接但它在基于xml的解释....任何人可以告诉我如何使用基于工厂模式的注释?

java spring annotations spring-mvc factory-pattern

15
推荐指数
1
解决办法
1万
查看次数

使用Ninject IOC替换工厂

我在解析器中有一个工厂方法.基本上当我加载一个令牌时,我会查找该令牌的处理程序,或者直接进入默认处理程序.我已将其实现为a switch和as,Dictionary<string,Type>但两种方法都要求我将映射存储在除处理程序类之外的其他位置.

我们正在使用Ninject for IOC,因此我意识到我也可以使用它

kernel.Get<ITokenHandler>(tokenName); 
Run Code Online (Sandbox Code Playgroud)

但这并没有节省我存储处理程序在2个位置可以处理的令牌的信息.有没有办法可以装饰处理程序,以便自动映射?

c# ninject inversion-of-control factory-pattern

14
推荐指数
1
解决办法
2772
查看次数

为什么存在静态Create方法?

我在想,为什么Create存在静态方法?

例如,为什么要使用此代码:

System.Xml.XmlReader reader = System.Xml.XmlReader.Create(inputUri);
Run Code Online (Sandbox Code Playgroud)

在这段代码上:

System.Xml.XmlReader reader = new System.Xml.XmlReader(inputUri);
Run Code Online (Sandbox Code Playgroud)

我找不到使用one over other的理由,也找不到使用这个构造的类与其他类之间的任何关系.

任何人都可以对此有所了解吗?

language-agnostic oop design-patterns factory-pattern

14
推荐指数
2
解决办法
1615
查看次数

工厂/抽象工厂混乱

在大约10个月的程序化PHP之后,我现在试图围绕基本的OOP原则和设计模式.这是一个爱好,我没有和我追求的那么多时间,所以请原谅这个问题的相当低级别.

我的网站(目前100%程序)是一个图书馆的核心.访问者发送库脚本2数据点 - 项目type和项目code.

Library.php 使用项类型来选择包含,include包括抓取代码以访问数据库然后构建页面.

一些例子:

[type]  [code]
 game    RoTo
 map     32
 unit    216
Run Code Online (Sandbox Code Playgroud)

一个示例链接将是 library.php?type=game&code=RoTo

一切都很好,但是当我开始使用OOP时,我看到了明显的简单入口点和继承路径,用于"客观化"这个系统.

class LibraryObject
{
    protected $_name;
    protected $_description;
}

class Game extends LibraryObject
{
    protected $_releaseDate;
    etc.
}
Run Code Online (Sandbox Code Playgroud)

我也对一些写得很好的课程给我的灵活性感到兴奋.

不过,设计模式的想法让我感到沮丧.它看起来像一个工厂模式,但我对FAF之间的差异感到困惑.我已经阅读了其他特别提出问题的SO问题,并且我已经阅读了OODesign上的示例,但我觉得它们是用不同的语言编写的,而且相当令人沮丧.

也许如果有人能用我自己的数据结构来解释它,那对我来说会更有意义吗?

抱歉,添麻烦了.

php oop design-patterns factory factory-pattern

14
推荐指数
1
解决办法
7229
查看次数

我是否正确实现了基于泛型的Java工厂?

我不相信我正在实现工厂模式,因为Application类' createDocument方法接受任何类类型,而不仅仅是子类Document.

换句话说,有没有办法可以限制createDocument方法只接受子类Document

  • Document.java

    package com.example.factory;
    
    public abstract class Document {
        public Document() {
            System.out.println("New Document instance created: " + this.toString());
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • DrawingDocument.java

    package com.example.factory
    
    public class DrawingDocument extends Document {
        public DrawingDocument() {
            System.out.println("New DrawingDocument instance created: " this.toString());
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • Application.java

    package com.example.factory;
    
    public class Application {
        public <T> T createDocument(Class<T> documentClass) {
            try {
                return documentClass.newInstance();
            } catch (InstantiationException e) {
                throw new IllegalArgumentException(e);
            } catch (IllegalAccessException …
    Run Code Online (Sandbox Code Playgroud)

java generics factory-pattern

14
推荐指数
2
解决办法
1万
查看次数

C#通用接口和工厂模式

我正在尝试创建一个Generic接口,其中一个方法的参数类型由泛型定义

编辑

我意识到我可能通过在Factory创建方法中指定一个类型参数来混淆问题后稍微改变了这个问题.我有两种类型的API调用,我需要对第三方API.第一个使用ID作为int从API检索记录.第二个也从API检索记录,但Id是一个字符串(guid).我有一个类用于每个记录类型(ClientEntity和InvoiceEntity),它们都实现了一个通用接口,我在其中传递Id类型

这是我在其中声明一个带有id参数的方法的接口

public interface IGeneric<TId>
{
    void ProcessEntity(TId id);
}
Run Code Online (Sandbox Code Playgroud)

我在几个类中实现了接口,一个将id设置为int,另一个设置为字符串.

public class ClientEntity: IGeneric<int> // Record with Id that is an int
{
    public void ProcessEntity(int id)
    {
        Console.WriteLine(id);
        // call 3rd party API with int Id
    }
}

public class InvoiceEntity: IGeneric<string> // Record with Id that is a string (guid)
{
    public void ProcessEntity(string id)
    {
        Console.WriteLine(id);
        // call 3rd party API with string Id
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道的是如何在工厂模式中使用它?

public static class GenericFactory
{
    public static …
Run Code Online (Sandbox Code Playgroud)

c# generics factory-pattern

13
推荐指数
2
解决办法
2万
查看次数