factory-pattern

Should I be using IEquatable to ease testing of factories?

℡╲_俬逩灬. 提交于 2020-01-03 11:57:19
问题 I often work with classes that represent entities produced from a factory. To enable easy testing of my factories easily I usually implement IEquatable<T> , whilst also overriding GetHashCode and Equals (as suggested by the MSDN). For example; take the following entity class which is simplified for example purposes. Typically my classes have a bunch more properties. Occasionally there is a also collection, which in the Equals method I check using SequenceEqual . public class Product :

Nice way of factory method pattern with inheritance

守給你的承諾、 提交于 2020-01-02 16:54:40
问题 Suppose I have following class hierarchy: class abstract Parent{} class FirstChild extends Parent {} class SecondChild extends Parent {} And I'd like to create DTO objects from each child: class abstract ParentDTO {} class FirstChildDTO extends ParentDTO{} class SecondChildDTO extends ParentDTO{} I think I need a factory method something like this: ParentDTO createFrom(Parent source); Is there any nice way to do this in Java without instanceof checks and if/else statements? EDIT : This

Factory to create different objects of same interface

与世无争的帅哥 提交于 2020-01-02 04:06:49
问题 I have 1 interface: public interface ISummary { int EventId {get; set;} } And many concrete classes that implement this interface: public class EmployeeSummary : ISummary { public int EventId {get; set;}, public int TotalUniqueCount {get; set;} public int Location {get; set;} } public class CarSummary : ISummary { public int EventId {get; set;} public int TotalMiles {get; set;} public int TotalHours {get; set;} } etc.... The only shared property is the EventId . Is there a way to have 1

Factory to create different objects of same interface

痴心易碎 提交于 2020-01-02 04:06:07
问题 I have 1 interface: public interface ISummary { int EventId {get; set;} } And many concrete classes that implement this interface: public class EmployeeSummary : ISummary { public int EventId {get; set;}, public int TotalUniqueCount {get; set;} public int Location {get; set;} } public class CarSummary : ISummary { public int EventId {get; set;} public int TotalMiles {get; set;} public int TotalHours {get; set;} } etc.... The only shared property is the EventId . Is there a way to have 1

Is the Factory Method Pattern more flexible than Simple Factory?

岁酱吖の 提交于 2020-01-02 03:49:06
问题 I've been reading the book Head First: Design Patterns, which I have found to be a good introduction to design patterns. However, I've got a question about a claim they make in Chapter 4: They define the "Simple Factory" pattern as follows (Java pseudocode): public abstract class Product { // Product characteristics // Concrete Products should subclass this } public class SimpleFactory { public Product createProduct(){ // Return an instance of some subclass of Product } } public class Store {

How to implement Singleton pattern in Dart using factory constructors?

荒凉一梦 提交于 2020-01-01 19:14:08
问题 I'm trying to implement the singleton pattern in a database helper class, but, I can't seem to understand the purpose of a factory constructor and if there's an alternative method to using it. class DbHelper { final String tblName =''; final String clmnName =''; final String clmnPass=''; DbHelper._constr(); static final DbHelper _db = new DbHelper._constr(); factory DbHelper(){ return _db;} Database _mydb; Future<Database> get mydb async{ initDb() { if(_mydb != null) { return _mydb; } _mydb =

Data Layer Abstract Factory

旧街凉风 提交于 2020-01-01 10:52:26
问题 I'm new on developing an Abstract Factory pattern, and would like to create an abstract factory in the data layer that will help me link this layer to any other databases for example sql and oracle. Can you help me on developing this task please. Note that the connection string of the database will be found in this layer not in the presentation.. Thanks EDITED public abstract class Database { public string connectionString; #region Abstract Functions public abstract IDbConnection

Abstract factory with abstract parameters?

不打扰是莪最后的温柔 提交于 2020-01-01 04:56:11
问题 I'm trying to design a good entity creation system with an abstract factory (as per http://www.dofactory.com/Patterns/PatternAbstract.aspx) but I'm struggling when it comes to instance specific parameters. For example: I have two abstract factories, one for creating a projectile, and one for creating a crate Now the factory can be either be one instance for each type, which is passed an abstract parameter set from a list (which in the base class would shared material, size etc), type specific

How to correctly make a thread safe Singleton Factory in Java? [duplicate]

允我心安 提交于 2020-01-01 02:35:07
问题 This question already has answers here : What is an efficient way to implement a singleton pattern in Java? [closed] (29 answers) Closed 5 years ago . This is the first time I am writing a Factory class. Below is my Factory class, I am not sure whether this is the correct way of making thread safe Singleton Factory class or not. I will be returning instance of my Client using this factory? public class ClientFactory { private static ClientFactory instance = null; private ClientFactory() { }

Passing Services using Dependency Injection and Factory Pattern in ASP.NET

最后都变了- 提交于 2019-12-30 05:05:10
问题 I am using ASP.NET Core, I know that such Logging mechanism is already provided by the framework, but using this to illustrate my problem. I am using kind of Factory pattern to build the Logger class, since I don't know the type of logging (because it is stored in DB). The ILogger Contract Log(string msg) Then LoggerFactory will return an ILogger after creating a Logger based on param passed from DB: public class LoggerFactory { public static Contracts.ILogger BuildLogger(LogType type) {