Java的23种设计模式(疯狂Java总结)--个人修改增强版

huanhao

贡献于2015-10-11

字数:0 关键词: Java开发 Java

⠜ϾҎׂᬍ๲ᔎ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 1 Ⳃᔩ 1. 䆒䅵῵ᓣ ........................................................................................................................................ 2 1.1 ߯ᓎൟ῵ᓣ .......................................................................................................................... 3 1.1.1 Ꮉॖᮍ⊩˄FactoryMethod˅ .................................................................................. 3 1.1.2 ᢑ䈵Ꮉॖ˄AbstractFactory˅ .................................................................................. 6 1.1.3 ᓎ䗴㗙῵ᓣ˄Builder˅ .......................................................................................... 10 1.1.4 ऩᗕ῵ᓣ˄Singleton˅ .......................................................................................... 14 1.1.5 ॳൟ῵ᓣ˄Prototype˅ ......................................................................................... 15 1.2 㒧ᵘൟ῵ᓣ ........................................................................................................................ 18 1.2.1 䗖䜡఼῵ᓣ˄Adapter˅ ........................................................................................ 18 1.2.2 ḹ᥹῵ᓣ˄Bridge˅ ............................................................................................... 20 1.2.3 㒘ড়῵ᓣ˄Composite˅ ........................................................................................ 24 1.2.4 㺙佄῵ᓣ˄Decorator˅ ......................................................................................... 28 1.2.5 ໪㾖῵ᓣ˄Facade˅ .............................................................................................. 30 1.2.6 ѿܗ῵ᓣ˄Flyweight˅ ....................................................................................... 33 1.2.7 ҷ⧚῵ᓣ˄Proxy˅ .............................................................................................. 36 1.3 㸠Ўൟ῵ᓣ ........................................................................................................................ 40 1.3.1 䋷ӏ䫒῵ᓣ˄Chain of Responsibility˅ ................................................................. 40 1.3.2 ੑҸ῵ᓣ˄Command˅......................................................................................... 44 1.3.3 㾷䞞఼῵ᓣ˄Interpreter˅ ................................................................................... 48 1.3.4 䗁ҷ఼῵ᓣ˄Iterator˅ ......................................................................................... 51 1.3.5 Ёҟ㗙῵ᓣ˄Mediator˅ ...................................................................................... 55 1.3.6 ໛ᖬᔩ῵ᓣ˄Memento˅ ..................................................................................... 58 1.3.7 㾖ᆳ㗙῵ᓣ˄Observer˅ ...................................................................................... 61 1.3.8 ⢊ᗕ῵ᓣ˄State˅ ................................................................................................. 66 1.3.9 ㄪ⬹῵ᓣ˄Strategy˅ ............................................................................................ 68 1.3.10 ῵ᵓᮍ⊩˄TemplateMethod˅ ........................................................................... 71 1.3.11 䆓䯂㗙῵ᓣ˄Visitor˅......................................................................................... 73 䆌੠哧ࢅӏԩҎ䖯㸠ܼ᭛䕀䕑ঞ᥼ᑓˈ៥Ӏᬒᓗ䰸㕆ৡᴗ໪ⱘϔߛᴗ߽DŽܕˈϟ ᴀ⠜ᴗ义 ˈᑊϨϡ䖱ড Apache 2.0 ण䆂ⱘࠡᦤ ࣙᣀ ḷ䙉ᕾ Apache 2.0 ण䆂DŽ೼ᅠᭈֱ⬭ܼ䚼᭛ᴀ ៥Ӏ೼ℸ䚥䞡ᅷᏗˈᴀ 䕃ӊᓔথџϮ԰ߎ㓉㭘䋵⤂DŽ ᳟টⱘᬃᣕˈ໻ᆊ䛑ᢅⴔϔϾⳂⱘ˖Ў೑ݙ䕃ӊ ᴀ᭛ḷ㓪ݭǃࠊ԰䖛⿟Ёᕫࠄњ⮃⢖Java㘨ⲳǃҹঞヨ㗙ᄺдᎹ԰䖛⿟໻䞣 ໄᯢ ᴀҎ䚂ㆅ˖yangenxiong@163.com ᕫDŽ ໻ᆊߚѿ JavaǃJava EE ᡔᴃǃ ヨ㗙⦄Ў⮃⢖ Java 㘨ⲳⱘᘏ⠜Џ˄䆎യ ID˖ᴼᘽ䲘˅ˈгᏠᳯ䗮䖛䆹ᑇৄϢ 䖯㸠ᡔᴃѸ⌕DŽ ᮍ䴶 㾕㾷DŽ⃶䖢໻ᆊϢヨ㗙ህ JavaǃJava EE Ⳍ JavaǃJava *E 乍Ⳃ᳝ϔᅮ䅸䆚 থ䖛໻䞣 JavaǃJava EE 乍Ⳃˈᇍ ⑤ᡔᴃ䛑䴲ᐌᛳ݈䍷ˈ᳒খϢᓔথǃЏᣕ ヨ㗙᳒ᏜҢᴢ߮㗕Ꮬᄺд Javaˈ⦄ሙᑓᎲDŽᇍ Java 䕃ӊᓔথǃ৘⾡ Java ᓔ ヨ㗙ㅔҟ ⮃⢖ Java 㘨ⲳ㔥ഔ˖http://www.crazyit.org ᠔᳝ⱘᡔᴃথᏪˈഛ᳝ಲ໡DŽ ⃶䖢ⱏᔩ⮃⢖ Java 㘨ⲳ䖯㸠ᡔᴃѸ⌕ˈ⮃⢖ Java 㘨ⲳⱘ䆎യᅫᮼᰃ˖ ᔧⱘഄᮍˈ䖬ᳯ৘ԡ䇏㗙ᣛߎϡᙄᔧⱘഄᮍDŽ ᳝ϡᙄ ᓣⱘ⧚㾷䛑ϡሑϔ㟈ˈ಴ℸˈৃ㛑ᴀ᭛ḷⱘ՟ᄤ ⬅Ѣ↣ϾҎᇍ䆒䅵 ফ䆒䅵ⱘФ䍷DŽ ᠔䰘ⱘ՟ᄤ䴲ᐌㅔऩˈ᜶᜶ⱘᓩᇐ䇏㗙Ң⌙ࠄ⏅њ㾷䆒䅵῵ᓣˈᑊ㛑ҢЁѿ Ѡकϝ⾡䆒䅵῵ᓣˈᑊ㛑ᮍ֓ⱘᶹ䯙৘⾡䆒䅵῵ᓣⱘ⫼⊩ঞ⊼ᛣ⚍DŽ ˈⳂⱘᰃЎњ䅽䇏㗙њ㾷 䗖⫼ᗻˈ῵ᓣⱘ㒘៤䚼ߚˈᑊ䰘ᏺ᳝ㅔऩⱘ՟ᄤ੠㉏ ᴀӑᐂࡽ᭛ḷЏ㽕ᰃЎњ৥䇏㗙ҟ㒡Ѡकϝ⾡䆒䅵῵ᓣˈࣙᣀ῵ᓣⱘᦣ䗄ˈ ࡽDŽ 䚼ߚˈ㓐ড়∛ᘏ៤䖭ӑ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ˈᏠᳯᇍ໻ᆊ᳝᠔ᐂ ݭѠकϝ⾡䆒䅵῵ᓣⱘㅔऩ՟ᄤǃᑊᭈ⧚Ѡकϝ⾡䆒䅵῵ᓣⱘ⧚䆎 ᕫˈᬙヨ㗙 ᳝ᛳѢ䆒䅵῵ᓣ೼᮹ᐌᓔথЁⱘ䞡㽕ᗻˈৠᯊヨ㗙г㞾㾝ᇍ䆒䅵῵ᓣᇣ᳝ᖗ ݙᆍㅔҟ 1. 䆒䅵῵ᓣ 2 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ ㉏೒ 䞡ᅮНᎹॖᮍ⊩ҹ䖨ಲϔϾ ConcreteProduct ᅲ՟DŽ 4.ConcreteCreator ৃҹ䇗⫼Ꮉॖᮍ⊩ҹ߯ᓎϔϾ Product ᇍ䈵DŽ ConcreteProduct ᇍ䈵DŽ Creator гৃҹᅮНϔϾᎹॖᮍ⊩ⱘ㔎ⳕᅲ⦄ˈᅗ䖨ಲϔϾ㔎ⳕⱘ ໄᯢᎹॖᮍ⊩ˈ䆹ᮍ⊩䖨ಲϔϾ Product ㉏ൟⱘᇍ䈵* 3.Creator ᅲ⦄ Product ᥹ষDŽ 2.ConcreteProduct ᅮНᎹॖᮍ⊩᠔߯ᓎⱘᇍ䈵ⱘ᥹ষDŽ 1.Product খϢ㗙 ϔϾᐂࡽᄤ㉏ᰃҷ⧚㗙䖭ϔֵᙃሔ䚼࣪ⱘᯊ׭DŽ 3.ᔧ㉏ᇚ߯ᓎᇍ䈵ⱘ㘠䋷ྨᠬ㒭໮Ͼᐂࡽᄤ㉏ЁⱘᶤϔϾˈᑊϨԴᏠᳯᇚા 2.ᔧϔϾ㉏Ꮰᳯ⬅ᅗⱘᄤ㉏ᴹᣛᅮᅗ᠔߯ᓎⱘᇍ䈵ⱘᯊ׭DŽ 1.ᔧϔϾ㉏ϡⶹ䘧ᅗ᠔ᖙ乏߯ᓎⱘᇍ䈵ⱘ㉏ⱘᯊ׭DŽ 䗖⫼ᗻ FactoryMethod ՓϔϾ㉏ⱘᅲ՟࣪ᓊ䖳ࠄ݊ᄤ㉏DŽ ᅮᅲ՟࣪ાϔϾ㉏DŽއ ᅮНϔϾ⫼Ѣ߯ᓎᇍ䈵ⱘ᥹ষˈ䅽ᄤ㉏ 1.1.1 Ꮉॖᮍ⊩˄FactoryMethod˅ Prototype ( ॳൟ῵ᓣ ) Builder ( ᓎ䗴㗙῵ᓣ ) Singleton ( ऩᗕ῵ᓣ ) AbstractFactory ( ᢑ䈵Ꮉॖ ) FactoryMethod ( Ꮉॖᮍ⊩ ) 1.1 ߯ᓎൟ῵ᓣ 3 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ { ;("! System.out.println("㗕Ꮬᅵᡍ԰Ϯ public void doWork() { public class TeacherWork implements Work { } } ԰Ϯ!");خ⫳ System.out.println("ᄺ public void doWork() { public class StudentWork implements Work { ConcreteProduct } void doWork(); public interface Work { product ՟ᄤ ljJava Ϣ῵ᓣNJЁⱘ೒˖ 4 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 5 } Creator public interface IWorkFactory { Work getWork(); } ConcreteCreator public class StudentWorkFactory implements IWorkFactory { public Work getWork() { return new StudentWork(); } } public class TeacherWorkFactory implements IWorkFactory { public Work getWork() { return new TeacherWork(); } } Test public class Test { public static void main(String[] args) { IWorkFactory studentWorkFactory = new StudentWorkFactory(); studentWorkFactory.getWork().doWork(); IWorkFactory teacherWorkFactory = new TeacherWorkFactory(); teacherWorkFactory.getWork().doWork(); } } ᴀҎᬍ䗴˖ public class Test { public static void main(String[] args) { IWorkFactory studentWorkFactory = new StudentWorkFactory(); Work studentWork = studentWorkFactory.getWork(); studentWork.doWork(); IWorkFactory teacherWorkFactory = new TeacherWorkFactory(); Work teatherWork = teacherWorkFactory.getWork(); teatherWork.doWork(); } } 䈵DŽ ᅮНϔϾᇚ㹿Ⳍᑨⱘ݋ԧᎹॖ߯ᓎⱘѻક 4.ConcreteProduct Ўϔ㉏ѻકᇍ䈵ໄᯢϔϾ᥹ষDŽ 3.AbstractProduct ᅲ⦄߯ᓎ݋ԧѻકᇍ䈵ⱘ᪡԰DŽ 2.ConcreteFactory ໄᯢϔϾ߯ᓎᢑ䈵ѻકᇍ䈵ⱘ᪡԰᥹ষDŽ 1.AbstractFactory খϢ㗙 4.ᔧԴᦤկϔϾѻક㉏ᑧˈ㗠াᛇᰒ⼎ᅗӀⱘ᥹ষ㗠ϡᰃᅲ⦄ᯊDŽ 3.ᔧԴ㽕ᔎ䇗ϔ㋏߫Ⳍ݇ⱘѻકᇍ䈵ⱘ䆒䅵ҹ֓䖯㸠㘨ড়Փ⫼ᯊDŽ 2.ϔϾ㋏㒳㽕⬅໮Ͼѻક㋏߫ЁⱘϔϾᴹ䜡㕂ᯊDŽ 1.ϔϾ㋏㒳㽕⣀ゟѢᅗⱘѻકⱘ߯ᓎǃ㒘ড়੠㸼⼎ᯊDŽ 䗖⫼ᗻ ㉏DŽ ᦤկϔϾ߯ᓎϔ㋏߫Ⳍ݇៪ⳌѦձ䌪ᇍ䈵ⱘ᥹ষˈ㗠᮴䳔ᣛᅮᅗӀ݋ԧⱘ 1.1.2 ᢑ䈵Ꮉॖ˄AbstractFactory˅ ষˈ㗠ϡ᥹㾺ાϔϾѻક㉏ᑨᔧ㹿ᅲ՟࣪䖭⾡㒚㡖DŽ ៤ЎϔϾᢑ䈵Ꮉॖ㾦㡆ˈҙ䋳䋷㒭ߎ݋ԧᎹॖ㉏ᖙ乏ᅲ⦄ⱘ᥹ˈخ԰Ѹ㒭ᄤ㉏এ Ꮉॖᮍ⊩῵ᓣ˖ḌᖗᎹॖ㉏ϡݡ䋳䋷᠔᳝ѻકⱘ߯ᓎˈ㗠ᰃᇚ݋ԧ߯ᓎⱘᎹ 䇈Ā㽕ϔϾ∝෵āˈ݋ԧ㽕ҔМḋⱘ∝෵ਸ਼ˈ䅽MMⳈ᥹䎳᳡ࡵਬ䇈ህ㸠њDŽˈܓ 䆄ԣᰃϔӊ⚺Ҏⱘџᚙˈ៥ϔ㠀䞛⫼ Factory Method ῵ᓣˈᏺⴔ MM ࠄ᳡ࡵਬ䙷 FACTORY METHOD—䇋 MM এ呺ᔧࢇৗ∝෵ˈϡৠⱘ MM ᳝ϡৠⱘষੇˈ㽕↣Ͼ䛑 ᔶ䈵↨ஏ˖ 㗕Ꮬᅵᡍ԰Ϯ! ԰Ϯ!خ⫳ᄺ result 6 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 7 ᅲ⦄ abstractProduct ᥹ষDŽ 5.Client ҙՓ⫼⬅ AbstractFactory ੠ AbstractProduct ㉏ໄᯢⱘ᥹ষ ㉏೒ ljJava Ϣ῵ᓣNJЁⱘ೒˖ ՟ᄤ abstractFactory public interface IAnimalFactory { ICat createCat(); IDog createDog(); } Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 8 ConcreteFactory public class BlackAnimalFactory implements IAnimalFactory { public ICat createCat() { return new BlackCat(); } public IDog createDog() { return new BlackDog(); } } public class WhiteAnimalFactory implements IAnimalFactory { public ICat createCat() { return new WhiteCat(); } public IDog createDog() { return new WhiteDog(); } } AbstractProduct public interface ICat { void eat(); } public interface IDog { void eat(); } Concreteproduct public class BlackCat implements ICat { public void eat() { System.out.println("The black cat is eating!"); } } Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 9 public class WhiteCat implements ICat { public void eat() { System.out.println("The white cat is eating!*); } } public class BlackDog implements IDog { public void eat() { System.out.println("The black dog is eating"); } } public class WhiteDog implements IDog { public void eat() { System.out.println("The white dog is eating!"); } } Client public static void main(String[] args) { IAnimalFactory blackAnimalFactory = new BlackAnimalFactory(); ICat blackCat = blackAnimalFactory.createCat(); blackCat.eat(); IDog blackDog = blackAnimalFactory.createDog(); blackDog.eat(); IAnimalFactory whiteAnimalFactory = new WhiteAnimalFactory(); ICat whiteCat = whiteAnimalFactory.createCat(); whiteCat.eat(); IDog whiteDog = whiteAnimalFactory.createDog(); whiteDog.eat(); } result The black cat is eating! The black dog is eating! The white cat is eating! The white dog is eating! ㉏೒ ࣙ৿ᅮН㒘៤䚼ӊⱘ㉏ˈࣙᣀᇚ䖭ѯ䚼ӊ㺙䜡៤᳔㒜ѻકⱘ᥹ষDŽ ᅗⱘ㺙䜡䖛⿟DŽ 㸼⼎㹿ᵘ䗴ⱘ໡ᴖᇍ䈵DŽConcreteBuilder ߯ᓎ䆹ѻકⱘݙ䚼㸼⼎ᑊᅮН 4.Product ᵘ䗴ϔϾՓ⫼ Builder ᥹ষⱘᇍ䈵DŽ 3.Director ᦤկϔϾẔ㋶ѻકⱘ᥹ষDŽ ᅮНᑊᯢ⹂ᅗ᠔߯ᓎⱘ㸼⼎DŽ ᅲ⦄ Builder ⱘ᥹ষҹᵘ䗴੠㺙䜡䆹ѻકⱘ৘Ͼ䚼ӊDŽ 2.ConcreteBuilder Ў߯ᓎϔϾ Product ᇍ䈵ⱘ৘Ͼ䚼ӊᣛᅮᢑ䈵᥹ষDŽ 1.Builder খϢ㗙 䆌㹿ᵘ䗴ⱘᇍ䈵᳝ϡৠⱘ㸼⼎ᯊDŽܕ 2.ᔧᵘ䗴䖛⿟ᖙ乏 ᯊDŽ 1.ᔧ߯ᓎ໡ᴖᇍ䈵ⱘㅫ⊩ᑨ䆹⣀ゟѢ䆹ᇍ䈵ⱘ㒘៤䚼ߚҹঞᅗӀⱘ㺙䜡ᮍᓣ 䗖⫼ᗻ ⱘ㸼⼎DŽ ᇚϔϾ໡ᴖᇍ䈵ⱘᵘᓎϢᅗⱘ㸼⼎ߚ⾏ˈՓᕫৠḋⱘᵘᓎ䖛⿟ৃҹ߯ᓎϡৠ 1.1.3 ᓎ䗴㗙῵ᓣ˄Builder˅ ⳌᑨⱘׂᬍDŽབ˖བԩ߯ᓎঞབԩ৥ᅶ᠋ッᦤկDŽخг㽕 ॖ䇋∖ेৃDŽ⍜䌍㗙᮴乏ׂᬍህৃҹ᥹㒇ᮄѻકDŽ㔎⚍ᰃᔧѻકׂᬍᯊˈᎹॖ㉏ Ꮉॖ῵ᓣ˖ᅶ᠋㉏੠Ꮉॖ㉏ߚᓔDŽ⍜䌍㗙ӏԩᯊ׭䳔㽕ᶤ⾡ѻકˈা䳔৥Ꮉ ৥᳡ࡵਬ䇈ĀᴹಯϾ叵㖙āህ㸠њDŽ呺ᔧࢇ੠㚃ᖋ෎ህᰃ⫳ѻ叵㖙ⱘ Factory ᰃ MM ⠅ৗⱘϰ㽓ˈ㱑✊ষੇ᳝᠔ϡৠˈԚϡㅵԴᏺ MM এ呺ᔧࢇ៪㚃ᖋ෎ˈাㅵ ABSTRACT FACTORY—䗑 MM ᇥϡњ䇋ৗ佁њˈ呺ᔧࢇⱘ叵㖙੠㚃ᖋ෎ⱘ叵㖙䛑 ᔶ䈵↨ஏ˖ 10 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 11 ՟ᄤ Builder public interface PersonBuilder { void buildHead(); void buildBody(); void buildFoot(); Person buildPerson(); } ConcreteBuilder public class ManBuilder implements PersonBuilder { Person person; public ManBuilder() { person = new Man(); } public void buildbody() { person.setBody("ᓎ䗴⬋Ҏⱘ䑿ԧ"); Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 12 } public void buildFoot() { person.setFoot("ᓎ䗴⬋Ҏⱘ㛮"); } public void buildHead() { person.setHead("ᓎ䗴⬋Ҏⱘ༈"); } public Person buildPerson() { return person; } } Director public class PersonDirector { public Person constructPerson(PersonBuilder pb) { pb.buildHead(); pb.buildBody(); pb.buildFoot(); return pb.buildPerson(); } } Product public class Person { private String head; private String body; private String foot; public String getHead() { return head; } public void setHead(String head) { this.head = head; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getFoot() { 䖯㸠ⱘᓎ䗴䖛⿟DŽ ⱘব࣪ˈᅶ᠋ϡᖙⶹ䘧ѻકݙ䚼㒘៤ⱘ㒚㡖DŽᓎ䗴῵ᓣৃҹᔎࠊᅲ㸠ϔ⾡ߚℹ偸 䖛⿟⫳៤݋᳝ϡৠⱘݙ䚼㸼䈵ⱘѻકᇍ䈵DŽᓎ䗴῵ᓣՓᕫѻકݙ䚼㸼䈵ৃҹ⣀ゟ ᓎ䗴῵ᓣ˖ᇚᇍ䈵ⱘݙ䚼㸼䈵੠ᇍ䈵ⱘ⫳៤䖛⿟ߚࡆᓔᴹˈҢ㗠ՓϔϾᓎ䗴 ⱘ㗏䆥ᴎདप˅⫼ܟ㕢ݯ೼Ӟᢝ 䆱њˈ೑໪ⱘ MM гৃҹ䕏ᵒ᧲ᅮˈ䖭ህᰃ៥ⱘĀ៥⠅Դ ”builderDŽ˄䖭ϔᅮ↨ Ͼᣝ䬂ˈ㾕ࠄMM៥া㽕ᣝᇍᑨⱘ䬂ˈᅗህ㛑໳⫼Ⳍᑨⱘ䇁㿔䇈ߎĀ៥⠅Դā䖭হ ⫼ཌྷӀⱘᮍ㿔䎳ཌྷ䇈䖭হ䆱઺ˈ៥᳝ϔϾ໮⾡䇁㿔㗏䆥ᴎˈϞ䴶↣⾡䇁㿔䛑᳝ϔ BUILDER—MM ᳔⠅਀ⱘህᰃĀ៥⠅Դā䖭হ䆱њˈ㾕ࠄϡৠഄᮍⱘ MM,㽕㛑໳ ᔶ䈵↨ஏ˖ ᓎ䗴⬋Ҏⱘ༈ ᓎ䗴⬋Ҏⱘ㛮 ᓎ䗴⬋Ҏⱘ䑿ԧ result } } System.out.println(person.getHead()); System.out.println(person.getFoot()); System.out.println(person.getBody()); Person person = pd.constructPerson(new ManBuilder()); PersonDirector pd = new PersonDirector(); public static void main(String[] args) { public class Test{ Test } public class Man extends Person { } } this.foot = foot; public void setFoot(String foot) { } return foot; 13 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ } ( public static void main(String[] args public class Test { Test } } return sing; } sing = new Singleton(); if (sing == null) { public static Singleton getInstance() { } private Singleton() { private static Singleton sing; public class Singleton { Singleton ՟ᄤ ㉏೒ ߯ᓎᅗ㞾Ꮕⱘଃϔᅲ՟DŽ ৃ㛑䋳 ㉏᪡԰DŽ 䆌ᅶ᠋䆓䯂ᅗⱘଃϔᅲ՟DŽInstance ᰃϔϾܕˈ ᅮНϔϾ Instance ᪡԰ Singleton খϢ㗙 ህ㛑Փ⫼ϔϾᠽሩⱘᅲ՟ᯊDŽ 2.ᔧ䖭Ͼଃϔᅲ՟ᑨ䆹ᰃ䗮䖛ᄤ㉏࣪ৃᠽሩⱘˈᑊϨᅶ᠋ᑨ䆹᮴䳔᳈ᬍҷⷕ 1.ᔧ㉏া㛑᳝ϔϾᅲ՟㗠Ϩᅶ᠋ৃҹҢϔϾӫ᠔਼ⶹⱘ䆓䯂⚍䆓䯂ᅗᯊDŽ 䗖⫼ᗻ ֱ䆕ϔϾ㉏ҙ᳝ϔϾᅲ՟ˈᑊᦤկϔϾ䆓䯂ᅗⱘܼሔ䆓䯂⚍DŽ 1.1.4 ऩᗕ῵ᓣ˄Singleton˅ 14 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 䱚㞾䑿ⱘ᥹ষDŽܟ ໄᯢϔϾ 1. Prototype খϢ㗙 ᮍ֓ϔѯDŽ 䱚ᅗӀৃ㛑↨↣⃵⫼ড়䗖ⱘ⢊ᗕ᠟Ꮉᅲ՟࣪䆹㉏᳈ܟᓎゟⳌᑨ᭄Ⳃⱘॳൟᑊ 4.ᔧϔϾ㉏ⱘᅲ՟া㛑᳝޴Ͼϡৠ⢊ᗕ㒘ড়Ёⱘϔ⾡ᯊDŽ ߯ᓎϔϾϢѻક㉏ሖ⃵ᑇ㸠ⱘᎹॖ㉏ሖ⃵ᯊDŽܡ 3.Ўњ䙓 2.ᔧ㽕ᅲ՟࣪ⱘ㉏ᰃ೼䖤㸠ᯊࠏᣛᅮᯊˈ՟བˈ䗮䖛ࡼᗕ㺙䕑DŽ 1.ᔧϔϾ㋏㒳ᑨ䆹⣀ゟѢᅗⱘѻક߯ᓎǃᵘ៤੠㸼⼎ᯊDŽ 䗖⫼ᗻ ⫼ॳൟᅲ՟ᣛᅮ߯ᓎᇍ䈵ⱘ⾡㉏ˈᑊϨ䗮䖛ᣋ䋱䖭ѯॳൟ߯ᓎᮄⱘᇍ䈵DŽ 1.1.5 ॳൟ῵ᓣ˄Prototype˅ ৃՓ⫼DŽ ㋏㒳ᦤկ䖭Ͼᅲ՟ऩ՟῵ᓣDŽऩ՟῵ᓣাᑨ೼᳝ⳳℷⱘĀऩϔᅲ՟āⱘ䳔∖ᯊᠡ ऩ՟῵ᓣ˖ऩ՟῵ᓣ⹂ֱᶤϔϾ㉏া᳝ϔϾᅲ՟ˈ㗠Ϩ㞾㸠ᅲ՟࣪ᑊ৥ᭈϾ ϾṺ଺ˈા᳝䖭Мདⱘџ) њخ߮ᠡ ݀ SigletonˈཌྷӀা㽕䇈䘧Ā㗕݀āˈ䛑ᰃᣛⱘৠϔϾҎˈ䙷ህᰃ៥ SINGLETON—׎᳝  Ͼⓖ҂ⱘ㗕ယˈཌྷӀⱘ㗕݀䛑ᰃ៥ˈ៥ህᰃ៥Ӏᆊ䞠ⱘ㗕 ᔶ䈵↨ஏ˖ singleton.Singleton@1c78e57 singleton.Singleton@1c78e57 result } } System.out.println(sing2); System.out.println(sing); Singleton sing2 = Singleton.getInstance(); Singleton sing = Singleton.getInstance(); 15 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 16 2. ConcretePrototype ᅲ⦄ϔϾܟ䱚㞾䑿ⱘ᪡԰DŽ 3. Client 䅽ϔϾॳൟܟ䱚㞾䑿Ң㗠߯ᓎϔϾᮄⱘᇍ䈵DŽ ㉏೒ ՟ᄤ Prototype public class Prototype implements Cloneable { private String name; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public Object clone(){ try { return super.clone(); } catch (Exception e) { e.printStackTrace(); return null; } } } Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 17 ConcretePrototype public class ConcretePrototype extends Prototype { public ConcretePrototype(String name) { setName(name); } } Client public class Test { public static void main(String[] args) { Prototype pro = new ConcretePrototype("prototype"); Prototype pro2 = (Prototype)pro.clone(); System.out.println(pro.getName()); System.out.println(pro2.getName()); } } result prototype prototype ᔶ䈵↨ஏ˖ PROTOTYPE—䎳 MM ⫼ QQ 㘞໽ˈϔᅮ㽕䇈ѯ⏅ᚙⱘ䆱䇁њˈ៥᧰䲚њད໮㙝咏 ⱘᚙ䆱ˈ䳔㽕ᯊা㽕 copy ߎᴹᬒࠄ QQ 䞠䴶ህ㸠њˈ䖭ህᰃ៥ⱘᚙ䆱 prototype њDŽ˄100 ഫ䪅ϔӑˈԴ㽕ϡ㽕˅ ॳྟ῵ൟ῵ᓣ˖䗮䖛㒭ߎϔϾॳൟᇍ䈵ᴹᣛᯢ᠔㽕߯ᓎⱘᇍ䈵ⱘ㉏ൟˈ✊ৢ ⫼໡ࠊ䖭Ͼॳൟᇍ䈵ⱘᮍ⊩߯ᓎߎ᳈໮ৠ㉏ൟⱘᇍ䈵DŽॳྟ῵ൟ῵ᓣܕ䆌ࡼᗕⱘ ๲ࡴ៪ޣᇥѻક㉏ˈѻક㉏ϡ䳔㽕䴲ᕫ᳝ӏԩџܜ⹂ᅮⱘㄝ㑻㒧ᵘˈॳྟ῵ൟ῵ ᓣ䗖⫼Ѣӏԩⱘㄝ㑻㒧ᵘDŽ㔎⚍ᰃ↣ϔϾ㉏䛑ᖙ乏䜡໛ϔϾܟ䱚ᮍ⊩DŽ ㉏೒ ᇍ Adaptee ⱘ᥹ষϢ Target ᥹ষ䖯㸠䗖䜡 4.Adapter ᅮНϔϾᏆ㒣ᄬ೼ⱘ᥹ষˈ䖭Ͼ᥹ষ䳔㽕䗖䜡DŽ 3.Adaptee Ϣヺড় Target ᥹ষⱘᇍ䈵णৠDŽ 2.Client ᅮН Client Փ⫼ⱘϢ⡍ᅮ乚ඳⳌ݇ⱘ᥹ষDŽ 1.Target খϢ㗙 ϔϾ䛑䖯㸠ᄤ㉏࣪ҹऍ䜡ᅗӀⱘ᥹ষDŽᇍ䈵䗖䜡఼ৃҹ䗖䜡ᅗⱘ⠊㉏᥹ষDŽ 3.˄ҙ䗖⫼Ѣᇍ䈵 Adapter˅ԴᛇՓ⫼ϔѯᏆ㒣ᄬ೼ⱘᄤ㉏ˈԚᰃϡৃ㛑ᇍ↣ ˄े䙷ѯ᥹ষৃ㛑ϡϔᅮݐᆍⱘ㉏˅णৠᎹ԰DŽ 2.Դᛇ߯ᓎϔϾৃҹ໡⫼ⱘ㉏ˈ䆹㉏ৃҹϢ݊ҪϡⳌ݇ⱘ㉏៪ϡৃ乘㾕ⱘ㉏ 1.ԴᛇՓ⫼ϔϾᏆ㒣ᄬ೼ⱘ㉏ˈ㗠ᅗⱘ᥹ষϡヺড়Դⱘ䳔∖DŽ 䗖⫼ᗻ Ѣ᥹ষϡݐᆍ㗠ϡ㛑ϔ䍋Ꮉ԰ⱘ䙷ѯ㉏ৃҹϔ䍋Ꮉ԰DŽ ᇚϔϾ㉏ⱘ᥹ষ䕀ᤶ៤ᅶ᠋Ꮰᳯⱘ঺໪ϔϾ᥹ষDŽAdapter ῵ᓣՓᕫॳᴀ⬅ 1.2.1 䗖䜡఼῵ᓣ˄Adapter˅ Proxy ( ҷ⧚῵ᓣ ) ῵ᓣ )ܗFlyweight ( ѿ Facade ( ໪㾖῵ᓣ ) Decorator ( 㺙佄῵ᓣ ) Composite ( 㒘ড়῵ᓣ ) Bridge ( ḹ᥹῵ᓣ ) Adapter ( 䗖䜡఼῵ᓣ ) 1.2 㒧ᵘൟ῵ᓣ 18 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 19 ՟ᄤ Target public interface Target { void adapteeMethod(); void adapterMethod(); } Adaptee public class Adaptee { public void adapteeMethod() { System.out.println("Adaptee method!"); } } Adapter public class Adapter implements Target { private Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; } public void adapteeMethod() { adaptee.adapteeMethod(); } public void adapterMethod() { system.out.println("Adapter method!"); } } Client ㉏᥹ষЁᰃৃ㾕ⱘ 4.˄C++˅Դᛇᇍᅶ᠋ᅠܼ䱤㮣ᢑ䈵ⱘᅲ⦄䚼ߚDŽ೼ C++Ёˈ㉏ⱘ㸼⼎೼ ᮄ㓪䆥DŽ 3.ᇍϔϾᢑ䈵ⱘᅲ⦄䚼ߚⱘׂᬍᑨᇍᅶ᠋ϡѻ⫳ᕅડˈेᅶ᠋ⱘҷⷕϡᖙ䞡 DŽܙᇍᅗӀ䖯㸠ᠽ 䖭ᯊBridge῵ᓣՓԴৃҹᇍϡৠⱘᢑ䈵᥹ষ੠ᅲ⦄䚼ߚ䖯㸠㒘ড়ˈᑊߚ߿ DŽܙ 2.㉏ⱘᢑ䈵ҹঞᅗⱘᅲ⦄䛑ᑨ䆹ৃҹ䗮䖛⫳៤ᄤ㉏ⱘᮍ⊩ࡴҹᠽ ᤶDŽ 㛑ᰃ಴Ўˈ೼⿟ᑣ䖤㸠ᯊࠏᅲ⦄䚼ߚᑨৃҹ㹿䗝ᢽ៪㗙ߛৃމ ՟བ䖭⾡ᚙ 1.ԴϡᏠᳯ೼ᢑ䈵੠ᅗⱘᅲ⦄䚼ߚП䯈᳝ϔϾ೎ᅮⱘ㒥ᅮ݇㋏DŽ 䗖⫼ᗻ ᇚᢑ䈵䚼ߚϢᅗⱘᅲ⦄䚼ߚߚ⾏ˈՓᅗӀ䛑ৃҹ⣀ゟഄব࣪DŽ 1.2.2 ḹ᥹῵ᓣ˄Bridge˅ ҹḍ᥂খ᭄䖨䖬ϔϾড়䗖ⱘᅲ՟㒭ᅶ᠋ッDŽ Ң㗠Փॳᴀ಴᥹ষॳ಴ϡऍ䜡㗠᮴⊩ϔ䍋Ꮉ԰ⱘϸϾ㉏㛑໳ϔ䍋Ꮉ԰DŽ䗖䜡㉏ৃ 䗖䜡఼˄বय़఼˅῵ᓣ˖ᡞϔϾ㉏ⱘ᥹ষবᤶ៤ᅶ᠋ッ᠔ᳳᕙⱘ঺ϔ⾡᥹ষˈ гϡⶹ䘧ҪӮϡӮ㗡៥) Adapterˈ䅽៥੠ Sarah ৃҹⳌѦѸ䇜њ 䇁ˈཌྷϡӮ䇈᱂䗮䆱ˈাད∖ࡽѢ៥ⱘ᳟ট kent њˈҪ԰Ў៥੠ Sarah П䯈ⱘ ADAPTER—೼᳟ট㘮ӮϞ⺄ࠄњϔϾ㕢ཇ SarahˈҢ佭␃ᴹⱘˈৃ៥ϡӮ䇈㉸ ᔶ䈵↨ஏ˖ Adapter method! Adaptee method! result } } target.adapterMethod(); target.adapteeMethod(); Target target = new Adapter(new Adaptee()); public static void main(String[] args) { public class Test { 20 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Abstraction ՟ᄤ ㉏೒ ᅲ⦄ Implementor ᥹ষᑊᅮНᅗⱘ݋ԧᅲ⦄DŽ 4.ConcreteImplementor ෎Ѣ䖭ѯ෎ᴀ᪡԰ⱘ䕗催ሖ⃵ⱘ᪡԰DŽ ϔ㠀ᴹ䆆ˈImplementor ᥹ষҙᦤկ෎ᴀ᪡԰ˈ㗠 Abstraction ߭ᅮНњ џᅲϞ䖭ϸϾ᥹ষৃҹᅠܼϡৠDŽ ᅮНᅲ⦄㉏ⱘ᥹ষˈ䆹᥹ষϡϔᅮ㽕Ϣ Abstraction ⱘ᥹ষᅠܼϔ㟈DŽ 3.Implementor Abstraction ᅮНⱘ᥹ষDŽ ⬅ܙ ᠽ 2.RefinedAbstraction 㓈ᡸϔϾᣛ৥ Implementor ㉏ൟᇍ䈵ⱘᣛ䩜DŽ ᅮНᢑ䈵㉏ⱘ᥹ষDŽ 1.Abstraction খϢ㗙 䖭Ͼ㉏Ё໮Ͼᇍ䈵ৃҹ݅ѿৠϔϾᄫヺІ㸼⼎˄S t r i n g R e p ˅DŽ ⶹ䘧䖭ϔ⚍DŽϔϾㅔऩⱘ՟ᄤ֓ᰃ Coplien ⱘ String ㉏[ C o p 9 2 ]ˈ೼ 5.Դᛇ೼໮Ͼᇍ䈵䯈݅ѿᅲ⦄˄ৃ㛑Փ⫼ᓩ⫼䅵᭄˅ˈԚৠᯊ㽕∖ᅶ᠋ᑊϡ generalizations ˅DŽ ៤ϸϾ䚼ߚDŽRumbaugh ⿄䖭⾡㉏ሖ⃵㒧ᵘЎĀጠ༫ⱘ᱂࣪ā˄nested  ᳝䆌໮㉏㽕⫳៤DŽ䖭ḋϔ⾡㉏ሖ⃵㒧ᵘ䇈ᯢԴᖙ乏ᇚϔϾᇍ䈵ߚ㾷 21 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 22 public abstract class Person { private Clothing clothing; private String type; public Clothing getClothing() { return clothing; } public void setClothing() { this.clothing = clothingFactory.getClothing();//᳝䯂乬ʽ } public void setType(String type) { this.type = type; } public String getType() { return this.type; } public abstract void dress(); } RefinedAbstraction public class Man extends person { public Man() { setType("⬋Ҏ"); } public void dress() { Clothing clothing = getclothing(); clothing.personDressCloth(this); } } public class Lady extends Person { public Lady() { setType("ཇҎ"); } Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 23 public void dress() { Clothing clothing = getClothing(); clothing.personDressCloth(this); } } Implementor public abstract class Clothing { public abstract void personDressCloth(person person); } ConcreteImplementor public class jacket extends Clothing { public void personDressCloth(Person person) { System.out.println(person.getType() + "こ偀⬆"); } } public class Trouser extends Clothing { public void personDressCloth(Person person) { System.out.println(person.getType() + "こ㺸ᄤ"); } } Test public class Test { public static void main(String[] args) { Person man = new Man(); Person lady = new Lady(); Clothing jacket = new Jacket(); Clothing trouser = new Trouser(); jacket.personDressCloth(man); trouser.personDressCloth(man); jacket.personDressCloth(lady); trouser.personDressCloth(lady); } } result ⱘ᪡԰DŽ ೼ Component ᥹ষЁᅲ⦄Ϣᄤ䚼ӊ᳝ ᄤ䚼ӊDŽټ ᄬ ѯ䚼ӊⱘ㸠ЎDŽ ᅮН᳝ᄤ䚼ӊⱘ 3.Composite ೼㒘ড়ЁᅮН㡖⚍ᇍ䈵ⱘ㸠ЎDŽ ೼㒘ড়Ё㸼⼎৊㡖⚍ᇍ䈵ˈ৊㡖⚍≵᳝ᄤ㡖⚍DŽ 2.Leaf ϟᅲ⦄ᅗDŽމ ⱘᚙ (ৃ䗝 ೼䗦ᔦ㒧ᵘЁᅮНϔϾ᥹ষˈ⫼Ѣ䆓䯂ϔϾ⠊䚼ӊˈᑊ೼ড় ໄᯢϔϾ᥹ষ⫼Ѣ䆓䯂੠ㅵ⧚ Component ⱘᄤ㒘ӊDŽ ϟˈᅲ⦄᠔᳝㉏᳝݅᥹ষⱘ㔎ⳕ㸠ЎDŽމ ೼䗖ᔧⱘᚙ Ў㒘ড়Ёⱘᇍ䈵ໄᯢ᥹ষDŽ 1.Component খϢ㗙 Ёⱘ᠔᳝ᇍ䈵DŽ 2.ԴᏠᳯ⫼᠋ᗑ⬹㒘ড়ᇍ䈵ϢऩϾᇍ䈵ⱘϡৠˈ⫼᠋ᇚ㒳ϔഄՓ⫼㒘ড়㒧ᵘ 1.Դᛇ㸼⼎ᇍ䈵ⱘ䚼ߚᭈԧሖ⃵㒧ᵘDŽ 䗖⫼ᗻ ᇍऩϾᇍ䈵੠㒘ড়ᇍ䈵ⱘՓ⫼݋᳝ϔ㟈ᗻDŽ ᇚᇍ䈵㒘ড়៤ᷥᔶ㒧ᵘҹ㸼⼎䚼ߚᭈԧⱘሖ⃵㒧ᵘDŽComposite Փᕫ⫼᠋ 1.2.3 㒘ড়῵ᓣ˄Composite˅ Փ⫼㒘ড়㘮ড়݇㋏㗠ϡᰃ㒻ᡓ݇㋏ˈҢ㗠Փϸ㗙ৃҹ⣀ゟⱘব࣪DŽ ҪӀП䯈ⱘᔎ݇㘨ব៤ᔅ݇㘨ˈгህᰃᣛ೼ϔϾ䕃ӊ㋏㒳ⱘᢑ䈵࣪੠ᅲ⦄࣪П䯈 ḹṕ῵ᓣ˖ᇚᢑ䈵࣪Ϣᅲ⦄࣪㜅㗺ˈՓᕫѠ㗙ৃҹ⣀ゟⱘব࣪ˈгህᰃ䇈ᇚ ড়ϔϟϡህ㸠њ њϾথൟᗢМ䇈ā䖭⾡䯂乬ˈ㞾Ꮕ⫼ BRIDGE 㒘خ઺DŽϡ㽕䯂៥ĀᮽϞ⺄ࠄ MM ᮄ ⱘথൟˈ㽕䇈Դⱘ༈থདⓖ҂خӊᮄ㸷᳡ˈ㽕䇈Դⱘ㸷᳡དⓖ҂઺ˈ⺄ࠄ MM ᮄ BRIDGE—ᮽϞ⺄ࠄMMˈ㽕䇈ᮽϞདˈᰮϞ⺄ࠄMMˈ㽕䇈ᰮϞད˗⺄ࠄMMこњ ᔶ䈵↨ஏ˖ ཇҎこ㺸ᄤ ཇҎこ偀⬆ ⬋Ҏこ㺸ᄤ ⬋Ҏこ偀⬆ 24 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 25 4.Client 䗮䖛 Component ᥹ষ᪡㒉㒘ড়䚼ӊⱘᇍ䈵DŽ ㉏೒˄঺໪ᡒⱘ˅ ՟ᄤ Component public abstract class Employer { private String name; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public abstract void add(Employer employer); public abstract void delete(Employer employer); public List employers; public void printInfo() { System.out.println(name); } public List getEmployers() { Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 26 return this.employers; } } Leaf public class Programmer extends Employer { public Programmer(String name) { setName(name); employers = null;//⿟ᑣਬ, 㸼⼎≵᳝ϟሲњ } public void add(Employer employer) { } public void delete(Employer employer) { } } public class ProjectAssistant extends Employer { public ProjectAssistant(String name) { setName(name); employers = null;//乍Ⳃࡽ⧚, 㸼⼎≵᳝ϟሲњ } public void add(Employer employer) { } public void delete(Employer employer) { } } Composite public class ProjectManager extends Employer { public ProjectManager(String name) { setName(name); employers = new ArrayList(); } Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 27 public void add(Employer employer) { employers.add(employer); } public void delete(Employer employer) { employers.remove(employer); } } Client public class Test { public static void main(String[] args) { Employer pm = new ProjectManager("乍Ⳃ㒣⧚"); Employer pa = new ProjectAssistant("乍Ⳃࡽ⧚"); Employer programmer1 = new Programmer("⿟ᑣਬϔ"); Employer programmer2 = new Programmer("⿟ᑣਬѠ"); pm.add(pa);//Ў乍Ⳃ㒣⧚⏏ࡴ乍Ⳃࡽ⧚ pm.add(programmer2);//Ў乍Ⳃ㒣⧚⏏ࡴ⿟ᑣਬ List ems = pm.getEmployers(); for (Employer em : ems) { System.out.println(em.getName()); } } } result 乍Ⳃࡽ⧚ ⿟ᑣਬѠ ᔶ䈵↨ஏ˖ COMPOSITE—Mary Ҟ໽䖛⫳᮹DŽĀ៥䖛⫳᮹ˈԴ㽕䗕៥ϔӊ⼐⠽DŽ”“௃ˈ ད৻ˈএଚᑫˈԴ㞾ᏅᣥDŽ”“䖭ӊ 7 ᘸᤎⓖ҂ˈфˈ䖭ᴵ㺭ᄤདⳟˈфˈ䖭Ͼ ࣙгϡ䫭ˈфDŽ”“ୖˈфњϝӊњਔˈ៥াㄨᑨ䗕ϔӊ⼐⠽ⱘ઺DŽ”“ҔМ ਔˈ7 ᘸࡴ㺭ᄤࡴࣙࣙˈℷད䜡៤ϔ༫ਔˈᇣྤˈ咏⚺Դࣙ䍋ᴹDŽ ”“……”ˈ MM 䛑Ӯ⫼ Composite ῵ᓣњˈԴӮњ≵᳝˛ ড়៤῵ᓣ˖ড়៤῵ᓣᇚᇍ䈵㒘㒛ࠄᷥ㒧ᵘЁˈৃҹ⫼ᴹᦣ䗄ᭈԧϢ䚼ߚⱘ݇ ㋏DŽড়៤῵ᓣህᰃϔϾ໘⧚ᇍ䈵ⱘᷥ㒧ᵘⱘ῵ᓣDŽড়៤῵ᓣᡞ䚼ߚϢᭈԧⱘ݇㋏ ⫼ᷥ㒧ᵘ㸼⼎ߎᴹDŽড়៤῵ᓣՓᕫᅶ᠋ッᡞϔϾϾऩ⣀ⱘ៤ߚᇍ䈵੠⬅ҪӀ໡ড় 㗠៤ⱘড়៤ᇍ䈵ৠㄝⳟᕙDŽ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 28 1.2.4 㺙佄῵ᓣ˄Decorator˅ ࡼᗕഄ㒭ϔϾᇍ䈵⏏ࡴϔѯ乱໪ⱘ㘠䋷DŽህ๲ࡴࡳ㛑ᴹ䇈ˈDecorator ῵ᓣ Ⳍ↨⫳៤ᄤ㉏᳈Ў♉⌏DŽ 䗖⫼ᗻ 1.೼ϡᕅડ݊Ҫᇍ䈵ⱘᚙމϟˈҹࡼᗕǃ䗣ᯢⱘᮍᓣ㒭ऩϾᇍ䈵⏏ࡴ㘠䋷DŽ 2.໘⧚䙷ѯৃҹ᩸䫔ⱘ㘠䋷DŽ 3.ᔧϡ㛑䞛⫼⫳៤ᄤ㉏ⱘᮍ⊩䖯㸠ᠽܙᯊDŽ খϢ㗙 1.Component ᅮНϔϾᇍ䈵᥹ষˈৃҹ㒭䖭ѯᇍ䈵ࡼᗕഄ⏏ࡴ㘠䋷DŽ 2.ConcreteComponent ᅮНϔϾᇍ䈵ˈৃҹ㒭䖭Ͼᇍ䈵⏏ࡴϔѯ㘠䋷DŽ 3.Decorator 㓈ᣕϔϾᣛ৥ Component ᇍ䈵ⱘᣛ䩜ˈᑊᅮНϔϾϢ Component ᥹ষϔ㟈 ⱘ᥹ষDŽ 4.ConcreteDecorator ৥㒘ӊ⏏ࡴ㘠䋷DŽ ㉏೒ C:\Users\fulin\AppData\Local\Temp\404683\images\Decorator.jpg ՟ᄤ Component } public class ManDecoratorB extends Decorator } } System.out.println("ݡৗϔ乓佁"); public void reEat() { } System.out.println("ManDecoratorA ㉏"); reEat(); super.eat(); public void eat() { public class ManDecoratorA extends Decorator { ConcreteDec*rator } } person.eat(); public void eat() { } this.person = person; public void setPerson(Person person) { protected Person person; public abstract class Decorator implements Person { Decorator } } System.out.println("⬋Ҏ೼ৗ"); public void eat() { public class Man implements Person { ConcreteComponent } void eat(); public interface Person { 29 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Ͼᄤ㋏㒳Ϣᅶ᠋ҹঞ݊Ҫⱘᄤ㋏㒳ߚ⾏ˈৃҹᦤ催ᄤ㋏㒳ⱘ⣀ゟᗻ੠ৃ⿏ỡᗻDŽ 2.ᅶ᠋⿟ᑣϢᢑ䈵㉏ⱘᅲ⦄䚼ߚП䯈ᄬ೼ⴔᕜ໻ⱘձ䌪ᗻDŽᓩܹfacadeᇚ䖭 ໮᭄⫼᠋ᴹ䇈Ꮖ㒣䎇໳ˈ㗠䙷ѯ䳔㽕᳈໮ⱘৃᅮࠊᗻⱘ⫼᠋ৃҹ䍞䖛facadeሖDŽ ᠋ᏺᴹϔѯՓ⫼Ϟⱘೄ䲒DŽFacade ৃҹᦤկϔϾㅔऩⱘ㔎ⳕ㾚೒ˈ䖭ϔ㾚೒ᇍ໻ ݋ৃ䞡⫼ᗻˈг᳈ᆍᯧᇍᄤ㋏㒳䖯㸠ᅮࠊˈԚ䖭г㒭䙷ѯϡ䳔㽕ᅮࠊᄤ㋏㒳ⱘ⫼ 㗠বᕫ䍞ᴹ䍞໡ᴖDŽ໻໮᭄῵ᓣՓ⫼ᯊ䛑Ӯѻ⫳᳈໮᳈ᇣⱘ㉏DŽ䖭Փᕫᄤ㋏㒳᳈ 1.ᔧԴ㽕ЎϔϾ໡ᴖᄤ㋏㒳ᦤկϔϾㅔऩ᥹ষᯊDŽᄤ㋏㒳ᕔᕔ಴Ўϡᮁⓨ࣪ 䗖⫼ᗻ ষˈ䖭Ͼ᥹ষՓᕫ䖭ϔᄤ㋏㒳᳈ࡴᆍᯧՓ⫼DŽ Ўᄤ㋏㒳Ёⱘϔ㒘᥹ষᦤկϔϾϔ㟈ⱘ⬠䴶ˈFacade ῵ᓣᅮНњϔϾ催ሖ᥹ 1.2.5 ໪㾖῵ᓣ˄Facade˅ ManDecoratorB ㉏ =============== ManDecoratorA ㉏ ݡৗϔ乓佁 ⬋Ҏ೼ৗ result } } md2.eat(); md2.setPerson(md1); md1.setPerson(man); ManDecoratorB md2 = new ManDecoratorB(); ManDecoratorA md1 = new ManDecoratorA(); Man man = new Man(); public static void main(String[] args) { public class Test { Test } } System.out.println("ManDecoratorB ㉏"); System.out.println("==============="); super.eat(); public void eat() { 30 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ ;() sb = new ServiceBImpl sa = new ServiceAImpl(); public Facade() { ServiceC sc; ServiceB sb; ServiceA sa; public class Facade { Facade ՟ᄤ ㉏೒ ≵᳝ facade ⱘӏԩⳌֵ݇ᙃ˗े≵᳝ᣛ৥ Facade ⱘᣛ䩜DŽ ໘⧚⬅ Facade ᇍ䈵ᣛ⌒ⱘӏࡵDŽ ᅲ⦄ᄤ㋏㒳ⱘࡳ㛑DŽ 2.Subsystemclasses ᇚᅶ᠋ⱘ䇋∖ҷ⧚㒭䗖ᔧⱘᄤ㋏㒳ᇍ䈵DŽ ⶹ䘧ાѯᄤ㋏㒳㉏䋳䋷໘⧚䇋∖DŽ 1.Facade খϢ㗙 䆃ˈҢ㗠ㅔ࣪њᅗӀП䯈ⱘձ䌪݇㋏DŽ ሖⱘܹষ⚍DŽབᵰᄤ㋏㒳П䯈ᰃⳌѦձ䌪ⱘˈԴৃҹ䅽ᅗӀҙ䗮䖛facade䖯㸠䗮 3.ᔧԴ䳔㽕ᵘᓎϔϾሖ⃵㒧ᵘⱘᄤ㋏㒳ᯊˈՓ⫼facade῵ᓣᅮНᄤ㋏㒳Ё↣ 31 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 32 sc = new ServiceCImpl(); } public void methodA() { sa.methodA(); sb.methodB(); } public void methodB() { sb.methodB(); sc.methodC(); } public void methodC() { sc.methodC(); sa.methodA(); } } Subsystemclasses public class ServiceAImpl implements ServiceA { public void methodA() { System.out.println("䖭ᰃ᳡ࡵ A"); } } public class ServiceBImpl implements ServiceB { public void methodB() { System.out.println("䖭ᰃ᳡ࡵ B"); } } public class ServiceCImpl implements ServiceC { public void methodC() { System.out.println("䖭ᰃ᳡ࡵ C"); } } Test public class Test { Ϟᯢᰒ᳝߿ⱘᇍ䈵ˈᷛ䆚⌟䆩ᇚ䖨ಲⳳؐDŽ 5.ᑨ⫼⿟ᑣϡձ䌪Ѣᇍ䈵ᷛ䆚DŽ⬅Ѣ Flyweight ᇍ䈵ৃҹ㹿݅ѿˈᇍѢὖᗉ 䈵DŽ 4.བᵰߴ䰸ᇍ䈵ⱘ໪䚼⢊ᗕˈ䙷Мৃҹ⫼Ⳍᇍ䕗ᇥⱘ݅ѿᇍ䈵পҷᕜ໮㒘ᇍ 3.ᇍ䈵ⱘ໻໮᭄⢊ᗕ䛑ৃবЎ໪䚼⢊ᗕDŽ ᓔ䫔DŽټ 2.ᅠܼ⬅ѢՓ⫼໻䞣ⱘᇍ䈵ˈ䗴៤ᕜ໻ⱘᄬ 1.ϔϾᑨ⫼⿟ᑣՓ⫼њ໻䞣ⱘᇍ䈵DŽ ᯊˈՓ⫼ Flyweight ῵ᓣ˖މ ᔧ䛑݋໛ϟ߫ᚙ 䗖⫼ᗻ 䖤⫼݅ѿᡔᴃ᳝ᬜഄᬃᣕ໻䞣㒚㉦ᑺⱘᇍ䈵DŽ ῵ᓣ˄Flyweight˅ܗ1.2.6 ѿ 䖭ᰃ᳡ࡵ C 䖭ᰃ᳡ࡵ B 䖭ᰃ᳡ࡵ B 䖭ᰃ᳡ࡵ A ======== 䖭ᰃ᳡ࡵ B 䖭ᰃ᳡ࡵ A result } } facade.methodB(); facade.methodA(); Facade facade = new Facade(); //facade System.out.println("========"); sb.methodB(); sa.methodA(); ServiceB sb = new ServiceBImpl(); ServiceA sa = new ServiceAImpl(); public static void main(String[] args) { 33 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ ㉏೒ FlyweightFactory ᇍ䈵ᦤկϔϾᏆ߯ᓎⱘᅲ՟៪㗙߯ᓎϔϾ˄བᵰϡᄬ೼ⱘ䆱˅DŽ ⹂ֱড়⧚ഄ݅ѿ flyweightDŽᔧ⫼᠋䇋∖ϔϾ flyweight ᯊˈ ߯ᓎᑊㅵ⧚ flyweight ᇍ䈵DŽ 4.FlyweightFactory ᐌᇚ ConcreteFlyweight ᇍ䈵԰Ўᄤ㡖⚍DŽ ೼ Flyweight ᇍ䈵㒧ᵘⱘᶤѯሖ⃵ˈUnsharedConcreteFlyweight ᇍ䈵䗮 㛑ˈԚᅗᑊϡᔎࠊ݅ѿDŽ ᑊ䴲᠔᳝ⱘ Flyweight ᄤ㉏䛑䳔㽕㹿݅ѿDŽFlyweight ᥹ষՓ݅ѿ៤Ўৃ 3.UnsharedConcreteFlyweight ⱘ˗ेˈᅗᖙ乏⣀ゟѢ ConcreteFlyweight ᇍ䈵ⱘഎ᱃DŽ ⱘ⢊ᗕᖙ乏ᰃݙ䚼ټ ConcreteFlyweight ᇍ䈵ᖙ乏ᰃৃ݅ѿⱘDŽᅗ᠔ᄬ ぎ䯈DŽټ ᅲ⦄ Flyweight ᥹ষˈᑊЎݙ䚼⢊ᗕ˄བᵰ᳝ⱘ䆱˅๲ࡴᄬ 2.ConcreteFlyweight ᦣ䗄ϔϾ᥹ষˈ䗮䖛䖭Ͼ᥹ষ flyweight ৃҹ᥹ফᑊ԰⫼Ѣ໪䚼⢊ᗕDŽ 1.Flyweight খϢ㗙 34 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ } ( public static void main(String[] args public class Test { Test } } return flyweights.size(); public static int getSize() { } return flyweights.get(key); } flyweights.put(key, new FlyweightImpl()); if (flyweights.get(key) == null) { public static Flyweight getFlyweight(String key) { } flyweights.put(arg, new FlyweightImpl()); public FlyweightFactory(String arg) { private static Map flyweights = new HashMap(); public class FlyweightFactory { FlyweightFactory } } System.out.println("খ᭄ؐ: " + arg); // TODO Auto-generated method stub public void action(int arg) { public class FlyweightImpl implements Flyweight { ConcreteFlyweight } void action(int arg); public interface Flyweight { Flyweight ՟ᄤ 35 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 3.ֱᡸҷ⧚˄ProtectionProxy˅᥻ࠊᇍॳྟᇍ䈵ⱘ䆓䯂DŽ 䳔㽕ᯊᠡӮⳳℷ߯ᓎDŽ 2.㰮ҷ⧚˄VirtualProxy˅ḍ᥂䳔㽕߯ᓎᓔ䫔ᕜ໻ⱘᇍ䈵ˈՓᕫℸᇍ䈵া೼ 䖰⿟ҷ⧚˄RemoteProxy˅ЎϔϾᇍ䈵೼ϡৠⱘഄഔぎ䯈ᦤկሔ䚼ҷ㸼DŽ.1 ˖މProxy ῵ᓣDŽϟ䴶ᰃϔѯৃҹՓ⫼ Proxy ῵ᓣᐌ㾕ᚙ ೼䳔㽕⫼↨䕗䗮⫼੠໡ᴖⱘᇍ䈵ᣛ䩜ҷ᳓ㅔऩⱘᣛ䩜ⱘᯊ׭ˈՓ⫼ 䗖⫼ᗻ Ў݊Ҫᇍ䈵ᦤկϔ⾡ҷ⧚ҹ᥻ࠊᇍ䖭Ͼᇍ䈵ⱘ䆓䯂DŽ 1.2.7 ҷ⧚῵ᓣ˄Proxy˅ 4 খ᭄ؐ: 4 খ᭄ؐ: 3 খ᭄ؐ: 2 true খ᭄ؐ: 1 result } } System.out.println(FlyweightFactory.getSize()); fly4.action(4); Flyweight fly5 = FlyweightFactory.getFlyweight("d"); fly4.action(3); Flyweight fly4 = FlyweightFactory.getFlyweight("c"); fly3.action(2); Flyweight fly3 = FlyweightFactory.getFlyweight("b"); System.out.println(fly1 == fly2); Flyweight fly2 = FlyweightFactory.getFlyweight("a"); fly1.action(1); Flyweight fly1 = FlyweightFactory.getFlyweight("a"); // TODO Auto-generated method stub 36 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ } () public ProxyObject Object obj; public class ProxyObject implements Object { Proxy ՟ᄤ C:\Users\fulin\AppData\Local\Temp\404683\images\Proxy.jpg ㉏೒ ᅮН Proxy ᠔ҷ㸼ⱘᅲԧDŽ 6.RealSubject ҹՓ⫼ ProxyDŽ ⱘഄᮍ䛑 ᅮН RealSubject ੠ Proxy ⱘ݅⫼᥹ষˈ䖭ḋህ೼ӏԩՓ⫼ RealSubject 5.Subject 4.ProtectionProxy Ẕᶹ䇗⫼㗙ᰃ৺݋᳝ᅲ⦄ϔϾ䇋∖᠔ᖙ䳔ⱘ䆓䯂ᴗ䰤DŽ 3.VirtualProxy ৃҹ㓧ᄬᅲԧⱘ䰘ࡴֵᙃˈҹ֓ᓊ䖳ᇍᅗⱘ䆓䯂DŽ থ䗕Ꮖ㓪ⷕⱘ䇋∖DŽ 2.RemoteProxy 䋳䋷ᇍ䇋∖ঞ݊খ᭄䖯㸠㓪ⷕˈᑊ৥ϡৠഄഔぎ䯈Ёⱘᅲԧ ⧚ⱘ㉏ൟ˖ ݊Ҫࡳ㛑ձ䌪Ѣ পˈᑊৃ㛑䋳䋷߯ᓎ੠ߴ䰸ᅗDŽ ᥻ࠊᇍᅲԧⱘ ᦤկϔϾϢ Subject ⱘ᥹ষⳌৠⱘ᥹ষˈ䖭ḋҷ⧚ህৃҹ⫼ᴹ᳓ҷᅲԧDŽ ⳌৠˈProxy Ӯᓩ⫼ SubjectDŽ ֱᄬϔϾᓩ⫼Փᕫҷ⧚ৃҹ䆓䯂ᅲԧDŽ㢹 RealSubject ੠ Subject ⱘ᥹ষ 1.Proxy খϢ㗙 5.䰆☿๭ҷ⧚ˈֱᡸⳂᷛˈϡ䅽ᙊᛣ⫼᠋᥹䖥DŽ ѯ䰘ࡴ᪡԰DŽ 4.ᱎ㛑ᣛᓩ˄SmartReference˅পҷњㅔऩⱘᣛ䩜ˈᅗ೼䆓䯂ᇍ䈵ᯊᠻ㸠ϔ 37 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 38 System.out.println("䖭ᰃҷ⧚㉏"); obj = new ObjectImpl(); } public void action() { System.out.println("ҷ⧚ᓔྟ"); obj.action(); System.out.println("ҷ⧚㒧ᴳ"); } } Subject public interface Object { void action(); } RealSubject public class ObjectImpl implements Object { public void action() { System.out.println("========"); System.out.println("========"); System.out.println("䖭ᰃ㹿ҷ⧚ⱘ㉏"); System.out.println("========"); System.out.println("========"); } } Test public class Test { public static void main() { Object obj = new ProxyObject(); obj.action(); } } result 䖭ᰃҷ⧚㉏ ҷ⧚ᓔྟ ======== ======== 䖭ᰃ㹿ҷ⧚ⱘ㉏ ᇍ䈵ⱘݙ䚼ሲᗻ Ꮖ㒣ᄬ೼ⱘ, ᑊᇚ݊԰Ўҷ⧚ܜ䗄ⱘᮍ⊩Փ⫼ҷ⧚῵ᓣ, 䙷Мⳳᅲ㾦㡆ᖙ乏ᰃџ Ҫᮍ⊩(preRequest(),postRequest()), ৃҹ໘⧚ϔѯ݊Ҫ䯂乬DŽབᵰ㽕ᣝ✻Ϟ ⦄೼⫼ProxySubject ᴹҷ⧚RealSubject ㉏, ৠḋ䖒ࠄⳂⱘ, ৠᯊ䖬ᇕ㺙њ݊ ⬅ҹϞҷⷕৃҹⳟߎ, ᅶ᠋ᅲ䰙䳔㽕䇗⫼ⱘᰃRealSubject ㉏ⱘrequest () ᮍ⊩, Sub.request(); Subject sub = new ProxySubject(); ᅶ᠋ッ䇗⫼: } postRequest(); realSubject.request(); ℸ໘ᠻ㸠ⳳᅲᇍ䈵ⱘrequest ᮍ⊩ } realSubject = new RealSubject(); if ( realSubject == null ) { preRequest(); public void request() { 䆹ᮍ⊩ᇕ㺙њⳳᅲᇍ䈵ⱘrequest ᮍ⊩ public ProxySubject() { } private RealSubject realSubject; ҹⳳᅲ㾦㡆԰Ўҷ⧚㾦㡆ⱘሲᗻ public class ProxySubject extends Subject{ ҷ⧚㾦㡆: } } System.out.println ("From real subject."); public void request(){ public RealSubject() { } public class RealSubject extends Subject{ ⳳᅲ㾦㡆: ᅲ⦄њSubject ⱘrequest()ᮍ⊩DŽ } abstract public void request(); abstract public class Subject{ ᢑ䈵㾦㡆: 㞾Ꮕ೼㔥Ϟᡒⱘ঺໪ⱘ՟ᄤ˖ ҷ⧚㒧ᴳ ======== ======== 39 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 40 1.3 㸠Ўൟ῵ᓣ Chain of Responsibility ( 䋷ӏ䫒῵ᓣ ) Command ( ੑҸ῵ᓣ ) Interpreter ( 㾷䞞఼῵ᓣ ) Iterator ( 䗁ҷ఼῵ᓣ ) Mediator ( Ёҟ㗙῵ᓣ ) Memento ( ໛ᖬᔩ῵ᓣ ) Observer ( 㾖ᆳ㗙῵ᓣ ) State ( ⢊*῵ᓣ ) Strategy ( ㄪ⬹῵ᓣ ) TemplateMethod ( ῵ᵓᮍ⊩ ) Vis*tor ( 䆓䯂㗙῵ᓣ ) 1.3.1 䋷ӏ䫒῵ᓣ˄Chain of Responsibility˅ Փ໮Ͼᇍ䈵䛑᳝ᴎӮ໘⧚䇋∖ˈҢ㗠䙓ܡ䇋∖ⱘথ䗕㗙੠᥹ᬊ㗙П䯈ⱘ㗺ড় ݇㋏DŽᇚ䖭ѯᇍ䈵䖲៤ϔᴵ䫒ˈᑊ⊓ⴔ䖭ᴵ䫒Ӵ䗦䆹䇋∖ˈⳈࠄ᳝ϔϾᇍ䈵໘⧚ ᅗЎℶDŽ 䖭ϔ῵ᓣⱘᛇ⊩ᰃˈ㒭໮Ͼᇍ䈵໘⧚ϔϾ䇋∖ⱘᴎӮˈҢ㗠㾷㗺থ䗕㗙੠᥹ ফ㗙. 䗖⫼ᗻ 1.᳝໮Ͼⱘᇍ䈵ৃҹ໘⧚ϔϾ䇋∖ˈાϾᇍ䈵໘⧚䆹䇋∖䖤㸠ᯊࠏ㞾ࡼ⹂ ᅮDŽ 2.Դᛇ೼ϡᯢ⹂ᣛᅮ᥹ᬊ㗙ⱘᚙމϟˈ৥໮Ͼᇍ䈵ЁⱘϔϾᦤѸϔϾ䇋∖DŽ 3.ৃ໘⧚ϔϾ䇋∖ⱘᇍ䈵䲚ড়ᑨ㹿ࡼᗕᣛᅮDŽ খϢ㗙 1.Handler ᅮНϔϾ໘⧚䇋∖ⱘ᥹ষDŽ ˄ৃ䗝˅ᅲ⦄ৢ㒻䫒DŽ 2.ConcreteHandler Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 41 ໘⧚ᅗ᠔䋳䋷ⱘ䇋∖DŽ ৃ䆓䯂ᅗⱘৢ㒻㗙DŽ བᵰৃ໘⧚䆹䇋∖ˈህ໘⧚ᅗ˗৺߭ᇚ䆹䇋∖䕀থ㒭ᅗⱘৢ㒻㗙DŽ 3.Client ৥䫒Ϟⱘ݋ԧ໘⧚㗙(ConcreteHandler)ᇍ䈵ᦤѸ䇋∖DŽ ㉏೒˄঺ᇏ˅ ՟ᄤ Handler public interface RequestHandle { void handleRequest(Request request); } ConcreteHandler public class HRRequestHandle implements RequestHandle { public void handleRequest(Request request) { if (request instanceof DimissionRequest) { System.out.println("㽕⾏㘠, Ҏџᅵᡍ!"); } System.out.println("䇋∖ᅠ↩"); } } ;( RequestHandle pm = new PMRequestHandle(hr RequestHandle hr = new HRRequestHandle(); public static void main(String[] args) { public class Test { Client } } } rh.handleRequest(request); } else { 乍Ⳃ㒘䭓ᅵᡍ!"); ,؛ System.out.println("㽕䇋 if (request instanceof LeaveRequest) { public void handleRequest(Request request) { } this.rh = rh; public TLRequestHandle(RequestHandle rh) { RequestHandle rh; public class TLRequestHandle implements RequestHandle { } } } rh.handleRequest(request); } else { System.out.println("㽕ࡴ㭾, 乍Ⳃ㒣⧚ᅵᡍ!"); if (request instanceof AddMoneyRequest) { public void handleRequest(Request request) { } this.rh = rh; public PMRequestHandle(RequestHandle rh) { RequestHandle rh; public class PMRequestHandle implements RequestHandle { 42 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ ᅮ໘⧚ℸ䇋އ䍋ᴹᔶ៤ϔᴵ䫒DŽ䇋∖೼䖭Ͼ䫒ϞӴ䗦ˈⳈࠄ䫒ϞⱘᶤϔϾᇍ䈵 䋷ӏ䫒῵ᓣ˖೼䋷ӏ䫒῵ᓣЁˈᕜ໮ᇍ䈵⬅↣ϔϾᇍ䈵ᇍ݊ϟᆊⱘᓩ⫼㗠᥹ ᡞ㒌ᴵӴ㒭㗕Ꮬњˈ਀䇈ᰃϾ㗕໘ཇਔˈᖿ䎥! བᵰϡᜓᛣ䇋৥ࠡӴāˈ㒌ᴵህϔϾ᥹ϔϾⱘӴϞএњˈ㊳㊩ˈӴࠄ㄀ϔᥦⱘ MM ៥ⱘཇ᳟ট৫˛خછˈࠡ䴶തњད޴Ͼⓖ҂ⱘ MM ઢˈᡒᓴ㒌ᴵˈݭϞ“Hi,ৃҹ CHAIN OF RESPONSIBLEITY—ᰮϞএϞ㣅䇁䇒ˈЎњདᓔ⑰തࠄњ᳔ৢϔᥦˈ ᔶ䈵↨ஏ˖ z ೼ϔϾ䋷ӏ䫒ϞৃҹӴ䗦᭄ϾੑҸDŽ ᥝ˅ˈϡৃҹӴ㒭໮Ͼϟᆊ z 䋷ӏ䫒῵ᓣ㽕∖೼ৠϔϾᯊ䯈䞠ˈੑҸাৃҹӴ㒭ϔϾϟᆊ˄៪໘⧚ гৃҹᰃϔϾ⦃˗䫒ⱘᢧᠥ㒧ᵘৃҹᰃऩ䖲䗮៪໮䖲䗮 z 䋷ӏ䫒῵ᓣϡᣛᅮ䋷ӏ䫒ⱘᢧᠥ㒧ᵘˈϔϾ䫒ৃҹᰃϔᴵ㒓ǃϔϾᷥˈ ߎᴹDŽ z 䋷ӏ䫒῵ᓣᑊϡ߯ᓎ䋷ӏ䫒ˈ䋷ӏ䫒ⱘ߯ᓎᖙ乏⬅㋏㒳ⱘ݊Ҫ䚼ߚ߯ᓎ ঺໪˖ 䇋∖ᅠ↩ 㽕⾏㘠, Ҏџᅵᡍ! ======== 㽕ࡴ㭾, 乍Ⳃ㒣⧚ᅵᡍ! =========== 䇋∖ᅠ↩ 㽕⾏㘠, Ҏџᅵᡍ! result } } pm.handleRequest(request); request = new DimissionRequest(); //乍Ⳃ㒣⧚Ϟ⧚䕲㘠䇋∖ System.out.println("========"); tl.handleRequest(request); request = new AddMoneyRequest(); //team leader ໘⧚ࡴ㭾䇋∖ System.out.println("==========="); tl.handleRequest(request); Request request = new DimissionRequest(); //team leader ໘⧚⾏㘠䇋∖ RequestHandle tl = new TLRequestHandle(pm); 43 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ ໄᯢᠻ㸠᪡԰ⱘ᥹ষDŽ 1.Command খϢ㗙 ᯊՓ⫼䆹῵ᓣгᯧѢ⏏ࡴᮄџࡵҹᠽሩ㋏㒳DŽ Command ᳝ϔϾ݀݅ⱘ᥹ষˈՓᕫԴৃҹ⫼ৠϔ⾡ᮍᓣ䇗⫼᠔᳝ⱘџࡵDŽৠ ࡵᇕ㺙њᇍ᭄᥂ⱘϔ㒘বࡼDŽCommand ῵ᓣᦤկњᇍџࡵ䖯㸠ᓎ῵ⱘᮍ⊩DŽ  䖭ḋϔ⾡㒧ᵘ೼ᬃᣕџࡵ( transaction )ⱘֵᙃ㋏㒳Ёᕜᐌ㾕DŽϔϾџ 5. ⫼ᵘᓎ೼ॳ䇁᪡԰Ϟⱘ催ሖ᪡԰ᵘ䗴ϔϾ㋏㒳DŽ Ҹᑊ⫼ Execute ᪡԰䞡ᮄᠻ㸠ᅗӀDŽ ϔ㟈ⱘׂᬍ᮹ᖫDŽҢዽ⑗Ёᘶ໡ⱘ䖛⿟ࣙᣀҢ⺕ⲬЁ䞡ᮄ䇏ܹ䆄ᔩϟᴹⱘੑ ᪡԰ˈৃҹ⫼ᴹֱᣕবࡼⱘϔϾټ ೼ Command ᥹ষЁ⏏ࡴ㺙䕑᪡԰੠ᄬ ϔ䘡DŽخ4. ᬃᣕׂᬍ᮹ᖫˈ䖭ḋᔧ㋏㒳ዽ⑗ᯊˈ䖭ѯׂᬍৃҹ㹿䞡 āDŽخExecute ᴹᅲ⦄䞡᭄ϡ䰤ⱘĀপ⍜ā੠Ā䞡 ৆߫㸼ЁDŽৃ䗮䖛৥ৢ੠৥ࠡ䘡ग़䖭ϔ߫㸼ᑊߚ߿䇗⫼ Unexecute ੠ ೼ϔϾग़ټ᪡԰ˈ䆹᪡԰প⍜Ϟϔ⃵ Execute 䇗⫼ⱘᬜᵰDŽᠻ㸠ⱘੑҸ㹿ᄬ ᯊ䖭Ͼ⢊ᗕ⫼ᴹ⍜䰸䆹᪡԰ⱘᕅડDŽCommand ᥹ষᖙ乏⏏ࡴϔϾ Unexecute 䍋ᴹˈ೼প⍜᪡԰ټ Command ⱘ Excute ᪡԰ৃ೼ᅲᮑ᪡԰ࠡᇚ⢊ᗕᄬ 3. ᬃᣕপ⍜᪡԰DŽ ᅲ⦄䆹䇋∖DŽܓҸᇍ䈵Ӵ䗕㒭঺ϔϾϡৠⱘ䖯⿟ᑊ೼䙷 ⱘ᥹ᬊ㗙ৃ⫼ϔ⾡Ϣഄഔぎ䯈᮴݇ⱘᮍᓣ㸼䖒ˈ䙷Мህৃᇚ䋳䋷䆹䇋∖ⱘੑ  ϔϾ Command ᇍ䈵ৃҹ᳝ϔϾϢ߱ྟ䇋∖᮴݇ⱘ⫳ᄬᳳDŽབᵰϔϾ䇋∖ 2. ೼ϡৠⱘᯊࠏᣛᅮǃᥦ߫੠ᠻ㸠䇋∖DŽ Command ῵ᓣᰃಲ䇗ᴎࠊⱘϔϾ䴶৥ᇍ䈵ⱘ᳓ҷકDŽ ೼ᶤ໘⊼ݠˈ㗠ᅗᇚ೼⿡ৢᶤϾ䳔㽕ⱘᯊ׭㹿䇗⫼DŽܜ䇗ߑ᭄ᰃᣛߑ᭄  ৃ⫼䖛⿟䇁㿔Ёⱘಲ䇗˄callback˅ߑ᭄㸼䖒䖭⾡খ᭄࣪ᴎࠊDŽ᠔䇧ಲ 1. ᢑ䈵ߎᕙᠻ㸠ⱘࡼ԰ҹখ᭄࣪ᶤᇍ䈵DŽ 䗖⫼ᗻ ᇍ䇋∖ᥦ䯳៪䆄ᔩ䇋∖᮹ᖫˈҹঞᬃᣕৃ᩸䫔ⱘ᪡԰DŽ ᇚϔϾ䇋∖ᇕ㺙ЎϔϾᇍ䈵ˈҢ㗠ՓԴৃ⫼ϡৠⱘ䇋∖ᇍᅶ᠋䖯㸠খ᭄࣪˗ 1.3.2 ੑҸ῵ᓣ˄Command˅ 䋷ӏ᥼㒭ϟᆊDŽϔϾ䇋∖ৃҹ᳔㒜ϡ㹿ӏԩ᥹ᬊッᇍ䈵᠔᥹ফDŽ ϟࡼᗕⱘ䞡ᮄ㒘㒛䫒੠ߚ䜡䋷ӏDŽ໘⧚㗙᳝ϸϾ䗝ᢽ˖ᡓᢙ䋷ӏ៪㗙ᡞމッⱘᚙ ∖DŽᅶ᠋ᑊϡⶹ䘧䫒ϞⱘાϔϾᇍ䈵᳔㒜໘⧚䖭Ͼ䇋∖ˈ㋏㒳ৃҹ೼ϡᕅડᅶ᠋ 44 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 45 2.ConcreteCommand ᇚϔϾ᥹ᬊ㗙ᇍ䈵㒥ᅮѢϔϾࡼ԰DŽ 䇗⫼᥹ᬊ㗙Ⳍᑨⱘ᪡԰ˈҹᅲ⦄ Executeˈ䋳䋷䇗⫼ੑҸᇍ䈵ᠻ㸠䇋∖DŽ ੑҸ᥹ষᅲ⦄ᇍ䈵ˈᰃĀ㰮āⱘᅲ⦄˗䗮ᐌӮᣕ᳝᥹ᬊ㗙ˈᑊ䇗⫼᥹ᬊ㗙ⱘ ࡳ㛑ᴹᅠ៤ੑҸ㽕ᠻ㸠ⱘ᪡԰DŽ 3.Client ߯ᓎϔϾ݋ԧੑҸᇍ䈵ᑊ䆒ᅮᅗⱘ᥹ᬊ㗙DŽ ߯ᓎ݋ԧⱘੑҸᇍ䈵ˈᑊϨ䆒㕂ੑҸᇍ䈵ⱘ᥹ᬊ㗙DŽ⊼ᛣ䖭Ͼϡᰃ៥Ӏᐌ㾘 ᛣНϞⱘᅶ᠋ッˈ㗠ᰃ೼㒘㺙ੑҸᇍ䈵੠᥹ᬊ㗙ˈ៪䆌ˈᡞ䖭ϾClient⿄Ў㺙䜡 㗙Ӯ᳈ད⧚㾷ˈ಴ЎⳳℷՓ⫼ੑҸⱘᅶ᠋ッᰃҢ Invoker ᴹ㾺থᠻ㸠DŽ 4.Invoker 㽕∖䆹ੑҸᠻ㸠䖭Ͼ䇋∖DŽ 㽕∖ੑҸᇍ䈵ᠻ㸠䇋∖ˈ䗮ᐌӮᣕ᳝ੑҸᇍ䈵ˈৃҹᣕ᳝ᕜ໮ⱘੑҸᇍ䈵DŽ 䖭Ͼᰃᅶ᠋ッⳳℷ㾺থੑҸᑊ㽕∖ੑҸᠻ㸠Ⳍᑨ᪡԰ⱘഄᮍˈгህᰃ䇈ⳌᔧѢՓ ⫼ੑҸᇍ䈵ⱘܹষDŽ 5.Receiver ⶹ䘧བԩᅲᮑϢᠻ㸠ϔϾ䇋∖Ⳍ݇ⱘ᪡԰DŽӏԩ㉏䛑ৃ㛑԰ЎϔϾ᥹ᬊ 㗙DŽ ᥹ᬊ㗙ˈⳳℷᠻ㸠ੑҸⱘᇍ䈵DŽӏԩ㉏䛑ৃ㛑៤ЎϔϾ᥹ᬊ㗙ˈা㽕ᅗ㛑໳ ᅲ⦄ੑҸ㽕∖ᅲ⦄ⱘⳌᑨࡳ㛑DŽ ㉏೒ ՟ᄤ Command public abstract class Command { Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 46 protected Receiver receiver; public Command(Receiver receiver) { this.receiver = receiver; } public abstract void execute(); } ConcreteCommand public class CommandImpl extends Command { public CommandImpl(Receiver receiver) { super(receiver); } public void execute() { receiver.request(); } } Invoker public class Invoker { private Command command; public void setCommand(Command command) { this.command = command; } public void execute() { command.execute(); } } Receiver public class Receiver { public void receive() { System.out.println("This is Receive class!"); } } Test 㸠ⱘDŽ㋏㒳ᬃᣕੑҸⱘ᩸⍜DŽ ᳈ϡᖙⶹ䘧䇋∖ᰃᗢМ㹿᥹ᬊˈҹঞ᪡԰ᰃ৺ᠻ㸠ˈԩᯊ㹿ᠻ㸠ҹঞᰃᗢМ㹿ᠻ ⱘϔᮍ੠থ䗕ⱘϔᮍ⣀ゟᓔᴹˈՓᕫ䇋∖ⱘϔᮍϡᖙⶹ䘧᥹ᬊ䇋∖ⱘϔᮍⱘ᥹ষˈ 䆌䇋∖ܕߎੑҸⱘ䋷ӏ੠ᠻ㸠ੑҸⱘ䋷ӏߚࡆᓔˈྨ⌒㒭ϡৠⱘᇍ䈵DŽੑҸ῵ᓣ ੑҸ῵ᓣ˖ੑҸ῵ᓣᡞϔϾ䇋∖៪㗙᪡԰ᇕ㺙ࠄϔϾᇍ䈵ЁDŽੑҸ῵ᓣᡞথ Ā៥ৠᯊ㒭៥ྤྤϝϾ⬋᳟ট䗕 COMMANDˈህ᭄Դ᳔ᇣ⇨ˈᠡ䇋៥ৗ䴶DŽ” ᓳᓳজӴ䗕䖛ᴹϔϾ COMMANDˈЎњᛳ䇶Ҫˈ៥䇋Ҫৗњ⹫ᴖ䝅䴶ˈાⶹ䘧Ҫ䇈˖ ֽП䯈Ӵ䗕ֵᙃˈཌྷᇍ៥᳝ҔМᣛ⼎ˈህݭϔᓴ㒌ᴵ䅽ཌྷᓳᓳᏺ㒭៥DŽ䖭ϡˈཌྷ ࡽѢཌྷᓳᓳ೼៥Ӏ׳COMMAND—׎᳝ϔϾ MM ᆊ䞠ㅵᕫ⡍߿Ϲˈ≵⊩㾕䴶ˈাད ᔶ䈵↨ஏ˖ ᥹ᬊ㗙 Receiver ᠡᰃⳳℷⱘੑҸᠻ㸠㗙DŽ ˖ܙ㸹 This is Receive class! result } } i.execute(); i.setCommand(cmd); Invoker i = new Invoker(); Command cmd = new CommandImpl(rec); Receiver rec = new Receiver(); pubbic static void main(String[] args) { public class Test { 47 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ ㉏೒ ᪡԰DŽ 䇗⫼㾷 䜡㗠៤DŽ 䆹ᢑ䈵䇁⊩ᷥ⬅ NonterminalExpression ੠ TerminalExpression ⱘᅲ՟㺙 ⊩ᷥDŽ Ͼ⡍ᅮⱘহᄤⱘᢑ䈵 ៪㹿㒭ᅮ 㸼⼎䆹᭛⊩ᅮНⱘ䇁㿔Ё ᵘᓎ 5.Client˄ᅶ᠋˅ ࣙ৿㾷䞞఼П໪ⱘϔѯܼሔֵᙃDŽ 4.Context˄Ϟϟ᭛˅ Ў᭛⊩Ёⱘ䴲㒜㒧ヺᅲ⦄㾷䞞(Interpret)᪡԰DŽ 3.NonterminalExpression(䴲㒜㒧ヺ㸼䖒ᓣ) ˄ᑨ䆹ᰃ SimpleExpression˅ ϔϾহᄤЁⱘ↣Ͼ㒜㒧ヺ䳔㽕䆹㉏ⱘϔϾᅲ՟DŽ ᅲ⦄Ϣ᭛⊩Ёⱘ㒜㒧ヺⳌ݇㘨ⱘ㾷䞞᪡԰DŽ 2.TerminalExpression(㒜㒧ヺ㸼䖒ᓣ ˄ᑨ䆹ᰃ AdvanceExpression˅ ໄᯢϔϾᢑ䈵ⱘ㾷䞞᪡԰ˈ䖭Ͼ᥹ষЎᢑ䈵䇁⊩ᷥЁ᠔᳝ⱘ㡖⚍᠔݅ѿDŽ 1.AbstractExpression(ᢑ䈵㸼䖒ᓣ) খϢ㗙 ⱘDŽ ϟ, 䕀ᤶ఼ҡৃ⫼㾷䞞఼῵ᓣᅲ⦄, 䆹῵ᓣҡᰃ᳝⫼މ⢊ᗕᴎDŽԚेՓ೼䖭⾡ᚙ ᇚᅗӀ䕀ᤶ៤঺ϔ⾡ᔶᓣDŽ՟བˈℷ߭㸼䖒ᓣ䗮ᐌ㹿䕀ᤶ៤ܜᷥᅲ⦄ⱘ㗠ᰃ佪 2.ᬜ⥛ϡᰃϔϾ݇䬂䯂乬DŽ᳔催ᬜⱘ㾷䞞఼䗮ᐌϡᰃ䗮䖛Ⳉ᥹㾷䞞䇁⊩ߚᵤ 1.䆹᭛⊩ㅔऩᇍѢ໡ᴖⱘ᭛⊩᭛⊩ⱘ㉏ሖ⃵বᕫᑲ໻㗠᮴⊩ㅵ⧚DŽ ᯊ䆹῵ᓣᬜᵰ᳔ད˖މ⊩ᷥᯊˈৃՓ⫼㾷䞞఼῵ᓣDŽ㗠ᔧᄬ೼ҹϟᚙ ᔧ᳝ϔϾ䇁㿔䳔㽕㾷䞞ᠻ㸠ᑊϨԴৃᇚ䆹䇁㿔Ёⱘহᄤ㸼⼎ЎϔϾᢑ䈵䇁 䗖⫼ᗻ Փ⫼䆹㸼⼎ᴹ㾷䞞䇁㿔ЁⱘহᄤDŽ 㒭ᅮϔϾ䇁㿔ˈᅮНᅗⱘ᭛⊩ⱘϔ⾡㸼⼎ˈᑊᅮНϔϾ㾷䞞఼ˈ䖭Ͼ㾷䞞఼ 1.3.3 㾷䞞఼῵ᓣ˄Interpreter˅ 48 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 49 ՟ᄤ AbstractExpression public abstract class Expression { abstract void interpret(Context ctx); } Expression public class AdvanceExpression extends Expression { void interpret(Context ctx) { System.out.println("䖭ᰃ催㑻㾷ᵤ఼!"); } } public class SimpleExpression extends Expression { void interpret(Context ctx) { System.out.println("䖭ᰃ᱂䗮㾷ᵤ఼!"); } } Context public class Context { private String content; private List list = new ArrayList(); public void setContent(String content) { this.content = content; } ఼῵ᓣЁ䳔㽕ᅮНϔϾҷ㸼᭛⊩ⱘੑҸ㉏ⱘㄝ㑻㒧ᵘˈгህᰃϔ㋏߫ⱘ㒘ড়㾘 ೼㾷䞞఼῵ᓣ䞠䴶ᦤࠄⱘ䇁㿔ᰃᣛӏԩ㾷䞞఼ᇍ䈵㛑໳㾷䞞ⱘӏԩ㒘ড়DŽ೼㾷䞞 㾷䞞఼῵ᓣᇚᦣ䗄ᗢḋ೼᳝њϔϾㅔऩⱘ᭛⊩ৢˈՓ⫼῵ᓣ䆒䅵㾷䞞䖭ѯ䇁হDŽ ᑊৠᯊᦤկϔϾ㾷䞞఼DŽᅶ᠋ッৃҹՓ⫼䖭Ͼ㾷䞞఼ᴹ㾷䞞䖭Ͼ䇁㿔ЁⱘহᄤDŽ 㾷䞞఼῵ᓣ˖㒭ᅮϔϾ䇁㿔ৢˈ㾷䞞఼῵ᓣৃҹᅮНߎ݊᭛⊩ⱘϔ⾡㸼⼎ˈ ✻ⴔϞ䴶ⱘ㛮ᴀᠻ㸠ህৃҹњDŽ ϔϾ Interpreterˈخৗ㽓令ⱘℹ偸ǃএⳟ⬉ᕅⱘᮍ⊩ㄝㄝˈ䎳 MM 㑺Ӯᯊˈা㽕 INTERPRETER—׎᳝ϔϾlj⊵ MM ⳳ㒣NJˈϞ䴶᳝৘⾡⊵ MM ⱘᬏ⬹ˈ↨བ䇈এ ᔶ䈵↨ஏ˖ 䖭ᰃ᱂䗮㾷ᵤ఼! 䖭ᰃ催㑻㾷ᵤ఼! 䖭ᰃ᱂䗮㾷ᵤ఼! result } } } eps.interpret(ctx); for (Expression eps : ctx.getList()) { ctx.add(new SimpleExpression()); ctx.add(new AdvanceExpression()); ctx.add(new SimpleExpression()); Context ctx = new Context(); public static void main(String[] args) { public class Test { Test } } return list; public List getList() { } list.add(eps); public void add(Expression eps) { } return this.content; public String getContent() { 50 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ ㉏೒ Ͼ䗖ᔧⱘᅲ՟. ݋ԧ㘮ড়ᅲ⦄߯ᓎⳌᑨ䗁ҷ఼ⱘ᥹ষˈ䆹᪡԰䖨ಲConcreteIteratorⱘϔ 4.ConcreteAggregate 㘮ড়ᅮН߯ᓎⳌᑨ䗁ҷ఼ᇍ䈵ⱘ᥹ষDŽ 3.Aggregate ᇍ䆹㘮ড়䘡ग़ᯊ䎳䏾ᔧࠡԡ㕂DŽ ݋ԧ䗁ҷ఼ᅲ⦄䗁ҷ఼᥹ষDŽ 2.ConcreteIterator ㋴ⱘ᥹ষDŽܗ 䗁ҷ఼ᅮН䆓䯂੠䘡ग़ 1.Iterator খϢ㗙 ेᬃᣕ໮ᗕ䗁ҷ DŽ 3.Ў䘡ग़ϡৠⱘ㘮ড়㒧ᵘᦤկϔϾ㒳ϔⱘ᥹ষ 2.ᬃᣕᇍ㘮ড়ᇍ䈵ⱘ໮⾡䘡ग़DŽ 1.䆓䯂ϔϾ㘮ড়ᇍ䈵ⱘݙᆍ㗠᮴䳔ᲈ䴆ᅗⱘݙ䚼㸼⼎DŽ 䗖⫼ᗻ 䚼㸼⼎DŽ ㋴, 㗠জϡ䳔ᲈ䴆䆹ᇍ䈵ⱘݙܗᦤկϔ⾡ᮍ⊩乎ᑣ䆓䯂ϔϾ㘮ড়ᇍ䈵Ё৘Ͼ 1.3.4 䗁ҷ఼῵ᓣ˄Iterator˅ 㑻㒧ᵘЁⱘᇍ䈵ⱘӏԩᥦ߫㒘ড়䛑ᰃϔϾ䇁㿔DŽ ߭DŽ↣ϔϾੑҸᇍ䈵䛑᳝ϔϾ㾷䞞ᮍ⊩ˈҷ㸼ᇍੑҸᇍ䈵ⱘ㾷䞞DŽੑҸᇍ䈵ⱘㄝ 51 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 52 C:\Users\fulin\AppData\Local\Temp\404683\images\Iterator.jpg ՟ᄤ Iterator public interface Iterator { Object next(); void first(); void last(); boolean hasNext(); } ConcreteIterator public class IteratorImpl implements Iterator { private List list; private int index; public IteratorImpl(List list) { index = 0; this.list = list; } public void first() { index = 0; } Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 53 public void last() { index = list.getSize(); } public Object next() { Object obj = list.get(index); index++; return obj; } public boolean hasNext() { return index < list.getSize(); } } Aggregate public interface List { Iterator iterator(); Object get(int index); int getSize(); void add(Object obj); } ConcreteAggregate public class ListImpl implements List { private Object[] list; private int index; private int size; public ListImpl() { index = 0; size = 0; list = new Object[100]; } public Iterator iterator() { return new IteratorImpl(this); Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 54 } public Object get(int index) { return list[index]; } public int getSize() { return this.size; } public void add(Object obj) { list[index++] = obj; size++; } } Test public class Test { public static void main(String[] args) { List list = new ListImpl(); list.add("a"); list.add("b"); list.add("c"); //㄀ϔ⾡䗁ҷᮍᓣ Iterator it = list.iterator(); while (it.hasNext()) { System.out.println(it.next()); } System.out.println("====="); //㄀Ѡ⾡䗁ҷᮍᓣ for (int i = 0; i < list.getSize(); i++) { System.out.println(list.get(i)); } } } result a b c ===== a * ݋ԧЁҟ㗙䗮䖛ण䇗৘ৠџᇍ䈵ᅲ⦄ण԰㸠Ў 2.ConcreteMediator Ёҟ㗙ᅮНϔϾ᥹ষ⫼ѢϢ৘ৠџ˄Colleague˅ᇍ䈵䗮ֵDŽ 1.Mediator খϢ㗙 3.ᛇᅮࠊϔϾߚᏗ೼໮Ͼ㉏Ёⱘ㸠Ўˈ㗠জϡᛇ⫳៤໾໮ⱘᄤ㉏DŽ 䈵DŽ 2.ϔϾᇍ䈵ᓩ⫼݊Ҫᕜ໮ᇍ䈵ᑊϨⳈ᥹Ϣ䖭ѯᇍ䈵䗮ֵᇐ㟈䲒ҹ໡⫼䆹ᇍ ⏋хϨ䲒ҹ⧚㾷DŽ 1.ϔ㒘ᇍ䈵ҹᅮН㡃དԚᰃ໡ᴖⱘᮍᓣ䖯㸠䗮ֵDŽѻ⫳ⱘⳌѦձ䌪݇㋏㒧ᵘ 䗖⫼ᗻ Ѧᓩ⫼ˈҢ㗠Փ݊㗺ড়ᵒᬷˈ㗠Ϩৃҹ⣀ゟഄᬍবᅗӀП䯈ⱘѸѦDŽ ⫼ϔϾЁҟᇍ䈵ᴹᇕ㺙ϔ㋏߫ⱘᇍ䈵ѸѦDŽЁҟ㗙Փ৘ᇍ䈵ϡ䳔㽕ᰒᓣഄⳌ 1.3.5 Ёҟ㗙῵ᓣ˄Mediator˅ ҹ⣀ゟѢ㘮䲚㾦㡆ব࣪DŽ ϾҹϞⱘ䗁ҷᄤᇍ䈵ˈ↣ϔϾ䗁ҷᄤⱘ䗁ҷ⢊ᗕৃҹᰃᕐℸ⣀ゟⱘDŽ䗁ҷㅫ⊩ৃ 䲚ᴀ䑿䱨ᓔDŽ䗁ҷᄤ῵ᓣㅔ࣪њ㘮䲚ⱘ⬠䴶DŽ↣ϔϾ㘮䲚ᇍ䈵䛑ৃҹ᳝ϔϾ៪ϔ ᇍ䈵ⱘᆍ఼ᇍ䈵DŽ䗁ҷᄤ῵ᓣᇚ䗁ҷ䘏䕥ᇕ㺙ࠄϔϾ⣀ゟⱘᄤᇍ䈵ЁˈҢ㗠Ϣ㘮 ݙ䚼㸼䈵DŽ໮Ͼᇍ䈵㘮೼ϔ䍋ᔶ៤ⱘᘏԧ⿄ПЎ㘮䲚ˈ㘮䲚ᇍ䈵ᰃ㛑໳ࣙᆍϔ㒘 ㋴㗠ϡᖙᲈ䴆㘮䲚ⱘܗ 䗁ҷᄤ῵ᓣ˖䗁ҷᄤ῵ᓣৃҹ乎ᑣ䆓䯂ϔϾ㘮䲚Ёⱘ ៥㛥㹟வⱘϔໄˈത೼ộᄤϞˈϔ઀⠭˖Ā៥фˈ៥фˈ䖬᳝৫˛” Mary˖Ā៥ⳟϞ䙷䕚⊩ᢝ߽䎥䔺” ៥˖Ā៥фˈ៥фˈ䖬᳝৫˛” Mary˖Ā៥ⳟϞњ␪䖍ⱘ䙷ᷟ߿๙” ៥˖Ā៥фˈ៥фˈ䖬᳝৫˛” ᢝⱘ䪏⷇”ܟ Mary˖Ā៥ⳟϞњ䙷Ͼϔ ៥˖ĀҔМᴵӊ៥䛑ㄨᑨˈԴ䇈৻” Mary˖Āᛇ㽕៥䎳Դ㒧ီˈᕫㄨᑨ៥ⱘᴵӊ”  ៥⠅Ϟњ Maryˈϡ乒ϔߛⱘ৥ཌྷ∖ီDŽ ᔶ䈵↨ஏ˖ c b 55 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ //ᅶ᠋ᴹњ, 䗮ⶹࠡৄ B if (content.equals("client")) { } ca.action(); //㗕ᵓᴹњ, 䗮ⶹਬᎹ A if (content.equals("boss")) { public void notice(String content) { } cb = new ColleagueB(); ca = new ColleagueA(); public ConcreteMediator() { private ColleagueB cb; private ColleagueA ca; public class ConcreteMediator extends Mediator { ConcreteMediator } public abstract void notice(String content); public abstract class Mediator { Mediator ՟ᄤ C:\Users\fulin\AppData\Local\Temp\404683\images\Mediator.jpg ㉏೒ ϢᅗⱘЁҟ㗙䗮ֵ ↣ϔϾৠџᇍ䈵೼䳔Ϣ݊Ҫⱘৠџ䗮ֵⱘᯊ׭ ↣ϔϾৠџ㉏䛑ⶹ䘧ᅗⱘЁҟ㗙ᇍ䈵DŽ 3.Colleagueclass њ㾷ᑊ㓈ᡸᅗⱘ৘ϾৠџDŽ 56 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ ড়DŽᔧᶤѯᇍ䈵П䯈ⱘ԰⫼থ⫳ᬍবيϡᖙⳌѦᯢᰒ԰⫼DŽҢ㗠ՓҪӀৃҹᵒᬷ 㗙῵ᓣࣙ㺙њϔ㋏߫ᇍ䈵ⳌѦ԰⫼ⱘᮍᓣˈՓᕫ䖭ѯᇍ䈵ذ㗙῵ᓣ˖䇗ذ 䇗 ៥ˈϔߛህ OK ଺ˈ׎ᕫࠄњಯϾ MM ⱘ⬉䆱DŽ ᯊ៥೼ᮕ䖍ˈᣝ✻৘㞾ⱘㅍⷕ᭄ㅫ䪅ˈ䌮њ䪅ⱘҢ៥䖭䞠ᣓˈ䌨њ䪅ⱘгҬ㒭 MEDIATOR—ಯϾ MM ᠧ咏ᇚˈⳌѦП䯈䇕ᑨ䆹㒭䇕໮ᇥ䪅ㅫϡ⏙Ἦњˈᑌѣᔧ ᔶ䈵↨ஏ˖ ࠡৄ⊼ᛣњ! ᱂䗮ਬᎹࡾ࡯Ꮉ԰ result } } med.notice("client"); //ᅶ᠋ᴹњ med.notice("boss"); //㗕ᵓᴹњ Mediator med = new ConcreteMediator(); public static void main(String[] args) { public class Test { Test } } System.out.println("ࠡৄ⊼ᛣњ!"); public void action() { public class ColleagueB extends Colleague { } } System.out.println("᱂䗮ਬᎹࡾ࡯Ꮉ԰"); public void action() { public class ColleagueA extends colleague { Colleagueclass } } } cb.action(); 57 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ খϢ㗙 㡖ᑊ⸈ണᇍ䈵ⱘᇕ㺙ᗻDŽ 2.བᵰϔϾ⫼᥹ষᴹ䅽݊ᅗᇍ䈵Ⳉ᥹ᕫࠄ䖭ѯ⢊ᗕˈᇚӮᲈ䴆ᇍ䈵ⱘᅲ⦄㒚 ࠡⱘ⢊ᗕDŽܜ໡ࠄ 䚼ߚ ⢊ᗕ䖭ḋҹৢ䳔㽕ᯊᅗᠡ㛑ᘶ 1.ᖙ乏ֱᄬϔϾᇍ䈵೼ᶤϔϾᯊࠏⱘ 䗖⫼ᗻ ᄬⱘ⢊ᗕDŽֱܜ䖭Ͼ⢊ᗕDŽ䖭ḋҹৢህৃᇚ䆹ᇍ䈵ᘶ໡ࠄॳ ೼ϡ⸈ണᇕ㺙ᗻⱘࠡᦤϟˈᤩ㦋ϔϾᇍ䈵ⱘݙ䚼⢊ᗕˈᑊ೼䆹ᇍ䈵П໪ֱᄬ 1.3.6 ໛ᖬᔩ῵ᓣ˄Memento˅ ⧚DŽ 䈵ⱘ㸠Ў੠ण԰ᢑ䈵࣪ˈᡞᇍ䈵೼ᇣሎᑺⱘ㸠ЎϞϢ݊Ҫᇍ䈵ⱘⳌѦ԰⫼ߚᓔ໘ 㗙῵ᓣᇚᇍذ㗙῵ᓣᇚ໮ᇍ໮ⱘⳌѦ԰⫼䕀࣪Ўϔᇍ໮ⱘⳌѦ԰⫼DŽ䇗ذ࣪DŽ䇗 ᯊˈϡӮゟेᕅડ݊Ҫⱘϔѯᇍ䈵П䯈ⱘ԰⫼DŽֱ䆕䖭ѯ԰⫼ৃҹᕐℸ⣀ゟⱘব 58 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ { ; this.state = state public void setState(String state) { } return state; public String getState() { } this.state = state; public Memento(String state) { private String state; public class Memento { Memento ՟ᄤ C:\Users\fulin\AppData\Local\Temp\404683\images\Memento.jpg ㉏೒ 䖯㸠᪡԰៪ẔᶹDŽ ϡ㛑ᇍ໛ᖬᔩⱘݙ 䋳䋷ֱᄬད໛ᖬᔩDŽ 3.Caretaker Փ⫼໛ᖬᔩᘶ໡ݙ䚼⢊ᗕ. ⱘݙ䚼⢊ᗕDŽ ॳথ఼߯ᓎϔϾ໛ᖬᔩ⫼ҹ䆄ᔩᔧࠡᯊࠏ 2.Originator ॳথ఼ᇍ䈵ⱘݙ䚼⢊ᗕDŽټ ໛ᖬᔩᄬ 1.Memento 59 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 60 } Originator public class Originator { private String state; public String getState() { return state; } public void setState(String state) { this.state = state; } public Memento createMemento() { return new Memento(state); } public void setMemento(Memento memento) { state = memento.getState(); } public void showState(){ System.out.println(state); } } Caretaker public class Caretaker { private Memento memento; public Memento getMemento(){ return this.memento; } public void setMemento(Memento memento){ this.memento = memento; } } Test public class Test { ᕙᬍবDŽ 2.ᔧᇍϔϾᇍ䈵ⱘᬍব䳔㽕ৠᯊᬍব݊ᅗᇍ䈵㗠ϡⶹ䘧݋ԧ᳝໮ᇥᇍ䈵᳝ ᇚ䖭Ѡ㗙ᇕ㺙೼⣀ゟⱘᇍ䈵ЁҹՓᅗӀৃҹ৘㞾⣀ゟഄᬍব੠໡⫼DŽ 1.ᔧϔϾᢑ䈵῵ൟ᳝ϸϾᮍ䴶݊ЁϔϾᮍ䴶ձ䌪Ѣ঺ϔᮍ䴶DŽ 䗖⫼ᗻ 䌪Ѣᅗⱘᇍ䈵䛑ᕫࠄ䗮ⶹᑊ㹿㞾ࡼ᳈ᮄDŽ ᅮНᇍ䈵䯈ⱘϔ⾡ϔᇍ໮ⱘձ䌪݇㋏ᔧϔϾᇍ䈵ⱘ⢊ᗕথ⫳ᬍবᯊ᠔᳝ձ 1.3.7 㾖ᆳ㗙῵ᓣ˄Observer˅ ᗕDŽ 䍋ᴹⱘ⢊ټ䍋ᴹˈҢ㗠ৃҹ೼ᇚᴹড়䗖ⱘᯊ׭ᡞ䖭Ͼᇍ䈵䖬ॳࠄᄬټ䚼࣪ˈᄬ 䈵DŽ໛ᖬᔩ῵ᓣⱘ⫼ᛣᰃ೼ϡ⸈ണᇕ㺙ⱘᴵӊϟˈᇚϔϾᇍ䈵ⱘ⢊ᗕᤝԣˈᑊ໪ ঺໪ϔϾᇍ䈵ݙ䚼⢊ᗕⱘᖿ✻ⱘᇍټ ໛ᖬᔩ῵ᓣ˖໛ᖬᔩᇍ䈵ᰃϔϾ⫼ᴹᄬ ᣋ䋱ϔӑᬒࠄ໛ᖬᔩ䞠䴶ֱᄬˈ䖭ḋৃҹ䱣ᯊᆳⳟҹࠡⱘ䆄ᔩ଺DŽ ✊ MM থ⦄њӮϡ催݈ⱘ઺ˈᑌѣ៥᳝Ͼ໛ᖬᔩˈ߮ᠡϢાϾ MM 䇈њҔМ䆱៥䛑 MEMENTO—ৠᯊ䎳޴Ͼ MM 㘞໽ᯊˈϔᅮ㽕䆄⏙Ἦ߮ᠡ䎳 MM 䇈њѯҔМ䆱ˈϡ ᔶ䈵↨ஏ˖ ᓔӮЁ ⴵ㾝Ё result } } org.showState(); org.setMemento(ctk.getMemento());//ᇚ᭄᥂䞡ᮄᇐܹ org.showState();//ᰒ⼎ org.setState("ⴵ㾝Ё"); ctk.setMemento(org.createMemento());//ᇚ᭄᥂ᇕ㺙೼ Caretaker Caretaker ctk = new Caretaker(); org.setState("ᓔӮЁ"); Originator org = new Originator(); public static void main(String[] args) { 61 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ ; List pols public abstract class Citizen { Subject ՟ᄤ C:\Users\fulin\AppData\Local\Temp\404683\images\Observer.jpg ㉏೒ Փ㞾䑿⢊ᗕϢⳂᷛⱘ⢊ᗕֱᣕϔ㟈 ᅲ⦄ Observer ⱘ᳈ᮄ᥹ষ ᳝݇⢊ᗕˈ䖭ѯ⢊ᗕᑨϢⳂᷛⱘ⢊ᗕֱᣕϔ㟈DŽټ ᄬ 㓈ᡸϔϾᣛ৥ ConcreteSubject ᇍ䈵ⱘᓩ⫼DŽ 4.ConcreteObserver˄݋ԧ㾖ᆳ㗙˅ ᔧᅗⱘ⢊ᗕথ⫳ᬍবᯊ৥ᅗⱘ৘Ͼ㾖ᆳ㗙থߎ䗮ⶹDŽ ᇚ᳝݇⢊ᗕᄬܹ৘ ConcreteObserver ᇍ䈵DŽ 3.ConcreteSubject˄݋ԧⳂᷛ˅ Ў䙷ѯ೼Ⳃᷛথ⫳ᬍবᯊ䳔㦋ᕫ䗮ⶹⱘᇍ䈵ᅮНϔϾ᳈ᮄ᥹ষDŽ 2.Observer˄㾖ᆳ㗙˅ ᦤկ⊼ݠ੠ߴ䰸㾖ᆳ㗙ᇍ䈵ⱘ᥹ষDŽ Ⳃᷛⶹ䘧ᅗⱘ㾖ᆳ㗙DŽৃҹ᳝ӏᛣ໮Ͼ㾖ᆳ㗙㾖ᆳৠϔϾⳂᷛDŽ 1.Subject˄Ⳃᷛ˅ খϢ㗙 ᅮ݊ᅗᇍ䈵ᰃ䇕DŽ؛ 3.ᔧϔϾᇍ䈵ᖙ乏䗮ⶹ݊ᅗᇍ䈵ˈ㗠ᅗজϡ㛑 62 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 63 String help = "normal"; public void setHelp(String help) { this.help = help; } public String getHelp() { return this.help; } abstract void sendMessage(String help); public void setPolicemen() { this.pols = new ArrayList(); } public void register(policeman pol) { this.pols.add(pol); } public void unRegister(Policeman pol) { this.pols.remove(pol); } } Observer public interface Policeman { void action(Citizen ci); } ConcreteSubject public class HuangPuCitizen extends Citizen { public HuangPuCitizen(Policeman pol) { setPolicemen(); register(pol); } public void sendMessage(String help) { setHelp(help); for(int i = 0; i < pols.size(); i++) { Policeman pol = pols.get(i); //䗮ⶹ䄺ᆳ㸠ࡼ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 64 pol.action(this); } } } public class TianHeCitizen extends Citizen { public TianHeCitizen(Policeman pol) { setPolicemen(); register(pol); } public void sendMessage(String help) { setHelp(help); for (int i = 0; i < pols.size(); i++) { Policeman pol = pols.get(i); //䗮ⶹ䄺ᆳ㸠ࡼ pol.action(this); } } } ConcreteObserver public class HuangPuPoliceman implements Policeman { public void action(Citizen ci) { String help = ci.getHelp(); if (help.equals("normal")) { System.out.println("ϔߛℷᐌ, ϡ⫼ߎࡼ"); } if (help.equals("unnormal")) { System.out.println("᳝⢃㔾㸠Ў, 咘ඨ䄺ᆳߎࡼ!"); } } } public class TianHePoliceman implements Policeman { public void action(Citizen ci) { String help = ci.getHelp(); if (help.equals("normal")) { System.out.println("ϔߛℷᐌ, ϡ⫼ߎࡼ"); } if (help.equals("unnormal")) { Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 65 System.out.println("᳝⢃㔾㸠Ў, ໽⊇䄺ᆳߎࡼ!"); } } } Test public class Test{ public static void main(String[] args) { Policeman thPol = new TianHePoliceman(); Policeman hpPol = new HuangPuPoliceman(); Citizen citizen = new HuangPuCitizen(hpPol); citizen.sendMessage("unnormal"); citizen.sendMessage("normal"); System.out.println("==========="); citizen = new TianHeCitizen(thPol); citizen.sendMessage("normal"); citizen.sendMessage("unnormal"); } } result ᳝⢃㔾㸠Ў, 咘ඨ䄺ᆳߎࡼ! ϔߛℷᐌ, ϡ⫼ߎࡼ =========== ϔߛℷᐌ, ϡ⫼ߎࡼ ᳝⢃㔾㸠Ў, ໽⊇䄺ᆳߎࡼ! ᔶ䈵↨ஏ˖ OBSERVER—ᛇⶹ䘧અӀ݀ৌ᳔ᮄ MM ᚙ᡹৫˛ࡴܹ݀ৌⱘ MM ᚙ᡹䚂ӊ㒘ህ㸠 њˈtom 䋳䋷᧰䲚ᚙ᡹ˈҪথ⦄ⱘᮄᚙ᡹ϡ⫼ϔϾϔϾ䗮ⶹ៥ӀˈⳈ᥹থᏗ㒭䚂 ӊ㒘ˈ៥Ӏ԰Ў䅶䯙㗙˄㾖ᆳ㗙˅ህৃҹঞᯊᬊࠄᚙ᡹଺ 㾖ᆳ㗙῵ᓣ˖㾖ᆳ㗙῵ᓣᅮНњϔ⾡ϔᇍ໮ⱘձ䌪݇㋏ˈ䅽໮Ͼ㾖ᆳ㗙ᇍ䈵 ৠᯊⲥ਀ᶤϔϾЏ乬ᇍ䈵DŽ䖭ϾЏ乬ᇍ䈵೼⢊ᗕϞথ⫳ব࣪ᯊˈӮ䗮ⶹ᠔᳝㾖ᆳ 㗙ᇍ䈵ˈՓҪӀ㛑໳㞾ࡼ᳈ᮄ㞾ᏅDŽ } public class Context Context ՟ᄤ C:\Users\fulin\AppData\Local\Temp\404683\images\State.jpg ㉏೒ ݇ⱘ㸠ЎDŽ ↣ϔᄤ㉏ᅲ⦄ϔϾϢ Context ⱘϔϾ⢊ᗕ 3.ConcreteStatesubclasses ᅮНϔϾ᥹ষҹᇕ㺙Ϣ Context ⱘϔϾ⡍ᅮ⢊ᗕⳌ݇ⱘ㸠ЎDŽ 2.State 㓈ᡸϔϾ ConcreteState ᄤ㉏ⱘᅲ՟ˈ䖭Ͼᅲ՟ᅮНᔧࠡ⢊ᗕDŽ ᅮНᅶ᠋ᛳ݈䍷ⱘ᥹ষDŽ 1.Context খϢ㗙 ৃҹϡձ䌪Ѣ݊Ҫᇍ䈵㗠⣀ゟব࣪DŽ ᇚᇍ䈵ⱘ⢊ᗕ԰ЎϔϾᇍ䈵ˈ䖭ϔᇍ䈵މ 䖭ՓᕫԴৃҹḍ᥂ᇍ䈵㞾䑿ⱘᚙ State ῵ᓣᇚ↣ϔϾᴵӊߚᬃᬒܹϔϾ⣀ゟⱘ㉏ЁDŽ 䗮ᐌ᳝໮Ͼ᪡԰ࣙ৿䖭ϔⳌৠⱘᴵӊ㒧ᵘDŽ 䖭Ͼ⢊ᗕ䗮ᐌ⫼ϔϾ៪໮ϾᵮВᐌ䞣㸼⼎DŽ ᗕDŽ 2.ϔϾ᪡԰Ё৿᳝ᑲ໻ⱘ໮ߚᬃⱘᴵӊ䇁হˈϨ䖭ѯߚᬃձ䌪Ѣ䆹ᇍ䈵ⱘ⢊ ⱘ㸠ЎDŽ Ѣᅗⱘ⢊ᗕᑊϨᅗᖙ乏೼䖤㸠ᯊࠏḍ᥂⢊ᗕᬍবᅗއ 1.ϔϾᇍ䈵ⱘ㸠Ўপ 䗖⫼ᗻ ⱘ㉏DŽ 䆌ϔϾᇍ䈵೼݊ݙ䚼⢊ᗕᬍবᯊᬍবᅗⱘ㸠ЎDŽᇍ䈵ⳟ䍋ᴹԐТׂᬍњᅗܕ ˅1.3.8 ⢊ᗕ῵ᓣ˄State 66 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 67 private Weather weather; public void setWeather(Weather weather) { this.weather = weather; } public Weather getWeather() { return this.weather; } public String weatherMessage() { return weather.getWeather(); } } State public interface Weather { String getWeather(); } Concrete*tatesubclasses public class Rain implements Weather { public String getWeather() { return "ϟ䲼"; } } public class Sunshine implements Weather { public String getWeather() { return "䰇ܝ"; } } Test public class Test{ public static void main(String[] args) { Context ctx1 = new Context(); ctx1.setWeather(new Sunshine()); 2.䳔㽕Փ⫼ϔϾㅫ⊩ⱘϡৠবԧDŽ 㸠Ўᴹ䜡㕂ϔϾ㉏ⱘᮍ⊩DŽ 1.䆌໮Ⳍ݇ⱘ㉏ҙҙᰃ㸠Ў᳝ᓖDŽĀㄪ⬹āᦤկњϔ⾡⫼໮Ͼ㸠ЎЁⱘϔϾ 䗖⫼ᗻ Փᕫㅫ⊩ৃ⣀ゟѢՓ⫼ᅗⱘᅶ᠋㗠ব࣪DŽ ᅮНϔ㋏߫ⱘㅫ⊩ᡞᅗӀϔϾϾᇕ㺙䍋ᴹᑊϨՓᅗӀৃⳌѦ᳓ᤶDŽᴀ῵ᓣ 1.3.9 ㄪ⬹῵ᓣ˄Strategy˅ 㒳֓ᬍব᠔䗝ⱘᄤ㉏DŽ ᇍ↣ϔϾ㋏㒳ৃ㛑পᕫⱘ⢊ᗕ߯ゟϔϾ⢊ᗕ㉏ⱘᄤ㉏DŽᔧ㋏㒳ⱘ⢊ᗕব࣪ᯊˈ㋏ ᛣ೒ᰃ䅽ϔϾᇍ䈵೼݊ݙ䚼⢊ᗕᬍবⱘᯊ׭ˈ݊㸠Ўг䱣ПᬍবDŽ⢊ᗕ῵ᓣ䳔㽕 ⱘ⢊ᗕᇍ䈵䞠ˈ↣ϔϾ⢊ᗕᇍ䈵䛑ሲѢϔϾᢑ䈵⢊ᗕ㉏ⱘϔϾᄤ㉏DŽ⢊ᗕ῵ᓣⱘ ᇍ䈵ⳟϞএ䈵ᰃᬍবњᅗⱘ㉏ϔḋDŽ⢊ᗕ῵ᓣᡞ᠔ⷨおⱘᇍ䈵ⱘ㸠Ўࣙ㺙೼ϡৠ 䆌ϔϾᇍ䈵೼݊ݙ䚼⢊ᗕᬍবⱘᯊ׭ᬍব㸠ЎDŽ䖭Ͼܕ ⢊ᗕ῵ᓣ˖⢊ᗕ῵ᓣ ઺DŽ ✊Դⳟ⬉ᕅ䖛⿟Ё㸼⦄㡃དⱘ䆱ˈгৃҹᡞ MM ⱘ⢊ᗕҢϡ䅼ॠϡ୰⃶ব៤୰⃶ М˛āˈᏆ㒣୰⃶ϞԴⱘMMህӮ䇈Ā޴⚍䩳˛ⳟᅠ⬉ᕅݡএ⊵৻ᗢМḋ˛āˈᔧ ଺āˈᇍԴϡ䅼ॠԚ䖬≵୰⃶Ϟⱘ MM ህӮ䇈Āདଞˈϡ䖛ৃҹᏺϞ៥ৠџ ᳝ϡৠˈ↨བԴ㑺ཌྷҞ໽ᰮϞএⳟ⬉ᕅˈᇍԴ≵݈䍷ⱘ MM ህӮ䇈 “᳝џᚙ STATE—䎳 MM Ѹᕔᯊˈϔᅮ㽕⊼ᛣཌྷⱘ⢊ᗕ઺ˈ೼ϡৠⱘ⢊ᗕᯊཌྷⱘ㸠ЎӮ ᔶ䈵↨ஏ˖ ϟ䲼 =============== ܝ䰇 result } } System.out.println(ctx2.weatherMessage()); ctx2.setWeather(new Rain()); Context ctx2 = new Context(); System.out.println("==============="); System.out.println(ctx1.weatherMessage()); 68 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 69 3.ㅫ⊩Փ⫼ᅶ᠋ϡᑨ䆹ⶹ䘧ⱘ᭄᥂DŽৃՓ⫼ㄪ⬹῵ᓣҹ䙓ܡᲈ䴆໡ᴖⱘǃϢ ㅫ⊩Ⳍ݇ⱘ᭄᥂㒧ᵘDŽ 4.ϔϾ㉏ᅮНњ໮⾡㸠ЎᑊϨ䖭ѯ㸠Ў೼䖭Ͼ㉏ⱘ᪡԰Ёҹ໮Ͼᴵӊ䇁হ ⱘᔶᓣߎ⦄DŽ ᇚⳌ݇ⱘᴵӊߚᬃ⿏ܹᅗӀ৘㞾ⱘ Strategy ㉏Ёҹҷ᳓䖭ѯᴵӊ䇁হDŽ খϢ㗙 1.Strategy ᅮН᠔᳝ᬃᣕⱘㅫ⊩ⱘ݀݅᥹ষDŽContext Փ⫼䖭Ͼ᥹ষᴹ䇗⫼ᶤ ConcreteStrategy ᅮНⱘㅫ⊩DŽ 2.ConcreteStrategy ҹ Strategy ᥹ষᅲ⦄ᶤ݋ԧㅫ⊩DŽ 3.Context ⫼ϔϾ ConcreteStrateg*ᇍ䈵ᴹ䜡㕂DŽ 㓈ᡸϔϾᇍ Strategy ᇍ䈵ⱘᓩ⫼DŽ ৃᅮНϔϾ᥹ষᴹ䅽 Stategy 䆓䯂ᅗⱘ᭄᥂DŽ ㉏೒ C:\Users\fulin\AppData\Local\Temp\404683\images\Strategy.jpg ՟ᄤ Strategy public abstract class Strategy { public abstract void method(); } ConcreteStrategy public class StrategyImplA extends Strategy { public void method() { System.out.println("䖭ᰃ㄀ϔϾᅲ⦄"); } Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 70 } public class StrategyImplB extends Strategy { public void method() { System.out.println("䖭ᰃ㄀ѠϾᅲ⦄"); } } public class StrategyImplC extends Strategy { public void method() { System.out.println("䖭ᰃ㄀ϝϾᅲ⦄"); } } Context public class Context { Strategy stra; public Context(Strategy stra) { this.stra = stra; } public void doMethod() { stra.method(); } } Test public class Test { public static void main(String[] args) { Context ctx = new Context(new StrategyImplA()); ctx.doMethod(); ctx = new Context(new strategyImplB()); ctx.doMethod(); ctx = new Context(new StrategyImplC()); ctx.doMethod(); } } result Ёⱘ᪡԰DŽ 䆹῵ᵓᮍ⊩ϡҙ䇗⫼ॳ䇁᪡԰ˈг䇗⫼ᅮН೼ AbstractClass ៪݊Ҫᇍ䈵 ᅲ⦄ϔϾ῵ᵓᮍ⊩ᅮНϔϾㅫ⊩ⱘ偼ᶊDŽ ҹᅲ⦄ϔϾㅫ⊩ⱘ৘ℹ偸DŽ ᅮНᢑ䈵ⱘॳ䇁᪡԰˄primitiveoperation˅ˈ݋ԧⱘᄤ㉏ᇚ䞡ᅮНᅗӀ 1.AbstractClass খϢ㗙 3.᥻ࠊᄤ㉏ᠽሩDŽ ᳔ৢˈ⫼ϔϾ䇗⫼䖭ѯᮄⱘ᪡԰ⱘ῵ᵓᮍ⊩ᴹ᳓ᤶ䖭ѯϡৠⱘҷⷕDŽ 䆚߿⦄᳝ҷⷕЁⱘϡৠП໘ˈᑊϨᇚϡৠП໘ߚ⾏Ўᮄⱘ᪡԰DŽܜ 佪 ໡DŽ ҷⷕ䞡ܡ 2.৘ᄤ㉏Ё݀݅ⱘ㸠Ўᑨ㹿ᦤপߎᴹᑊ䲚ЁࠄϔϾ݀݅⠊㉏Ёҹ䙓 1.ϔ⃵ᗻᅲ⦄ϔϾㅫ⊩ⱘϡবⱘ䚼ߚˈᑊᇚৃবⱘ㸠Ў⬭㒭ᄤ㉏ᴹᅲ⦄DŽ 䗖⫼ᗻ ѯ⡍ᅮℹ偸DŽ TemplateMethod Փᕫᄤ㉏ৃҹϡᬍবϔϾㅫ⊩ⱘ㒧ᵘेৃ䞡ᅮН䆹ㅫ⊩ⱘᶤ ᅮНϔϾ᪡԰Ёⱘㅫ⊩ⱘ偼ᶊˈ㗠ᇚϔѯℹ偸ᓊ䖳ࠄᄤ㉏ЁDŽ 1.3.10 ῵ᵓᮍ⊩˄TemplateMethod˅ ᬍ䛑ϡӮᕅડࠄ⦃๗੠ᅶ᠋ッDŽׂ ˈޣ㉏ˈ৘⾡ㅫ⊩೼݋ԧⱘㄪ⬹㉏ЁᦤկDŽ⬅Ѣㅫ⊩੠⦃๗⣀ゟᓔᴹˈㅫ⊩ⱘ๲ ϟথ⫳ব࣪DŽㄪ⬹῵ᓣᡞ㸠Ў੠⦃๗ߚᓔDŽ⦃๗㉏䋳䋷㓈ᣕ੠ᶹ䆶㸠Ўމッⱘᚙ ゟⱘ㉏ЁˈҢ㗠ՓᕫᅗӀৃҹⳌѦ᳓ᤶDŽㄪ⬹῵ᓣՓᕫㅫ⊩ৃҹ೼ϡᕅડࠄᅶ᠋ ㄪ⬹῵ᓣ˖ㄪ⬹῵ᓣ䩜ᇍϔ㒘ㅫ⊩ˈᇚ↣ϔϾㅫ⊩ᇕ㺙ࠄ݋᳝݅ৠ᥹ষⱘ⣀ ᖗˈ៥ⱘ䗑 MM 䫺ಞЁ᳝ད໮ Strategy ઺DŽ ⱘ߭এৗᇣৗᬜᵰϡ䫭ˈ᳝ⱘএ⍋䖍⌾⓿᳔ড়䗖ˈऩⳂⱘ䛑ᰃЎњᕫࠄ MM ⱘ㢇 STRATEGY—䎳ϡৠ㉏ൟⱘ MM 㑺Ӯˈ㽕⫼ϡৠⱘㄪ⬹ˈ᳝ⱘ䇋⬉ᕅ↨䕗དˈ᳝ ᔶ䈵↨ஏ˖ 䖭ᰃ㄀ϝϾᅲ⦄ 䖭ᰃ㄀ѠϾᅲ⦄ 䖭ᰃ㄀ϔϾᅲ⦄ 71 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 72 2.ConcreteClass ᅲ⦄ 䇁᪡԰ҹᅠ៤ㅫ⊩ЁϢ⡍ᅮᄤ㉏Ⳍ݇ⱘℹ偸DŽ ㉏೒ C:\Users\fulin\AppData\Local\Temp\404683\images\TemplateMethod.jpg ՟ᄤ AbstractClass public abstract class Template { public abstract void print(); public void update() { System.out.println("ᓔྟᠧॄ"); for (int i = 0; i < 10; i++) { print(); } } } ConcreteClass public class TemplateConcrete extends Template { @*verride public void print() { System.out.println("䖭ᰃᄤ㉏ⱘᅲ⦄"); } } Test public class Test { public static void main(String[] args) { Template temp = new TemplateConcrete(); temp.update(); } } result བᵰᇍ䈵㒧ᵘ㉏㒣ᐌᬍবˈ䙷Мৃ㛑䖬ᰃ೼䖭ѯ㉏ЁᅮН䖭ѯ᪡԰䕗དDŽ ӋDŽ 㽕ᕜ໻ⱘҷ ᬍবᇍ䈵㒧ᵘ㉏䳔㽕䞡ᅮНᇍ᠔᳝䆓䯂㗙ⱘ᥹ষˈ䖭ৃ㛑 3.ᅮНᇍ䈵㒧ᵘⱘ㉏ᕜᇥᬍবˈԚ㒣ᐌ䳔㽕೼ℸ㒧ᵘϞᅮНᮄⱘ᪡԰DŽ ⫼ࠄⱘ᪡԰DŽ ᔧ䆹ᇍ䈵㒧ᵘ㹿ᕜ໮ᑨ⫼݅ѿᯊˈ⫼ Visitor ῵ᓣ䅽↣Ͼᑨ⫼ҙࣙ৿䳔㽕 Visitor ՓᕫԴৃҹᇚⳌ݇ⱘ᪡԰䲚Ё䍋ᴹᅮН೼ϔϾ㉏ЁDŽ 䅽䖭ѯ᪡԰Ā∵ᶧā䖭ѯᇍ䈵ⱘ㉏DŽܡ䙓 Դᛇ 2.䳔㽕ᇍϔϾᇍ䈵㒧ᵘЁⱘᇍ䈵䖯㸠ᕜ໮ϡৠⱘᑊϨϡⳌ݇ⱘ᪡԰ˈ DŽ ᮑϔѯձ䌪Ѣ݊݋ԧ㉏ⱘ᪡ 1.ϔϾᇍ䈵㒧ᵘࣙ৿ᕜ໮㉏ᇍ䈵ˈᅗӀ᳝ϡৠⱘ᥹ষˈ㗠Դᛇᇍ䖭ѯᇍ䈵ᅲ 䗖⫼ᗻ ㋴ⱘᮄ᪡԰DŽܗ㋴ⱘ㉏ⱘࠡᦤϟᅮН԰⫼Ѣ䖭ѯܗ ᅗՓԴৃҹ೼ϡᬍব৘ ㋴ⱘ᪡԰DŽܗ 㸼⼎ϔϾ԰⫼Ѣᶤᇍ䈵㒧ᵘЁⱘ৘ 1.3.11 䆓䯂㗙῵ᓣ˄Visitor˅ ࠊᅮϔϾ乊㑻䘏䕥Ḛᶊˈ㗠ᇚ䘏䕥ⱘ㒚㡖⬭㒭݋ԧⱘᄤ㉏এᅲ⦄DŽܜ⦄DŽ ৠⱘᄤ㉏ৃҹҹϡৠⱘᮍᓣᅲ⦄䖭ѯᢑ䈵ᮍ⊩ˈҢ㗠ᇍ࠽ԭⱘ䘏䕥᳝ϡৠⱘᅲ ݋ԧᵘ䗴ᄤⱘᔶᓣᅲ⦄ˈ✊ৢໄᯢϔѯᢑ䈵ᮍ⊩ᴹ䖿Փᄤ㉏ᅲ⦄࠽ԭⱘ䘏䕥DŽϡ ໛ϔϾᢑ䈵㉏ˈᇚ䚼ߚ䘏䕥ҹ݋ԧᮍ⊩ҹঞޚ ῵ᵓᮍ⊩῵ᓣ˖῵ᵓᮍ⊩῵ᓣ ݋ԧᅲ⦄ ˗ 䖭ህ㽕ⳟԴ䱣ᴎᑨব଺ˈ⫣خⱘ 䛑᳝ϡϔḋˈމᡮǃ䖯এܿ໻ℹ偸(Template method)ˈԚ↣Ͼℹ偸䩜ᇍϡৠⱘᚙ ሔǃሩᓔ䗑∖ǃ᥹ਏǃࠡ៣ǃࡼ᠟ǃ⠅ۉ䆚ࠄϞᑞⱘϡবⱘℹ偸ߚЎᎻ䘛ǃᠧ⸈ TEMPLATE METHOD——ⳟ䖛ljབԩ䇈᳡ཇ⫳ϞᑞNJ䖭䚼㒣݌᭛ゴ৫˛ཇ⫳Ң䅸 䖭ᰃᄤ㉏ⱘᅲ⦄ 䖭ᰃᄤ㉏ⱘᅲ⦄ 䖭ᰃᄤ㉏ⱘᅲ⦄ 䖭ᰃᄤ㉏ⱘᅲ⦄ 䖭ᰃᄤ㉏ⱘᅲ⦄ 䖭ᰃᄤ㉏ⱘᅲ⦄ 䖭ᰃᄤ㉏ⱘᅲ⦄ 䖭ᰃᄤ㉏ⱘᅲ⦄ 䖭ᰃᄤ㉏ⱘᅲ⦄ 䖭ᰃᄤ㉏ⱘᅲ⦄ ᓔྟᠧॄ 73 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 74 খϢ㗙 1.Visitor Ў䆹ᇍ䈵㒧ᵘЁ ConcreteEle*ent ⱘ↣ϔϾ㉏ໄᯢϔϾ Visit ᪡԰DŽ 䆹᪡԰ⱘৡᄫ੠⡍ᕕᷛ䆚њথ䗕*isit 䇋∖㒭䆹䆓䯂㗙ⱘ䙷Ͼ㉏DŽ 䖭Փᕫ䆓䯂㗙ৃҹ⹂ᅮℷ㹿䆓䯂ܗ㋴ ݋ԧⱘ㉏DŽ 䖭ḋ䆓䯂㗙ህৃҹ䗮䖛䆹ܗ㋴ⱘ⡍ᅮ᥹ষⳈ 䆓䯂ᅗDŽ 2.Concret*Visitor ᅲ⦄↣Ͼ⬅ Visitor ໄᯢⱘ᪡԰DŽ ↣Ͼ᪡԰ᅲ⦄ᴀㅫ⊩ⱘϔ䚼ߚˈ㗠䆹ㅫ⊩⠛ᮁЗᰃᇍᑨѢ㒧ᵘЁᇍ䈵ⱘ ㉏DŽ Concret*Visitor*䆹ㅫ⊩ᦤկњϞϟ᭛ᑊᄬ ᅗⱘሔ䚼⢊ᗕDŽ 䖭ϔ⢊ᗕᐌᐌ೼䘡ग़䆹㒧ᵘⱘ䖛⿟Ё㌃ 㒧ᵰDŽ 3.Element ᅮНϔϾ Accept ᪡԰ˈᅗ ϔϾ䆓䯂㗙Ўখ᭄DŽ 4.ConcreteElement ᅲ⦄ Accept ᪡԰ˈ䆹᪡԰ҹϔϾ䆓䯂㗙Ўখ᭄DŽ 5.ObjectStru*ture 㛑ᵮВᅗⱘܗ㋴DŽ *ҹᦤկϔϾ催ሖⱘ᥹ষҹܕ䆌䆹䆓䯂㗙䆓䯂ᅗⱘܗ㋴DŽ ৃҹᰃϔϾ໡ড়៪ᰃϔϾ䲚ড়ˈབϔϾ߫㸼៪ϔϾ᮴ᑣ䲚ড়DŽ ㉏೒ C:\Users\fulin\AppData\Local\Temp\404683\images\Visitor.jpg ՟ᄤ Visitor public interface Visitor { public void visitString(StringElement stringE); Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 75 public void visitFloat(FloatElement floatE); public void visitCollection(Collection collection); } ConcreteVisitor public class ConcreteVisitor implements Visitor { public void visitCollection(Collection collection) { // TODO Auto-generated method stub Iterator iterator = collection.iterator(); while (iterator.hasNext()) { Object o = iterator.next(); if (o instanceof Visitable) { ((Visitable)o).accept(this); } } } public void visitFloat(FloatElement floatE) { System.out.println(floatE.getFe()); } public void visitString(StringElement stringE) { System.out.println(stringE.getSe()); } } Element public interface Visitable { public void accept(Visitor visitor); } ConcreteElement public class FloatElement implements Visitable { private Float fe; public FloatElement(Float fe) { this.fe = fe; } Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅ 76 public Float getFe() { return this.fe; } public void accept(Visitor visitor) { visitor.visitFloat(this); } } public class StringElement implements Visitable { private String se; public StringElement(String se) { this.se = se; } public String getSe() { return this.se; } public void accept(Visitor visitor) { visitor.visitString(this); } } Test public class Test { public static void main(String[] args) { Visitor visitor = new ConcreteVisitor(); StringElement se = new StringElement("abc"); se.accept(visitor); FloatElement fe = new FloatElement(new Float(1.5)); fe.accept(visitor); S*stem.out.println("==========="); List result = new ArrayList(); result.add(new StringEle*ent("abc")); result.add(new StringElement("abc")); result.add(new StringElement("abc")); result.add(new FloatElement(new Float(1.5))); result.add(new FloatElement(new Float(1.5))); result.add(new FloatElement(new Float(1.5))); ৠⱘㄝ㑻㒧ᵘⱘ៤ਬ㉏DŽ 㗠ϡᰃᬒࠄᅗⱘᄤ㉏ЁDŽ䆓䯂㗙῵ᓣৃҹ䎼䖛޴Ͼ㉏ⱘㄝ㑻㒧ᵘ䆓䯂ሲѢϡ ЁDŽᔧՓ⫼䆓䯂㗙῵ᓣᯊˈ㽕ᇚሑৃ㛑໮ⱘᇍ䈵⌣㾜䘏䕥ᬒ೼䆓䯂㗙㉏Ёˈ ῵ᓣᇚ᳝݇ⱘ㸠Ў䲚ЁࠄϔϾ䆓䯂㗙ᇍ䈵Ёˈ㗠ϡᰃߚᬷࠄϔϾϾⱘ㡖⚍㉏ 㗙῵ᓣՓᕫ๲ࡴᮄⱘ᪡԰বⱘᕜᆍᯧˈህᰃ๲ࡴϔϾᮄⱘ䆓䯂㗙㉏DŽ䆓䯂㗙 㒧ᵘϞⱘ᪡԰П䯈ⱘ㗺ড়㾷㜅ᓔˈՓᕫ᪡԰䲚ড়ৃҹⳌᇍ㞾⬅ⱘⓨ࣪DŽ䆓䯂 ϡবDŽ䆓䯂㗙῵ᓣ䗖⫼Ѣ᭄᥂㒧ᵘⳌᇍ᳾ᅮⱘ㋏㒳ˈᅗᡞ᭄᥂㒧ᵘ੠԰⫼Ѣ Ϟⱘ᪡԰DŽϔᮺ䖭ѯ᪡԰䳔㽕ׂᬍⱘ䆱ˈ᥹ফ䖭Ͼ᪡԰ⱘ᭄᥂㒧ᵘৃҹֱᣕ ㋴Пܗ 䆓䯂㗙῵ᓣ˖䆓䯂㗙῵ᓣⱘⳂⱘᰃᇕ㺙ϔѯᮑࡴѢᶤ⾡᭄᥂㒧ᵘ ህ䕏ᵒ໮њ˗ ᵓḍ᥂ MM ⱘ⡍⚍䗝ϔᴳ㢅ˈ䅽⼐કᑫ㗕ᵓгḍ᥂↣ϾҎ⡍⚍䗝ϔᓴवˈ䖭ḋ ϔϟ Visitorˈ䅽㢅ᑫ㗕خϾҎા᧲ᕫ⏙Ἦˈ䖬ᰃᡒ㢅ᑫ㗕ᵓ੠⼐કᑫ㗕ᵓ MM 䗕ⱘ㢅䛑㽕䩜ᇍཌྷϾҎⱘ⡍⚍ˈ↣ᓴव⠛г㽕ḍ᥂ϾҎⱘ⡍⚍ᴹᣥˈ៥ϔ VISITOR—ᚙҎ㡖ࠄњˈ㽕㒭↣Ͼ MM 䗕ϔᴳ剰㢅੠ϔᓴव⠛ˈৃᰃ↣Ͼ 1.5 1.5 1.5 abc abc abc =========== 1.5 abc result } } visitor.visitCollection(result); 77 Java 䆒䅵῵ᓣ˄⮃⢖ Java 㘨ⲳ⠜˅

下载文档,方便阅读与编辑

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 10 金币 [ 分享文档获得金币 ] 2 人已下载

下载文档

相关文档