When to use Factory method pattern?

Use a factory method (not abstract factory) when you want to reuse common functionality with different components. Example: Imagine you have an M16 rifle. Something like this: public class M16 { private Scope scope = new StandardScope(); private SecondaryWeapon secondary = new Bayonet(); private Camouflage camo = new DesertCamo(); public double getMass() { // Add … Read more

Populating an association with children in factory_girl

The Factory.after_ hooks appear to be the only way to do this successfully. I’ve figured out a way to maintain the build strategy without duplicating code: Factory.define :foo do |f| f.name “A Foo” f.after(:build) { |foo| foo.bars << Factory.build(:bar, :foo => foo) } f.after(:create) { |foo| foo.bars.each { |bar| bar.save! } } end The documentation … Read more

Factory, Abstract Factory and Factory Method

The Gang Of Four “Design Patterns; Elements of Reusable Object-Oriented Software” book contains two entries, “Abstract Factory” (aka ‘Virtual Constructor’) and “Factory Method”. I don’t know about “Concrete Factory.” I’ve heard the term, but never given it too much thought. Factory Method In “Factory Method” an object has a method which is responsible for the … Read more

What exactly is a Class Factory?

Here’s some supplemental information that may help better understand several of the other shorter, although technically correct, answers. In the strictest sense a Class Factory is a function or method that creates or selects a class and returns it, based on some condition determined from input parameters or global context. This is required when the … Read more

How to avoid ‘instanceof’ when implementing factory design pattern?

You could implement the Visitor pattern. Detailed Answer The idea is to use polymorphism to perform the type-checking. Each subclass overrides the accept(Visitor) method, which should be declared in the superclass. When we have a situation like: void add(Vehicle vehicle) { //what type is vehicle?? } We can pass an object into a method declared … Read more

Laravel – Seeding Many-to-Many Relationship

You can use attach() or sync() method on a many-to-many relationship. There are multiple ways you can approach this. Here one of them: // Populate roles factory(App\Role::class, 20)->create(); // Populate users factory(App\User::class, 50)->create(); // Get all the roles attaching up to 3 random roles to each user $roles = App\Role::all(); // Populate the pivot table … Read more

How to pass arguments to Laravel factories?

The attributes you pass to the create function will be passed into your model definition callback as the second argument. In your case you don’t even need to access those attributes, since they’ll automatically be merged in: $business = factory(App\Business::class)->create(); factory(App\User::class, 5)->create([ ‘business_id’ => $business->id, ]); Adapt this to your needs.

Factory classes [closed]

Here is a real live factory from my code base. It’s used to generated a sampler class that knows how to sample data from some dataset (it’s originally in C#, so excuse any java faux-pas) class SamplerFactory { private static Hashtable<SamplingType, ISampler> samplers; static { samplers = new Hashtable<SamplingType, ISampler>(); samplers.put(SamplingType.Scalar, new ScalarSampler()); samplers.put(SamplingType.Vector, new … Read more