我需要帮助设计模式识别。这是我的Java代码
public class A extend B {
public A (String arg1){
super(arg1);
}
public C newInstance(String arg2){
return new C(arg1, arg2);
}
}
有什么想法吗?是哪种模式?
UPD
public abstract class B {
protected final String arg;
public B(String arg) {
this.arg = arg;
食谱有不同的一般类型,每种一般类型都有不同的方法。数据库查询URL中提供的ID以确定类型,并使用不同的类。
一种选择是:
$c['recipeFactory'] = function ($c) {
return new RecipeFactory($this->get('pdo'));
};
$app->put('/recipes/{id:[0-9]+}', function (Request $request, Response $response, $args) {
//Factory will query DB
不确定我的标题是否正确,因为我甚至不确定我是否使用了正确的术语。
我有一个类,它的属性是一个对象。设置此属性时,必须创建对象。我的问题是如何在没有紧密耦合的情况下做到这一点?
示例:
class A
{
protected $_depending;
protected $_somePropertyObject;
public function __construct(\Lib\Dependency $depending)
{
$this->_setDepending($depending);
}
protected f
下面是Seth博客“”中工厂构造函数的用法。
class Symbol {
final String name;
static Map<String, Symbol> _cache = new Map<String, Symbol>();
factory Symbol(String name) {
if (_cache.containsKey(name)) {
return _cache[name];
} else {
final symbol = new Symbol._internal(name);
我有一个抽象工厂方法类,如下所示:
public abstract class OpClass
{
public abstract IHou OperationInvoke(string opClass);
}
public class FactoryClass : OpClass
{
public override IHou OperationInvoke(string opClass)
{
if(opClass == "T")
{
最近我遇到了这种类型的代码:
final class OnlyMe {
private int a;
private int b;
//setter and getters for a and b.
private OnlyMe(){}
public static OnlyMe getOnlyMeObj(int c) {
// use c value to connect to database
// to populate a and b
if(rs.next()) {
OnlyMe onlyMe = new OnlyMe();
onlyMe.a =
我解析/处理来自许多不同流(具有不同格式)的数据,并且系统中不同数据源的数量不断增加。我有一个工厂类,它基于一个配置文件,指定源代码将给我适当的解析器/处理器对(遵循一个小的公共接口),如下所示:
static Foo* FooFactory::createFoo(source c, /*couple flags*/)
{
switch (c)
{
case SOURCE_A:
{
//3 or 4 lines to put together a parser for A, and something to process st
我正在尝试创建一个向其传递参数x并返回一个新类C的函数。C应该是固定基类A的子类,只有一个附加内容:添加某个类属性并将其设置为等于x。
换句话说:
class C(A):
C.p = x # x is the parameter passed to the factory function
这很容易做到吗?有什么我应该注意的问题吗?
在Dart中,工厂构造函数需要来自编码器的更多逻辑,但与const构造函数没有太大区别,除非它们允许“非最终”实例变量。
他们对const建设者的优点是什么?
谢谢你们所有人。
编辑的
下面是Seth博客“Dart”中工厂构造函数的用法--试图理解“工厂”构造函数的价值。
class Symbol {
final String name;
static Map<String, Symbol> _cache = new Map<String, Symbol>();
factory Symbol(String name) {
if (_cache.co
虽然这个话题已经讨论过很多次了,但我还是觉得自己被它弄糊涂了。
我有一个简单的代码示例:
public class FruitFactory {
public static Apple getApple() {
return new Apple();
}
public static Banana getBanana() {
return new Banana();
}
public static Orange getOrange() {
return new Orange();
}
}
这
我对C#语言很陌生,在一个网站上看到了这种声明。我还没见过这种构造函数(?)以前,我真的很困惑
public class Employee {
public string Name { get; set; }
public string Title { get; set; }
public static Employee GetEmployee() { //Constructor? Method?
var emp = new Employee() {
Name = "Somebody", Title =
在Java语言中,这个C#声明的等价物是什么?
public class AsyncThreadPool<T, K> where T : IAsyncThread<K> {
而IAsyncThread是一个接口
public interface IAsyncThread<T>
{
T GetAsyncUsedObject();
void StartAsyncRequest();
}
我试过了:
public class AsyncThreadPool<T extends IAsyncThread<K>, K &
请注意,我已经修改了问题中的代码。
请参阅下面的服务器端代码(WCF服务):
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
namespace WcfService1
{
public class WindsorInstaller : IWindsorI
我已经开始学习WCF了,我已经完全糊涂了。我读了一些关于工厂模式的文章,我不知道ChannelFactory<>是如何或为什么实现它的。
也就是说,工厂模式的整个思想是工厂从客户端抽象出产品的创建和初始化,因此,如果引入了新的产品类型,客户端代码就不需要更改,因此可以立即开始使用新产品。
ChannelFactory<IRequestChannel> factory = new
ChannelFactory<IRequestChannel>(binding, address);
IRequestChannel cha
我正在阅读有关静态工厂方法的文章。静态工厂方法编码技术是只适用于Java,还是同样适用于C# .Net?看起来更像是Java的东西。 https://dzone.com/articles/constructors-or-static-factory-methods class Color {
private final int hex;
static Color makeFromRGB(String rgb) {
return new Color(Integer.parseInt(rgb, 16));
}
static Color makeFr
我需要一些公共功能,而派生类需要具有单例行为。例如:
class A
{
//common file functions
}
class B : public A
{
//Needs to be singleton . since there is specific file of this class
}
class C : public A
{
//Needs to be singleton. There is also specific file of this class
}
在这种情况下,是否可以将派生类作为单例,或者组合是个好主意?
这个包在pub dev 这里上
它在吉特布这里上
class IocContainer {
final Map<Type, Object Function(IocContainer container)> _map;
IocContainer(this._map);
T get<T>() => _map[T]!(this) as T;
}
class IocContainerBuilder {
final Map<Type, Object Function(IocContainer container)> _map = {};