常见的设计模式

设计模式是软件工程的基础     多态是设计模式的基础

设计模式有创建型、结构型、行为型

创建型比较常见的有:单例模式、简单工厂模式、工厂模式、抽象工厂模式

结构型比较常见的:适配器模式、享元模式

行为型模式常见的:观察者模式。


单列模式
//一个类只有一个对象
//懒汉式

class Single
{
private:
Single()
{


public:
static single *getSingle()
{
if(s==NULL)
s=new Single();
return s;
}
static Single *FreeSingle()
{
if(s!=NULL)
{
delete s;
s=NULL;
}

}
protected:
private:
static Single *s;

};
Single *Single::s=NULL;//静态变量初始化

void main()
{
Single *p1=Single::getSingle();
Single *p2=Single::getSingle();
Single::FreeSingle();
}

//简单工厂模式


class Fruit
{
virtual void getFruit()=0;

};
class Banana :public Fruit
{
void getFruit()
{
cout<<"香蕉";
}
};
class Apple :public Fruit
{
void getFruit()
{
cout<<"苹果";
}
};
class Factory
{
public:
Fruit *create(char *p)//新增会修改
{
if(strcmp(p,"banana")==0)
{
return new Banana;
}

else if(strcmp(p,"apple")==0)

{

return new Apple;

}


}
    
};
void main()
{
Factory *f=new Factory;
Fruit* fr=NULL;


fr=f->create("banana");
fr->getFruit();
delete fr;

delete(f);
}

//工厂模式
class Fruit
{
virtual void getFruit()=0;

};
class Banana :public Fruit
{
void getFruit()
{
cout<<"香蕉";
}
};
class Apple :public Fruit
{
void getFruit()
{
cout<<"苹果";
}
};
class AbFactory
{
virtual Fruit* createFruit()=0;
};
class banFactory public:AbFactory
{
virtual Fruit* createFruit()
{
return new Banana;
}
};
class AppFactory public:AbFactory
{
virtual Fruit* createFruit()
{
return new Apple;
}
};


void main()
{
AbFactory *af=NULL:
Fruit *f=NULL;
af=new BanFactory;
f=af->createFruit();
f->getFruit();
delete f;
delete af;
}

//抽象工厂模式
class Fruit
{
virtual void getFruit()=0;

};
class AbFactory//写死了 缺点
{
virtual Fruit* createBanan()=0;
virtual Fruit* createApple()=0;
};
class SourthFactory: public AbFactory
{
virtual Fruit* createBanan()
{
return new SourthBan;
}
virtual Fruit* createApple()
{
return new SourthApp;
}
};
class NorthFactory: public AbFactory
{
virtual Fruit* createBanan()
{
return new NorthBan;
}
virtual Fruit* createApple()
{
return new NorthApp;
}
};


class NorthBan :public Fruit{
virtual void getFruit()
{
cout<<"北向蕉";
}
};
class NorthApp :public Fruit{
virtual void getFruit()
{
cout<<"";
}
};
class SourthBan :public Fruit{
virtual void getFruit()
{
cout<<"南香蕉";
}
};
class SourthApp :public Fruit{
virtual void getFruit()
{
cout<<"";
}
};
void main()
{
AbFactory *af=NULL;
Fruit *f=NULL;
af=new SourthFactory;
f=af->createBanan();
f->getFruit();
delete f;
f=af->createApp();
f->getFruit();
delete f;
delete af;

}
--------------

//适配器模式
class Current18
{
virtual void useCurrent18()=0;
};
class  Current220
{
void useCurrent220()
{
cout<<"我是22";
}
};
class adapt:public Current18
{
adapt(Current220 *current)
{
mcurrent=current;
}
virtual void useCurrent18()
{
mcurrent->useCurrent220();
}
private:
Current220 *mcurrent;
};
void main()
{
Current220 *c20=NULL;
c20=new Current220;
adapt *a=NULL;
a=new adapt(c20);
a->useCurrent18();
delete c20;
delete a;
}

//观察者模式
class Secret
{
public:
secret()
{
m_list.clear();
}
void notify(string s)
{
for(list<watch*> ::iterator it=m_list.begin();it!=m_list.end();i++)
{
cout<<(*it)->update(s);
}
}
void setWatch(watch *w)
{
m_list.push_back(w);
}
private:
list<watch*> m_list;
}
class watch
{
public:
watch(Secret *s)
{
this->s=s;
}
update(string s)
{
cout<<s;
}
private:
secret *s;
};
void main()
{
secret *s=NULL;
watch *w1=NULL;
watch *w2=NULL;
s=new secret;
w1=new watch(s);
w2=new watch(s);
s->setWatch(w1);
s->setWatch(w2);
s->notify("");s->notify("");
delete s;
delete w1;
delete w2;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值