What do I gain by using the Strategy pattern in this case?

I wrote a program with Java that plays simple music. Currently chords have only one way (‘strumming pattern’) to be played. I want to expand this and create different ‘strumming patterns’ that chords can use to play their notes.

The Chord class has a play() method that is responsible for playing the chord. Currently it contains the logic for the only ‘strumming pattern’ of how the notes are played.

To add new strumming patterns, the simplest approach is to change play() to something like this:

void play(int strummingStyle){
    if(strummingStyle == REGULAR_STYLE) playRegular();
    if(strummingStyle == SOMETHING_ELSE_STYLE) playSomethingElse();
    // .. etc
}

Have a method for each strumming style, and parameterize the method to play a specific style.

However using the Strategy pattern feels like a better approach. What I mean is to encapsulate each strumming pattern in a subclass of StrummingPattern and set the Chord to a specific strumming pattern: chord.setStrummingPattern(StrummingPattern pattern);.

play() would then simply delegate to the strumming pattern like so:

void play(){
    pattern.play();
}

While this clearly feels like the better, more OO approach – I find that I can’t explain myself what the actual benefits are. And it’s important for me to actually understand why I’m doing something.

Please explain why using the Strategy pattern in a case like this is a better approach than the more naive ‘a method represents a behavior’ approach. What exactly are the Strategy pattern’s benefits in this kind of situation? Why should I use it?

In the first example you need to modify an existing class to add new behaviour, in the second with the strategy pattern you can add new funcionality to your system adding new classes and without modify the current code (or with minimal modifications).

This has a lot of advantages:

  • Less risk of breaking existing functionality
  • Smaller classes vs classes that grow and grow
  • Better for teamwork, think in N programmers each one adding new “strumms” at the same time with the two approaches
  • Your systems its more extensible, in the future you can make this api public and other people can add new elements (chords, strumms) to your system

In terms of SOLID, the second solution its more Open-Close and as a consequence the resultant classes are more Single responsability.

Use SOLID, GRASP, or other OO principles like cohesion and coupling,think in the cost of adding new functionally, think in the cost of understanding the system for a new programmer as tools to evaluate your designs and learn to decide WHY a design its better or not than other. The patterns often offers good solutions or pointers (often not) you need to rely on more fundamental principles of OO to evaluate and know WHY, you are in the good direction if you “feel” that needs this Why’s, its better to not use patterns than to use then blindly.

1

“However using the Strategy pattern feels like a better approach”

Yes. 🙂

Your instincts are right on this one.

In OO, you want to avoid that kind of “if this, do this; if that do that” logic, because the type system handles that for you very elegantly. (I actually have an aversion to if…then logic in general, because as the logic branches it makes my brain hurt more and more).

(A corollary, if you’re checking types with if blah instanceof Blah, then you probably need to think harder about your OO design, because you’re cheating on polymorphism and peeking at the underlying type. The class should just know what it needs to do, and the caller should just know how to call it.)

The main advantage of the Strategy pattern is leveraging the dynamic dispatch aspect of the language to do the “if…then” block for you. So you only have to make sure your Strategy classes are correct and that the consumer of them calls the play() method correctly. It scales in complexity better, because you can just drop in new strategy classes over time. Testing is tons and tons easier, because it’s all nicely decoupled.

If you want to convince yourself, do it both ways. Write an example with 5 alternatives as the block of “if…thens”, and another example with 5 alternatives in the strategy pattern.

Then write unit tests to 100% coverage for both cases.

😉

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

What do I gain by using the Strategy pattern in this case?

I wrote a program with Java that plays simple music. Currently chords have only one way (‘strumming pattern’) to be played. I want to expand this and create different ‘strumming patterns’ that chords can use to play their notes.

The Chord class has a play() method that is responsible for playing the chord. Currently it contains the logic for the only ‘strumming pattern’ of how the notes are played.

To add new strumming patterns, the simplest approach is to change play() to something like this:

void play(int strummingStyle){
    if(strummingStyle == REGULAR_STYLE) playRegular();
    if(strummingStyle == SOMETHING_ELSE_STYLE) playSomethingElse();
    // .. etc
}

Have a method for each strumming style, and parameterize the method to play a specific style.

However using the Strategy pattern feels like a better approach. What I mean is to encapsulate each strumming pattern in a subclass of StrummingPattern and set the Chord to a specific strumming pattern: chord.setStrummingPattern(StrummingPattern pattern);.

play() would then simply delegate to the strumming pattern like so:

void play(){
    pattern.play();
}

While this clearly feels like the better, more OO approach – I find that I can’t explain myself what the actual benefits are. And it’s important for me to actually understand why I’m doing something.

Please explain why using the Strategy pattern in a case like this is a better approach than the more naive ‘a method represents a behavior’ approach. What exactly are the Strategy pattern’s benefits in this kind of situation? Why should I use it?

In the first example you need to modify an existing class to add new behaviour, in the second with the strategy pattern you can add new funcionality to your system adding new classes and without modify the current code (or with minimal modifications).

This has a lot of advantages:

  • Less risk of breaking existing functionality
  • Smaller classes vs classes that grow and grow
  • Better for teamwork, think in N programmers each one adding new “strumms” at the same time with the two approaches
  • Your systems its more extensible, in the future you can make this api public and other people can add new elements (chords, strumms) to your system

In terms of SOLID, the second solution its more Open-Close and as a consequence the resultant classes are more Single responsability.

Use SOLID, GRASP, or other OO principles like cohesion and coupling,think in the cost of adding new functionally, think in the cost of understanding the system for a new programmer as tools to evaluate your designs and learn to decide WHY a design its better or not than other. The patterns often offers good solutions or pointers (often not) you need to rely on more fundamental principles of OO to evaluate and know WHY, you are in the good direction if you “feel” that needs this Why’s, its better to not use patterns than to use then blindly.

1

“However using the Strategy pattern feels like a better approach”

Yes. 🙂

Your instincts are right on this one.

In OO, you want to avoid that kind of “if this, do this; if that do that” logic, because the type system handles that for you very elegantly. (I actually have an aversion to if…then logic in general, because as the logic branches it makes my brain hurt more and more).

(A corollary, if you’re checking types with if blah instanceof Blah, then you probably need to think harder about your OO design, because you’re cheating on polymorphism and peeking at the underlying type. The class should just know what it needs to do, and the caller should just know how to call it.)

The main advantage of the Strategy pattern is leveraging the dynamic dispatch aspect of the language to do the “if…then” block for you. So you only have to make sure your Strategy classes are correct and that the consumer of them calls the play() method correctly. It scales in complexity better, because you can just drop in new strategy classes over time. Testing is tons and tons easier, because it’s all nicely decoupled.

If you want to convince yourself, do it both ways. Write an example with 5 alternatives as the block of “if…thens”, and another example with 5 alternatives in the strategy pattern.

Then write unit tests to 100% coverage for both cases.

😉

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật