我想使用工厂类和方法来生成GUI组件,但我不知道应该如何以及在哪个类中声明各种侦听器并将其添加到组件中.
如果我有一个简单的工厂类,如下面列出的那样,我应该在它返回到调用类之前向该按钮添加一个ActionListener.如果答案为"是",那么如何添加监听器?
class GUIFactory
{
public static JButton getJButton()
{
JButton aButton = new JButton();
return aButton;
}
}
Run Code Online (Sandbox Code Playgroud)
假设我想使用getJButton()方法向GUI添加5个按钮,我将如何编写ActionListener以便知道单击了哪个按钮?
或者应该将监听器添加到调用类中?
JFrame gui = new JFrame();
gui.add(AppFactory.getJButton());
Run Code Online (Sandbox Code Playgroud)
我尝试了以下内容
gui.add(GUIFactory.getJButton().addActionListener(new guiButtonListener()));
Run Code Online (Sandbox Code Playgroud)
并得到一个错误:
这里不允许使用"void"类型.
我正在尝试注册工厂方法来创建开放泛型类型的实例MongoCollection<>.但是,当我GetInstance看来它使用的是MongoCollection的构造函数而不是工厂方法.
var mongo = new MongoConfiguration("mongodb://localhost", "test");
For(typeof (MongoCollection<>)).Use(c =>
{
var requestedType = c.BuildStack.Current.RequestedType; // set breakpoint here
var type = requestedType.GetGenericArguments()[0];
return mongo.GetCollection(type);
});
Run Code Online (Sandbox Code Playgroud)
然后我做
ObjectFactory.GetInstance<MongoCollection<User>>();
Run Code Online (Sandbox Code Playgroud)
当我运行该GetInstance行时,它永远不会在工厂方法中遇到断点,但它会抛出一个StructureMapException说法"没有为PluginFamily MongoDb.Driver.MongoServerSettings定义默认实例".有一个构造函数MongoCollection需要一个MongoServerSettings,但我不希望结构图使用该构造函数,我希望它使用我的工厂方法.
任何想法为什么它不使用工厂方法?这是一个错误吗?
我有这个代码:
function createRect(x, y, w, h)
local rect = {
type = "rect",
x = x,
y = y,
w = w,
h = h,
translate = function(rect, vector)
assert(vector.type == "vector2d")
local rect = shapes.createRect(rect.x + vector.x, rect.y + vector.y, rect.w, rect.h)
end,
}
return rect
end
translate = function(rect, vector)
assert(vector.type == "vector2d")
local rect = shapes.createRect(rect.x + vector.x, rect.y + vector.y, rect.w, rect.h)
end
local o = createRect(2,3,4,5)
local q = createRect(2,3,4,5)
print(o.translate, q.translate, translate)
Run Code Online (Sandbox Code Playgroud)
这是一些非常简单的代码,用于测试Lua中的测试工厂函数,非常让人联想到JS模块模式.在谈论工厂功能时,人们通常会抱怨的是内存占用.因为o …
我有一个接口,IMessage一个类有几种方法可以创建不同类型的消息,如下所示:
class MessageService
{
IMessage TypeAMessage(param 1, param 2)
IMessage TypeBMessage(param 1, param 2, param 3, param 4)
IMessage TypeCMessage(param 1, param 2, param 3)
IMessage TypeDMessage(param 1)
}
Run Code Online (Sandbox Code Playgroud)
我不希望这个类完成创建这些消息的所有工作,所以它只是委托给一个MessageCreatorFactory 产生IMessageCreator取决于给定类型的枚举(基于消息类型TypeA,TypeB,TypeC等的枚举)
interface IMessageCreator
{
IMessage Create(MessageParams params);
}
Run Code Online (Sandbox Code Playgroud)
所以,我有4个实现IMessageCreator:TypeAMessageCreator,TypeBMessageCreator,TypeCMessageCreator,TypeDMessageCreator
我确定这个,除了因为每个类型需要不同的参数我必须创建一个MessageParams对象,其中包含4个不同参数的4个属性,但每个参数中只使用其中一些IMessageCreator.
有替代方案吗?另一个想法是让一个param数组作为Create方法中的参数,但是这看起来更糟,因为你不知道params是什么.或者在接口中创建几个Create的重载,如果它们不适合那个特定的实现,它们中的一些会抛出异常(即你调用了一个需要更多参数的方法,所以你应该调用其中一个重载.)
这看起来好吗?有更好的解决方案吗?
我有一个接口InterfaceBase和一些从它派生的接口Interface1, Interface2.接下来我有实现InterfaceX接口的类,而不是基类.
现在,我是仿制药的初学者,这样的许多新方法在我的头脑中变得非常混乱:(.我想创建工厂(静态类),我称之为类似的东西
Interface1 concrete1 = Factory.Get<Interface1>();
Run Code Online (Sandbox Code Playgroud)
这是我的(示例)工厂实现,不起作用:
public static class Factory {
public static T Get<T>() where T: InterfaceBase{
Type type = typeof(T);
//return new Concrete1() as T; // type T cannot be used with the as
//return new Concrete1() as type; //type not found
//return new Concrete1(); // cannot implicitly convert
//return new Concrete1() as InterfaceBase; //cannot convert IBase to T
//return new Concrete1() as Interface1; //cannot convert Interface1 to T
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试URLSession在 Swift 中创建一个子类(原因无关紧要,但与测试有关)。我需要它与 adelegate和特定的 一起使用URLSessionConfiguration,这是 上的只读属性URLSession。URLSession使用委托进行初始化的通常方法是使用以下代码完成的,它可以完美地工作:
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: nil)
Run Code Online (Sandbox Code Playgroud)
现在让我们创建一个子类:
class MyURLSession : URLSession {}
let session = MyURLSession(configuration:
URLSessionConfiguration.default, delegate: nil, delegateQueue: nil) // Compile error
Run Code Online (Sandbox Code Playgroud)
初始化程序触发下一个编译错误:
error: argument passed to call that takes no arguments
根据 Swift Language Guide rule 1 for Automatic Initializer Inheritance:
If your subclass doesn’t define any designated initializers, it
automatically inherits all of its superclass designated …Run Code Online (Sandbox Code Playgroud) 我正在阅读有关构建器模式的内容,并且像往常一样我对工厂模式感到困惑。
我看过一篇很好的文章,它展示了抽象工厂和构建器模式之间的区别。
http://champika-nirosh.blogspot.in/2008/04/what-is-difference- Between-abstract.html
但我的困惑是,除了类似于抽象工厂的构建器模式之外,我觉得它还类似于工厂方法模式。不确定我的理解是否正确。但在工厂方法中,我们也使用一个单独的工厂(具体工厂中的方法)来创建一个特定的对象(而不是一系列产品)。在这种情况下,构建器与工厂方法模式有何不同。我知道 Builder 需要更多步骤来创建对象,除此之外,是否有任何特定场景需要我们使用一个而不是另一个?请指导我。
谢谢。
我在理解[[self alloc] init] 编写工厂方法的过程中遇到了麻烦.据我所知,工厂方法方便的方法来创建一个类的实例,而且他们做的alloc,init和autorelease你.我可以看到它是如何形成的,例如在NSArray使用工厂方法声明属性时arrayWithArray:,或者array等等要求它进行设置.我可以清楚地看到这与对allocand 的彻底(显式)调用有何不同init.
我的问题是我不了解更深层次的工厂方法.我在网上遇到一个解释说,不是调用alloc和init显式,而是可以使用类工厂方法基本上封装这样的东西:
+(instancetype)createWithString:(NSString *)string
{
return [[self alloc] initWithString:string];
}
Run Code Online (Sandbox Code Playgroud)
但是,如何instancetype和[self alloc]有效地让子类要利用类工厂方法?
如何根据 SpringBoot 上的请求参数选择服务实现?我可以通过手动实例化服务来做到这一点,但这并没有利用 Spring Injection 功能。我也可以分别自动装配这两个服务,但我想如果我有更多的实现会污染类。
这是一个粗略的例子:
@RestController
class RestControllerTest {
@Autowired
PizzaService pizzaService;
public void bakePizza(@RequestParam("type") String type,@RequestParam("extra") String extra) {
if (type.equals("cheese")) {
//set pizzaService Cheese Implementation
pizzaService = new CheezePizza();
} else {
//set PizzaService vegetable Impleentation;
pizzaService = new VegetablePizza();
}
pizzaService.prepareIngredients(extra);
pizzaService.bakePizza();
}
}
public abstract class PizzaService {
String ingredients;
public abstract void prepareIngredients(String exraIngredient);
public void bakePizza() {
System.out.println("baking pizza with " + ingredients);
}
}
class CheezePizza extends PizzaService {
@Override
public …Run Code Online (Sandbox Code Playgroud) 我遇到了一个讨厌的问题,编译器声称它operator=被删除了,但它就在那里.经过几个小时的尝试,我制作了一个最小的解决方案来重现这个问题.我正在使用MSVC Community Edition 2017 15.7.5(截至今天的最新版本,2018-07-20),并将其设置为'C++ 17'
代码不是微不足道的; 概念是模板类TT用于强制foo在一组类中存在静态成员函数Fn.这与工厂模式非常相似,只是此解决方案不会创建类实例,而是报告有关类的静态详细信息.
在最后一行的赋值中报告错误,并读取(底部的完整错误列表):
"错误C2280:'C&C :: operator =(const C&)':尝试引用已删除的函数"
但第5行定义了这个运算符,右边是那些装饰器?
失败的赋值尝试将返回的值分配给const std::vector<C>&类成员变量.
我认为这const是产生问题,但删除const每个函数中的所有函数没有任何区别; 同一行中的错误相同.
问题:为什么编译器报告这个,以及可能的修复方法是什么?
我认为这一定是我想念的傻事,但我找不到它.
#include <vector>
class C
{
public:
C(int ii) : i(ii) {}
C& operator=(const C&) = default; /// HERE is the assignment operator
const int i;
};
typedef std::vector<C> CVec; // shorthand for a vector of C's
template <class T> // this template forces classes F1, …Run Code Online (Sandbox Code Playgroud) factory-method ×10
inheritance ×2
.net ×1
architecture ×1
c# ×1
c#-4.0 ×1
c++ ×1
c++17 ×1
class ×1
closures ×1
initializer ×1
interface ×1
java ×1
listeners ×1
lua ×1
objective-c ×1
parameters ×1
spring ×1
structuremap ×1
swift ×1
swing ×1
templates ×1
urlsession ×1