r/javahelp • • Apr 12 '17

Solved What are the simplest design patterns for beginners to learn.

I have to write a paper on a design pattern, and I'm trying to find one that is easy to understand. Factory seems simple but I think several other people are doing it also.

Edit: Thanks everyone I went with the Strategy Pattern.

20 Upvotes

19 comments sorted by

5

u/Cicko24 Apr 12 '17

Builder, Memento, State, Strategy, Flyweight? Those should be easy to implement and understand.

3

u/Hoog1neer Apr 12 '17 edited Apr 14 '17

As aforementioned, Singleton is probably the simplest. Other good ones: Observer, Decorator, Factory, Composite.

3

u/desrtfx Out of Coffee error - System halted Apr 13 '17

Head over to Java Design Patterns for simple, in-depth explanations of the patterns with sample code. You can filter the patterns by difficulty. Filter by "Difficulty Beginner".

IMO, the Observer, Strategy, State, Facade, Composite, Flyweight, and Builder patterns are the easiest.

5

u/snotsnot Apr 12 '17

Singleton?

-3

u/[deleted] Apr 13 '17

IMO the singleton is the worst design pattern and you should not use it. it's is IMO a bad practice to use it.

Factory Pattern and so on can be learn. The list on tutorialspoint about design patterns contains some good and helpful patterns.

5

u/morhp Professional Developer Apr 13 '17

Just saying something is bad doesn't make sense. Please give or link some argument.

I think the Singleton design pattern isn't necessarily bad. You often have classes where you only need one instance from. Like you have a Clock interface and have an implementation class that implements this clock using the current system time. There will be never a case where you need two different instances of this class (unless you're doing something really wrong). So I'd implement this as a singleton. Or Collections.emptyList() is a completely fine singleton.

Of course you can misuse the singleton pattern and in the worst case it's as bad as static variables, but if used correctly, it can be great to simplify things and reduce memory usage.

1

u/[deleted] Apr 13 '17

I agree with you. I personally don't use Singeltons, because - you said it in your last part - it can be misused very very easy. There are good cases/reasons to use singeltons, like you said. But everyone has to decide if and when he wants to use the singleton. As I said, I don't like it, and I don't want to use it (or try to reduce the useage of it as much as possible). Generally I am very carefull towards design patterns.

BTW: I am wondering how long the list of pro/cons in the german wikipedia is, for the singleton. I expected the english wikipedia to contain some pros/cons of the pattern, but there ain't any.

2

u/wildjokers Apr 13 '17

Saying "you should not use it" is a pretty bold statement. I would change that to you should use it very sparingly. I use it frequently for configuration classes because configuration by definition is global state. I can either pass a configuration object around to every class that needs it (a giant PITA) or I can make my configuration a singleton.

In Grails the configuration object is a singleton, and singleton is the recommended scope for Controller objects.

The Runtime class in the JDK is a singleton (http://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html)

It has its uses.

1

u/morhp Professional Developer Apr 13 '17

configuration by definition is global state

Until you want to run two instances of your program with different configurations in the same JVM. Can always happen. For example I developed sometime ago a client application that spoke to a server using some configuration. The configuration would have worked fine as a singleton. Until it became necessary to connect to two different servers at the same time so they could exchange data. Had I used singletons everywhere, that would have required a lot of refactoring. Instead there were only a few badly programmed caches using static maps that needed changes.

If you need to pass around configurations across your whole program, that smells like bad modulation and makes testing single components probably very difficult. I'd be very careful with singletons and only use them for simple, immutable objects.

2

u/original_4degrees Apr 12 '17

MVC. a classic

2

u/metricchicken Apr 13 '17

While not a pattern, have you checked out Effective Java by Joshua Bloch? The book does a great job at explaining certain patterns and why they are effective.

1

u/knoam Apr 12 '17

Null object pattern

1

u/bitNation Apr 12 '17

Template Method Pattern. One of my favorites.

2

u/HelperBot_ Apr 12 '17

Non-Mobile link: https://en.wikipedia.org/wiki/Template_method_pattern


HelperBot v1.1 /r/HelperBot_ I am a bot. Please message /u/swim1929 with any feedback and/or hate. Counter: 55252

1

u/robot_lords Software Engneer in Test Apr 13 '17

Singleton is probably the easiest

1

u/morhp Professional Developer Apr 13 '17

Singleton isn't really easy if done correctly. Serialization can be a problem if needed, and threadsafe lazy initialization, too.

I'd say Builder and Command pattern are probably easier.

-1

u/[deleted] Apr 13 '17

And IMO the worst

1

u/Chknbone Noobie Brewster Apr 13 '17 edited Apr 13 '17

Remindme! 2 days