wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

SE2 (LAB) Week 3 – 4 Strategy Pattern

Total questions: 23

Worksheet time: 12mins

Name
Class
Date
1.

Your task is to refactor this code using the Strategy pattern. All classes and methods have been made public for clarity. Which of the following approaches represents the BEST implementation?

a)

No changes are needed, the current code with conditional statements already effectively implements the Strategy pattern.

b)

Create a public enum GradingMethod and use a switch statement in calculateGrade to choose the appropriate calculation based on the enum value. Implement specific logic within each case of the switch statement.

c)

Modify the calculateGrade method to accept a public interface GradingStrategy as an argument and delegate the calculation logic to it. Implement separate concrete strategy classes (e.g., AverageGradingStrategy, WeightedGradingStrategy) that extend the interface.

d)

Replace the calculateGrade method with separate public methods for each grading method (e.g., calculateAverageGrade, calculateWeightedGrade). Each method would contain the specific calculation logic.

2.

You're developing a shopping cart application. You want to offer different discount calculations based on user type (e.g., standard discount, bulk discount, loyalty discount). Which approach would you use?

a)

It depends on whether you want a fixed discount based on user type or one that changes dynamically based on cart contents.

b)

Define different states for the cart (e.g., active, checkout, complete) and associate different discount calculations with each state.

c)

Create separate implementations for each discount type (e.g., StandardDiscount, BulkDiscount, LoyaltyDiscount) and dynamically choose the appropriate strategy based on the user.

d)

Build a composite discount object by combining different basic discount objects (e.g., PercentageDiscount, FlatFeeDiscount) based on user eligibility.

3.

Imagine you're building a music player that supports different playback modes (shuffle, repeat, repeat one). Currently, your code tightly couples the playback logic directly within the MusicPlayer class. You want to improve modularity and flexibility. Which pattern can achieve this by decoupling the playback logic?

a)

Define states for different playback modes and transition logic between them in the MusicPlayer class.

b)

Create a single instance of a "PlaybackManager" class responsible for all playback modes

c)

None of these choices. Tightly coupled code is necessary for playback modes.

d)

Implement separate concrete strategies for each playback mode, allowing flexible switching within the MusicPlayer.

 

4.

You're designing a complex system with numerous interrelated components involved in processing orders. You want to simplify user interaction and improve code maintainability. Should you use the Strategy or Facade pattern?

a)

Both Strategy and Facade patterns would achieve the same outcome, so the choice boils down to personal preference.

b)

Use the Facade pattern to create a single entry point for order processing, hiding the complexity of interacting with all the individual components behind a simplified interface.

c)

Use the Strategy pattern to define different order processing algorithms (e.g., express, standard, bulk) and dynamically switch between them based on customer needs.

d)

It's impossible to know without seeing the specific code structure.

5.

What distinguishes the Factory pattern from the Strategy pattern?

a)

The Strategy pattern requires more complex interfaces and classes compared to the Factory pattern.

b)

The Factory pattern is always superior to the Strategy pattern for creating different types.

c)

The Factory pattern focuses on object creation, while the Strategy pattern deals with object behavior.

d)

Both patterns achieve the same outcome, so the choice comes down to personal preference.

6.

You're building a content management system (CMS) with different publishing workflows based on content type (blog post, news article, product page). Currently, your code handles each workflow directly within the individual content type classes. To improve flexibility and maintainability, you're considering design patterns. Which pattern best suits this scenario?

a)

Create specific implementations of each publishing workflow, enabling dynamic switching based on content type within the CMS.

b)

Define states for different stages of the publishing workflow (draft, review, published) and transition logic between them within each content type class.

c)

Create distinct factories for each content type, responsible for generating objects with the appropriate publishing workflow logic embedded.

d)

All of these approaches could be beneficial for this scenario.

7.

public interface PublishingWorkflowStrategy {
  public void ___Q1___  (Content content);
}

a)

publish

b)

PublishingWorkflowStrategy

c)

content

8.

public class BlogPostPublishing implements ___Q2___  {

a)

publish

b)

PublishingWorkflowStrategy

c)

content

9.

  public void publish(Content content) {
    // Specific workflow logic for blog posts (e.g., SEO optimization, social media promotion)
    System.out.println("Publishing blog post: " +
___Q3___  .getTitle());
  }

a)

publish

b)

PublishingWorkflowStrategy

c)

content

10.

  private ___Q1____ content;

a)

Content

b)

strategy

c)

content

11.

if (content.getType() == ContentType.BLOG_POST) {
       
___Q2____  = new BlogPostPublishing();
}

a)

Content

b)

strategy

c)

content

12.

strategy.  ___Q3____  (content);

a)

Content

b)

strategy

c)

content

13.

Which code snippet best uses the Strategy pattern to choose the calculation method?

a)

OPTION C

b)

OPTION B

c)

OPTION D

d)

OPTION A

14.

    ShoppingCart cart = new ____Q1_____ ;

a)

ShoppingCart()

b)

chosenStrategy

c)

strategy

d)

 shippingCost

e)

getShippingFromUser()

15.

switch (____Q2_____)

a)

ShoppingCart()

b)

chosenStrategy

c)

strategy

d)

shippingCost

e)

getShippingFromUser()

16.

cart.setShippingStrategy(____Q3_____); 

a)

ShoppingCart()

b)

chosenStrategy

c)

strategy

d)

shippingCost

e)

getShippingFromUser()

17.

    double ____Q4_____  = cart.getShippingCost();

a)

ShoppingCart()

b)

chosenStrategy

c)

strategy

d)

shippingCost

e)

getShippingFromUser()

18.

  private static String _____Q5____  {
    return "standard";

}

a)

ShoppingCart()

b)

 chosenStrategy

c)

strategy

d)

shippingCost

e)

getShippingFromUser()

19.

this.strategy = ____Q1____;

a)

strategy

b)

calculateCost

c)

ShippingStrategy

d)

return

e)

BASE_PRICE

20.


      return strategy
. ____Q2____ (totalWeight, "defaultDestination"); 

a)

strategy

b)

calculateCost

c)

ShippingStrategy

d)

 return

e)

BASE_PRICE

21.

public class StandardShipping implements ___Q3_____

a)

strategy

b)

calculateCost

c)

ShippingStrategy

d)

 return

e)

BASE_PRICE

22.

  public double calculateCost(double weight, String destination) {
    ____Q4____  weight * BASE_PRICE_PER_KG;
  }

a)

strategy

b)

calculateCost

c)

ShippingStrategy

d)

return

e)

BASE_PRICE

23.

  private final double ____Q5____ = 15.0;

a)

strategy

b)

calculateCost

c)

ShippingStrategy

d)

return

e)

BASE_PRICE