现在写一个用状态模式实现的根据上班时间状态变化而行为变化的小程序,当时间<12上午上班时间,
<13午休时间,<17下午上班时间,<21加班时间,根据时间不同,条用的类方法不同。
状态模式适用于当对象的状态改变时,行为也改变,就可以使用状态模式
状态接口:
public interface State {
void writeProgram(Work work);
}
不同的实现类:
public class AfterNoonState implements State {
@Override
public void writeProgram(Work work) {
if(work.getHour()<17){
System.out.println("工作");
}else {
work.setCurrent(new EveningState());
work.writeProgram();
}
}
}
public class EveningState implements State {
@Override
public void writeProgram(Work work) {
if(work.isFinish()){
work.setCurrent(new RestState());
work.writeProgram();
}else {
if(work.getHour()<21){
System.out.println("加班");
}else {
work.setCurrent(new SleepState());
work.writeProgram();
}
}
}
}
public class ForenoonState implements State {
@Override
public void writeProgram(Work work) {
if(work.getHour()<12){
System.out.println("工作时间");
}else {
work.setCurrent(new NoonState());
work.writeProgram();
}
}
}
public class NoonState implements State {
@Override
public void writeProgram(Work work) {
if(work.getHour()<13){
System.out.println("午睡");
}else {
work.setCurrent(new AfterNoonState());
work.writeProgram();
}
}
}
public class RestState implements State {
@Override
public void writeProgram(Work work) {
System.out.println("回家");
}
}
public class SleepState implements State {
@Override
public void writeProgram(Work work) {
System.out.println("睡觉");
}
}
调用状态的类:
public class Work {
private State current;
public Work(double hour,boolean finish){
current = new ForenoonState();
this.hour = hour;
this.finish = finish;
}
private double hour;
public double getHour() {
return hour;
}
public State getCurrent() {
return current;
}
public void setCurrent(State current) {
this.current = current;
}
private boolean finish;
public boolean isFinish() {
return finish;
}
public void writeProgram(){
current.writeProgram(this);
}
}
测试类:
public class Test {
public static void main(String[] args) {
Work work = new Work(20, true);
work.writeProgram();
}
}
这是表数据
SELECT * FROM t_user WHERE username IS NULL;查询到id=8的数据
SELECT * FROM t_user WHERE username IS NOT NULL;查询到id=11,12,13,14的四条数据
SELECT * FROM t_user WHERE username ='';查询到id=11,12,13的三条数据
SELECT * FROM t_user WHERE username ='aa';查询到id=14的数据
get和load方法都是是利用对象的主键得到对象,并可以使对象处于持久态。
load方法获取对象时不会立即执行查询操作,而是在第一次使用对象是再去执行查询操作。如果查询的对象在数据库中不存在,load方法返回值不会为null,在第一次使用时抛出org.hibernate.ObjectNotFoundException异常。
使用get方法获取对象时会立即执行查询操作,并且对象在数据库中不存在时返回null值。
所以我们在实际使用中多使用get方法,这样我们可以先判断得到的对象是否是null,再操作
1.在配置文件中的配置:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql:///struts" />
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
<bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<bean id="userSimpleJdbcTemplateDao" class="com.yjw.dao.UserSimpleJdbcTemplateDao">
<property name="simpleJdbcTemplate" ref="simpleJdbcTemplate"></property>
</bean>
2.dao中的写法:
package com.yjw.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import com.yjw.bean.User;
public class UserSimpleJdbcTemplateDao {
private SimpleJdbcTemplate simpleJdbcTemplate;
public void setSimpleJdbcTemplate(SimpleJdbcTemplate simpleJdbcTemplate) {
this.simpleJdbcTemplate = simpleJdbcTemplate;
}
public void save(User user){
String sql = "INSERT INTO t_user (username,PASSWORD) VALUES (?,?)";
simpleJdbcTemplate.update(sql, user.getUsername(),user.getPassword());
}
public void update(User user){
String sql = "UPDATE t_user SET username=:username ,PASSWORD=:password WHERE id=:id?";
simpleJdbcTemplate.update(sql, user.getUsername(),user.getPassword(),user.getId());
}
public void delete(int id){
String sql = "delete from t_user where id=?";
simpleJdbcTemplate.update(sql, id);
}
private class UserRowmapper implements RowMapper<User> {
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setUsername(rs.getString("username"));
u.setPassword(rs.getString("password"));
return u;
}
}
public User getUser(int id) {
String sql = "select id,username,password from t_user where id=?";
User user = simpleJdbcTemplate.queryForObject(sql, new UserRowmapper(), id);
return user;
}
public List<User> getList(){
String sql = "select id,username,password from t_user ";
List<User> list = simpleJdbcTemplate.query(sql, new UserRowmapper() );
return list;
}
}
四种通知的执行地方:前置通知
try{
业务代码
后置通知
} catch{
异常通知
} finally{
最终通知
}
1.需要的jar包:aspectjrt.jar,aspectjweaver.jar,cglib-nodep-2.1.3.jar
2.在配置文件中加入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<bean id="myXmlAspect" class="com.yjw.aspect.MyXmlAspect"/>
<!-- 配置事务切面-->
<aop:config>
<aop:aspect ref="myXmlAspect">
<aop:pointcut expression="execution(* com.yjw.dao..*.*(..))" id="pointcut"/>
<aop:before method="beforeAdvice" pointcut-ref="pointcut"/>
<aop:after-returning method="afterReturningAdvice" returning="value" pointcut-ref="pointcut"/>
<aop:after-throwing method="exceptionAdvice" pointcut-ref="pointcut" throwing="ex"/>
<aop:after method="afterAdvice" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
package com.yjw.aspect;
public class MyXmlAspect {
//前置通知
public void beforeAdvice(){
System.out.println("前置通知");
}
//异常通知,接收异常信息
public void exceptionAdvice(Exception ex){
System.out.println("异常出现"+ex.getMessage());
}
//后置通知,可以接收方法的返回值
public void afterReturningAdvice(Object value){
System.out.println("后置通知"+value);
}
//最终通知
public void afterAdvice(){
System.out.println("after");
}
}
查询集合
String sql = "select id, address,userid from t_address where userid in(select id from t_user where id in (select id from t_card where cardnum=112))";
SQLQuery query = session.createSQLQuery(sql).addEntity(Address.class);
List list = session.createSQLQuery(sql).list();
for(Object a : list){
Object[] address = (Object[]) a;
System.out.println(address[0]+" "+address[1]+" "+address[2]);
}
查询一个对象
String sql = "select id,username,password from t_user where id in (select id from t_card where cardnum=112)";
SQLQuery query = session.createSQLQuery(sql).addEntity(User.class);
User user = (User) query.uniqueResult();
System.out.println(user.getPwd());
单例就是在系统运行中只有一个实例对象
public class Factory {
private static Factory factory = new Factory();
private Factory(){
System.out.println("--");
}
public static Factory getFactory(){
return factory;
}
public void say(){
System.out.println("say");
}
}
第二种
public class Factory {
private static Factory factory =null;
private Factory(){
System.out.println("--");
}
public synchronized static Factory getFactory(){
if(factory==null){
factory = new Factory();
}
return factory;
}
public void say(){
System.out.println("say");
}
}
将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
例子,让羊的叫声和狼一样,羊就是一个适配器:
public class Wolf {
public void run() {
System.out.println("wolf run");
}
public void ho() {
System.out.println("wolf ho");
}
}
适配器接口:
public interface Sheep {
public void run();
public void ho();
}
实现类:
public class SheepAdapter implements Sheep{
private Wolf wolf;
public SheepAdapter(Wolf wolf) {
this.wolf = wolf;
}
@Override
public void run() {
wolf.run();
}
@Override
public void ho() {
wolf.ho();
}
}
测试类:
public static void main(String[] args) {
Wolf w = new Wolf();
Sheep sa = new SheepAdapter(w);
sa.run();
sa.ho();
}
}
动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。
例子:
被装饰的接口:
public interface Cake {
public float cost();
}
接口的实现类:
public class MilkCake implements Cake{
@Override
public float cost() {
return 100f;
}
}
装饰者抽象类:
public abstract class CakeDecorator implements Cake{
}
装饰者的实现类
public class Chocolate extends CakeDecorator{
private Cake cake;
public Chocolate(Cake cake) {
this.cake = cake;
}
@Override
public float cost() {
float chocolatePrice = 25f;
return cake.cost() + chocolatePrice;
}
}
public class Berry extends CakeDecorator {
private Cake cake;
public Berry(Cake cake) {
this.cake = cake;
}
@Override
public float cost() {
float berryPrice = 5f;
return cake.cost() + berryPrice;
}
}
测试类:
public class Test {
public static void main(String[] args) {
MilkCake mc = new MilkCake();//牛奶蛋糕
System.out.println(mc.cost());
Berry b = new Berry(mc);//牛奶草莓蛋糕
System.out.println(b.cost());
Chocolate c = new Chocolate(b);//牛奶草莓巧克力蛋糕
System.out.println("付款:" + c.cost());
}
}