Design Partern设计模式
一.设计模式原则:a.开闭原则(对扩展开放,修改关闭);
b.组合聚合复用原则(组合可选用功能)
c.接口隔离原则(防止接口过肥);
d.law of Remeter(抵米特法则,通过中间件对外访问);
e.依赖倒转原则(依赖接口);
f:the substitution of Lee(多态,向上转型以父类出现);
二.主要模式:a.简单工厂模式:主要利用抽象类可以很好的实现开闭原则.种类改变情况:
类: 1.public abstract class FruitFactory { public abstract Fruit creatFruit(); }
2.public class AppleFactory extends FruitFactory {
public Fruit creatFruit() { return new Apple(); } } //李式替代原则,向上转型
3. public abstract class Fruit { public void display() { } }
4. public class Apple extends Fruit { public void display() {
System.out.println("this is apple"); } }
5.对外访问: public class Client {
public static void main(String args[]) { FruitFactory ff=new AppleFactory();
Fruit apple=ff.creatFruit(); apple.display(); } } //向下转型
b.工厂模式2(抽象工厂,种类不变,但分组改变的情况):
类: 1.public abstract class Factory {
public abstract Desk creatDesk(); public abstract Chair creatChair(); }
2. public class GlassFactory extends Factory {
public Desk creatDesk() { return new GlassDesk(); }
public Chair creatChair() { return new GlassChair(); } }
3. public abstract class Chair { public abstract void display(); }
4. public class GlassChair extends Chair {
public void display() { System.out.println("this is glassChair!"); }}
5.测试: public class Client { public static void main(String args[]) {
Factory fc=new GlassFactory(); Chair glasschair=fc.creatChair();
glasschair.display(); } } //向下转型,以父类的名义调用所有方法
成员变量为static的时候立即开辟空间,但static方法在调用时才开辟空间,称缓式初始化
c.单例模式:只产生一个实例连接:特点:a.实例保存在一个static成员变量里面
b.构造方法是private的,在外类不可new;c.通过static方法返回单例实例.
区别:a.饿汗模式在成员变量声明时即对单例初始化
b.懒汉式在获取实例的static方法中才对单例初始化,并对方法加synchronized
类.a.public class Singleton {
private static Singleton instance=new Singleton();
private Singleton(){ } //只留一个getInstance接口
public static Singleton getInstance(){ return instance; } }
b. public class SingletonLazy { //方法是个动作,可同时改变值,但static变量是空间
private static SingletonLazy instance; private SingletonLazy(){ }
public synchronized static SingletonLazy getInstance(){
if(instance==null) instance=new SingletonLazy(); return instance; }}
在外类中只可以通过如下调用: Singleton s=Singleton.getInstance();
d.适配器模式:
一.类适配器:1. public interface Target {
public void operator1(); public void operator2(); }
2.public class Adeptee {
public void operator1(){ System.out.println("this is Adeptee function");} }
3. public class Adepter extends Adeptee implements Target {//将所有方法放在适配器中
public void operator2() { System.out.println("this is new function"); }}
二.对象适配器public class Adepter2 implements Target {
private Adeptee adeptee; //通过组合思想,用有参构造方法
public Adepter2(Adeptee adeptee){ this.adeptee=adeptee; }
public void operator1(){ adeptee.operator1(); }
public void operator2(){ System.out.println("tagert function!");}}
e.装饰模式
类:1。public abstract class Phone { public abstract void call(); }
2.public class NomalPhone extends Phone { //在不影响原有功能的基础上进行扩展
public void call() { System.out.println("nomal phone!"); }}
3.public abstract class PhoneDecorator extends Phone { protected Phone phone;
public PhoneDecorator(Phone phone){ this.phone=phone; }
public void call() { } }
4. public class Mp3Phone extends PhoneDecorator {
public Mp3Phone(Phone phone) { super(phone); } //子类一定要先对父类初始化
private void mp3Phone(){ System.out.println("this is mp3phone!"); }
public void call() { phone.call( ); mp3Phone(); }}
5.测试:public class Client { public static void main(String args[]){
Phone mp3phone=new Mp3Phone(new NomalPhone()); mp3phone.call();
Phone allphone=new Mp3Phone(new PhotoPhone(new NomalPhone()));
allphone.call();}} (Photo.phone参数àDecoratoràPhoto.callàphone.call()àmp3.call)
备注:super只是用来初始化父类的构造方法,多态的方式是通过new得到身份的,与其他无关
f.命令模式:
类:1.public class ControlPanel { //命令传递者
private Command onComm;
public ControlPanel(Command onComm) { this.onComm=onComm; }
public void on() { onComm.handle(); }}
2. public abstract class Command { protected Television tv; //命令
public Command(Television tv){ this.tv=tv; }
public abstract void handle( ); }
3. public class TurnOn extends Command {
public TurnOn(Television tv) { super(tv); }
public void handle() { tv.open(); }}
4. public class Television{ public void open(){System.out.println("open!"); }//执行
5.调用:public class Client { public static void main(String args[]){
Television tv=new Television();
ControlPanel cp=new ControlPanel(new TurnOn(tv)); cp.on(); } }