所以,谈到工厂的主题,我想知道他们是如何建立的.
从我的立场,我可以看到3种类型的工厂:
一体
一个工厂,基本上包含应用程序中使用的所有类.感觉它只是为了拥有一个工厂而拥有一个工厂,而且并不真正感觉有条理.
示例(其中ClassA,Class B和ClassC没有任何共同之处,除非在同一个App中):
class Factory
{
public static function buildClassA()
public static function buildClassB()
public static function buildClassC()
}
Run Code Online (Sandbox Code Playgroud)
提供的代码示例使用PHP.但是,这个问题与语言无关.
内置工厂
下一个是将静态函数与常规函数混合,以便创建特殊的创建模式(请参阅此问题)
例:
class ClassA
{
public static function buildClass()
public function __construct()
}
Run Code Online (Sandbox Code Playgroud)
工厂在旁边
我能想到的最后一个是为个别班级或个别班级设立工厂.这似乎可变,以统一的方式使用.
示例(其中ClassA,B和C相关,并且1,2和3相关):
class FactoryAlpha
{
public static function buildClassA()
public static function buildClassB()
public static function buildClassC()
}
class FactoryNumeric
{
public static function buildClass1()
public static function buildClass2()
public static function buildClass3()
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:所有这些糟糕的想法,其中任何一个都是坏主意吗?还有其他创建工厂的方法吗?这些中的任何一个都是好主意吗?什么是创建工厂的好/最佳方式.
这是我的意思的一个例子:
class MyDecorator(object):
def __call__(self, func):
# At which point would I be able to access the decorated method's parent class's instance?
# In the below example, I would want to access from here: myinstance
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
class SomeClass(object):
##self.name = 'John' #error here
name="John"
@MyDecorator()
def nameprinter(self):
print(self.name)
myinstance = SomeClass()
myinstance.nameprinter()
Run Code Online (Sandbox Code Playgroud)
我需要装饰实际的课吗?
我正在创建自己的带有渐变背景的UITableViewCells.我已经完成了所有的逻辑和绘图,但我想解决的一件事是我的自定义单元格角落周围的"厚片":
如果你放大角落,你可以看到我在说什么.这是我用来生成单元格的代码:
CGContextRef c = UIGraphicsGetCurrentContext();
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef myGradient = nil;
CGFloat components[8] = TABLE_CELL_BACKGROUND;
CGContextSetStrokeColorWithColor(c, [[UAColor colorWithWhite:0.7 alpha:1] CGColor]);
CGContextSetLineWidth(c, 2);
CGContextSetAllowsAntialiasing(c, YES);
CGContextSetShouldAntialias(c, YES);
CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, minx, miny);
CGPathAddArcToPoint(path, NULL, minx, maxy, midx, maxy, kDefaultMargin);
CGPathAddArcToPoint(path, NULL, maxx, maxy, maxx, miny, kDefaultMargin);
CGPathAddLineToPoint(path, NULL, maxx, miny);
CGPathAddLineToPoint(path, NULL, minx, …Run Code Online (Sandbox Code Playgroud) 我有一个简单的问题:实例化C#委托与仅传递函数引用相比有什么好处?我的意思是:
为什么:
Thread t = new Thread(new ThreadStart(SomeObject.SomeMethod));
Run Code Online (Sandbox Code Playgroud)
当你能做到:
Thread t = new Thread(SomeObject.SomeMethod);
Run Code Online (Sandbox Code Playgroud)
两者都会根据我的经验编译和工作......我错过了什么?
比方说,我有一个URI http://127.0.0.1/somecontroller/someaction#12345,带我到someAction()someController控制器的动作.从那里,我能够通过检索Request对象$this->getRequest().
我还能够从Request对象中检索有关URI的各种信息.
但是,我如何检索片段(即例如#之后的"12345"部分)?既没有getRequestUri()也没getParams()出现片段部分.
谢谢!
我正在使用Python Imaging Library使用定义颜色关系的查找表为黑白图像着色.查找表只是一个256元素的RGB元组列表:
>>> len(colors)
256
>>> colors[0]
(255, 237, 237)
>>> colors[127]
(50, 196, 33)
>>>
Run Code Online (Sandbox Code Playgroud)
我的第一个版本使用了getpixel()和putpixel()方法:
for x in range(w):
for y in range(h):
pix = img.getpixel((x,y))
img.putpixel((x,y), colors[pix[0]])
Run Code Online (Sandbox Code Playgroud)
这非常缓慢.一份profile报告指出这些putpixel和getpixel方法是罪魁祸首.稍微调查(即阅读文档),我发现" 请注意,这种方法相对较慢. "re : putpixel. (实际运行时间:对于1024x1024图像为53s in putpixel和getpixel50ss)
根据文档中的建议,我使用im.load()并直接使用像素访问:
pixels = img.load()
for x in range(w):
for y in range(h):
pix = pixels[x, y]
pixels[x, y] = colors[pix[0]]
Run Code Online (Sandbox Code Playgroud)
处理加速了一个数量级,但仍然 …
python image-manipulation image image-processing python-imaging-library
它看起来不像Zend_Db_Select's on子句中有任何参数替换.
我不能只做以下事情,这非常烦人:
$select->joinLeft('st_line_item','st_line_item.order_id = st_order.id and st_line_item.status = ?')
Run Code Online (Sandbox Code Playgroud)
那么在流畅的界面中有什么惯用的替代方案呢?我可以做一些事情,比如在外面准备连接子句,但这不是重点.
使用scipy interpolate.splprep函数获取参数的参数样条曲线u,但域u不是样条曲线的线积分,它是输入坐标的分段线性连接.我试过了integrate.splint,但这只是给了个人积分u.显然,我可以数字地整合一堆笛卡尔差分距离,但我想知道是否有闭合形式的方法来获得我忽略的样条或样条线段(使用scipy或numpy)的长度.
编辑:我正在寻找一种封闭形式的解决方案或一种非常快速的方式来收敛机器精度答案.我几乎放弃了数字根寻找方法,现在主要是在一个封闭形式的答案之后.如果任何人有任何集成椭圆函数的经验或者可以指向一个好的资源(除了Wolfram),那就太好了.
我将尝试使用Maxima来尝试获得我认为是样条曲线的一个段的函数的无限积分:我在MathOverflow上交叉发布了它
我有以下类层次结构:
template <typename T>
class base
{
public:
void f() {}
};
class class_a : public base<class_a> {};
class class_b : public base<class_b>,
public class_a
{
using base<class_b>::f;
};
int main()
{
class_b b;
b.f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Comeu和英特尔C++ v11声称一切都很好,但GCC(4.4.1)和VC++ 2008似乎抱怨(http://codepad.org/KQPDsqSp),例如:
g++ -pedantic -Wall -o test test.cpp
test.cpp: In function ‘int main()’:
test.cpp:5: error: ‘void base<T>::f() [with T = class_b]’ is inaccessible
test.cpp:14: error: within this context
Run Code Online (Sandbox Code Playgroud)
我相信代码很好,但是我可能错了,我希望来自SO C++社区的人能够对这个问题提供一些见解.
注意:在class_b中的using指令之前添加"public"可以解决gcc和VS的问题.应用using指令的类的访问器部分是否应该覆盖基类的派生模式(public,private)?
简而言之就是这样
假设你想打印出那些伟大的ascii艺术图像之一.如果不单独"追逐"每条线,你怎么能这样做呢?