public interface Human {
public void jump();
public void say(String text);
}
public abstract class AbstractHuman implements Human {
protected String name;
public AbstractHuman(String name) {
this.name = name;
}
@Override
public void say(String text) {
System.out.println(name + ": " + text);
}
}
public class Man extends AbstractHuman {
public Man(String name) {
super(name);
}
@Override
public void jump() {
System.out.println("Man jump");
}
}
public class Woman extends AbstractHuman {
public Woman(String name) {
super(name);
}
@Override
public void jump() {
System.out.println("Woman jump");
}
}
public interface HumanFactory {
public static Human createHuman(String name, boolean kadyk) {
if (kadyk) {
return new Man(name);
}
return new Woman(name);
}
}
import java.util.LinkedList;
import java.util.List;
public class Main {
static class DBData {
String name;
boolean kadyk;
public DBData(String name, boolean kadyk) {
this.name = name;
this.kadyk = kadyk;
}
}
public static void main(String[] args) {
List<DBData> source = new LinkedList<>();
source.add(new DBData("Ваня", true));
source.add(new DBData("Маша", false));
source.add(new DBData("Катя", true));
source.add(new DBData("Сергей", true));
List<Human> humanList = new LinkedList<>();
for(DBData data : source) {
humanList.add(HumanFactory.createHuman(data.name, data.kadyk));
}
for(Human human : humanList) {
human.say("Hello");
human.jump();
}
}
}
public static interface Action {
void apply(int power);
String getActionName();
}
public static class JumpAction implements Action {
private int gravity;
public JumpAction(int gravity) {
this.gravity = gravity;
}
@Override
public void apply(int power) {
if (power > gravity) {
System.out.println("прыгает на " + (power - gravity) + " метров...");
} else {
System.out.println("немогу подпрыгнуть...");
}
}
@Override
public String getActionName() {
return "прыжок";
}
}
public static interface Human {
void doAction(Action action);
}
public static class Man implements Human {
public String name;
private int power = 10;
public Man(String name) {
this.name = name;
}
@Override
public void doAction(Action action) {
System.out.println(name + " делает " + action.getActionName());
action.apply(power);
}
}
public static class Woman implements Human {
public String name;
private int power = 7;
public Woman(String name) {
this.name = name;
}
@Override
public void doAction(Action action) {
System.out.println(name + " вертя задницей делает " + action.getActionName());
action.apply(power);
}
}
public static void main(String[] args) {
JumpAction jump = new JumpAction(8);
Man man = new Man("Анатолий");
man.doAction(jump);
Woman woman = new Woman("Наталья");
woman.doAction(jump);
}
Анатолий делает прыжок
прыгает на 2 метров...
Наталья вертя задницей делает прыжок
немогу подпрыгнуть...