Rule mode and template method in Java

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.