-2

I have a project coming up and we have to create a game using JavaFX and we have to use 5 design patterns. I understand there are many design patterns in Java, but my question is: How do I use multiple patterns in one program? So far in class we have only been using one design pattern per game/program and I was just wondering how do I go about using 5 instead of one?

yenyen
  • 35
  • 1
  • 8
  • This question is too broad and vague, but let me rephrase it in a more concrete way: _I know there are lots of woodworking tools available, but how do I use multiple tools for one project? So far in my woodworking class we've been using only one tool per project and I'm wondering how I use 5 instead of one_. You could substitute many things for "woodworking". "auto mechanic" also works. – Jim Garrison Feb 27 '16 at 07:26
  • Asking the same question again without any modification won't miraculously make it on-topic. That said if it just says use, not implement you can very easily cover 3 patterns: Observer (any listener added to a property / binding uses this pattern), Decorator (`Pane`s / `ScrollPane` ect), iterator (any [`Collection.iterator()`](https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#iterator--)); I'm not so the person who has given you this task will accept this... – fabian Feb 27 '16 at 09:40
  • 1. Write the game. Maybe you make some conscious decisions about using some design patterns when you do it (e.g. "I'll factor the game data away from the presentation using MVC"). 2. Evaluate the code: what design patterns are used? 3. If there are fewer than five, determine where you can improve the code by using some design patterns. Refactor accordingly. 4. Repeat steps 2 and 3 until done. – James_D Feb 27 '16 at 14:18

1 Answers1

0

GO to this website: it has alot of really good examples in Java for you. When you use design patterns you are not using them because you have to. You use them because they make your code better, last longer, easier to understand, and a large variety of other reasons. You definantely should use factoy (to make different charecters), builder (load DB, or game), Decorater (so you can easily make changes later).

Start with the Singleton pattern, because it is easy to understand and needed for most programs. I leave the last pattern for you to decide. Good luck.

fabian
  • 67,623
  • 12
  • 74
  • 102
  • I'm not sure how a factory could benefit character creation... Also a builder for laoding a DB or game??? I'm not so sure you didn't confuse this with the Data Access Object / Facade pattern... A good answer would provide a better explanation than just dropping some keywords. But the question isn't on-topic, so don't expect too much positive response to this answer. Also singleton is considered an anti-pattern by some people (including me) and if there's need for it, it's probably just a indicator that the overall design of the program was flawed from the start... – fabian Feb 27 '16 at 10:04
  • I'm not sure that you are familiar with these concepts. Every time you use an array list you are using a factory design pattern (that is how it was made. Every time you use the driver manager (to connect to a DB) you are using something that uses the builder technique. Please do not answer questions you do not know the answer to. Because "the overall design of the program was flawed from the start" does not help anyone. – Jon_the_developer Feb 27 '16 at 15:25
  • Regarding the singleton: http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons . Regarding the factory pattern: At least now I know you have no idea about the factory pattern. There's nothing even remotely related to the factory pattern in the `ArrayList` class. Regarding the builders: of course i see builders could be used in that context, but it's far from self-explanatory. Regarding the *"Please do not answer questions you do not know the answer to."* a) Well, I didn't b) I assure you I know a thing or two about design patterns. – fabian Feb 27 '16 at 15:59
  • 1
    Are you both talking about the factory method pattern or the abstract factory pattern? `List` uses the factory method pattern in the creation of an iterator... I don't see where the abstract factory pattern would fit. Not sure how anyone can speculate as to which patterns would be useful without knowing anything at all about the application being written. – James_D Feb 27 '16 at 16:09