我是一个很新的PHP,并试图学习.我有2个类似的课程.我想在传递count和object类型时创建那些对象.我读了一些关于工厂模式的文章 这是我的工厂类:
class AssetFactory
{
private static $table;
public static $objects = array();
public static function Create($asset,$count)
{
switch ($asset) {
case "Item":
self::$table = "items";
break;
case "Job":
self::$table = "jobs";
break;
}
$db = new Database();
$rows = $db->query("SELECT * FROM ".self::$table." LIMIT ".$count);
foreach($rows as $row)
{
self::$objects[] = new $asset($row);
}
return self::$objects;
}
}
Run Code Online (Sandbox Code Playgroud)
当我需要5件物品时我会使用:
$myItems = AssetFactory::Create('Item',5);
Run Code Online (Sandbox Code Playgroud)
当我需要一些我使用的工作时:
$myJobs= AssetFactory::Create('Job',5);
Run Code Online (Sandbox Code Playgroud)
项目和工作是类似的课程.我的问题就在这里,正如我所说,我试图学习.我这样做对吗?我了解工厂模式吗?有关于此的任何好文件(我在php.net上阅读所有内容,还有其他任何东西).
我现在了解工厂设计模式背后的理论,但似乎找不到任何实际的使用示例。有人可以好心地提供一个吗?
在代码审查中,出现了以下评论:"我认为,您实际上是指将工厂注入实例,以便您可以在需要时重新启动工厂." 并且"重要提示:工厂不应该是静态的,而应该注入."
Test.java:
Foo foo = FooFactory.get(argument);
Run Code Online (Sandbox Code Playgroud)
FooFactory.java:
public final class FooFactory {
public static Foo get(String argument) {
return new Foo();
}
}
Run Code Online (Sandbox Code Playgroud)
我应该怎么做呢?"重新绑定"在评论者的第一条评论中意味着什么?
如何使Ninject在运行时基于变量实例化对象?
我试图在控制器动作中注入正确的存储库 - MVC 3 - 基于来自用户输入的参数.如果用户输入"宝马",将绑定ICarRepository 到BMWRepository,如果他输入"KIA" KiaRepository将被注入.
[HttpPost]
public ActionResult SearchResult(FormCollection values)
{
string carModel = values["model"];
ICarRepository myRepository = RepositoryFactory.getRepository(carModel);
.....
}
Run Code Online (Sandbox Code Playgroud)
这是通过开关/案例noob实例化或参数化工厂知道的,我知道如何在没有Ninject的情况下手动完成,请查看此处介绍的4种方法探索工厂模式
我的问题是如何用Ninject做到这一点?
我想知道是否有更好的方法来实现createEmployee(),它使用字典或其他方式快速查找所请求的类型而不是if-else块.
function Clerk( options ) {
this.hourRate = options.hourRate || 20;
this.firstName = options.firstName || "no first name";
this.lastName = options.lastName || "no last name";
this.id = options.id || "-9999999999";
}
function Manager( options) {
this.hourRate = options.hourRate || 200;
this.firstName = options.firstName || "no first name";
this.lastName = options.lastName || "no last name";
this.id = options.id || "-9999999999";
this.yearBonus = options.yearBonus || "200000";
}
function Teacher( options) {
this.hourRate = options.hourRate || 100;
this.firstName = options.firstName || "no first …Run Code Online (Sandbox Code Playgroud) 我正在阅读Effective java item# 2- Builder模式
http://www.informit.com/articles/article.aspx?p=1216151&seqNum=2
这里说的是java bean并不是创建多参数对象的有效方法。但是如果我这样有javabean怎么办:
// JavaBeans Pattern
public class NutritionFacts {
private final int servingSize ;
private final int servings ;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
public NutritionFacts() { }
// Setters
public void setServingSize(int val) { servingSize = val; }
public void setServings(int val) { servings = val; }
public void setCalories(int val) { calories = val; }
public void setFat(int val) { fat …Run Code Online (Sandbox Code Playgroud) 对Haskell而言是新手并进行实验我遇到了一个问题,看起来我想在构造函数中使用守卫.我真正的实验涉及更多,但它归结为类似的东西
data X a = Zero
| a==0 = Zero
| otherwise = Some a
Run Code Online (Sandbox Code Playgroud)
我知道这是错误的语法,特别是因为它a是一个类型变量,而不是一个值变量,所以a==0无论如何都是无意义的.
背景是价值Zero并且Some 0在所有方面都应该相等,我甚至会说相同.事实上,我从来不想构造一个Some 0,这应该被禁止,或者如上所述,默默地转换为Zero已经在构造函数中.
我认为Haskell中有一个共同的习惯用法.在Java中,我只需要创建两个构造函数private并提供工厂方法来创建一个或另一个而不是some(0)返回.这也是你在Haskell中对此的看法吗?ZeroSome(0)
编辑:好的,谷歌搜索更多我终于找到了Haskell Factory Function条目,这似乎是答案的一部分.
我正在尝试使用Go实现工厂模式,这里是示例https://play.golang.org/p/ASU0UiJ0ch
我有一个接口Pet和一个struct Dog所以Dog应该具有Pet的属性,在这种情况下只有一个是物种,当试图通过工厂NewPet初始化对象Dog时,有人可以建议.
创建对象的方式和位置应该returnShapeType删除?
这是一个工厂方法演示程序.
请出示代码.
class Shape
{
public:
Shape() {}
virtual void print() {std::cout << "\nFrom shape print";}
};
class Triangle: public Shape
{
public:
Triangle(){}
virtual void print() {std::cout << "\nFrom triangle print";}
};
class Rectangle: public Shape
{
public:
Rectangle(){}
virtual void print() {std::cout << "\nFrom rect print";}
};
class CreateShapeObject
{
public:
CreateShapeObject() {}
Shape *returnShapeType( std::string arg )
{
if (arg == "Triangle")
return new Triangle;
else if (arg == "Rectangle")
return new Rectangle;
} …Run Code Online (Sandbox Code Playgroud) 我和老师争论这是否是工厂模式.我可以让你的一些人输入吗?
public class UrlFactory {
private static boolean testMode;
private static long testDate;
public static URLConnection fetchConnection(String url)
throws IOException
{
URL address = new URL(url);
if(testMode)
return new MockURLConnection(address, testDate);
return address.openConnection();
}
public static void SetTestMode(long testDate)
{
UrlFactory.testMode = true;
UrlFactory.testDate = testDate;
}
public static void UnSetTestMode()
{
UrlFactory.testMode = false;
}
}
Run Code Online (Sandbox Code Playgroud)