|
5 | 5 | ## **1、开闭原则(Open Close Principle)**
|
6 | 6 |
|
7 | 7 | ```Properties
|
8 |
| -开闭原则的意思是:**对扩展开放,对修改关闭**。在程序需要进行拓展的时候,不能去修改原有的代码,实现一个热插拔的效果。简言之,是为了使程序的扩展性好,易于维护和升级。想要达到这样的效果,我们需要使用接口和抽象类,后面的具体设计中我们会提到这点。 |
| 8 | +开闭原则的意思是:对扩展开放,对修改关闭。在程序需要进行拓展的时候,不能去修改原有的代码,实现一个热插拔的效果。简言之,是为了使程序的扩展性好,易于维护和升级。想要达到这样的效果,我们需要使用接口和抽象类,后面的具体设计中我们会提到这点。 |
9 | 9 | ```
|
10 | 10 |
|
11 | 11 | ## **2、里氏代换原则(Liskov Substitution Principle)**
|
@@ -944,7 +944,7 @@ public class Main {
|
944 | 944 | }
|
945 | 945 | }
|
946 | 946 | ```
|
947 |
| -## //TODO 3.3 解释器模式(Interpreter Pattern) |
| 947 | +## 3.3 * 解释器模式(Interpreter Pattern) |
948 | 948 |
|
949 | 949 | > 这种模式实现了一个表达式接口,该接口解释一个特定的上下文。这种模式被用在 SQL 解析、符号处理引擎等。
|
950 | 950 | >
|
@@ -1007,11 +1007,261 @@ public class Main{
|
1007 | 1007 |
|
1008 | 1008 |
|
1009 | 1009 |
|
1010 |
| -## 3.4 迭代器模式(Iterator Pattern) |
1011 |
| -## 3.5 中介者模式(Mediator Pattern) |
1012 |
| -## 3.6 备忘录模式(Memento Pattern) |
1013 |
| -## 3.7 观察者模式(Observer Pattern) |
| 1010 | +## 3.4 *迭代器模式(Iterator Pattern) |
| 1011 | + |
| 1012 | +>提供一种方式顺序访问聚合对象的各个元素,无需暴露对象的内部保存. 意思就是内部封装实现了遍历操作 |
| 1013 | + |
| 1014 | +```java |
| 1015 | +//接口 |
| 1016 | +public interface Iterator { |
| 1017 | + public boolean hasNext(); |
| 1018 | + public Object next(); |
| 1019 | +} |
| 1020 | + |
| 1021 | +public interface Container { |
| 1022 | + public Iterator getIterator(); |
| 1023 | +} |
| 1024 | + |
| 1025 | +//实现 |
| 1026 | +public class NameRepository implements Container { |
| 1027 | + public String names[] = {"Robert" , "John" ,"Julie" , "Lora"}; |
| 1028 | + |
| 1029 | + @Override |
| 1030 | + public Iterator getIterator() { |
| 1031 | + return new NameIterator(); |
| 1032 | + } |
| 1033 | + //内部类 |
| 1034 | + private class NameIterator implements Iterator { |
| 1035 | + |
| 1036 | + int index; |
| 1037 | + |
| 1038 | + @Override |
| 1039 | + public boolean hasNext() { |
| 1040 | + if(index < names.length){ |
| 1041 | + return true; |
| 1042 | + } |
| 1043 | + return false; |
| 1044 | + } |
| 1045 | + |
| 1046 | + @Override |
| 1047 | + public Object next() { |
| 1048 | + if(this.hasNext()){ |
| 1049 | + return names[index++]; |
| 1050 | + } |
| 1051 | + return null; |
| 1052 | + } |
| 1053 | + } |
| 1054 | +} |
| 1055 | +//通过NameRepository内部类的试下,进而实现Iteration的内部封装 |
| 1056 | +``` |
| 1057 | + |
| 1058 | + |
| 1059 | + |
| 1060 | +## 3.5 * 中介者模式(Mediator Pattern) |
| 1061 | + |
| 1062 | +- 中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护。中介者模式属于行为型模式。 |
| 1063 | + |
| 1064 | + ```java |
| 1065 | + //例如:SpringMVC中的Controller; 方法show(User user,String message) user和message信息 |
| 1066 | + ``` |
| 1067 | + |
| 1068 | + |
| 1069 | + |
| 1070 | +## 3.6 *备忘录模式(Memento Pattern) |
| 1071 | + |
| 1072 | +- 保存一个对象的状态,以便在适当的时候恢复对象. |
| 1073 | + |
| 1074 | +- 不破坏封装性前提,捕获一个对象内部状态,并且在对象之外保存这个状态 |
| 1075 | + |
| 1076 | +- 应用:1、后悔药。 2、打游戏时的存档。 3、Windows 里的 ctri + z。 4、IE 中的后退。 4、数据库的事务管理。 |
| 1077 | + |
| 1078 | + ```java |
| 1079 | + //被Originator类使用到类 |
| 1080 | + public class Memento { |
| 1081 | + private String state; |
| 1082 | + |
| 1083 | + public Memento(String state){ |
| 1084 | + this.state = state; |
| 1085 | + } |
| 1086 | + |
| 1087 | + public String getState(){ |
| 1088 | + return state; |
| 1089 | + } |
| 1090 | + } |
| 1091 | + |
| 1092 | + //Originator设置的时候,自动设置了Memento类 |
| 1093 | + public class Originator { |
| 1094 | + private String state; |
| 1095 | + |
| 1096 | + public void setState(String state){ |
| 1097 | + this.state = state; |
| 1098 | + } |
| 1099 | + |
| 1100 | + public String getState(){ |
| 1101 | + return state; |
| 1102 | + } |
| 1103 | + |
| 1104 | + public Memento saveStateToMemento(){ |
| 1105 | + return new Memento(state); |
| 1106 | + } |
| 1107 | + |
| 1108 | + public void getStateFromMemento(Memento Memento){ |
| 1109 | + state = Memento.getState(); |
| 1110 | + } |
| 1111 | + } |
| 1112 | + ``` |
| 1113 | + |
| 1114 | + ```java |
| 1115 | + public class CareTaker { |
| 1116 | + private List<Memento> mementoList = new ArrayList<Memento>(); |
| 1117 | + |
| 1118 | + public void add(Memento state){ |
| 1119 | + mementoList.add(state); |
| 1120 | + } |
| 1121 | + public Memento get(int index){ |
| 1122 | + return mementoList.get(index); |
| 1123 | + } |
| 1124 | + } |
| 1125 | + |
| 1126 | + //最终实现 |
| 1127 | + public class MementoPatternDemo { |
| 1128 | + public static void main(String[] args) { |
| 1129 | + Originator originator = new Originator(); |
| 1130 | + CareTaker careTaker = new CareTaker(); |
| 1131 | + originator.setState("State #1"); |
| 1132 | + originator.setState("State #2"); |
| 1133 | + careTaker.add(originator.saveStateToMemento()); |
| 1134 | + originator.setState("State #3"); |
| 1135 | + careTaker.add(originator.saveStateToMemento()); |
| 1136 | + originator.setState("State #4"); |
| 1137 | + |
| 1138 | + System.out.println("Current State: " + originator.getState()); |
| 1139 | + originator.getStateFromMemento(careTaker.get(0)); |
| 1140 | + System.out.println("First saved State: " + originator.getState()); |
| 1141 | + originator.getStateFromMemento(careTaker.get(1)); |
| 1142 | + System.out.println("Second saved State: " + originator.getState()); |
| 1143 | + } |
| 1144 | + } |
| 1145 | + |
| 1146 | + //原理: |
| 1147 | + //Originator设置的时候,自动设置了Memento,并且把Memento放入List中 |
| 1148 | + //恢复的时候, 需要状态的时候 直接从List中获取 |
| 1149 | + ``` |
| 1150 | + |
| 1151 | + |
| 1152 | + |
| 1153 | +## 3.7 * 观察者模式(Observer Pattern) |
| 1154 | + |
| 1155 | +- 对象存在一对多关系时,使用观察者模式.比如,当一个对象被修改时候,则会通知它的依赖对象. |
| 1156 | + |
| 1157 | +- 使用面向对象技术,将这种依赖关系弱化. |
| 1158 | + |
| 1159 | + > **关键代码:**在抽象类里有一个 ArrayList 存放观察者们。 |
| 1160 | + |
| 1161 | + ```java |
| 1162 | + //观察者 |
| 1163 | + public abstract class Observer { |
| 1164 | + protected Subject subject; |
| 1165 | + public abstract void update(); |
| 1166 | + } |
| 1167 | + |
| 1168 | + //学科类 |
| 1169 | + public class Subject { |
| 1170 | + |
| 1171 | + private List<Observer> observers |
| 1172 | + = new ArrayList<Observer>(); |
| 1173 | + private int state; |
| 1174 | + |
| 1175 | + public int getState() { |
| 1176 | + return state; |
| 1177 | + } |
| 1178 | + |
| 1179 | + public void setState(int state) { |
| 1180 | + this.state = state; |
| 1181 | + notifyAllObservers(); |
| 1182 | + } |
| 1183 | + |
| 1184 | + public void attach(Observer observer){ |
| 1185 | + observers.add(observer); |
| 1186 | + } |
| 1187 | + |
| 1188 | + public void notifyAllObservers(){ |
| 1189 | + for (Observer observer : observers) { |
| 1190 | + observer.update(); |
| 1191 | + } |
| 1192 | + } |
| 1193 | + } |
| 1194 | + ``` |
| 1195 | + |
| 1196 | + ```java |
| 1197 | + //具体观察者 |
| 1198 | + public class BinaryObserver extends Observer{ |
| 1199 | + |
| 1200 | + public BinaryObserver(Subject subject){ |
| 1201 | + this.subject = subject; |
| 1202 | + //当前观察者,放入Subject类的通知List中 |
| 1203 | + this.subject.attach(this); |
| 1204 | + } |
| 1205 | + |
| 1206 | + @Override |
| 1207 | + public void update() { |
| 1208 | + System.out.println( "Binary String: " |
| 1209 | + + Integer.toBinaryString( subject.getState() ) ); |
| 1210 | + } |
| 1211 | + } |
| 1212 | + |
| 1213 | + public class OctalObserver extends Observer{ |
| 1214 | + |
| 1215 | + public OctalObserver(Subject subject){ |
| 1216 | + this.subject = subject; |
| 1217 | + this.subject.attach(this); |
| 1218 | + } |
| 1219 | + |
| 1220 | + @Override |
| 1221 | + public void update() { |
| 1222 | + System.out.println( "Octal String: " |
| 1223 | + + Integer.toOctalString( subject.getState() ) ); |
| 1224 | + } |
| 1225 | + } |
| 1226 | + |
| 1227 | + public class HexaObserver extends Observer{ |
| 1228 | + |
| 1229 | + public HexaObserver(Subject subject){ |
| 1230 | + this.subject = subject; |
| 1231 | + this.subject.attach(this); |
| 1232 | + } |
| 1233 | + |
| 1234 | + @Override |
| 1235 | + public void update() { |
| 1236 | + System.out.println( "Hex String: " |
| 1237 | + + Integer.toHexString( subject.getState() ).toUpperCase() ); |
| 1238 | + } |
| 1239 | + } |
| 1240 | + ``` |
| 1241 | + |
| 1242 | + ```java |
| 1243 | + public class ObserverPatternDemo { |
| 1244 | + public static void main(String[] args) { |
| 1245 | + Subject subject = new Subject(); |
| 1246 | + |
| 1247 | + new HexaObserver(subject); |
| 1248 | + new OctalObserver(subject); |
| 1249 | + new BinaryObserver(subject); |
| 1250 | + //三个具体的实现,放入List中, Subject设置的时候,通过list遍历 通知给 |
| 1251 | + System.out.println("First state change: 15"); |
| 1252 | + subject.setState(15); |
| 1253 | + System.out.println("Second state change: 10"); |
| 1254 | + subject.setState(10); |
| 1255 | + } |
| 1256 | + } |
| 1257 | + ``` |
| 1258 | + |
| 1259 | + **具体理解:客户A,B,C三个客户,都想知道一个公司的信息,我订阅了这个消息.当公司消息更改的时候;要让客户A,B,C执行他们所对应的方法[名字一样,执行的逻辑各不相同]** |
| 1260 | + |
1014 | 1261 | ## 3.8 状态模式(State Pattern)
|
| 1262 | + |
| 1263 | +- 允许对象在内部状态发生改变时改变它的行为,对象看起来好像修改了它的类。 |
| 1264 | + |
1015 | 1265 | ## 3.9 策略模式(Strategy Pattern)
|
1016 | 1266 | ## 3.10 模板方法模式(Template Method Pattern)
|
1017 | 1267 | ## 3.11 访问者模式(Visitor Pattern)
|
|
0 commit comments