Friday, February 28, 2020

The Strategy Pattern



The strategy pattern allows you to change the behavior of a class during run-time. 

This pattern is used whenever a class needs to exhibit variant behavior depending on the state of an object that may has been changed by user interaction, per example.

The implementation of the strategy

For strategy we need to implement a interface for the behavior that will changes in run-time.  


After the interface it's necessary to implement the concretes classes with the desired behaviors.


And finally a class using the strategy to change the behavior in run-time.


The way the strategies are selected is an implementation detail, it does not alter the effect of the pattern.

Main photo by O12 at Unsplash.

Thank you for reading and if you have a question use the comments!

Best regards,
Daniel Maurer
Read More

Wednesday, February 26, 2020

Object Adaper or Class Adapter Pattern


The object adapter and class adapter pattern are two paterns to allows you to provide an object instance to a client that has a dependency on an interface that your instance does not implement.

The unique different is that lass adapter uses the inheritance for the adapter and object adapter uses the composition, these patterns are the same behavior.

Which is the better?

The most of developers are told to favor composition over inheritance because that inheritance is whitebox reuse that means that you know how is implemented and this increase the complexity of the code and  prevents the adaptability to other situations.

The composition makes the subclass dependent merely ons ist interface, which is blackbox reuse so that the implementation can vary without adversey affecting clients.

Main photo by Liam Briese at Unsplash.

Thank you for reading and if you have a question use the comments!

Best regards,
Daniel Maurer
Read More

Tuesday, February 25, 2020

The Object Adapter Pattern


 

Adapter Pattern

The Adapter pattern allows you to provide an object instance to a client that has a dependency on an interface that your instance does not implement.

The implementation of adapter pattern called Object Adapter

Exist two ways of the implements  the adapter pattern, bellow you can see the object adapter pattern.

Now think that we already have a great implementation of code that do anything.


But the client can't be altered to fit the desired interface that you can see below.


To resolve this problem an Adapter class is created that fulfills the desired interface of the client and basically will implements the method of the interface that calls the method of the our great implementation.


And finally, below the client using the adapter.


This was a simple implementation of class adapter only to learn the principle of the pattern. In real world the implementations will be more hard but the principle will remain being simple.

Main photo by Murat Onder at Unsplash.

Thank you for reading and if you have a question use the comments!

Best regards,
Daniel Maurer








Read More

Monday, February 24, 2020

The Class Adapter Pattern

 

Adapter Pattern 

The Adapter pattern allows you to provide an object instance to a client that has a dependency on an interface that your instance does not implement.

The implementation of adapter pattern called Class Adapter 

Exist two ways of the implements the adapter pattern, bellow you can see the class adapter pattern.

Now think that we already have a great implementation of code that do anything.


But the client can't be altered to fit the desired interface because 

... 

whatever

Then an Adapter class is created that fulfills the desired interface of the client and basically will implements the method of the interface that calls the method of the our great implementation.


And finally, below the client using the adapter.


This was a simple implementation of class adapter only to learn the principle of the pattern. In real world the implementations will be more hard but the principle will remain being simple.

Main photo by Steve Johnson at Unsplash.

Thank you for reading and if you have a question use the comments!

Best regards,
Daniel Maurer

Read More