小编Dav*_*uth的帖子

Python中的代理模式

我的代码中实现了很多类.现在我意识到,对于为所有这些类调用的每个方法,我需要添加一行:

with service as object:
Run Code Online (Sandbox Code Playgroud)

所以我试图使用代理模式自动完成工作,这是我的示例代码

class A(object):
    def __init__(self, name):
        self.name = name
    def hello(self):
        print 'hello %s!' % (self.name)
    def __enter__(self):
        print 'Enter the function'
        return self
    def __exit__(self, exc_type, exc_value, traceback):
        print 'Exit the function'
class Proxy(object):
    def __init__(self, object_a):
#        object.__setattr__(self, '_object_a', object_a)
        self._object_a = object_a

    def __getattribute__(self, name):
        service = object.__getattribute__(self, '_object_a')
        with service as service:
            result = getattr(service, name)
        return result    

if __name__=='__main__':
    a1 = A('A1')
    b = Proxy(a1)
    b.hello()
    a2 = A('A2')
    b …
Run Code Online (Sandbox Code Playgroud)

python proxy-pattern

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

Java - 纪念品模式和撤消

我正在实现一个撤消/重做功能,它需要我使用备忘录模式。

部分程序的流程:“...程序然后使用Memento Pattern存储之前的Vector,然后将新创建的对象添加到Vector。之后,用户可以选择show命令来显示Vector内部的内容,他也可以输入undo命令来恢复,可以重复undo,直到恢复到原来的状态……”

从我的研究中,我知道会有一个发起者、纪念品和看守者。

这是我的看守计划

public class CareTaker {
      private Memento m;
      private Stack s;
      private Vector v;
      // Some of the implementation are not shown

