|
| 1 | +### **Behavioral Design Patterns in Java** |
| 2 | + |
| 3 | +Behavioral design patterns focus on communication between objects and the delegation of responsibility. These patterns help define how objects interact while ensuring flexibility and scalability. |
| 4 | + |
| 5 | +This repository contains implementations of the following behavioral patterns in Java: |
| 6 | + |
| 7 | +- [Observer](#observer) |
| 8 | +- [Strategy](#strategy) |
| 9 | +- [State](#state) |
| 10 | +- [Mediator](#mediator) |
| 11 | +- [Command](#command) |
| 12 | +- [Chain of Responsibility](#chain-of-responsibility) |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## **Observer** |
| 17 | + |
| 18 | +The **Observer** pattern defines a dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. |
| 19 | + |
| 20 | +### Implementation Files |
| 21 | + |
| 22 | +- [Observer.java](./observer/Observer.java) |
| 23 | +- [ConcreteSubject.java](./observer/ConcreteSubject.java) |
| 24 | +- [ConcreteObserver.java](./observer/ConcreteObserver.java) |
| 25 | + |
| 26 | +### Test Files |
| 27 | + |
| 28 | +- [ConcreteObserverTest.java](../../test/behavioral/observer/ConcreteObserverTest.java) |
| 29 | +- [ConcreteSubjectTest.java](../../test/behavioral/observer/ConcreteSubjectTest.java) |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +## **Strategy** |
| 34 | + |
| 35 | +The **Strategy** pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from the clients that use it. |
| 36 | + |
| 37 | +### Implementation Files |
| 38 | + |
| 39 | +- [Strategy.java](./strategy/Strategy.java) |
| 40 | +- [ConcreteStrategyA.java](./strategy/ConcreteStrategyA.java) |
| 41 | +- [ConcreteStrategyB.java](./strategy/ConcreteStrategyB.java) |
| 42 | +- [Context.java](./strategy/Context.java) |
| 43 | + |
| 44 | +### Test Files |
| 45 | + |
| 46 | +- [ConcreteStrategyATest.java](../../test/behavioral/strategy/ConcreteStrategyATest.java) |
| 47 | +- [ConcreteStrategyBTest.java](../../test/behavioral/strategy/ConcreteStrategyBTest.java) |
| 48 | +- [ContextTest.java](../../test/behavioral/strategy/ContextTest.java) |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## **State** |
| 53 | + |
| 54 | +The **State** pattern allows an object to alter its behavior when its internal state changes. The object appears to change its class. |
| 55 | + |
| 56 | +### Implementation Files |
| 57 | + |
| 58 | +- [State.java](./state/State.java) |
| 59 | +- [ConcreteStateA.java](./state/ConcreteStateA.java) |
| 60 | +- [ConcreteStateB.java](./state/ConcreteStateB.java) |
| 61 | +- [Context.java](./state/Context.java) |
| 62 | + |
| 63 | +### Test Files |
| 64 | + |
| 65 | +- [ConcreteStateATest.java](../../test/behavioral/state/ConcreteStateATest.java) |
| 66 | +- [ConcreteStateBTest.java](../../test/behavioral/state/ConcreteStateBTest.java) |
| 67 | +- [ContextTest.java](../../test/behavioral/state/ContextTest.java) |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## **Mediator** |
| 72 | + |
| 73 | +The **Mediator** pattern defines an object that encapsulates communication between multiple objects to promote loose coupling. |
| 74 | + |
| 75 | +### Implementation Files |
| 76 | + |
| 77 | +- [Mediator.java](./mediator/Mediator.java) |
| 78 | +- [ConcreteMediator.java](./mediator/ConcreteMediator.java) |
| 79 | +- [Colleague.java](./mediator/Colleague.java) |
| 80 | +- [ConcreteColleagueA.java](./mediator/ConcreteColleagueA.java) |
| 81 | +- [ConcreteColleagueB.java](./mediator/ConcreteColleagueB.java) |
| 82 | + |
| 83 | +### Test Files |
| 84 | + |
| 85 | +- [ConcreteColleagueATest.java](../../test/behavioral/mediator/ConcreteColleagueATest.java) |
| 86 | +- [ConcreteColleagueBTest.java](../../test/behavioral/mediator/ConcreteColleagueBTest.java) |
| 87 | +- [ConcreteMediatorTest.java](../../test/behavioral/mediator/ConcreteMediatorTest.java) |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## **Command** |
| 92 | + |
| 93 | +The **Command** pattern encapsulates a request as an object, thereby allowing parameterization of clients, queuing of requests, and logging of executed commands. |
| 94 | + |
| 95 | +### Implementation Files |
| 96 | + |
| 97 | +- [Command.java](./command/Command.java) |
| 98 | +- [ConcreteCommand.java](./command/ConcreteCommand.java) |
| 99 | +- [Receiver.java](./command/Receiver.java) |
| 100 | +- [Invoker.java](./command/Invoker.java) |
| 101 | + |
| 102 | +### Test Files |
| 103 | + |
| 104 | +- [ReceiverTest.java](../../test/behavioral/command/ReceiverTest.java) |
| 105 | +- [ConcreteCommandTest.java](../../test/behavioral/command/ConcreteCommandTest.java) |
| 106 | +- [InvokerTest.java](../../test/behavioral/command/InvokerTest.java) |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## **Chain of Responsibility** |
| 111 | + |
| 112 | +The **Chain of Responsibility** pattern lets multiple objects handle a request without the sender needing to know which object will handle it. |
| 113 | + |
| 114 | +### Implementation Files |
| 115 | + |
| 116 | +- [Handler.java](./chainofresponsibility/Handler.java) |
| 117 | +- [ConcreteHandlerA.java](./chainofresponsibility/ConcreteHandlerA.java) |
| 118 | +- [ConcreteHandlerB.java](./chainofresponsibility/ConcreteHandlerB.java) |
| 119 | +- [ConcreteHandlerC.java](./chainofresponsibility/ConcreteHandlerC.java) |
| 120 | + |
| 121 | +### Test Files |
| 122 | + |
| 123 | +- [ConcreteHandlerATest.java](../../test/behavioral/chainofresponsibility/ConcreteHandlerATest.java) |
| 124 | +- [ConcreteHandlerBTest.java](../../test/behavioral/chainofresponsibility/ConcreteHandlerBTest.java) |
| 125 | +- [ConcreteHandlerCTest.java](../../test/behavioral/chainofresponsibility/ConcreteHandlerCTest.java) |
| 126 | +- [ChainOfResponsibilityTest.java](../../test/behavioral/chainofresponsibility/ChainOfResponsibilityTest.java) |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## **Conclusion** |
| 131 | + |
| 132 | +This repository demonstrates the use of behavioral patterns in Java with clear examples and test cases for each pattern. Follow the links above to view the source code and test files for detailed insights into each pattern. |
| 133 | + |
| 134 | +Happy coding! 🚀 |
| 135 | + |
0 commit comments