abstract-factory


Abstract factory bad design?


I have car factories that build cars of different sizes.
I have 2 factories: USA and Thailand that make car sizes Big, Middle, and Little.
But I have a problem: the Thailand factory does not make big cars.
Code:
public enum CarSize { Big, Middle, Little,}
public interface ICarFactory {
ICar CreateCar(CarSize carSize);
}
public class USACarFactory : ICarFactory {
public ICar CreateCar(CarSize carSize) {
ICar car = null;
switch (carSize) {
case CarSize.Little:
car = new USALittleCar();
break;
case CarSize.Middle:
car = new USAMiddleCar();
break;
case CarSize.Big:
car = new USABigCar();
break;
}
return car;
}
}
public class ThailandCarFactory : ICarFactory {
public ICar CreateCar(CarSize carSize){
ICar car = null;
switch (carSize) {
case CarSize.Little:
car = new ThailandLittleCar();
break;
case CarSize.Middle:
car = new ThailandMiddleCar();
break;
case CarSize.Big:
//no big cars in Thailand!!!!
break;
}
return car;
}
}
public interface ICar { }
public class USABigCar : ICar { }
public class USAMiddleCar : ICar { }
public class USALittleCar : ICar { }
public class ThailandMiddleCar : ICar { }
public class ThailandLittleCar : ICar { }
What should I do?
Is my Abstract Factory poorly designed?
#zzfima, picking up the idea from COM, how about querying the interface and find out it if supports creating car of a specified size and invoke suitably? The code definitely realizes the abstract factory pattern.
case CarSize.Big:
car = new ThailandBigCar();
break;
Try that. That should allow you to make big cars.
public interface ICar { }
public class USABigCar : ICar { }
public class USAMiddleCar : ICar { }
public class USALittleCar : ICar { }
public class ThailandMiddleCar : ICar { }
public class ThailandLittleCar : ICar { }
public class ThailandBigCar : ICar { }

Related Links

Why use an abstract factory
Differences between Abstract Factory Pattern and Factory Method,confused by one case
Abstract factory bad design?

Categories

HOME
webview
knockout.js
class
livecode
applescript
key
add-on
operating-system
biztalk
mongoid
repair
angular-meteor
clickable-image
game-maker
createjs
xbap
stimulsoft
mahout-recommender
bnf
mixpanel
jqxgrid
exec-maven-plugin
maven-plugin
significance
multiple-tables
infrared
pe
rxjs5
software-packaging
web-push
managed-c++
fusetools
ipa
onchange
cube
basic-authentication
substance
delayed-job
osx-lion
express-session
rails-postgresql
xerces-c
linq.js
icepdf
yoast
minikube
julius-speech
dredd
sonarqube5.2
cyanogenmod
spring.net
aurelia-fetch-client
freshdesk
fitbit
d3v4
maatwebsite-excel
nitrousio
dcount
system.net.mail
nsdata
openseadragon
dpkg
dojo-build
vaadin4spring
verisign
accessibilityservice
flush
diawi
ui4j
nastran
bjam
android-studio-import
pythonxy
extend
apple
wso2ml
supercomputers
replicaset
scala-macros
innerhtml
incron
sql-processor
phonegap-facebook-plugin
fasterxml
password-recovery
zend-mail
inequality
siena
git-reset
refit
oxygene
armv6
eclim
progress-db
colon
statechart
blobstorage
itunes-sdk
lynx
spark-view-engine
instance-variables
zotonic
kobold2d
monocross
hardcode
uploading
3des
resharper-5.1
aspbutton
facebook-fbml
data-retrieval
eai
inversion
rakudo
motif
remote-working
downloadfile
jvm-bytecode

Resources

Encrypt Message