我的代码中实现了很多类.现在我意识到,对于为所有这些类调用的每个方法,我需要添加一行:
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) 我正在实现一个撤消/重做功能,它需要我使用备忘录模式。
部分程序的流程:“...程序然后使用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) 我对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) 这种情况多次发生在我身上,我不知道如何解决它.
当一些接口实现不使用它的功能时,接口隔离原则是为了防止情况 - 这是显而易见的.通常情况下我有一个接口列表,我想用它们做一些事情.让我们看看没有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) 我有与多个数据库(如 Sybase、Postgress、DB2 等)对话的代码。所以我有不同的模块,对这些数据库中的每一个都有不同的查询。我从 H2 和 JUnit 开始进行单元测试。由于 Sybase、Postgres、DB2 等查询在 H2 中不起作用,我开始将我现有的目标查询转换为 H2 可以接受的格式,我发现许多 SQL 函数,如 RANK()、分区、复杂连接更新、case when 等在 H2 中不受支持或无法按预期工作。我如何对这段代码进行单元测试?
我应该使用相应的数据库对每个 db 模块进行单元测试吗?例如,Sybase 模块是否具有与 Sybase 而不是 H2 对话的单元测试,或者如果我对我的数据库代码进行单元测试,我是否必须使用 H2?
我需要在求职面试中创建客户端 - 服务器程序,其中客户端发送命令并且服务器处理它们.
服务器需要能够轻松更改,这意味着以后可能会有更多的命令类型.
所以我使用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) 有没有办法明确地匹配以下步骤?
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'.
提前致谢.
用户故事/功能与用例/场景有何区别?任何指示将不胜感激。
仅仅是粒度,类似于史诗和用户故事吗?
在我的任务文件中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) 我可能对这个问题听起来很愚蠢,但我真的很困惑.创建命令,查询,commandhanlder,queryhandler和存储库以及使用依赖注入来分别根据查询和命令解析查询处理程序和命令处理程序是否有资格作为cqs或cqrs?
或者使用任务并行库为命令和查询处理程序限定为cqs而不是cqs?
或者它是否真的基于用例是否存在协作域的场景 - >多个用户试图访问有限的数据.
design-patterns domain-driven-design command-query-separation cqrs
java ×4
c# ×2
cucumber ×2
convention ×1
cqrs ×1
cucumber-jvm ×1
database ×1
gherkin ×1
interface-segregation-principle ×1
jdbc ×1
junit ×1
memento ×1
python ×1
rake ×1
ruby ×1
specflow ×1
speclog ×1
unit-testing ×1