Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

A complete guide on Behavioral Design Pattern and Chain of Responsibility Pattern in Java

Posted on Sep 13 • Originally published at ifourtechnolab.com In this blog, we’ll learn behavioral design patterns where and when we can use this particular pattern, and its types.Also, we’ll particularly focus on the chain of responsibility pattern which is a type of behavioral design pattern.A design pattern in which more emphasis is given to how the objects interact with each other.Also as the name suggests a chain of responsibility i.e., objects have their responsibilities/tasks to perform.In behavioral design patterns, there should be minimum hard coding, the objects should be loosely coupled and coherence should be more.Now, let us understand the terms coupling and coherence. These are significant terms in design patterns and also in software engineering.Coupling - Coupling means interdependency here in terms of objects. The objects should be less dependent on each other for a particular task, otherwise, due to a failure or delay in one object can lead to a system failure.Coherence - Coherence means that the objects/modules should work together so that they complement each other rather than creating a burden on the system.A design pattern in which objects show a chain behavior is a chain of responsibility pattern.Whenever a user sends a request it passes through a chain of an object and any object can send a response accordingly. So there is the least coupling between the objects.Now, let us understand the chain of responsibility pattern with an example. The given below UML diagram will make you easily understand. [Img Source]So here we have three different Logger class which extends the main abstract logger class and a client class.Each class has its attributes and methods like Logger has output info, error info, debug info as attributes, and set Logger, Message, display Log Info as methods.Console Based Logger class has a console-based logger and displays Log Info as methods but no attributes of its own. Debug Based Logger has Debug Based Logger(int levels) and displays Log Info as methods but no attributes of itThe client class which is Chain of Responsibility Client has de chain() and main methods.Main logger classConsole based logger classError based logger classClient class / main classOutput:AbilityClientCONSOLE LOGGER INFO: Enter the sequence of valuesCONSOLE LOGGER INFO: An error has occurred nowERROR LOGGER INFO: An error has occurred nowCONSOLE LOGGER INFO: This was the error now debugging is compiledERROR LOGGER INFO: This was the error now debugging is compiledDEBUG LOGGER INFO: This was the error now debugging is compiledSo in this blog, we focused learning on the chain of responsibility pattern which is one of the behavioral design patterns, its uses, advantages, and also when to use these design patterns, etc. along with an example.Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Toma Rares - Sep 12 Ibrahim Aziz - Sep 11 Alan Johnson - Sep 3 Mainul Hasan - Sep 11 Once suspended, ifourtechnolab will not be able to comment or publish posts until their suspension is removed. Once unsuspended, ifourtechnolab will be able to comment and publish posts again. Once unpublished, all posts by ifourtechnolab will become hidden and only accessible to themselves. If ifourtechnolab is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Harshal Suthar. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag ifourtechnolab: ifourtechnolab consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging ifourtechnolab will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

A complete guide on Behavioral Design Pattern and Chain of Responsibility Pattern in Java

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×