Album Cover

Design Patterns Vol. 1 Java

Close Preview
The Players
These are the players that Abstract Factory brings to the stage:
  • Consumer — The object that needs Products that work together.
  • Products — The instances that are created and delivered to the Consumer by an Abstract Factory instance.
  • Abstract Factory — The object that produces the Products.
Variations
Part Factory Players
The standard Abstract Factory provides several Factory Methods that return compatible Products.
All the Products returned by these methods are guaranteed to work together. Think of these parts as part of the same Product Line or Brand.
Abstract Factory instances themselves are polymorphic. By calling the Factory Methods on a suitable Abstract Factory instance, you can create a compatible set of parts.
A Trader takes Product Specification
and returns a compatible Product
A Trader is an Abstract Factory that exposes a single Factory Method. While a traditional Abstract Factory determines what kind of Product to create based upon which Factory Method is called, a Trader makes this decision based upon its Specification argument. All these Products are part of the same Product Line, naturally.
The code below shows a PartFactoryTrader whose makeFactory(...) method returns a new PartFactory object for the specified brand.

package com.industriallogic.examples;
import java.util.HashMap;
public class PartFactoryTrader { private HashMap<String, PartFactory> factories;
private PartFactoryTrader() { factories = new HashMap<String, PartFactory>(); factories.put("Tata", new PartFactory(new Chassis("Tata"), new Transmission("Tata"), new Engine("Tata"))); factories.put("GreatWall", new PartFactory(new Chassis("GreatWall"), new Transmission( "GreatWall"), new Engine("GreatWall"))); factories.put("Ford", new PartFactory(new Chassis("Ford"), new Transmission("Ford"), new Engine("Ford"))); }
public PartFactory makeFactory(String brand){ return (PartFactory) factories.get(brand).clone(); }
public PartFactory findFactory(String brand){ return (PartFactory) factories.get(brand); }
static private PartFactoryTrader trader = new PartFactoryTrader();
static public PartFactoryTrader soleInstance() { return trader; } }
(Reading...)
| Tweet...
Page rating: | Rate this Page