Design Partern設(shè)計(jì)模式
一.設(shè)計(jì)模式原則:a.開(kāi)閉原則(對(duì)擴(kuò)展開(kāi)放,修改關(guān)閉);
b.組合聚合復(fù)用原則(組合可選用功能)
c.接口隔離原則(防止接口過(guò)肥);
d.law of Remeter(抵米特法則,通過(guò)中間件對(duì)外訪問(wèn));
e.依賴倒轉(zhuǎn)原則(依賴接口);
f:the substitution of Lee(多態(tài),向上轉(zhuǎn)型以父類出現(xiàn));
二.主要模式:a.簡(jiǎn)單工廠模式:主要利用抽象類可以很好的實(shí)現(xiàn)開(kāi)閉原則.種類改變情況:

類: 1.public abstract class FruitFactory { public abstract Fruit creatFruit(); }
2.public class AppleFactory extends FruitFactory {
public Fruit creatFruit() { return new Apple(); } } //李式替代原則,向上轉(zhuǎn)型
3. public abstract class Fruit { public void display() { } }
4. public class Apple extends Fruit { public void display() {
System.out.println("this is apple"); } }
5.對(duì)外訪問(wèn): public class Client {
public static void main(String args[]) { FruitFactory ff=new AppleFactory();
Fruit apple=ff.creatFruit(); apple.display(); } } //向下轉(zhuǎn)型
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.測(cè)試: public class Client { public static void main(String args[]) {
Factory fc=new GlassFactory(); Chair glasschair=fc.creatChair();
glasschair.display(); } } //向下轉(zhuǎn)型,以父類的名義調(diào)用所有方法
成員變量為static的時(shí)候立即開(kāi)辟空間,但static方法在調(diào)用時(shí)才開(kāi)辟空間,稱緩式初始化
c.單例模式:只產(chǎn)生一個(gè)實(shí)例連接:特點(diǎn):a.實(shí)例保存在一個(gè)static成員變量里面
b.構(gòu)造方法是private的,在外類不可new;c.通過(guò)static方法返回單例實(shí)例.
區(qū)別:a.餓汗模式在成員變量聲明時(shí)即對(duì)單例初始化
b.懶漢式在獲取實(shí)例的static方法中才對(duì)單例初始化,并對(duì)方法加synchronized
類.a.public class Singleton {
private static Singleton instance=new Singleton();
private Singleton(){ } //只留一個(gè)getInstance接口
public static Singleton getInstance(){ return instance; } }
b. public class SingletonLazy { //方法是個(gè)動(dòng)作,可同時(shí)改變值,但static變量是空間
private static SingletonLazy instance; private SingletonLazy(){ }
public synchronized static SingletonLazy getInstance(){
if(instance==null) instance=new SingletonLazy(); return instance; }}
在外類中只可以通過(guò)如下調(diào)用: 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"); }}
二.對(duì)象適配器public class Adepter2 implements Target {
private Adeptee adeptee; //通過(guò)組合思想,用有參構(gòu)造方法
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 { //在不影響原有功能的基礎(chǔ)上進(jìn)行擴(kuò)展
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); } //子類一定要先對(duì)父類初始化
private void mp3Phone(){ System.out.println("this is mp3phone!"); }
public void call() { phone.call( ); mp3Phone(); }}
5.測(cè)試: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參數(shù)àDecoratoràPhoto.callàphone.call()àmp3.call)
備注:super只是用來(lái)初始化父類的構(gòu)造方法,多態(tài)的方式是通過(guò)new得到身份的,與其他無(wú)關(guān)
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!"); }//執(zhí)行
5.調(diào)用:public class Client { public static void main(String args[]){
Television tv=new Television();
ControlPanel cp=new ControlPanel(new TurnOn(tv)); cp.on(); } }