|
@@ -0,0 +1,71 @@
|
|
|
+#include<iostream>
|
|
|
+#include<vector>
|
|
|
+
|
|
|
+//适配器模式
|
|
|
+//适配器模式的核心是对原有的接口进行封装,并且对原有的数据类型进行重整的一种设计模式
|
|
|
+
|
|
|
+//我们有如下这样的数据
|
|
|
+struct point //一个点
|
|
|
+{
|
|
|
+ int x,y;
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+struct line
|
|
|
+{
|
|
|
+ point begin;
|
|
|
+ point end;
|
|
|
+};
|
|
|
+
|
|
|
+//目标用户的函数(一般来自与库或者框架)对数据有特殊要求
|
|
|
+void aim_fun(std::vector<line> rectangle) //目标函数期待传入一个矩形
|
|
|
+{}
|
|
|
+
|
|
|
+
|
|
|
+//但是我们设计的数据只有点和线,即我们需要对我们的数据进行拼装以适应对应的框架
|
|
|
+
|
|
|
+class adapter_lazy //懒汉式
|
|
|
+{
|
|
|
+private:
|
|
|
+ line& line_1;
|
|
|
+ line& line_2;
|
|
|
+ line& line_3;
|
|
|
+ line& line_4;
|
|
|
+ std::vector<line> rectangle;
|
|
|
+public:
|
|
|
+ adapter_lazy(line& a,line& b,line& c,line& d):line_1(a),line_2(b),line_3(c),line_4(d){}
|
|
|
+ std::vector<line> create()
|
|
|
+ {
|
|
|
+ rectangle.emplace_back(line_1);
|
|
|
+ rectangle.emplace_back(line_2);
|
|
|
+ rectangle.emplace_back(line_3);
|
|
|
+ rectangle.emplace_back(line_4);
|
|
|
+ return rectangle;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+class adapter_hungry //懒汉式
|
|
|
+{
|
|
|
+private:
|
|
|
+
|
|
|
+ std::vector<line> rectangle;
|
|
|
+public:
|
|
|
+ adapter_hungry(line& line_1,line& line_2,line& line_3,line& line_4):rectangle(){
|
|
|
+ rectangle.emplace_back(line_1);
|
|
|
+ rectangle.emplace_back(line_2);
|
|
|
+ rectangle.emplace_back(line_3);
|
|
|
+ rectangle.emplace_back(line_4);
|
|
|
+ }
|
|
|
+ std::vector<line> create()
|
|
|
+ {
|
|
|
+
|
|
|
+ return rectangle;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+int main()
|
|
|
+{
|
|
|
+
|
|
|
+}
|