Policy pattern: Defines the algorithm family, encapsulates each other, so that they can be replaced each other, this pattern makes the change of the algorithm independent of the client who uses the algorithm.
The subclass family of the parent class needs to extend the new function frequently, in order to use the parent class to add the subclass more flexibly, and to write the behavior of the parent class as an interface (interface);
Use the Set () method to reset the behavior of the interface. Methods that provide invocation of the behavior (such as perform) need to invoke the intrinsic method of the Interface (interface) (Fly & Quack).
Code:
Package strategy;
Public abstract class Duck {//abstract class
Flybehavior Flybehavior;
Quackbehavior Quackbehavior;
Public Duck () {//constructor
} public
abstract void display ();//abstract function public
void Performfly () {
Flybehavior.fly ();
}
public void Performquack () {
quackbehavior.quack ();
}
public void Swim () {
System.out.println ("All ducks float, even decoys!");
}
public void Setflybehavior (Flybehavior fb) {
flybehavior = fb;
}
public void Setquackbehavior (Quackbehavior qb) {
quackbehavior = QB;
}
}
Flybehavior and Quackbehavior are two interfaces (interface), containing methods fly () and quack ();
Methods of inheriting interfaces must use the methods of Fly () and quack ();
Code:
/**
* @time 2014.5.22
/package strategy;
/**
* @author C.l.wang
*
*
/public interface Flybehavior {public
void Fly ();
}
/**
* @time 2014.5.22
/package strategy;
/**
* @author C.l.wang
*
*
/public interface Quackbehavior {public
void quack ();
}
The implementation of the interface.
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/
Code:
/**
* @time 2014.5.22
/package strategy;
/**
* @author C.l.wang
*
*
/public class Flynoway implements Flybehavior {* *
(Non-javadoc) c32/>* @see Strategy. Flybehavior#fly ()
*
/@Override public
void Fly () {
//TODO auto-generated stub
System.out.println ("I can ' t fly!");
}
/**
* * * * * *
package strategy;
/**
* @author Administrator
* * *
/Public
class Flyrocketpowerd implements Flybehavior {
/* ( Non-javadoc)
* @see strategy. Flybehavior#fly ()
*
/@Override public
void Fly () {
//TODO auto-generated stub
System.out.println ("I ' m flying with a rocket!");
}
/**
* * * * * *
package strategy;
/**
* @author Administrator
* * *
/Public
class Flyrocketpowerd implements Flybehavior {
/* ( Non-javadoc)
* @see strategy. Flybehavior#fly ()
*
/@Override public
void Fly () {
//TODO auto-generated stub
System.out.println ("I ' m flying with a rocket!");
}
/**
* * * * * *
package strategy;
/**
* @author Administrator
*
/public class Mutequack implements Quackbehavior {
/* (Non-javadoc )
* @see strategy. Quackbehavior#quack ()
*
/@Override public
void Quack () {
//TODO auto-generated stub
System.out.println ("<< silence >>");
}
/**
* * * * * *
package strategy;
/**
* @author Administrator
* * *
/Public
class Quack implements Quackbehavior {
/* ( Non-javadoc)
* @see strategy. Quackbehavior#quack ()
*
/@Override public
void Quack () {
//TODO auto-generated stub
System.out.println ("quack!");
}
/**
* * * * * *
package strategy;
/**
* @author Administrator
* * *
/Public
class Squeak implements Quackbehavior {* *
(Non-javadoc) c10/>* @see Strategy. Quackbehavior#quack ()
*
/@Override public
void Quack () {
//TODO auto-generated stub
System.out.println ("Squeak");
}
Inherits the subclass of the parent class by specifying the type of the behavior used (that is, the implementation of the interface (interface)) and providing its own display () function;
Code:
/**
* * * * * *
package strategy;
/**
* @author Administrator
*
/public class Mallardduck extends Duck {public
mallardduck () {
Quackbehavior = new Quack ();
Flybehavior = new Flywithwings ();
}
/* (Non-javadoc)
* @see strategy. Duck#display ()
*
/@Override public
void display () {
//TODO auto-generated method stub
System.out.println ("I ' m a real mallard duck!");
}
/**
* * * * * *
package strategy;
/**
* @author Administrator
* * *
/Public
class Modelduck extends Duck {public
Modelduck () {
Flybehavior = new Flynoway ();
Quackbehavior = new Quack ();
}
/* (Non-javadoc)
* @see strategy. Duck#display ()
*
/@Override public
void display () {
//TODO auto-generated method stub
System.out.println ("I ' m a model duck.");
}
At execution time, you can change the behavior of the parent class by simply executing the method (perform) of each subclass, or using the set () method.
Code:
/**
* * * * * *
package strategy;
/**
* @author Administrator
*/public
class Miniducksimulator {
/**
* @param args
*/< C12/>public static void Main (string[] args) {
//TODO auto-generated method stub
Duck = new Mallard ( );
Mallard.performquack ();
Mallard.performfly ();
Duck model = new Modelduck ();
Model.performfly ();
Model.performquack ();
Model.setflybehavior (New Flyrocketpowerd ());
Model.performfly ();
}
Object-oriented principles:
1. Package change parts;
2. Multi-use combination, less inheritance;
3. For interface programming, not for implementation programming;