Does the strategy pattern incur more overhead than a switch statement?











up vote
0
down vote

favorite












I have a switch statement that currently handles around 20 different numerical event types. The event ID ranges from 1000-5000. The switch statement is becoming more difficult to maintain and read.



My idea is to put each event type into its own class and then store it into an EnumMap. This approach will make the code maintainable and easier to read but what am I really sacrificing here?



Each client (ranging from 1000-5000 clients) that connects will have its own initialization of this EnumMap along with the all the event type classes. This is a lot of overhead, is it not?



In regards to performance, is there any downside to using an EnumMap over a switch? Does javac compile them similarly? There will be times where every millisecond counts.










share|improve this question


















  • 2




    "Each client (ranging from 1000-5000 clients) that connects will have its own initialization of this EnumMap along with the all the event type classes" Why? Can't you have a single shared copy and pass the client-specific information (this or whatever) to the implementations stored in the map?
    – T.J. Crowder
    Nov 18 at 15:00










  • @T.J.Crowder Yes I could. Making the EnumMap static is something I was considering as well and I suppose that addresses my overhead questions.
    – user0000001
    Nov 19 at 15:26















up vote
0
down vote

favorite












I have a switch statement that currently handles around 20 different numerical event types. The event ID ranges from 1000-5000. The switch statement is becoming more difficult to maintain and read.



My idea is to put each event type into its own class and then store it into an EnumMap. This approach will make the code maintainable and easier to read but what am I really sacrificing here?



Each client (ranging from 1000-5000 clients) that connects will have its own initialization of this EnumMap along with the all the event type classes. This is a lot of overhead, is it not?



In regards to performance, is there any downside to using an EnumMap over a switch? Does javac compile them similarly? There will be times where every millisecond counts.










share|improve this question


















  • 2




    "Each client (ranging from 1000-5000 clients) that connects will have its own initialization of this EnumMap along with the all the event type classes" Why? Can't you have a single shared copy and pass the client-specific information (this or whatever) to the implementations stored in the map?
    – T.J. Crowder
    Nov 18 at 15:00










  • @T.J.Crowder Yes I could. Making the EnumMap static is something I was considering as well and I suppose that addresses my overhead questions.
    – user0000001
    Nov 19 at 15:26













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a switch statement that currently handles around 20 different numerical event types. The event ID ranges from 1000-5000. The switch statement is becoming more difficult to maintain and read.



My idea is to put each event type into its own class and then store it into an EnumMap. This approach will make the code maintainable and easier to read but what am I really sacrificing here?



Each client (ranging from 1000-5000 clients) that connects will have its own initialization of this EnumMap along with the all the event type classes. This is a lot of overhead, is it not?



In regards to performance, is there any downside to using an EnumMap over a switch? Does javac compile them similarly? There will be times where every millisecond counts.










share|improve this question













I have a switch statement that currently handles around 20 different numerical event types. The event ID ranges from 1000-5000. The switch statement is becoming more difficult to maintain and read.



My idea is to put each event type into its own class and then store it into an EnumMap. This approach will make the code maintainable and easier to read but what am I really sacrificing here?



Each client (ranging from 1000-5000 clients) that connects will have its own initialization of this EnumMap along with the all the event type classes. This is a lot of overhead, is it not?



In regards to performance, is there any downside to using an EnumMap over a switch? Does javac compile them similarly? There will be times where every millisecond counts.







java switch-statement javac strategy-pattern enum-map






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 at 14:56









user0000001

7922623




7922623








  • 2




    "Each client (ranging from 1000-5000 clients) that connects will have its own initialization of this EnumMap along with the all the event type classes" Why? Can't you have a single shared copy and pass the client-specific information (this or whatever) to the implementations stored in the map?
    – T.J. Crowder
    Nov 18 at 15:00










  • @T.J.Crowder Yes I could. Making the EnumMap static is something I was considering as well and I suppose that addresses my overhead questions.
    – user0000001
    Nov 19 at 15:26














  • 2




    "Each client (ranging from 1000-5000 clients) that connects will have its own initialization of this EnumMap along with the all the event type classes" Why? Can't you have a single shared copy and pass the client-specific information (this or whatever) to the implementations stored in the map?
    – T.J. Crowder
    Nov 18 at 15:00










  • @T.J.Crowder Yes I could. Making the EnumMap static is something I was considering as well and I suppose that addresses my overhead questions.
    – user0000001
    Nov 19 at 15:26








2




2




"Each client (ranging from 1000-5000 clients) that connects will have its own initialization of this EnumMap along with the all the event type classes" Why? Can't you have a single shared copy and pass the client-specific information (this or whatever) to the implementations stored in the map?
– T.J. Crowder
Nov 18 at 15:00




"Each client (ranging from 1000-5000 clients) that connects will have its own initialization of this EnumMap along with the all the event type classes" Why? Can't you have a single shared copy and pass the client-specific information (this or whatever) to the implementations stored in the map?
– T.J. Crowder
Nov 18 at 15:00












@T.J.Crowder Yes I could. Making the EnumMap static is something I was considering as well and I suppose that addresses my overhead questions.
– user0000001
Nov 19 at 15:26




@T.J.Crowder Yes I could. Making the EnumMap static is something I was considering as well and I suppose that addresses my overhead questions.
– user0000001
Nov 19 at 15:26

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53362222%2fdoes-the-strategy-pattern-incur-more-overhead-than-a-switch-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53362222%2fdoes-the-strategy-pattern-incur-more-overhead-than-a-switch-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy