终于得空,本来想看看android的源码,但是想起以前看源码时总有点晕,不清楚为什么要这样设计代码结构,所以决定先把设计模式的相关知识学习一下。
买了n久的大话设计模式啊。。终于下定决心开始啃了。。。泪奔。。。
1.简单工厂模式
感觉简单工厂模式就是利用多态的特性,在实例化时,根据具体的子类,来实现不同的执行效果。
/**收费模式:正常,反现,折扣*/
enum Charge{NORMAL,RETURN,REBATE};
/**收费接口*/
interface CashSuper
{
public double acceptCash(double money);
}
class CashNormal implements CashSuper
{
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return money;
}
}
class CashRebate implements CashSuper
{
double rate;
CashRebate(double rate)
{
this.rate=rate;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return rate*money;
}
}
class CashReturn implements CashSuper
{
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
if(money>300)
{
return money-100;
}
return money;
}
}
class CashFactory
{
public static CashSuper getAcceptCash(Charge charge)
{
switch(charge)
{
case NORMAL:
return new CashNormal();
case RETURN:
return new CashReturn();
case REBATE:
return new CashRebate(0.8);
default:
return new CashNormal();
}
}
}
public class SimpleFactory {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CashSuper cashSuper;
cashSuper=CashFactory.getAcceptCash(Charge.NORMAL);
System.out.println("Normal(1000):"+cashSuper.acceptCash(1000));
cashSuper=CashFactory.getAcceptCash(Charge.REBATE);
System.out.println("Rebate(1000):"+cashSuper.acceptCash(1000));
cashSuper=CashFactory.getAcceptCash(Charge.RETURN);
System.out.println("Return(1000):"+cashSuper.acceptCash(1000));
}
}
子类实现接口,工厂类通过一个静态方法返回接口类型的对象。
2.策略模式
感觉就是在原来的工厂类的基础里,多组合了一个成员变量。
class CashContext
{
CashSuper mCashSuper;
public CashContext(CashSuper cashSuper)
{
mCashSuper=cashSuper;
}
public double acceptCash(double money)
{
return mCashSuper.acceptCash(money);
}
}
class CashContext2
{
CashSuper mCashSuper;
public CashContext2(Charge charge)
{
switch(charge)
{
case NORMAL:
mCashSuper=new CashNormal();
break;
case REBATE:
mCashSuper=new CashRebate(0.8);
break;
case RETURN:
mCashSuper=new CashReturn();
break;
default:
mCashSuper=new CashNormal();
}
}
public double acceptCash(double money)
{
return mCashSuper.acceptCash(money);
}
}
public class Strategy {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CashContext cashContext;
cashContext=new CashContext(new CashNormal());
System.out.println("Normal(1000):"+cashContext.acceptCash(1000));
cashContext=new CashContext(new CashRebate(0.8));
System.out.println("Rebate(1000):"+cashContext.acceptCash(1000));
cashContext=new CashContext(new CashReturn());
System.out.println("Return(1000):"+cashContext.acceptCash(1000));
CashContext2 cashContext2;
cashContext2=new CashContext2(Charge.NORMAL);
System.out.println("Normal(1000):"+cashContext2.acceptCash(1000));
cashContext2=new CashContext2(Charge.REBATE);
System.out.println("Rebate(1000):"+cashContext2.acceptCash(1000));
cashContext2=new CashContext2(Charge.RETURN);
System.out.println("Return(1000):"+cashContext2.acceptCash(1000));
}
}
还需要进一步学习。。。现在只知其实现,不知其为何这样实现。。。
Head first
看了Head first的策略模式,感觉策略模式的核心就是要针对接口编程,而非针对实现编程,同时要尽量用"has a"去组合,而非用"is a"去继承.
比如鸟具有fly的能力,但是有的鸟能用翅膀飞,有的鸟靠火箭飞(神火飞鸦),所以在设计鸟这个类时,组合一个fly的接口,在实例化鸟后,再通过set方法向鸟的fly的成员变量set具体是翅膀飞还是火箭飞。