public class Steps {
public void goSouth() {
System.out.println("step to south");
}
public void goNorth() {
System.out.println("step to north");
}
public void goEast() {
System.out.println("step to east");
}
public void goWest() {
System.out.println("step to west");
}
}
public abstract class StepsCommand implements Command {
protected Steps steps = new Steps();
}
public interface Command {
void execute();
}
public class GoEastCommand extends StepsCommand {
@Override
public void execute() {
steps.goEast();
}
}
public class GoNorthCommand extends StepsCommand {
@Override
public void execute() {
steps.goNorth();
}
}
public class GoSouthCommand extends StepsCommand {
@Override
public void execute() {
steps.goSouth();
}
}
public class GoWestCommand extends StepsCommand {
@Override
public void execute() {
steps.goWest();
}
}
public class Navigator {
private final List<StepsCommand> steps = new LinkedList<>();
private final List<StepsCommand> path = new LinkedList<>();
public Navigator registerStep(StepsCommand step) {
steps.add(step);
return this;
}
public void go() {
for(StepsCommand step : steps) {
step.execute();
((LinkedList)path).addFirst(step);
}
steps.clear();
}
public void goBack() {
for(StepsCommand step : path) {
step.execute();
}
path.clear();
}
}
public class Client {
public static void main(String[] args) {
Navigator navigator =
new Navigator().registerStep(new GoEastCommand())
.registerStep(new GoNorthCommand())
.registerStep(new GoNorthCommand())
.registerStep(new GoSouthCommand());
System.out.println("go");
navigator.go();
System.out.println("go back");
navigator.goBack();
}
}
/**
* Output:
* go
* step to east
* step to north
* step to north
* step to south
* go back
* step to south
* step to north
* step to north
* step to east
*/
четверг, 29 марта 2012 г.
Паттерн Комманда(Command). Пример кода
среда, 14 марта 2012 г.
Паттерн Цепочка обязанностей(Chain of responsibility). Пример кода
public class HouseProject {
private final Set steps = new HashSet();
public HouseProject(Steps... steps) {
this.steps.addAll(Arrays.asList(steps));
}
public enum Steps {
CREATE_BASEMENT,
ADD_FLOOR,
CREATE_ROOF,
BUILD_FENCE
}
public Set getSteps() {
return steps;
}
}
public abstract class Builder {
protected Builder nextBuilder;
private final HouseProject.Steps step;
public Builder(HouseProject.Steps step) {
this.step = step;
}
public abstract void buildImpl();
public void build(HouseProject project) {
if (project.getSteps().contains(step)) {
buildImpl();
}
if (nextBuilder != null) {
nextBuilder.build(project);
}
}
public Builder setNext(Builder builder) {
nextBuilder = builder;
return builder;
}
}
public class BasementBuilder extends Builder {
public BasementBuilder() {
super(HouseProject.Steps.CREATE_BASEMENT);
}
@Override
public void buildImpl() {
System.out.println("Building basement");
}
}
public class FloorBuilder extends Builder {
public FloorBuilder() {
super(HouseProject.Steps.ADD_FLOOR);
}
@Override
public void buildImpl() {
System.out.println("Building floor");
}
}
public class RoofBuilder extends Builder {
public RoofBuilder() {
super(HouseProject.Steps.CREATE_ROOF);
}
@Override
public void buildImpl() {
System.out.println("Building roof");
}
}
public class FecneBuilder extends Builder {
public FecneBuilder() {
super(HouseProject.Steps.BUILD_FENCE);
}
@Override
public void buildImpl() {
System.out.println("Building fence");
}
}
public class Client {
public static void main(String[] args) {
Builder firstBuilder, lastBuilder;
firstBuilder = lastBuilder = new BasementBuilder();
lastBuilder = lastBuilder.setNext(new FloorBuilder())
.setNext(new RoofBuilder())
.setNext(new FecneBuilder());
HouseProject hp = new HouseProject(
HouseProject.Steps.CREATE_BASEMENT,
HouseProject.Steps.ADD_FLOOR,
HouseProject.Steps.CREATE_ROOF
);
firstBuilder.build(hp);
}
}
/**
* Output:
* Building basement
* Building floor
* Building roof
*/
Подписаться на:
Сообщения (Atom)