这种模式比较啰嗦,不方便维护
package strategy;
public class ZoombieTest {
public static void main(String[] args) {
AbstractZombie normalZombie = new NormalZombie();
AbstractZombie flagZombie = new FlagZombie();
normalZombie.display();
normalZombie.move();
normalZombie.attack();
System.out.println("--------------");
flagZombie.display();
flagZombie.move();
flagZombie.attack();
}
}
abstract class AbstractZombie{
public abstract void display();
public void attack(){
System.out.println("咬.");
}
public void move(){
System.out.println("一步一步移动.");
}
}
class NormalZombie extends AbstractZombie{
@Override
public void display() {
System.out.println("我是普通僵尸.");
}
}
class FlagZombie extends AbstractZombie{
@Override
public void display() {
System.out.println("我是旗手僵尸.");
}
}
class BigHeadZombie extends AbstractZombie{
@Override
public void display() {
System.out.println("大头.");
}
@Override
public void attack() {
System.out.println("头撞");
}
}
class XxxZombie extends BigHeadZombie{
@Override
public void move() {
System.out.println("xxx移动行为");
}
}
package strategy;
public class StrategyTest {
public static void main(String[] args) {
Zombie zombie = new NormalZomble();
zombie.display();
zombie.attack();
zombie.move();
//下面两句 改变僵尸的攻击方式
zombie.setAttackable(new HitAttack());
zombie.attack();
}
}
interface Moveable{
void move();
}
interface Attackable{
void attack();
}
abstract class Zombie{
abstract public void display();
Moveable moveable;
Attackable attackable;
abstract void move();
abstract void attack();
public Zombie(Moveable moveable, Attackable attackable) {
this.moveable = moveable;
this.attackable = attackable;
}
public Moveable getMoveable() {
return moveable;
}
public void setMoveable(Moveable moveable) {
this.moveable = moveable;
}
public Attackable getAttackable() {
return attackable;
}
public void setAttackable(Attackable attackable) {
this.attackable = attackable;
}
}
class BiteAttack implements Attackable{
@Override
public void attack() {
System.out.println("咬.");
}
}
class HitAttack implements Attackable{
@Override
public void attack() {
System.out.println("打.");
}
}
class StepByStepMove implements Moveable{
@Override
public void move() {
System.out.println("一步一步移动.");
}
}
class FlogZombie extends Zombie{
public FlogZombie(){
super(new StepByStepMove(),new BiteAttack());
}
public FlogZombie(Moveable moveable, Attackable attackable) {
super(moveable, attackable);
}
@Override
public void display() {
System.out.println("我是旗手僵尸");
}
@Override
void move() {
moveable.move();
}
@Override
void attack() {
attackable.attack();
}
}
class NormalZomble extends Zombie{
public NormalZomble(){
super(new StepByStepMove(),new BiteAttack());
}
public NormalZomble(Moveable moveable, Attackable attackable) {
super(moveable, attackable);
}
@Override
public void display() {
System.out.println("我是普通僵尸");
}
@Override
void move() {
moveable.move();
}
@Override
void attack() {
attackable.attack();
}
}
实际运用
package strategy;
import java.util.Arrays;
import java.util.Comparator;
public class ComparatorTest {
public static void main(String[] args) {
Person[] persons = {new Person(10,99),new Person(18,99),new Person(15,122)};
//Arrays.sort(persons,new SortByAge()); //年纪进行排序
Arrays.sort(persons,new SortByHeight());//身高进行排序
print(persons);
}
static void print(Person[] array){
for (int i = 0; i <array.length ; i++) {
System.out.println(array[i]);
}
}
}
class Person{
int age;
int height;
public Person(int age, int height) {
this.age = age;
this.height = height;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", height=" + height +
'}';
}
}
class SortByAge implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
if(o1.getAge()>o2.getAge()){
return 1;
}else if(o1.getAge()<o2.getAge()){
return -1;
}
return 0;
}
}
class SortByHeight implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
if(o1.getHeight() > o2.height){
return 1;
}else if(o1.getHeight()<o2.getHeight()){
return -1;
}
return 0;
}
}