1

My workmate asked me this question today and I couldn't really give him short answer without using examples...

So let me ask you guys this question "What are design patterns" and try to give short answer that is the easiest to understand

  • 3
    SO isn't a dictionary. There are literally dozens, maybe hundreds, of definitions and explanations online; you must have looked for the answer yourself and read at least a few of these - what exactly do you not understand? _That_ would be a reasonable question for SO, but not this IMO – Clive Jul 02 '14 at 12:41
  • A cursory internet search seems like a good place to start... http://en.wikipedia.org/wiki/Design_pattern – David Jul 02 '14 at 12:45
  • 5
    I personally like this question. SO is a place for professionals and passionate people - those who'd know programming patterns by heart and practice. What's better group to ask when learning? Or more: when learning how to teach? – hauron Jul 02 '14 at 12:46
  • 1
    @hauron A book? Their teacher? One of the dozens of resources that already answer this question with a search for "design patterns"? This is an ambiguous request with no objective answer - it's a bad fit for Q+A. And since this is a Q+A site... – Clive Jul 02 '14 at 12:59
  • 2
    @Clive Whenever I try explaining the design patterns, people start to get bored after 2 sentences. Giving a short explanation is some kind of Challenge. personally, I'd like to protect this question – yannicuLar Jul 02 '14 at 13:01
  • @yannicuLar with utmost respect, whether or not you bore somebody talking about something has nothing to do with whether a question about it is on topic here – Clive Jul 02 '14 at 13:02
  • 2
    I tried to find one sentence answer to this question: there isn't. SO Is specific kind of resource, I like how answers are structured. Most of programmers start to search answers here because this resource is RELIABLE. If there isn't answer to this question yet why not create one? Let's see this example: http://stackoverflow.com/questions/111102/how-do-javascript-closures-work - there are dozens of answers what is closure on the internet but there isn't 'SO style' answer yet so... what's wrong in asking here? – user3752786 Jul 02 '14 at 13:02
  • I've already given my reasons, you'd have to ask all the other close-voters individually for their motivation. I would suspect it's roughly the same as mine but I could be wrong – Clive Jul 02 '14 at 13:04
  • 2
    @Clive There's tons of questions with answers being picked subjectively based on coding style, personal preference, etc. I still think this thread has a chance of finding short, strict answer to question "what is a programming pattern" (e.g. I like yannicuLar's answer) – hauron Jul 02 '14 at 13:06
  • 1
    @hauron and that's fine - this is what voting is for, you have your say and I have mine :) – Clive Jul 02 '14 at 13:07
  • @Clive I just think this is not the *Help me, I'm too bored to google* or *I spent 2 mins reading the wikipedia article and I don't get it* kind of question. Giving a Short answer to this question IS a challenge – yannicuLar Jul 02 '14 at 13:07
  • 2
    This question is actually good, since OP already knows what a design pattern is. He's asking for a **short answer** which is a pretty hard task – Christopher Francisco Jul 02 '14 at 13:09
  • 3
    If the answer to this question is so obvious and so easy to find why there isn't any good, one sentence explanation posted below yet? @Clive – user3752786 Jul 02 '14 at 13:10
  • We're getting way off song here. Let's agree that you think it's on topic, and I think it's off topic, and move on. We could argue the finer points for ever, let's leave it to the community in general to see whether this is an appropriate question or not – Clive Jul 02 '14 at 13:10

3 Answers3

2

Design patterns are different approaches or tools or guides to solve Software Design problems. Mastering them, helps you

  1. Identify a pattern of problems or requirements
  2. Propose one ore more solutions that address this problem, evaluate the pros/cons and pick the most suitable

That would be my short explanation without using examples. But I always like using examples anyway, so if i had to provide examples too, I'd pick these to demonstrate the 'if you see this, do that' way of working with design patterns

  1. When you know you have to provide an 'undo' feature in your transaction you know that the Command Pattern is a way to go

  2. When you have to use a Resource manager, it would be nice to use the Singleton pattern

  3. But while designing the AI for your RGP game NPCs, you can use Strategy pattern or Template pattern. Some might even choose Decorator Pattern. There's not always a definite winner. Depends on what suits you better.

yannicuLar
  • 3,083
  • 3
  • 32
  • 50
1

Common scenarios of dividing responsibilities, tasks and information among modules, and defining the relations between them?

hauron
  • 4,550
  • 5
  • 35
  • 52
0

Design Patterns are patterns you use to construct a class based on certain conditions.

Example:

  1. If there is a class that should be only 1 instance in the whole application (like a database adapter class) you go for Singleton pattern, which is a pattern for constructing an object once. Only 1 instance.

  2. When you have classes with a lot of attributes and default values you go for Builder, which lets you construct an object by chaining it's setters

    MyClass myClass = new MyClass.Builder()
        .setTitle("my title")
        .setBody("this is a body for the class")
        .setEnabled(true)
        .build();
    
  3. When you have operations between business objects that are related but none of them owns the other, you go for Facade, which creates a facade of your business to handle said objects.

    I.E: you have Movies, Sales objects, but you can only create a Sale if there are movies in the inventory. You can't use Sales to check Movies but you can create a facade called Store and handle both object's operation like Store#createSale().

  4. When you have event-based code execution (like on click), you go for a Listener or Callback.

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
  • 1
    "..you use to construct a class based on certain conditions" Many Design Patterns will introduce a bucket of classes, not just one :) – yannicuLar Jul 02 '14 at 14:01