Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Recall that we used the example of a bunch of superheroes in the last session on Creational design patterns. Let's take the same example here
Recall that we used the example of a bunch of superheroes in the last session on Creational design patterns. Let's take the same example here and try to create a Bridge pattern considering the following scenario:
Lets consider superheroes for now: The Magician and Avon The Slayer. There are functions that will be done by these two Appear, Attack and Retreat. There will be types of actions for each of the superheroes AvonAction and TheMagicianAction.
There will be preactions: onActivate, apply and onDeactivate. These preactions will be defined for the Superhero Action class. These preactions will define when the superhero will perfom hisher action.
You can refer the following steps to create the solution for the problem:
Create an interface which declares the superhero actions
Create classes: The Magician and Avon the Slayer which defines all actions declared in the Revenger interface
Create the interface class for the Superhero Action which will declare the preactions
Create the implementation of the preactions for both the superheroes
Write the client code for the actions
Create hierarchies of the superheroes and the impact of their actions, and subsequently make appropriate function calls such that the console output looks like this
The Magician receives a call from the victim.
The Magician has appeared from nowhere! on calling the appear functionmethod
The Magician is in action. on calling the appear functionmethod which calls the onActivate function
The Magician flies and punches the villain in the face. on calling the attack functionmethod
The punch from The Magician goes through the face of the villain and paralyzes the villain. on calling the attack functionmethod which calls the apply function
The Magician disappears. on calling the retreat functionmethod
The Magician prepares for her next attack. on calling the retreat functionmethod which calls the onDeactivate function
Avon the Slayer is being called.
Avon The Slayer has appeared from nowhere! on calling the appear functionmethod
Avon The Slayer is in action. on calling the appear functionmethod which calls the onActivate function
Avon the Slayer swings his sword swiftly to attack the villain. on calling the attack functionmethod
Avon's sword is deadly and cuts of the villain's arm. on calling the attack functionmethod which calls the apply function
Avon The Slayer disappears. on calling the retreat functionmethod
Avon the Slayer prepares for his next attack. on calling the retreat functionmethod which calls the onDeactivate function
Note: The action of the superhero and the impact of the action are to be separated using the concepts of the Bridge pattern.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started