Factory in Java when concrete objects take different constructor parameters

You have two options: 1) Abstract Factory: RectangularShape extends Shape RoundShape extends Shape and RectangularShapeFactory and RoundShapeFactory 2) Builder (see also Item 2 in Effective Java) public Shape { private final int x; private final int y; private final double radius; private Shape(Builder builder) { x = builder.x; y = builder.y; radius = builder.radius; } … Read more

Class factory in Python

I think using a function is fine. The more interesting question is how do you determine which registrar to load? One option is to have an abstract base Registrar class which concrete implementations subclass, then iterate over its __subclasses__() calling an is_registrar_for() class method: class Registrar(object): def __init__(self, domain): self.domain = domain class RegistrarA(Registrar): @classmethod … Read more

AngularJS: factory $http.get JSON file

Okay, here’s a list of things to look into: 1) If you’re not running a webserver of any kind and just testing with file://index.html, then you’re probably running into same-origin policy issues. See: https://code.google.com/archive/p/browsersec/wikis/Part2.wiki#Same-origin_policy Many browsers don’t allow locally hosted files to access other locally hosted files. Firefox does allow it, but only if the … Read more

Non-Singleton Services in AngularJS

I’m not entirely sure what use case you are trying to satisfy. But it is possible to have a factory return instances of an object. You should be able to modify this to suit your needs. var ExampleApplication = angular.module(‘ExampleApplication’, []); ExampleApplication.factory(‘InstancedService’, function(){ function Instance(name, type){ this.name = name; this.type = type; } return { … Read more

Factory pattern in C#: How to ensure an object instance can only be created by a factory class?

You can make the constructor private, and the factory a nested type: public class BusinessObject { private BusinessObject(string property) { } public class Factory { public static BusinessObject CreateBusinessObject(string property) { return new BusinessObject(property); } } } This works because nested types have access to the private members of their enclosing types. I know it’s … Read more

What does java:comp/env/ do?

Quoting https://web.archive.org/web/20140227201242/http://v1.dione.zcu.cz/java/docs/jndi-1.2/tutorial/beyond/misc/policy.html At the root context of the namespace is a binding with the name “comp”, which is bound to a subtree reserved for component-related bindings. The name “comp” is short for component. There are no other bindings at the root context. However, the root context is reserved for the future expansion of the policy, … Read more

Is there a way to instantiate objects from a string holding their class name?

Nope, there is none, unless you do the mapping yourself. C++ has no mechanism to create objects whose types are determined at runtime. You can use a map to do that mapping yourself, though: template<typename T> Base * createInstance() { return new T; } typedef std::map<std::string, Base*(*)()> map_type; map_type map; map[“DerivedA”] = &createInstance<DerivedA>; map[“DerivedB”] = … Read more

How to name factory like methods?

Some random thoughts: ‘Create’ fits the feature better than most other words. The next best word I can think of off the top of my head is ‘Construct’. In the past, ‘Alloc’ (allocate) might have been used in similar situations, reflecting the greater emphasis on blocks of data than objects in languages like C. ‘Create’ … Read more

Design Patterns: Factory vs Factory method vs Abstract Factory

All three Factory types do the same thing: They are a “smart constructor”. Let’s say you want to be able to create two kinds of Fruit: Apple and Orange. Factory Factory is “fixed”, in that you have just one implementation with no subclassing. In this case, you will have a class like this: class FruitFactory … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)