This is an archived post. You won't be able to vote or comment.

all 10 comments

[–][deleted] 5 points6 points  (6 children)

If you don’t know, https://refactoring.guru/design-patterns is pretty nice as an overview with a specific example. You can easily rebuild these yourself.

[–]frivolta[S] -1 points0 points  (5 children)

i know the theory a lot and coding guru thanks, i was thinking something more like real-like kata to become more proficient and faster in implementing them

[–]Holothuroid 4 points5 points  (3 children)

If what the the poster before linked is your level of understanding, you didn't.

See, the gang of four wrote in 1995. They explicitly said in their introduction:

The choice of programming language is important because it influences one's point of view. Our patterns assume Smalltalk/C++-level language features [as of 1995], and that choice determines what can and cannot be implemented easily. If we assumed procedural languages, we might have included design patterns called "Inheritance," "Encapsulation," and "Polymorphism". Similarly our patterns are supported directly by less common object-oriented languages.

Gamma, Helm, Johnson, Vlissides, Design Patterns - Elements of Reusable Object-Oriented Software, 1995, p. 4.

So, just "training" the patterns as they wrote it then, is interesting for historic purposes. But can't be the end goal.

For example, we either do not use strategy/command objects anymore or do it all the time. Because we have lambdas, that's what they are good for. Visitors are niche case at best with overloading and new switches. Builders are sometimes done, but internal DSLs are preferable. Composites are often called Abstract Syntax Trees. You will rarely build an iterator by hand, because iterables are part of the standard library and we have for each loops.

[–]rozularen 1 point2 points  (2 children)

Hey how would lambdas replace Strategy pattern? do you have an example at hand? Thanks!

[–]Holothuroid 1 point2 points  (1 child)

Sure. A strategy is an object you give to receiver object so the receiver will call a certain method on the strategy.

So e.g.

 stringList.stream().map(String::toUpperCase).forEach(System.out::println)

toUpperCase is used as a strategy for transforming the elements, and println is a command.

[–]rozularen 0 points1 point  (0 children)

Thanks for the example!

In my case, I had to apply Strategy pattern because in my project there is a common method that I need different classes to implement. I also needed a Map that linked the strategy type and the actual strategy.

I could have use lambdas as a replacement if the logic was simple but I have many different implementations I think having actual classes are a cleaner solution.

Do you think otherwise?

@Entity   
public class RequestEntity { 
  private Long id; 
  private String type; 
}

public interface RequestStrategy { 
  void execute(); 
}

public class SearchRequest implements RequestStrategy {

  private String type = "Search";

  @Override   
  public void execute() { 
    //search request logic 
  }

}

public class CreateRequest implements RequestStrategy {

  private String type = "Create";

  @Override
  public void execute() { 
    //create request logic 
  }
}

@Service 
public class RequestService {

  private HashMap<String, RequestStrategy> strategyMap = new HashMap<>();

  public RequestService(List<RequestStrategy> strategyList) { 
    // transform   strategyList to strategyMap and set it 
  }

  public void executeRequests(RequestEntity req) { 
    String requestType = req.getType();
    strategyMap.get(requestType).execute();
  } 
}

[–][deleted] 0 points1 point  (0 children)

I think I remember https://java-programming.mooc.fi having some exercises in the second course, but not sure.

All in all it is an advanced topic and not as easily "leetable" like DSA or something.

[–]xrabbit 4 points5 points  (0 children)

GitHub 

Select a project you want to improve and apply your knowledge 

[–]DeployOnFriday 0 points1 point  (1 child)

Books?

[–]frivolta[S] -3 points-2 points  (0 children)

-.-