Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
haso2007 authored Aug 8, 2017
1 parent 4f53cf5 commit 0453463
Show file tree
Hide file tree
Showing 23 changed files with 1,852 additions and 0 deletions.
113 changes: 113 additions & 0 deletions Decorate mode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@

#include <string>
#include <iostream>
using namespace std;
//人 是一个抽象的接口,C++中没有abstract,也没有抽象类,只有用virtual只能修饰方法
//如果有女同事那她是一定要先穿胸衣的,那就要增加ConcretComponent了。
class Person //这里真正变成一个抽象类,具体名字移到实现类中
{

//private:
// string m_strName;
public:
// Person(string strName)
// {
// m_strName=strName;
// }
// Person(){}//这里重载,可以没有名字
virtual void Show()=0;//纯虚函数,相当于abstract也可以只留一个大括号.但是=0有利于
// {继承类的类型检查,如MalePerson写为 show会报错,但大括号不会。
// cout<<"装扮的是:"<<m_strName<<endl;//C++串尾显示,C#直接用+号 ,如Console.Write(""+m_strname)
// }
};

class FemalePerson:public Person
{
private:
string m_strName;
public:
FemalePerson(string strName) //在引用new时使用的构造函数,无需类型
{
m_strName = strName;
}
FemalePerson()
{
m_strName = "无名";
}
virtual void Show()
{
cout<<"装扮的是:男性"<<m_strName<<endl;
}
};

class MalePerson:public Person
{
private:
string m_strName;
// protected:
// Person* m_component; //C#中无需加此字段,调用父类的构造函数只需用base即可
public:
MalePerson(string strName) //在引用new时使用的构造函数,无需类型
{
m_strName = strName;
}
MalePerson()
{
m_strName = "无名";
}
virtual void Show()
{
cout<<"胸衣\n"<<"装扮的是:女性"<<m_strName<<endl;
}
};
//装饰类,在客户端中是不用出现的,与上下文是不同的。聚合+继承的关系
class Finery : public Person //C++ 父类的类型public要加,c#中不用加
{
protected:
Person* m_component;
public://子类对象可以直接使用
void Decorate(Person* component)
{
m_component=component;
}
virtual void Show()
{
m_component->Show();
}
};
//T恤 等等对象会根据调用顺序而链式显示出来。只要改下顺序就可以得到不同顺序
class TShirts: public Finery
{
public:
virtual void Show()
{
cout<<"T Shirts"<<endl;
m_component->Show();//直接使用父类中protected的字段,而这个show具体实现就要看m_component了。
}//如果m_component还是 TShirt的引用则会 引起死循环
};
//裤子
class BigTrouser :public Finery
{
public:
virtual void Show()
{
cout<<"Big Trouser"<<endl;
m_component->Show();
}
};

//客户端
int main()
{
MalePerson *p=new MalePerson();
FemalePerson *p1 = new FemalePerson("小王");
BigTrouser *bt=new BigTrouser();
TShirts *ts=new TShirts();
//如果bt 的方法Decorate(arg),arg 还是bt则会无限循环。 完全可用子类对象代替基类
ts->Decorate(p);// 注意是 bt调用基类人,第一次修饰,先显示ts本身的show()结果
bt->Decorate(ts);//然后ts又调用了bt,再修饰上面
bt->Show(); //最后才show出来。
//ts->Show();
system("pause");
//return 0;
}
58 changes: 58 additions & 0 deletions Untitled2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include<iostream>
#include <vector>
#include <string>
using namespace std;

class Prototype //抽象基类
{
private:
string m_strName;
public:
Prototype(string strName){ m_strName = strName; }
Prototype() { m_strName = " "; }
void Show()
{
cout<<m_strName<<endl;
}
virtual Prototype* Clone() = 0 ;
} ;

// class ConcretePrototype1
class ConcretePrototype1 : public Prototype
{
public:
ConcretePrototype1(string strName) : Prototype(strName){}
ConcretePrototype1(){}

virtual Prototype* Clone()
{
ConcretePrototype1 *p = new ConcretePrototype1() ;
*p = *this ; //复制对象,注意与p=this的区别
return p ;
}
} ;

// class ConcretePrototype2
class ConcretePrototype2 : public Prototype
{
public:
ConcretePrototype2(string strName) : Prototype(strName){}
ConcretePrototype2(){}

virtual Prototype* Clone()
{
ConcretePrototype2 *p = new ConcretePrototype2() ;
*p = *this ; //复制对象
return p ;
}
} ;

//客户端
int main()
{
ConcretePrototype1* test = new ConcretePrototype1("小王");
ConcretePrototype2* test2 = (ConcretePrototype2*)test->Clone();
test->Show();
test2->Show();
return 0;
}
50 changes: 50 additions & 0 deletions adaptor mode1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
using namespace std;

class Target
{
public:
virtual void Request()
{
cout<<"普通的请求"<<endl;
}
};

class Adaptee
{
public:
void SpecificalRequest()
{
cout<<"特殊请求"<<endl;
}
};

class Adapter :public Target
{
private:
Adaptee* ada;
public:
virtual void Request()
{
ada->SpecificalRequest();
Target::Request();
}
Adapter()
{
ada=new Adaptee();
}
~Adapter()
{
delete ada;
}
};

//客户端:
int main()
{
Adapter * ada=new Adapter();
ada->Request();
delete ada;
system("pause");
return 0;
}
80 changes: 80 additions & 0 deletions adaptor mode2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>
#include <string>
using namespace std;

class Player
{
protected:
string name;
public:
Player(string strName) { name = strName+ ":"; }
virtual void Attack()=0;
virtual void Defense()=0;
};

class Forwards : public Player
{
public:
Forwards(string strName):Player(strName){}
public:
virtual void Attack()
{
cout<<name<<"Forwards attack"<<endl;
}
virtual void Defense()
{
cout<<name<<"Forwards defense"<<endl;
}
};
//这个中锋是外来的,不是我写的,功能一样,但是接口不一样,怎么办
class Center
{
private:
string name;
public:
Center(string strName)
{name = strName + ":";}
public://这个中锋只懂得ChineseAttach这个方法,不明白Attach的意思
void JingGong()
{
cout<<name<<"中场进攻"<<endl;
}
void FanShou()
{
cout<<name<<"中场防守"<<endl;
}
};

//为中场翻译
class TransLater: public Player
{
private:
Center *player;//指定Center类对象player需要翻译,外来人口啦
public://继承父类的构造函数,同时将外来人口接口实现
TransLater(string strName):Player(strName)
{
player = new Center(strName);
}
virtual void Attack()//将中文转为英文的一般性接口
{
player->JingGong();
}
virtual void Defense()
{
player->FanShou();
}
};


//客户端
int main()
{
Player *Cp=new TransLater("姚明");
Player *Fp = new Forwards("Rose");
Cp->Attack();//这时外来人口就可以和本地人一样使用Attach方法。
Fp->Attack() ;//一样的方法却有两国语言, 这样Rose和姚明都听得懂了
delete(Cp);
delete(Fp);
system("pause");
return 0;
}
Loading

0 comments on commit 0453463

Please sign in to comment.