-2

This question already has an answer here:

I'm struggling to come up with the name of a design pattern that I need to write about. I've been lead to think it was abstraction but apparently not.

Let's say I had a driver class which has a Drive method:

public void Drive()
{
    if(IsOnGasPedal)
    {
        //Lots of code
    }
    if(IsOnBrake)
    {
        //Lots of code
    }
    if(IsOnClutch)
    {
        //Lots of code
    }
}

And I change the method to:

public void Drive()
{
    if(IsOnGasPedal)
    {
        Car.Gas()
    }
    if(IsOnBrake)
    {
        Car.Brake()
    }
    if(IsOnClutch)
    {
        Car.Clutch()
    }
}

I have moved the complex and long code into separate methods in my Car property so that the Drive method is more organised. This is an example of refactoring but that is not the word. I thought it was either encapsulation or abstraction but I can't get any solid answers. What is this pattern called?

marked as duplicate by gnat, amon, Ben Aaronson, user22815, Jimmy Hoffa Jun 28 '15 at 23:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    I don't think this is a "design pattern" per se. Encapsulation and/or abstraction (which are not patterns but goals or ideals) probably are the most precise single words for it. – Ixrec Jun 28 '15 at 18:54
  • Have you already driven on a Ralley? On almost every curve you need to operate all 3 pedals with your 2 feet. Your code doesn't look like as it could handle that properly. – ott-- Jun 28 '15 at 21:13
  • 1
    @ott-- You do realise this is just an example? This was written purely to show the pattern in question. – Frayt Jun 29 '15 at 0:42
5

To be clear, none of "refactoring", "abstraction" or "encapsulation" are design patterns.

Neither is what you posted. As you say, it is a refactoring. Specifically, it is the one that Martin Fowler named Extract Method. The definition is "Turn the fragment into a method whose name explains the purpose of the method.", which is precisely what you are doing.

It does also fall under the definition of abstraction. Structure and Interpretation of Computer Programs, for example, describes this as:

[The means] by which compound elements can be named and manipulated as units.

Compound elements, in this case, would refer to the multiple expressions which make up the extracted methods.

1

Arguably it is simply the application of structured programming which was named long before design patterns were born.

As a refactoring, you can call what you have done 'Extract method'

1

Well I think people are missing the point that you're delegating the behavior that was once in the Driver class into the Car class, so it's not really Extract Method because otherwise it would be in the same class. This is either simple composition (not a DP) but if you want to be kind, it might be an implementation of Strategy, which some people do consider a design pattern.

Not the answer you're looking for? Browse other questions tagged or ask your own question.