设计模式总结
2016-08-08 本文已影响8人
朱Simon
1、模式依赖基础与原则
1.1、基础
抽象,封装,多态,继承
1.2、原则
封装变化,多用组合、少用继承,针对接口编程,不针对实现编程
2、策略模式
策略模式定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
2.1、实例:鸭子的行为测试
#抽象类:duck
package com.zhukunshuai.duck.group;
import com.zhukunshuai.duck.flybehavior.FlyBehavior;
import com.zhukunshuai.duck.quackbehavior.QuackBehavior;
public abstract class Duck {
FlyBehavior flyBehavior;
QuackBehavior quackBehavior;
public Duck() {} //无参构造方法(默认添加,因此可不写)
public abstract void display(); //抽象方法必须在继承中被重写
##动态设定方法
public void setFlyBehavior(FlyBehavior fb){
flyBehavior=fb;
}
public void setQuackBehavior(QuackBehavior qb){
quackBehavior=qb;
}
public void performFly() {
flyBehavior.fly();
}
public void performQuack() {
quackBehavior.quack();
}
public void swim() {
System.out.println("All ducks float,even decoys");
}
}
抽象类设定方法接口:FlyBehavior、QuackBehavior 及其接口实现类
#设定:接口FlyBehavior
package com.zhukunshuai.duck.flybehavior;
public interface FlyBehavior {
public void fly();
}
#接口FlyBehavior 实现类FlyNoWay
package com.zhukunshuai.duck.flybehavior;
public class FlyNoWay implements FlyBehavior {
public void fly(){ //重写接口方法
System.out.println("I can't fly!!");
}
}
#接口FlyBehavior 实现类FlyWithWings
package com.zhukunshuai.duck.flybehavior;
public class FlyWithWings implements FlyBehavior {
public void fly(){
System.out.println("I'm flying!!");
}
}
#设定:接口QuackBehavior
package com.zhukunshuai.duck.quackbehavior;
public interface QuackBehavior {
public void quack();
}
#接口QuackBehavior 实现类Quack
package com.zhukunshuai.duck.quackbehavior;
public class Quack implements QuackBehavior {
public void quack() {
System.out.println("Quack");
}
}
#接口QuackBehavior 实现类Squeak
package com.zhukunshuai.duck.quackbehavior;
public class Squeak implements QuackBehavior {
public void quack() {
System.out.println("Squeak");
}
}
ModelDuck、MallardDuck继承duck类
package com.zhukunshuai.duck.group;
import com.zhukunshuai.duck.flybehavior.FlyNoWay;
import com.zhukunshuai.duck.quackbehavior.Quack;
public class ModelDuck extends Duck {
public ModelDuck(){
flyBehavior=new FlyNoWay();
quackBehavior=new Quack();
}
public void display(){ //重写抽象方法
System.out.println("I'm a model duck!!");
}
}
package com.zhukunshuai.duck.group;
import com.zhukunshuai.duck.flybehavior.FlyNoWay;
import com.zhukunshuai.duck.quackbehavior.MuteQuack;
public class MallardDuck extends Duck {
public MallardDuck() {
quackBehavior = new Squeak();
flyBehavior = new FlyWithWings();
}
public void display() {
System.out.println("I'm a real Mallard duck!!");
}
}
测试实例
package com.zhukunshuai.duck;
import com.zhukunshuai.duck.flybehavior.FlyNoWay;
import com.zhukunshuai.duck.group.Duck;
import com.zhukunshuai.duck.group.ModelDuck;
import com.zhukunshuai.duck.quackbehavior.Quack;
import com.zhukunshuai.duck.quackbehavior.QuackBehavior;
public class miniDuck {
public static void main(String[] args) {
/*Duck mallard = new MallardDuck();
mallard.performFly();
mallard.performQuack();
mallard.swim();*/
Duck model=new ModelDuck();
model.performFly(); //输出:I'm flying!!
model.performQuack(); //输出:Squeak
model.setFlyBehavior(new FlyNoWay());
QuackBehavior quack = new Quack();
model.setQuackBehavior(quack);
model.performFly(); //输出:I can't fly!!
model.performQuack(); //输出:Quack
}
}
输出结果:
I'm flying!!
Squeak
I can't fly!!
Quack
2.2、实例:chess中
package com.zhukunshuai.chess.role;
import com.zhukunshuai.chess.weapon.WeaponBehavior;
public abstract class Role {
WeaponBehavior weaponBehavior;
public Role() {}
public abstract void display();
public void setWeapon(WeaponBehavior weapon) {
weaponBehavior = weapon;
}
public void performweapon() {
weaponBehavior.useweapon();
}
public void getname() {
System.out.println("this is chess!!");
}
}
package com.zhukunshuai.chess.role;
import com.zhukunshuai.chess.weapon.BowAndArrowBehavior;
public class Queen extends Role{
public Queen(){
weaponBehavior=new BowAndArrowBehavior();
}
public void display(){
System.out.println("I am queen");
}
}
package com.zhukunshuai.chess.role;
import com.zhukunshuai.chess.weapon.AxeBehavior;
public class King extends Role{
public King(){
weaponBehavior=new AxeBehavior();
}
public void display(){
System.out.println("I am King");
}
}
package com.zhukunshuai.chess.weapon;
public interface WeaponBehavior {
public void useweapon();
}
package com.zhukunshuai.chess.weapon;
public class AxeBehavior implements WeaponBehavior {
public void useweapon(){
System.out.println("使用斧子");
}
}
package com.zhukunshuai.chess.weapon;
public class BowAndArrowBehavior implements WeaponBehavior {
public void useweapon(){
System.out.println("使用弓箭");
}
}
测试实例
package com.zhukunshuai.chess;
import com.zhukunshuai.chess.role.King;
import com.zhukunshuai.chess.role.Role;
import com.zhukunshuai.chess.weapon.SwordBehavior;
public class strange {
public static void main(String[] args) {
Role member=new King();
member.display();
member.getname();
member.performweapon();
member.setWeapon(new BowAndArrowBehavior());
member.performweapon();
}
}
输出结果:
I am King
this is chess!!
使用斧子
使用弓箭