#include <iostream>
using namespace std;
class Base
{
public:
void DoWhat()
{
this->Printf1();
this->Printf2();
}
virtual void Printf1() = 0;
virtual void Printf2() = 0;
};
class Son1 : public Base
{
public:
void Printf1()
{
cout << "Son1::Printf1()" << endl;
}
void Printf2()
{
cout << "Son1::Printf2()" << endl;
}
};
class Son2 : public Base
{
public:
void Printf1()
{
cout << "Son2::Printf1()" << endl;
}
void Printf2()
{
cout << "Son2::Printf2()" << endl;
}
};
class Contex
{
public:
Contex(Base *p)
{
ptr = p;
}
void Contexfunction()
{
ptr->DoWhat();
}
private:
Base *ptr;
};
int main()
{
Contex *p = new Contex(new Son1());
p->Contexfunction();
p = new Contex(new Son2());
p->Contexfunction();
//将基类中的公共接口封装到一个类中,让基类算法的实现细节在子类中实现,
//但是具体的调用细节则延迟到封装的类中。
return 0;
}
下面是我的代码实现:
#include <iostream>
using namespace std;
//Stratgy策略模式。
class Algorithm
{
public:
Algorithm(int a[], int n)
{
data = new int[n];
int i = 0;
for (;i<n;i++)
{
data[i] = a[i];
}
size = n;
}
void Interface()
{
this->Sort();//公共的接口。
this->Printf();
}
void Printf()
{
int i = 0;
for (; i < size; i++)
{
cout << data[i] << " ";
}
cout << endl;
}
virtual void Sort() = 0;
protected:
int size;
int *data;
};
class Hash : public Algorithm//堆排序,名字起错,也不想改了。
{
public:
Hash(int a[], int n) :Algorithm(a, n)
{
Hashsort();
}
void Sort()
{
int *b = new int[size];
int n = size;
int i = 0;
for (; i < n; i++)
{
b[i] = data[0];
data[0] = data[size - 1];
size--;
Hashsort(data, 0);
}
delete[]data;
data = NULL;
data = b;
size = n;
}
void Hashsort()
{
int n = size / 2;
while (n >= 0)
{
Hashsort(data,n);
n--;
}
}
private:
void Hashsort(int a[], int n)
{
int i = n;
int j = i * 2 + 1;
int temp;
while (j < size)
{
if (j + 1 < size && data[j] > data[j + 1])
j = j + 1;
if (data[i]>data[j])
{
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
i = j;
j = i * 2 + 1;
}
}
private:
};
#define _SIZE_ 11
//定义最大的位数。
class Set : public Algorithm//基数排序。
{
public:
struct Node
{
int data;
Node *next;
Node(int d = int()) :data(d), next(NULL){}
};
struct MyNode
{
Node *adj;
int count;//计数。
MyNode():adj(NULL), count(0){}
};
Set(int a[], int n) : Algorithm(a, n)
{
int i = 0;
node = new MyNode[11];
Node *p = NULL;
Node *pr = NULL;
int index;
for (; i < n; i++)
{
pr = NULL;
index = GetIndex(a[i]);
p = node[index].adj;
node[index].count++;
Node *s = new Node(a[i]);
if (p == NULL)
{
node[index].adj = s;
}
else
{
while (p != NULL && p->data<a[i])
{
pr = p;
p = p->next;
}
if (p == NULL)
{
pr->next = s;
}
else
{
if (pr == NULL)
{
s->next = p;
node[index].adj = s;
}
else
{
s->next = p;
pr->next = s;
}
}
}
}
int k = 11;
int j = 0;
for (i = 0; i < k; i++)
{
if (node[i].count != 0)
{
p = node[i].adj;
while (p != NULL)
{
data[j++] = p->data;
p = p->next;
}
}
}
}
void Sort()
{
cout << "hello,this is 基数排序!!!" << endl;
}
int GetIndex(int x)
{
int count = 0;
while (x>=0)
{
x /=10;
count++;
if (x == 0)return count;
}
}
private:
MyNode *node;
};
class Contex
{
public:
Contex(Algorithm *p)
{
ptr = p;
}
void ContexInterface()
{
ptr->Interface();
}
private:
Algorithm *ptr;
};
int main()
{
int a[] = {6,7,34,2,0,123,24,5,3,8};
Contex *ct = new Contex(new Set(a,10));
//此处构造相应的算法子类。
ct->ContexInterface();
//此处调用公共接口函数来调用算法函数。
return 0;
}