      public void create() {
            // Some of the implementation are not shown
            // Assuming Vector is named "v"
            // Passing Vector to memento
            m = new Memento(v);
            s.add(m);
      }
      public void undo() {
          v = s.pop().restore();
      }
}
public class Memento {
    private Vector _v;
    public Memento(Vector v) {
      _v = v;
    }
    public …
Run Code Online (Sandbox Code Playgroud)

java design-patterns memento

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

Java Flyweight模式:外在和内在状态?

我对Flyweight模式的这些状态的差异感到困惑.

我知道intrinsic状态是共享的状态,而Extrinsic不是.

但是,我没有看到extrinsic状态在模式中或以下示例中的重要性:

public static void main(String[] args) {
        // Create the flyweight factory...
        EngineFlyweightFactory factory = new EngineFlyweightFactory();
        // Create the diagnostic tool
        DiagnosticTool tool = new EngineDiagnosticTool();
        // Get the flyweights and run diagnostics on them
        Engine standard1 = factory.getStandardEngine(1300); //intrinsic
        standard1.diagnose(tool); //extrinsic

        Engine standard2 = factory.getStandardEngine(1300); //intrinsic
        standard2.diagnose(tool); //extrinsic

        Engine standard3 = factory.getStandardEngine(1300); //intrinsic
        standard3.diagnose(tool); //extrinsic

        Engine standard4 = factory.getStandardEngine(1600); //intrinsic
        standard4.diagnose(tool); //extrinsic

        Engine standard5 = factory.getStandardEngine(1600); //intrinsic
        standard5.diagnose(tool); …
Run Code Online (Sandbox Code Playgroud)

java design-patterns flyweight-pattern extrinsic-parameters

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

接口隔离原理用法

这种情况多次发生在我身上,我不知道如何解决它.

当一些接口实现不使用它的功能时,接口隔离原则是为了防止情况 - 这是显而易见的.通常情况下我有一个接口列表,我想用它们做一些事情.让我们看看没有ISP的例子:

  public interface IPerson
    {
           void Run();
           void Eat();
    }

public class AbcPerson : IPerson
{
       void Run(){};
       void Eat(){};
}

public class XyzPerson : IPerson
{
       void Run(){};
       void Eat(){};
}

List<IPerson> People {get;set;}
Run Code Online (Sandbox Code Playgroud)

而且我想和每个人一起跑.

      foreach(var person in People)
      {
         person.Run();
      }
Run Code Online (Sandbox Code Playgroud)

现在我想和所有人一起吃饭.

          foreach(var person in People)
          {
             person.Eat();
          }
Run Code Online (Sandbox Code Playgroud)

现在,如果我想使用ISP,我应该将代码更改为:

        public interface IRunnable
        {
               void Run();
        }
        public interface IEatable
        {
               void Eat();
        }

    public class AbcPerson : IRunnable,IEatable
    {
           void Run(){};
           void …
Run Code Online (Sandbox Code Playgroud)

c# convention interface-segregation-principle

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

JDBC/数据库单元测试的最佳实践

我有与多个数据库(如 Sybase、Postgress、DB2 等)对话的代码。所以我有不同的模块,对这些数据库中的每一个都有不同的查询。我从 H2 和 JUnit 开始进行单元测试。由于 Sybase、Postgres、DB2 等查询在 H2 中不起作用,我开始将我现有的目标查询转换为 H2 可以接受的格式,我发现许多 SQL 函数,如 RANK()、分区、复杂连接更新、case when 等在 H2 中不受支持或无法按预期工作。我如何对这段代码进行单元测试?

我应该使用相应的数据库对每个 db 模块进行单元测试吗?例如,Sybase 模块是否具有与 Sybase 而不是 H2 对话的单元测试,或者如果我对我的数据库代码进行单元测试,我是否必须使用 H2?

java database junit unit-testing jdbc

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

Command模式或Strategy模式更适合客户端 - 服务器调用吗?

我需要在求职面试中创建客户端 - 服务器程序,其中客户端发送命令并且服务器处理它们.

服务器需要能够轻松更改,这意味着以后可能会有更多的命令类型.

所以我使用Strategy实现它,这意味着类处理命令看起来类似于:

public class ServerProcessor
{
    Dictionary<string, Action<string>> commandsType;

    public ServerProcessor()
    {
        commandsType = new Dictionary<string, Action<string>>();
    }

    public void RegisterCommand(string commandName, Action<string> command)
    {
        commandsType.Add(commandName, command);
    }

    public void Process(string commandName, string vars)
    {
        if (commandsType.ContainsKey(commandName))
        {
            commandsType[commandName].Invoke(vars);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我这样做之后,面试官说我需要使用Command模式实现它,但没有说明原因.

Command模式将是这样的:

public interface ICommand
{
    void Execute(string vars);
    string GetName();
}

public class ServerProcessor2
{
    List<ICommand> commandsType;

    public ServerProcessor2()
    {
        commandsType = new List<ICommand>();
    }

    public void RegisterCommand(ICommand commandName)
    {
        commandsType.Add(commandName);
    }

    public void …
Run Code Online (Sandbox Code Playgroud)

c# design-patterns strategy-pattern command-pattern

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

如何在Cucumber-JVM中明确地匹配这些步骤?

有没有办法明确地匹配以下步骤?

And I should have 2 alerts
And I should have 2 alerts with param 71
Run Code Online (Sandbox Code Playgroud)

我已将它们实现为:

@And("^I should have (\\d+) alerts")
@And("^I should have (\\d+) alerts with param (\\d+)")
Run Code Online (Sandbox Code Playgroud)

当Cucumber评估第二步时,它表示它是模糊的,因为它可以匹配两个步骤定义中的任何一个,即使它有额外的单词'with param'.

提前致谢.

java cucumber cucumber-jvm

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

用户故事/功能与用例/场景有何区别?

用户故事/功能与用例/场景有何区别?任何指示将不胜感激。

仅仅是粒度,类似于史诗和用户故事吗?

specifications cucumber specflow gherkin speclog

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

Rails:如何在 rake 任务中定义函数

在我的任务文件中post.rake,我想重用一个函数

  def save_post(title, href, source)
    post = Post.new(title: title, url: href, source: source)
    if post.save
      puts title + 'saved'
    else
      puts title + 'not saved'
    end
  end
Run Code Online (Sandbox Code Playgroud)

但是,当我在这个文件中定义它并重新使用它时,它返回

NoMethodError: undefined method `save_post' for main:Object
Run Code Online (Sandbox Code Playgroud)

post.rake如下所示:

task :fetch_post => :environment do
  require 'nokogiri'
  require 'open-uri'

  url = 'http://example.com'
  doc = Nokogiri::HTML(open(url) )
  puts doc.css("title").text
  doc.css(".a").each do |item_info|
    title = item_info.text
    href = item_info['href']
    save_post(title, href)
  end

  def save_post(title, href)
    post = Post.new(title: title, url: href)
    if post.save …
Run Code Online (Sandbox Code Playgroud)

ruby rake ruby-on-rails

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

如何识别项目是否使用了CQS或CQRS?CQS和CQRS有什么区别?

我可能对这个问题听起来很愚蠢,但我真的很困惑.创建命令,查询,commandhanlder,queryhandler和存储库以及使用依赖注入来分别根据查询和命令解析查询处理程序和命令处理程序是否有资格作为cqs或cqrs?

或者使用任务并行库为命令和查询处理程序限定为cqs而不是cqs?

或者它是否真的基于用例是否存在协作域的场景 - >多个用户试图访问有限的数据.

design-patterns domain-driven-design command-query-separation cqrs

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