设计模式:
A:创建型
B:结构性
C:行为型
创建型模式:
1,简单工厂模式
2,工厂方法模式
3,设计模式
---装饰模式
简单工厂模式:
也称静态工厂模式
---其实没啥,类似于多态,比较扯
工厂模式:
用抽象类实现,不影响代码
单例模式:
--饿汉式(安全,可靠):
public class Single {
private Single(){}//不让别人造对象
static Single sing = new Single();
public static Single getSingle(){
return sing;
}
}
--懒汉式(有线程安全问题):
加个锁搞定,不适合开发
public class Single {
private Single(){}//不让别人造对象
static Single sing = null;
public synchronized static Single getSingle(){
if(sing==null){
sing = new Single();
}
return sing;
}
}
Runtime --jdk 提供的一个单例类
装饰模式:
指定增强对象,由对象本身调用方法增强自己
代码简单演示:
public class Zhuangshi {
}
interface A{
int add(int x,int y);
}
class b implements A{
public int add(int x, int y) {
// TODO Auto-generated method stub
return x+y;
}
}
class c extends b{
private b b1;
//指定增强对象
public c(b b1) {
this.b1=b1;
}
@Override
public int add(int x, int y) {
return b1.add(x, y)+1;//在这儿还是由对象调用它本身的方法来增强
}
}