我必须为大学课程实现一个多生产者/多消费者示例应用程序,并且很难找到以下问题的解决方案,这并没有让我觉得我做错了什么;)
我必须实现 aProducer产生给定类型的Component( CPUComponent, MainboardComponent. 公共Component类的所有子类)。a 的每个实例Producer只会产生给定数量的一种类型的组件(例如只有主板),然后退出。
的Components为都或多或少不变的对象(仅final字段)和所有逻辑都在共同的基类实现Component(下面简化的)
public abstract class Component implements Serializable
{
private final long id;
public Component(int id) { ... }
public long getId()
{
return id;
}
}
Run Code Online (Sandbox Code Playgroud)
的子类Component只是原始的,比如
public class CPUComponent extends Component
{
public CPUComponent(long id) { ... }
}
Run Code Online (Sandbox Code Playgroud)
由于语言是 Java,我无法使用泛型轻松解决此对象生成问题(就像在 C# 中一样,因为我无法在 Java 中实例化泛型类型参数的新对象)。所以我开始实现一个工厂
public interface ComponentFactory {
Component createComponent(Producer …Run Code Online (Sandbox Code Playgroud) 我对调试和发布设置进行了几处更改,现在我想恢复出厂设置。我怎么能那样做?
我有一个用打字稿编写的服务作为一个类。在这个类中,我定义了一个注入依赖项的静态工厂。
当我压缩我的应用程序时,依赖项正在被压缩并且我收到一个未定义的提供程序错误。
这是我的服务:
export class TInterceptor {
public static $inject = ['$q', '$rootScope'];
public static Factory($q:ng.IQService, $rootScope:ng.IRootScopeService)
{
return new TInterceptor($q, $rootScope);
}
constructor(private $q:ng.IQService, private $rootScope:ng.IRootScopeService){}...}
Run Code Online (Sandbox Code Playgroud)
此处调用的服务:
angular
.module('t')
.config(config);
function config($httpProvider:ng.IHttpProvider)
{
$httpProvider.interceptors.push(TInterceptor.Factory);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何确保在压缩代码时保护依赖项不被覆盖?
为了测试我对 Dart 的理解,我制作了一个 2D 不可变向量,它不仅存储了它的 x 和 y 分量,还存储了它的角度和长度。它们仅在构建时根据 x 和 y 值计算。我很快意识到需要使用初始化列表或this-parameter 快捷方式设置最终字段,这不允许太多计算值。就像这个答案指出的那样,我不得不使用工厂构造函数从其 x 和 y 分量中创建我的向量。工厂构造函数然后在调用私有构造函数之前验证输入并计算长度和角度。
import 'dart:math';
class ImmutableVector {
// private fields
final double _x;
final double _y;
final double _angle;
final double _length;
// getters
double get x => _x;
double get y => _y;
double get angle => _angle;
double get length => _length;
/// Constructs a vector out of cartesian components.
///
/// Converts both arguments to doubles.
/// …Run Code Online (Sandbox Code Playgroud) 我有一个我自动装配的 HsqldbReconciler(用于与 HSQLDB 数据库“工作”),例如:
@Autowired
HsqldbReconciler hsqldbReconciler;
Run Code Online (Sandbox Code Playgroud)
将来会有 OracleReconciler、MssqlReconciler 等。我需要根据用户选择的连接类型来使用它们。
我应该如何实施?通常我会有一种工厂,它只返回所需的协调器。我目前可以想象,spring 的唯一方法是自动装配每个 Reconciler 的一个实例,然后在代码中使用其中一个。有没有更好的办法?
我正在使用 FactoryBot(以前称为 FactoryGirl)为我的测试创建一些工厂数据。我有一个通过模式看起来像这样的模型(精简为相关内容):
create_table "items", force: :cascade do |t|
t.text "team"
t.text "feature_id"
t.text "feature"
t.timestamps
end
Run Code Online (Sandbox Code Playgroud)
但是,feature_id并且feature不是对特征对象的引用……它们只是字符串。
我这样定义我的工厂:
FactoryBot.define do
factory :item do
team "TheTeam"
sequence(:feature_id) {|n| "Feature#{n}" }
feature { Faker::Lorem.sentence }
end
end
Run Code Online (Sandbox Code Playgroud)
简单的案例有效:
> FactoryBot.create(:item)
=> #<Item:0x007fad8cbfc048
id: 1,
team: "TheTeam",
feature_id: "Feature1",
feature: "Qui voluptatem animi et rerum et.",
created_at: Wed, 10 Jan 2018 02:40:01 UTC +00:00,
updated_at: Wed, 10 Jan 2018 02:40:01 UTC +00:00>
Run Code Online (Sandbox Code Playgroud)
但是当我想指定我自己feature_id的时,就会发生这种情况:
> FactoryBot.create(:item, feature_id: …Run Code Online (Sandbox Code Playgroud) 我创建了一个管道和一个触发器,我试图让触发器在部署后立即自动启动。我在文档中看到部署后触发器的状态为“停止”,我想问一下如何在模板部署中更改它,以便在每次部署后都不需要使用 powershell 脚本。
我试图在我的 TD 中使用“runtimeState”参数,但它被替换为“stopped”。
这是我的触发器:
{
"name": "[concat(variables('factoryName'), '/periodicTrigger')]",
"type": "Microsoft.DataFactory/factories/triggers",
"apiVersion": "2018-06-01",
"properties": {
"description": "Periodic trigger for the backup pipeline.",
"annotations": [],
"runtimeState": "Started",
"pipelines": [
{
"pipelineReference": {
"referenceName": "[concat(variables('mainStorageName'),'backupPipeline')]",
"type": "PipelineReference"
},
"parameters": {}
}
],
"type": "ScheduleTrigger",
"typeProperties": {
"recurrence": {
"frequency": "Day",
"interval": 1,
"startTime": "2018-08-21T13:28:07.785Z",
"timeZone": "UTC",
"schedule": {
"minutes": [
0
],
"hours": [
3
]
}
}
}
},
"dependsOn": [
"[concat(variables('factoryId'), '/pipelines/',concat(variables('mainStorageName'),'backupPipeline'))]"
]
},
Run Code Online (Sandbox Code Playgroud) 本教程中的工厂显然违反了 OCP。每次在系统中添加一个形状,我们都需要在工厂中添加它以支持它。我正在考虑另一个实现,我想知道是否有任何缺点。
public class ShapeFactory {
//use getShape method to get object of type shape
public Shape getShape(Class<? extends Shape> shapeType){
return shapeType.newInstance();
}
}
Run Code Online (Sandbox Code Playgroud)
这个实现看起来不违反OCP,也不复杂。有什么原因我找不到任何提到它的工厂设计模式教程吗?
laravel 的一个新工厂看起来是这样的;
<?php
/* @var $factory \Illuminate\Database\Eloquent\Factory */
use App\Model;
use Faker\Generator as Faker;
$factory->define(Model::class, function (Faker $faker) {
return [
//
];
});
Run Code Online (Sandbox Code Playgroud)
变量未$factory在此文件中定义。这个变量是如何以及在哪里定义的?甲dd($factory)结果如预期中的\Illuminate\Database\Eloquent\Factory对象
我正在尝试创建一个包,允许在模型类本身内部指定模型工厂定义。
例如:
class User extends Authenticatable
{
use LaravelUIModel, Notifiable;
protected $hidden = ['password', 'remember_token'];
protected $casts = ['email_verified_at' => 'datetime'];
public function definition(Generator $faker)
{
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
}
Run Code Online (Sandbox Code Playgroud)
所以我创建了一个名为 的特征LaravelUIModel,我试图在其中指定一个动态newFactory:
trait LaravelUIModel
{
use HasFactory;
protected static function newFactory()
{
$factory = new Factory;
$factory->setModel(static::class);
return $factory->new();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,$model工厂属性受到保护,所以我创建了自己的Factory类来扩展 EloquentFactory …