Posts

Showing posts from May, 2018

Behavioral Patterns - Mediator Pattern Exercise

behavioral-patterns-mediator-pattern-exerciseThe Mediator Pattern defines an object that encapsulates how a set of objects interact. In this exercise, we will create a mediator that helps us handle the cross-cutting business logic in the Command Pattern Exercise.Reading Materials https://en.wikipedia.org/wiki/Mediator_pattern (15 minutes reading)Practice MaterialsExtend the Command Pattern exercise by applying the Mediator Pattern to a new SignAllCommand class, which delegate the request to a mediator that does the actual business logic.If you haven’t done the Command Pattern Exercise but you are already familiar with the Command Pattern, you can go straight to this exercise.Components:Command (The common interface that has the execute and undo methods) or Colleague - CommandConcreteCommand (The actual command that implements the Command interface, and takes a receiver and calls its member methods.) - SignAllCommand and WriteCommandClient (The entry class that creates the command and …

Behavioral Patterns - Iterator Pattern Exercise

behavioral-patterns-iterator-pattern-exerciseThe iterator pattern is widely used in collections, it simplifies the way a collection could be iterated as well as provides a mutable capability to the collection during the iteration.Most languages provide an interface to the iterator pattern. We will implement the interface to create an iterable polyline data type. This exercise is fun enough that challenges your fundamental programming skills on the data structures and algorithms.Reading Materials https://en.wikipedia.org/wiki/Iterator_pattern (10 minutes reading)Practice MaterialsCreate a class called Polyline that implements the iterator pattern.The Polyline class could be used inside of a foreach loop to retrieve each point without exposing the index of them.The Polyline class should provide methods to extend polyline.The Polyline class should provide methods to remove points from polyline over the iteration.The Polyline class can only use the array as underlining data structure.Tips…

Behavioral Patterns - Chain of Responsibility Pattern Exercise

behavioral-patterns-chain-of-responsibility-pattern-exerciseThe Chain of Responsibility Pattern is very useful to avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along with the chain until an object handles it. We will implement a na├»ve security filter chain using this pattern in our practice exercise. You will certainly enjoy this exercise as it is a great way to get you totally master this pattern.Reading Materials https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern (15 minutes reading)Practice MaterialsUse chain of responsibility pattern to create a naive security filter chain that will check the following responsibilities in order:If any endpoint matches the requestFalse - return Not FoundIf the endpoint has any security rulesFalse - return ResourceIf the request contains any authorization headerFalse - return Unauthorized requestIf the authorization is …

Behavioral Patterns - Command Pattern Exercise

behavioral-patterns-command-pattern-exerciseThe Command Pattern is almost the second most popular pattern after the Singleton Pattern. It can be used to encapsulate each request to an object and turn the client application to a stateless application while the delegated object who receives the invocation persists its own state. In this exercise, we will create an operation manager which basically can change some state and undo the changes. The example I created is a Document Writer, which can write lines and undo the lines are written. For more challenge, there is also a bonus point – a custom data structure that you will need to implement – to mimic preventing the system crashing from not-enough-memory.Reading Materials https://en.wikipedia.org/wiki/Command_pattern (20 minutes reading)Practice MaterialsUse the Command Pattern to create a naive operation manager that can execute a sequence of operations and undo the last 3 operations. (For beginner player, you may skip the requirement…

Add Model Attributes to Layout Template

add-model-attributes-to-layout-template@ControllerAdvice is widely used to create exception handlers in a cross-cutting way when writing a Spring MVC or REST application. It could also be used to solve adding model attributes to any groups of controllers so that the model could be shared in some specific views such as a layout template. This lesson teaches the strategy to use @ControllerAdvice annotation to solve this problem.StoryWhen we have a component that is used in multiple views - such as a navigation menu with a menu service that retrieves the menu items from a repository, we need to add this data to the model. However, the navigation menu always appear in many views, it is unwise to write the code to call the menu service and bind to the model in all related controllers.SolutionSolution 1One solution is to create a abstract controller that implements this logic and ask all other controllers in whose views the navigation menu appears to extend this abstract controller. The dow…

Spring @Transactional - Propagation Type

spring-transactional-propagation-typeSpring @Transactional annotation is concise and powerful. To understand what’s the best property setting for your use case in this annotation could be confusing if you don’t clearly understand what transaction management key concepts are. In this series, we will talk about the key concepts in transaction management that could be configured through the @Transactional annotation. This article focuses on the propagation type.What is PropagationPropagation describes the transactional behavior from the client when a transactional method is called. In Spring-managed transactions, there are two types of transactions – physical and logical transactions. The physical transaction is always the outer transaction while the logical transaction is the inner transaction that is mapped to the physical transaction based on the propagation type as the transaction propagates.Propagation TypesThere are seven types of propagation that we could use for the propagation p…

Spring @Transactional - Isolation Level

spring-transactional-isolation-levelSpring @Transactional annotation is concise and powerful. To understand what’s the best property setting for your use case in this annotation could be confusing if you don’t clearly understand what transaction management key concepts are. In this series, we will talk about the key concepts in transaction management that could be configured through the @Transactional annotation. This article focuses on the isolation level and its side effects.What is IsolationIsolation defines the rules how one running transaction interacts with other concurrently running transactions. The higher the isolation level, the lower the concurrency is. In other words, the higher isolation level, the slower performance is. On the other hand, the lower isolation, the more side effects it comes with. To understand each isolation level and its side effects helps to find the right isolation for your specific use case.Isolation Levels and Its Side EffectsThere are four isolation…