Posts

Showing posts with the label Behavioral Patterns

Refactoring Chain of Responsibility Pattern with Lambdas

Image
Many existing object-oriented design patterns can be made redundant or written in a more concise way using lambda expressions. In this post, I would like to explain how to refactor  Chain of Responsibility Pattern  using  lambda expressions ?. This post may help in some situations like if we have implemented Observer Design Pattern in our projects using JDK 6 or 7. Now we want to use JDK 8 then we can refactor implementation of  Chain of Responsibility Pattern  using  lambda expressions  or we newly implement  Chain of Responsibility Pattern  using  lambda expressions  in our projects. Let's understand this refactoring technique with detail example. You can refer there is a separate post for  Chain of Responsibility Pattern . If you want to revise or read the lambda expressions then visit  lambda expressions . The chain of responsibility pattern is a common solution to create a chain of processing objects (such as a c...

Refactoring Observer Design Pattern with Lambdas

Image
Many existing object-oriented design patterns can be made redundant or written in a more concise way using lambda expressions. In this post, I would like to explain how to refactor Observer Design Pattern using lambda expressions ?. This post may help in some situations like if we have implemented Observer Design Pattern in our projects using JDK 6 or 7. Now we want to use JDK 8 then we can refactor implementation of  Observer Design Pattern  using  lambda expressions  or we newly implement  Observer Design Pattern  using  lambda expressions  in our projects. Let's understand this refactoring technique with detail example. You can refer there is a separate post for  Observer Design Pattern . If you want to revise or read the lambda expressions then visit:  lambda expressions The observer design pattern is a common solution when an object (called the subject) needs to automatically notify a list of other objects (called observers) w...

Refactoring Strategy Design Pattern with Lambdas

Image
In this post, I would like to explain how to refactor Strategy Design Pattern using lambda expressions ?. This post may help in some situations like if we have implemented  Strategy Design Pattern  in our projects using JDK 6 or 7. Now we want to use JDK 8 then we can refactor implementation of  Strategy Design Pattern  using  lambda expressions  or we newly implement  Strategy Design Pattern  using  lambda expressions  in our projects. Let's understand this refactoring technique with detail example. If you want to revise or read the Strategy Design Pattern then visit:  Strategy Design Pattern . If you want to revise or read the lambda expressions then visit:  lambda expressions Strategy Design Pattern can be written in a more concise way of using lambda expressions. The  Strategy Design Pattern  states that it is a common solution for representing a family of algorithms and letting you choose among them at runt...

Delegation Pattern

Image
Intent It is a technique where an object expresses certain behavior to the outside but in reality delegates responsibility for implementing that behaviour to an associated object. Also known as Proxy Pattern Explanation Delegation is a way of reusing and extending the behavior of a class. It works writing a new class that  incorporates the functionality of the original class by using an instance of the original class and calling its  methods. The advantage of delegation is that it is easy to compose behavior at runtime. Implementation Let's take an example of Printers Implementation .  In this example the delegates are CanonPrinter , EpsonPrinter or HpPrinter they all implement Printer. The PrinterController  is a delegator class which also implements Printer . PrinterController is not responsible for the actual desired action but is actually delegated to a helper class either CanonPrinter, EpsonPrinter or HpPrinter . The consumer does not have or require knowledg...

Null Object Design Pattern

Image
Intent In most object-oriented languages, such as Java or C#, references may be null. These references need to be checked to ensure they are not null before invoking any methods because methods typically cannot be invoked on null references. Instead of using a null reference to convey the absence of an object (for instance, a non-existent customer), one uses an object which implements the expected interface, but whose method body is empty. The advantage of this approach over a working default implementation is that a Null Object is very predictable and has no side effects: it does nothing. Structure Participants Client  requires a collaborator. AbstractObject  declares the interface for Client's collaborator. implements default behavior for the interface common to all classes, as appropriate. RealObject defines a concrete subclass of AbstractObject whose instances provide useful behavior that Client expects. NullObject provides an interface identical to AbstractObject's so t...