Please enable JavaScript.
Coggle requires JavaScript to display documents.
Behavioral Pattern - Coggle Diagram
Behavioral Pattern
Type
Iterator Pattern
definition
let traverse elements of a collection without exposing its underlying representation(list,stack,tree)
introduction
not only about traversing through a collection, provide different kind of iterators based on our requirements
hides the actual implementation of traversal through the collection and client programs just use iterator methods
-
applicability
when the collection has a complex data structure under the hood, but want to hide complexity from clients
-
code to be able to traverse different data structures or when types of these structures are unknown beforehand
pros
single responsibility Principle: clean up the client code and the collections by extracting bulky traversal algorithms into separate classes
open/close principle: implement new types of collections and iterators and pass them to existing code without breaking anything
iterate over the same collection in parallel because each iterator object contains its own iteration state
-
-
Visitor Pattern
definition
this pattern lets define a subscription mechanism to notify multiple objects about any events that happens to the object they are observing
-
Applicability
when changes to the state if one object may require changing other objects, and the actual set of objects is unknown beforehand or changes dynamically
when some objects in app must observe others, but only for a limited time or in specific cases
pros
open/closed principle: can introduce new subscriber classes without having to change the publisher's code
-
-
Observer Pattern
-
-
applicability
-
when a behavior makes sense only in some classes of a class hierarchy, but not in others
pros
open/closed principle: can introduce new behavior that can work with objects of different classes without changing these classes
-
-
cons
-
visitors might lack the necessary access to the private fields and methods of the elements that they are supposed to work with
Strategy Pattern
definition
this pattern lets define a family of algorithms. Put each of them into a separate class, and make their objects interchangeable
-
Applicability
to use different variants of an algorithm within a object and be able to switch from one algorithm to another during runtime
-
to isolate the business logic of a class from the implementation details of algorithms that may not be as important un the context of that logic
class has a massive conditional statement that switches between different variants of the same algorithm
-
cons
If only have a couple of algorithms and they rarely change,there's no real reason to overcomplicate the program with new classes and interfaces that come along with the pattern
-
a lot of modern programming languages have functional type support that lets implement different versions of an algorithm inside a set of anonymous functions. Then could use these functions exactly as used the strategy objects, but without bloating code with extra classes and interfaces
Mediator Pattern
definition
This pattern makes it possible to lessen chaotic object dependencies.
The pattern prevents the objects from interacting directly and compels them to work together exclusively through a mediator object
introduction
this pattern is very helpful in an enterprise application where multiple objects are interacting with each other
focuses on
provide a mediator between objects for communication and help in implementing lose-coupling between objects
-
Applicability
use when hard to change some of the classes because they are tightly coupled to a bunch of other classes
use when can't reuse a component in a different program because it's too dependent on other components
use when you find yourself creating tons of component subclass just to reuse some basic behavior in various contexts
pros
can extract the communications between various components into a single place, making it easier to comprehend and maintain
-
-
-
-
-
Command Pattern
definition
design pattern turns a request into a stand-alone object containing all the requested information. this transformation lets pass requests as method arguments, delay or queue a request's execution, and supports undoable operations
Applicability
-
to queue operations, schedule their execution, or execute them remotely
-
pros
Single responsibility principle: decouple classes that invoke operations from classes that perform these operations
-
-
-
-
-
-