Skip to main content

What is Design pattern?

Created
Active
Viewed 339 times
12 replies
3

I am little bit confused about design pattern. Is it necessary to use design pattern in every application? If not where we should use and why?

  • 171
  • 1
  • 3
  • 14

12 replies

Sorted by:
78323338
1

Patterns are a tool, like many things in programming. Learn about a tool (e.g. one design pattern) and how/where to use it, its advantages and tradoffs etc. Then, make an informed decision whether your current task would be better off with that tool applied.

78323366
0
  • 33.6k
  • 27
  • 101
  • 149

What does this have to do with PHP or Laravel? Wouldn't this be more suitable as a question?

78323468
2
  • 198.8k
  • 55
  • 450
  • 857

Oh so many questions at once, hope it helps:

Is it necessary to use design pattern in every application?

No, absolutely not.

If not where we should use and why?

You find them, like you learn other words. They allow us then to communicate.


NOTE: LARAVEL

Humans invent words, make up terms, work together but also isolated and fast.

When only using such words in their bubble the main name stands for more than the sum: Laravel. Totally crazy with the terms, sometimes even up to this day: implementation specified only.

What a generically used term actually means remains overshadowed by the dust of the past, and names that sound like patterns are not patterns.


What is Design pattern?

IIRC, Christopher Alexander explained this. Wikipedia should have first pointers. https://en.wikipedia.org/wiki/Design_pattern

78323794
5
  • 29.3k
  • 9
  • 63
  • 85

Do you need to use if or loops? Do you need to use cutlery eat food? Do you need to use socks?

The answer to all of these is "no". You don't need to. Plenty of times you can avoid it. However, all of these are still handy tools that we do use most of the time without even thinking about it much.

Same with design patterns. They aren't a thing you have to mull over "should I or shouldn't I". In fact, it's pretty much impossible to avoid design patterns. But when you're not thinking about your application from design perspective, you fall into anti-pattern space where you still use recognised patterns but they are just dragging you down. God objects, inner platforms, exceptions as control flow, etc.

78324279
3

Design patterns are descriptive, not prescriptive. As you gain experience, you will notice them naturally emerging. Applying them intentionally is almost always complexity for the sake of complexity.

78328088
1

Design patterns are well thought out solutions for commonly occurring problems in application design. There is no absolute need to use them but if you use them well you can avoid reinventing the wheel.

For example. Let's say that you have some worker object that will do some work and return instance of result object. Naive way of implementing this might be something like:

class Result
{
    // ... result class implementation
}

class Worker
{
    public function doWork(): Result
    {
        $result = new Result();
        // ... do some work and hydrate Result instance
        return $result;
    }
}

But code like this makes classes Worker and Result coupled too tightly. This introduces some issues that might not be obvious at first but that can hurt you later.

When you write unit test for Worker class, you can't mock instance of Result that's created inside of doWork() method. If you later introduce some changes that would make the test fail you wouldn't know if it was broken by changes made inside of Worker class or by changes made inside of Result class.

Another problem might be when you want to extend your application and you want you your Worker to produce results for different consumers where each consumer would need different implementation of result class.

Let's see how we can decouple Worker and Result classes, make them easier to test and make it easier to add new functionality to app.

interface ResultInterface
{
    // ... some methods to hydrate result instance with data eg.
    public function setSomeProperty(): void;
    // ... some methods to allow access to the result data
    public function getSomeProperty(): string;
}

class Result implements ResultInterface
{
    // ... result class implementation
}

class Worker
{
    public function __construct(
        private readonly ResultInterface $resultPrototype
    ) {
    }

    public function doWork(): ResultInterface
    {
        $result = clone $this->resultPrototype;
        // ... do some work and hydrate Result instance
        $result->setSomeProperty(...);
        return $result;
    }
}

Now our Worker class doesn't really care what implementation of ResultInterface is used to hold the work result. It can be supplied with whatever implementation we need be it a mock object for test or different implementations for different consumers. To achieve that we've used two different design patterns.

First used design pattern is prototype. This pattern allows you to create instances of classes by copying them from a single instance that serves as template. Our template is being held in $resultPrototype property. We needed to know what class exactly we were using when we created new result instance with new Result() but now we don't really need to know what class we are using when we just copy it with clone $this->resultPrototype.

To completely get rid of the need to know what class we will use for result we are using dependency injection pattern. This pattern shifts responsibility for creating dependency instances from our Worker class to whoever is creating the instance of Worker class. The dependency is created outside of Worker then it's passed (injected) to worker's constructor.

As you can see, naive approach might be a bit less code to write and it might seem "good enough" at first. But it will reach its limits sooner rather than later. If you want to deal with its issues knowing and using design patterns can save you time you would need to look for solution with some trial and error approach. On the other side each pattern was made for solving specific problem. If you overuse design patterns for situations they weren't made for you can easily make your code unnecessarily complicated.

78333120
0
  • 171
  • 1
  • 3
  • 14
Author

Thanks for brief explanation. It will help me a lot.

78328851
1

Hi,

Yes, it's best practice to use it. A design pattern is not specific to a language but to a way of coding, and how you organize your project. When you begin, it's almost impossible to avoid it.

https://en.wikipedia.org/wiki/Software_design_pattern
Enjoy ! :)

79369849
0

I saw a good number of beginners trying to do the worst thing: sticking one or another design pattern in every application. It looks like their task is to please the authors of the patterns.

You need to remember: what are the ultimate goals of your activity? Who benefits from it: your team, your customer, or some other people?

First of all, you need to think about the goals of every application and try to reach them. When you designed it to the stage when you may have some options to choose from, you can look around and ask: what other developers did in simular situation? And then it's time for design patterns. Remember: you are not a slave of your patterns, but patterns should be at your service.

79406409
0

It is not necessary to use design patterns for every application, but it will come in handy when the project grows more and more.

It is mainly used in large systems from what I know, and that is the biggest use case, but understanding it helps even in smaller projects

79409599
0

Design patterns are typical solutions to commonly occurring problems in software design. They are like pre-made blueprints that you can customize to solve a recurring design problem in your code. Unless you writing a very simple application, a design pattern's scope is a function, module, etc. Not the whole application.

Is it necessary to use? To answer this, try to compare it to chess openings. Not need to use, but certainly helps.

79477273
0

Hi,

you don't need to use design patterns.

However, if your code base gets too big and you didn't use them, you will struggle more and more to understand how your code works.

For example:

SOLID:

Single responsibility principle:

One class should only have one public method, that can be called from other classes.

Like: public function validateMyXmlInput($xml);

The upside here is, that anyone who wants to use the class, can only use the validate method.

If you exposed more methods someone might think: oh, there is some method I need for this thing.

And then you end up in every piece of code uses every method from every class somewhere and can't make any adjustments in the old code, since you don't know where it is used.

So again: you don't need to, but the more the code amount grows, the more it is recommended.

X