#590 Add explanation to Decorator pattern

This commit is contained in:
Ilkka Seppälä
2017-09-02 23:10:39 +03:00
parent 6c9e005a79
commit 9d2f0c6c71
8 changed files with 103 additions and 173 deletions

View File

@ -28,22 +28,29 @@ import org.slf4j.LoggerFactory;
/**
* Decorator that adds a club for the troll
*/
public class ClubbedTroll extends TrollDecorator {
public class ClubbedTroll implements Troll {
private static final Logger LOGGER = LoggerFactory.getLogger(ClubbedTroll.class);
private Troll decorated;
public ClubbedTroll(Troll decorated) {
super(decorated);
this.decorated = decorated;
}
@Override
public void attack() {
super.attack();
decorated.attack();
LOGGER.info("The troll swings at you with a club!");
}
@Override
public int getAttackPower() {
return super.getAttackPower() + 10;
return decorated.getAttackPower() + 10;
}
@Override
public void fleeBattle() {
decorated.fleeBattle();
}
}

View File

@ -1,53 +0,0 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.decorator;
/**
* TrollDecorator is a decorator for {@link Troll} objects. The calls to the {@link Troll} interface
* are intercepted and decorated. Finally the calls are delegated to the decorated {@link Troll}
* object.
*
*/
public class TrollDecorator implements Troll {
private Troll decorated;
public TrollDecorator(Troll decorated) {
this.decorated = decorated;
}
@Override
public void attack() {
decorated.attack();
}
@Override
public int getAttackPower() {
return decorated.getAttackPower();
}
@Override
public void fleeBattle() {
decorated.fleeBattle();
}
}

View File

@ -34,7 +34,7 @@ import static org.mockito.internal.verification.VerificationModeFactory.times;
public class ClubbedTrollTest {
@Test
public void testSmartHostile() throws Exception {
public void testClubbedTroll() throws Exception {
// Create a normal troll first, but make sure we can spy on it later on.
final Troll simpleTroll = spy(new SimpleTroll());