The Abstract Factory Pattern is the first Pattern I learned about. I think it’s one of the patterns you can use in almost all projects. The idea behind it is “to provide an interface for creating families of related of dependent objects without specifying their concrete classes”. If you search in google for “Abstract Factory Pattern” you’ll find a picture that demonstrates the relationships between the objects. Click on “read more” to see the example code I wrote. If I interpreted or impemented the design pattern in a wrong way let me know it.
// :: WORKAROUNDS :: //
// createEmptyMovieClip
MovieClip.prototype.createEmptyMovieClip2 = MovieClip.prototype.createEmptyMovieClip;
MovieClip.prototype.createEmptyMovieClip = function(n, d, c){
var mcP = MovieClip.prototype;
MovieClip.prototype = c.prototype;
var mc = this.createEmptyMovieClip2(n, d);
MovieClip.prototype = mcP;
return mc;
}
// getNextDepth
MovieClip.prototype.getNextDepth = function(){
var t = -Infinity;
for(var i in this){
if(this[i].getDepth() != null && this[i]._parent == this){
t = Math.max(t, this[i].getDepth());
}
}
return (t > -1) ? ++t: 0;
}
////////////////////////////
// :: Abstract Factory :: //
////////////////////////////
GeometryFactoryClass = function(){
if(arguments[0] == “NO_INIT“) return;
}
GeometryFactoryClass.prototype.init = function(){
this.createSquare();
this.createCircle();
}
GeometryFactoryClass.prototype.createSquare = function(){
this.square = DESTINATION.createEmptyMovieClip(”square_mc“, DESTINATION.getNextDepth(), SquareClass);
this.square.setSize(100);
this.square.setPosition(150, 25);
}
GeometryFactoryClass.prototype.createCircle = function(){
this.circle = DESTINATION.createEmptyMovieClip(”circle_mc“, DESTINATION.getNextDepth(), CircleClass);
this.circle.setRadius(50);
this.circle.setPosition(75, 75);
}
////////////////////////////
// :: Concrete Factory :: //
////////////////////////////
// Red Geometry Factory
RedGeometryFactoryClass = function(){
}
RedGeometryFactoryClass.prototype = new GeometryFactoryClass(”NO_INIT“);
RedGeometryFactoryClass.prototype.createSquare = function(){
super.createSquare();
this.square.setBackgroundColor(”0xFF0000“);
this.square.init();
}
RedGeometryFactoryClass.prototype.createCircle = function(){
super.createCircle();
this.circle.setBackgroundColor(”0xFF0000“);
this.circle.init();
}
// Green Geometry Factory
GreenGeometryFactoryClass = function(){
}
GreenGeometryFactoryClass.prototype = new GeometryFactoryClass(”NO_INIT“);
GreenGeometryFactoryClass.prototype.createSquare = function(){
super.createSquare();
this.square.setBackgroundColor(”0×00FF00“);
this.square.init();
}
GreenGeometryFactoryClass.prototype.createCircle = function(){
super.createCircle();
this.circle.setBackgroundColor(”0×00FF00“);
this.circle.init();
}
////////////////////////////
// :: Abstract Product :: //
////////////////////////////
// Geometry
Geometry = function(){
if(arguments[0] == “NO_INIT“) return;
}
Geometry.prototype = new MovieClip();
Geometry.prototype.init = function(){
this.draw();
}
Geometry.prototype.setBackgroundColor = function(color){
this.backgroundColor = color;
}
Geometry.prototype.setPosition = function(x, y){
this._x = x;
this._y = y;
}
// Square
SquareClass = function(){
}
SquareClass.prototype = new Geometry(”NO_INIT“);
SquareClass.prototype.draw = function(){
with(this){
lineStyle(1, 0×000000, 100);
beginFill(this.backgroundColor, 100);
moveTo(0, 0);
lineTo(this.size, 0);
lineTo(this.size, this.size);
lineTo(0, this.size);
lineTo(0, 0);
endFill();
}
}
SquareClass.prototype.setSize = function(size){
this.size = size;
}
// Circle
CircleClass = function(){
}
CircleClass.prototype = new Geometry(”NO_INIT“);
CircleClass.prototype.draw = function(){
var sgm = 8;
var periode = 2*Math.PI;
var sgmAngle = periode/sgm;
var cRangle = sgmAngle/2;
var cRx = this.radius/Math.cos(cRangle);
var cRy = this.radius/Math.cos(cRangle);
this.lineStyle(1, 0×000000, 100);
this.beginFill(this.backgroundColor, 100);
this.moveTo(this.radius, 0);
for (var angle = sgmAngle; angle<=periode; angle += sgmAngle) {
var cX = Math.cos(angle-cRangle)*cRx;
var cY = Math.sin(angle-cRangle)*cRy;
var pX = Math.cos(angle)*this.radius;
var pY = Math.sin(angle)*this.radius;
this.curveTo(cX, cY, pX, pY);
}
this.endFill();
}
CircleClass.prototype.setRadius = function(radius){
this.radius = radius;
}
//////////////////
// :: Client :: //
//////////////////
ClientClass = function(widgetFactory){
widgetFactory.init();
}
// example
_global.DESTINATION = this;
this.onMouseDown = function(){
widgetFactory = new RedGeometryFactoryClass();
client = new ClientClass(widgetFactory);
}
this.onMouseUp = function(){
widgetFactory = new GreenGeometryFactoryClass();
client = new ClientClass(widgetFactory);
}
widgetFactory = new GreenGeometryFactoryClass();
client = new ClientClass(widgetFactory);
I hope you got the idea.
—————————————————————————————–
Here’s another example. Not from me but I “translated” it into ActionScript. It was originally writeen in Java.
// :: CALENDAR :: //
Calendar = function(){
this.dayArray = [”Sunday“, “Monday“, “Tuesday“, “Wednesday“, “Thursday“, “Friday“, “Saturday“];
this.date = new Date();
}
Calendar.prototype.getDay = function(){
var currentDay = this.date.getDay();
return this.dayArray[currentDay];
}
// :: SOUP :: //
Soup = function(){
}
Soup.prototype.getSoupName = function(){
return this.soupName;
}
Soup.prototype.getSoupIngredients = function(){
return this.soupIngredients.toString();
}
// :: CHICKEN SOUP //
ChickenSoup = function(){
this.soupName = “ChickenSoup“;
this.soupIngredients = new Array();
this.soupIngredients.push(”1 Pound diced chicken“);
this.soupIngredients.push(”1/2 cup rice“);
this.soupIngredients.push(”1 cup bullion“);
this.soupIngredients.push(”1/16 cup butter“);
this.soupIngredients.push(”1/4 cup diced carrots“);
}
ChickenSoup.prototype = new Soup();
// :: CLAM CHOWDER :: //
ClamChowder = function(){
this.soupName = “ClamChowder“;
this.soupIngredients = new Array();
this.soupIngredients.push(”1 Pound Fresh Clams“);
this.soupIngredients.push(”1 cup fruit or vegetables“);
this.soupIngredients.push(”1/2 cup milk“);
this.soupIngredients.push(”1/4 cup butter“);
this.soupIngredients.push(”1/4 cup chips“);
}
ClamChowder.prototype = new Soup();
// :: FISH CHOWDER :: //
FishChowder = function(){
this.soupName = “FishChowder“;
this.soupIngredients = new Array();
this.soupIngredients.push(”1 Pound Fresh fish“);
this.soupIngredients.push(”1 cup fruit or vegetables“);
this.soupIngredients.push(”1/2 cup milk“);
this.soupIngredients.push(”1/4 cup butter“);
this.soupIngredients.push(”1/4 cup chips“);
}
FishChowder.prototype = new Soup();
// :: MINNESTRONE :: //
Minnestrone = function(){
this.soupName = “Minnestrone“;
this.soupIngredients = new Array();
this.soupIngredients.push(”1 Pound tomatos“);
this.soupIngredients.push(”1/2 cup pasta“);
this.soupIngredients.push(”1 cup tomato juice“);
}
Minnestrone.prototype = new Soup();
// :: PASTA FAZOULE :: //
PastaFazoule = function(){
this.soupName = “PastaFazoule“;
this.soupIngredients = new Array();
this.soupIngredients.push(”1 Pound tomatos“);
this.soupIngredients.push(”1/2 cup pasta“);
this.soupIngredients.push(”1/2 cup diced carrots“);
this.soupIngredients.push(”1 cup tomato juice“);
}
PastaFazoule.prototype = new Soup();
// :: TOFU SOUP :: //
TofuSoup = function(){
this.soupName = “TofuSoup“;
this.soupIngredients = new Array();
this.soupIngredients.push(”1 Pound tofu“);
this.soupIngredients.push(”1 cup carrot juice“);
this.soupIngredients.push(”1/4 cup spirolena“);
}
TofuSoup.prototype = new Soup();
// :: VEGETABLE SOUP :: //
VegetableSoup = function(){
this.soupName = “VegetableSoup“;
this.soupIngredients = new Array();
this.soupIngredients.push(”1 cup bullion“);
this.soupIngredients.push(”1/4 cup carrots“);
this.soupIngredients.push(”1/4 cup potatoes“);
}
VegetableSoup.prototype = new Soup();
// :: ABSTRACT SOUP FACTORY :: //
AbstractSoupFactory = function(){
}
AbstractSoupFactory.prototype.getFactoryLocation = function(){
return this.factoryLocation;
}
AbstractSoupFactory.prototype.makeChickenSoup = function(){
return new ChickenSoup();
}
AbstractSoupFactory.prototype.makeClamChowder = function(){
return new ClamChowder();
}
AbstractSoupFactory.prototype.makeFishChowder = function(){
return new FishChowder();
}
AbstractSoupFactory.prototype.makeMinnestrone = function(){
return new Minnestrone();
}
AbstractSoupFactory.prototype.makePastaFazoule = function(){
return new PastaFazoule();
}
AbstractSoupFactory.prototype.makeTofuSoup = function(){
return new TofuSoup();
}
AbstractSoupFactory.prototype.makeVegetableSoup = function(){
return new VegetableSoup();
}
// :: BOSTON CONCRETE SOUP FACTORY :: //
BostonConcreteSoupFactory = function(){
this.factoryLocation = “Boston“;
}
BostonConcreteSoupFactory.prototype = new AbstractSoupFactory();
BostonConcreteSoupFactory.prototype.makeClamChowder = function(){
return new BostonClamChowder();
}
BostonConcreteSoupFactory.prototype.makeFishChowder = function(){
return new BostonFishChowder();
}
// :: BOSTON CLAM CHOWDER :: //
BostonClamChowder = function(){
this.soupName = “QuoahogChowder“;
this.soupIngredients = new Array();
this.soupIngredients.push(”1 Pound Fresh Quoahogs“);
this.soupIngredients.push(”1 cup corn“);
this.soupIngredients.push(”1/2 cup heavy cream“);
this.soupIngredients.push(”1/4 cup butter“);
this.soupIngredients.push(”1/4 cup potato chips“);
}
BostonClamChowder.prototype = new ClamChowder();
// :: BOSTON FISH CHOWDER :: //
BostonFishChowder = function(){
this.soupName = “ScrodFishChowder“;
this.soupIngredients = new Array();
this.soupIngredients.push(”1 Pound Fresh Scrod“);
this.soupIngredients.push(”1 cup corn“);
this.soupIngredients.push(”1/2 cup heavy cream“);
this.soupIngredients.push(”1/4 cup butter“);
this.soupIngredients.push(”1/4 cup potato chips“);
}
BostonFishChowder.prototype = new FishChowder();
// :: HONOLULU CONCRETE SOUP FACTORY :: //
HonoluluConcreteSoupFactory = function(){
this.factoryLocation = “Honolulu“;
}
HonoluluConcreteSoupFactory.prototype = new AbstractSoupFactory();
HonoluluConcreteSoupFactory.prototype.makeClamChowder = function(){
return new HonoluluClamChowder();
}
HonoluluConcreteSoupFactory.prototype.makeFishChowder = function(){
return new HonoluluFishChowder();
}
// :: HONOLULU CLAM CHOWDER :: //
HonoluluClamChowder = function(){
this.soupName = “PacificClamChowder“;
this.soupIngredients = new Array();
this.soupIngredients.push(”1 Pound Fresh Pacific Clams“);
this.soupIngredients.push(”1 cup pineapple chunks“);
this.soupIngredients.push(”1/2 cup coconut milk“);
this.soupIngredients.push(”1/4 cup SPAM“);
this.soupIngredients.push(”1/4 cup taro chips“);
}
HonoluluClamChowder.prototype = new ClamChowder();
// :: HONOLULU FISH CHOWDER :: //
HonoluluFishChowder = function(){
this.soupName = “OpakapakaFishChowder“;
this.soupIngredients = new Array();
this.soupIngredients.push(”1 Pound Fresh Opakapaka Fish“);
this.soupIngredients.push(”1 cup pineapple chunks“);
this.soupIngredients.push(”1/2 cup coconut milk“);
this.soupIngredients.push(”1/4 cup SPAM“);
this.soupIngredients.push(”1/4 cup taro chips“);
}
HonoluluFishChowder.prototype = new FishChowder();
// example
// :: TEST ABSTRACT SOUP FACTORY :: //
TestAbstractSoupFactory = function(){
var concreteSoupFactory = new BostonConcreteSoupFactory();
var soupOfTheDay = this.makeSoupOfTheDay(concreteSoupFactory);
trace(”The Soup of the day in ” + concreteSoupFactory.getFactoryLocation() + “ is ” + soupOfTheDay.getSoupName() + “.“);
trace(”The Soup ingredients are ” + soupOfTheDay.getSoupIngredients() + “.“);
trace(”——————————————————————-“);
var concreteSoupFactory = new HonoluluConcreteSoupFactory();
var soupOfTheDay = this.makeSoupOfTheDay(concreteSoupFactory);
trace(”The Soup of the day in ” + concreteSoupFactory.getFactoryLocation() + “ is ” + soupOfTheDay.getSoupName() + “.“);
trace(”The Soup ingredients are ” + soupOfTheDay.getSoupIngredients() + “.“);
}
TestAbstractSoupFactory.prototype.makeSoupOfTheDay = function(concreteSoupFactory){
var todayCalendar = new Calendar();
var dayOfWeek = todayCalendar.getDay();
if (dayOfWeek == “Monday“)
return concreteSoupFactory.makeChickenSoup();
else if (dayOfWeek == “Tuesday“)
return concreteSoupFactory.makeClamChowder();
else if (dayOfWeek == “Wednesday“)
return concreteSoupFactory.makeFishChowder();
else if (dayOfWeek == “Thursday“)
return concreteSoupFactory.makeMinnestrone();
else if (dayOfWeek == “Friday“)
return concreteSoupFactory.makePastaFazoule();
else if (dayOfWeek == “Saturday“)
return concreteSoupFactory.makeTofuSoup();
else return concreteSoupFactory.makeVegetableSoup();
}
mySoup = new TestAbstractSoupFactory();
