The rule mode is a behavior mode, which embodies the basic principles of two fundamental designs for objects: 1. encapsulation; 2. Interface
1) Many related classes only have different behaviors. "Policy" provides a method to configure a class with one of multiple actions.
2) There are some classes in the system that only behave differently. You can consider using the Policy mode for restructuring;
3) when multiple Condition Selection statements exist in the system, you can consider using the Policy mode for restructuring.
Advantages:
1. Use combinations to dynamically add behaviors, but use less inheritance (inheritance increases coupling ).
2. It is intended for abstract (Interface) programming, rather than specific programming.
3. The policy mode encapsulates various things (algorithms) that may change, so that the changed parts and unchanged parts are separated, thus reducing code coupling.
Disadvantage: the client must know all the policy classes and decide which policy class to use. Therefore, the client must understand each algorithm.
DAO class:
Public userinfo findusername (string name)
{
Userinfo = NULL;
Try
{
String SQL = "select * From userinfo where uname =? ";
Userinfo = (userinfo) Super. Get (SQL, new object [] {name}, new mapper ()
{
@ Override
Public object mappingobject (resultset RS)
{
Try
{
If (Rs. Next ()){
Userinfo user = new userinfo ();
User. setuname (Rs. getstring ("uname "));
User. setupass (Rs. getstring ("upass "));
Return user;
}
} Catch (sqlexception ex)
{
// Todo auto-generated Catch Block
Ex. printstacktrace ();
Return NULL;
}
Return NULL;
}
});
Return userinfo;
} Catch (exception E)
{
E. printstacktrace ();
Return NULL;
}
}
Mapper interface:
Public interface mapper
{
Public object mappingobject (resultset RS );
}
Template Method: