Created
May 21, 2017 08:56
-
-
Save fermopili/4bc48740e06e6166b0c3a58d93a0c5ca to your computer and use it in GitHub Desktop.
com.javarush.task.task37.task3710 . Decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3710.decorators; | |
import com.javarush.task.task37.task3710.shapes.Shape; | |
/** | |
* Created by Administrator on 21.05.2017. | |
*/ | |
public class RedShapeDecorator extends ShapeDecorator | |
{ | |
private Shape shape; | |
public RedShapeDecorator(Shape decoratedShape) | |
{ | |
super ( decoratedShape ); | |
shape = decoratedShape; | |
} | |
private void setBorderColor(Shape shape) | |
{ | |
System.out.println ( "Setting border color for " + shape.getClass ( ).getSimpleName ( ) + " to red." ); | |
} | |
@Override | |
public void draw() | |
{ | |
setBorderColor ( shape ); | |
super.draw ( ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3710.decorators; | |
import com.javarush.task.task37.task3710.shapes.Shape; | |
public abstract class ShapeDecorator implements Shape { | |
protected Shape decoratedShape; | |
public ShapeDecorator(Shape decoratedShape) { | |
this.decoratedShape = decoratedShape; | |
} | |
@Override | |
public void draw() { | |
decoratedShape.draw(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3710.shapes; | |
public class Circle implements Shape { | |
@Override | |
public void draw() { | |
System.out.println("Drawing a shape: CIRCLE!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3710.shapes; | |
public class Rectangle implements Shape { | |
@Override | |
public void draw() { | |
System.out.println("Drawing a shape: RECTANGLE!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3710.shapes; | |
public interface Shape { | |
void draw(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3710; | |
import com.javarush.task.task37.task3710.decorators.RedShapeDecorator; | |
import com.javarush.task.task37.task3710.shapes.Circle; | |
import com.javarush.task.task37.task3710.shapes.Rectangle; | |
import com.javarush.task.task37.task3710.shapes.Shape; | |
/* | |
Decorator | |
*/ | |
public class Solution { | |
public static void main(String[] args) { | |
Shape circle = new Circle(); | |
Shape redCircle = new RedShapeDecorator(new Circle()); | |
Shape redRectangle = new RedShapeDecorator(new Rectangle()); | |
System.out.println("Simple circle"); | |
circle.draw(); | |
System.out.println("\nApplied RedShapeDecorator to the circle"); | |
redCircle.draw(); | |
System.out.println("\nApplied RedShapeDecorator to the rectangle"); | |
redRectangle.draw(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
taskKey="com.javarush.task.task37.task3710" | |
Decorator | |
Создай класс RedShapeDecorator в пакете Decorators. Он должен расширять функциональность объектов типа Shape | |
не меняя их структуру. | |
Я уже создал абстрактный класс ShapeDecorator, поэтому: | |
1) Класс RedShapeDecorator сделай наследником класса ShapeDecorator. | |
2) Реализуй приватный метод setBorderColor с одним параметром типа ShapeDecorator. | |
Он должен выводить на экран фразу "Setting border color for XXX to red.", | |
где XXX - имя конкретного декорируемого класса (можешь воспользоваться методами | |
getClass().getSimpleName() вызванными на объекте полученном в качестве параметра). | |
3) Переопредели метод draw, в нем сначала измени цвет отображаемого объекта | |
с помощью метода setBorderColor, а потом нарисуй его. | |
Требования: | |
1. Класс RedShapeDecorator должен быть потомком класса ShapeDecorator. | |
2. Метод setBorderColor должен выводить на экран фразу соответствующую условию задачи. | |
3. Метод setBorderColor должен быть приватным. | |
4. В методе draw класса RedShapeDecorator должен быть изменен цвет декорируемого объекта и вызван его метод draw. | |
5. Публичный конструктор класса RedShapeDecorator должен принимать один параметр типа Shape. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment