首页 > 其他分享 >中介者模式

中介者模式

时间:2022-12-26 10:59:01浏览次数:30  
标签:String void 模式 name Member 中介 public string

Java  


//Member.java
package shiyan19;

public abstract class Member {
    protected AbstractChatroom room;
    protected String name;
    public Member(String a){
        name=a;
    }
    public String getname(){
        return name;
    }
    public void setname(String a){
        name=a;
    }
    public AbstractChatroom getroom(){
        return room;
    }
    public void setroom(AbstractChatroom a){
        room=a;
    }
    public abstract void sendtext(String a,String mess);
    public abstract void sendimage(String a,String image);
    public void receivetext(String a,String mess){
        System.out.println(a+"发送的给"+name+"内容是"+mess);
    }
    public void receiveimage(String a,String image){
        System.out.println(a+"发送的给"+name+"内容是"+image);
    }
}
//CommonMember.java
package shiyan19;


public class CommonMember extends Member{
    public CommonMember(String a){
        super(a);
    }
    public void sendtext(String a,String c){
        System.out.println("普通会员发送消息");
        room.sendtext(name,a,c);
    }
    public void sendimage(String a,String c){
        System.out.println("普通会员不能发送图片");
    }
}
//DiamondMember.java
package shiyan19;

public class DiamondMember extends Member{
    public DiamondMember(String a){
        super(a);
    }
    public void sendtext(String a,String c){
        System.out.println("钻石会员发送消息");
        room.sendtext(name,a,c);
    }
    public void sendimage(String a,String c){
        System.out.println("钻石会员发送图片");
        room.sendimage(name,a,c);
    }
}
//NewGroup.java
package shiyan19;

import java.util.HashMap;
import java.util.Map;
public class NewGroup extends AbstractChatroom{

        Map<String,Member> table = new HashMap<>();
        public void register(Member member) {
            table.put(member.getname(), member);
            member.setroom(this);
        }

        public void sendtext(String from, String to, String message) {
            if(message.length()>=100){
                System.out.println("超过100个字符,发送失败");
            }else{
                Member member = table.get(to);
                if(member!=null)
                    member.receivetext(from, message);
            }
        }

        public void sendimage(String from, String to, String message) {
            if(message.length()>=20){
                System.out.println("图片超过20M,发送失败");
            }else{
                Member member = table.get(to);
                if(member!=null)
                    member.receiveimage(from, message);
            }
        }

}
//NewMember.java
package shiyan19;

public class NewMember extends Member{
    public NewMember(String a){
        super(a);
    }
    public void sendtext(String a,String c){
        System.out.println("新会员发送消息:");
        room.sendtext(name,a,c);
    }
    public void sendimage(String a,String c){
        System.out.println("新会员发送图片:");
        room.sendimage(name,a,c);
    }
}
//AbstractChatroom.java
package shiyan19;

public abstract class AbstractChatroom {
    public abstract void register(Member m);
    public abstract void sendtext(String a,String b,String mess);
    public abstract void sendimage(String a,String b,String mess);
}
//ChatGroup.java
package shiyan19;

import java.util.*;

public class ChatGroup extends AbstractChatroom{
    private Hashtable ms=new Hashtable();
    public void register(Member m)
    {
        if(!ms.contains(m)){
            ms.put(m.getname(),m);
            m.setroom(this);
        }
    }
    public void sendtext(String a,String b,String c){
        Member m=(Member)ms.get(b);
        String newm=c;
        newm=c.replaceAll("日","*");
        m.receivetext(a,newm);
    }
    public void sendimage(String a,String b,String c){
        Member m=(Member)ms.get(b);
        if(c.length()>5){
            System.out.println("图片较大,发送失败");
        }else{
            m.receiveimage(a,c);
        }
    }
}
//main.java
package shiyan19;

public class main {
    public static void main(String args[]){
        AbstractChatroom r1=new NewGroup();
        Member m1,m2,m3;
        m1=new NewMember("小李");
        m2=new NewMember("小红");
        m3=new NewMember("张三");
        r1.register(m1);
        r1.register(m2);
        r1.register(m3);
        m1.sendtext("小红",":小红,你好");
        m2.sendtext("小李",":小李,你好");
        m1.sendtext("小红",":吃饭了吗?");
        m2.sendtext("小李",":吃了");
        m1.sendtext("张三",":---------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        m3.sendimage("小李",":哈哈.jpg");
        m2.sendimage("张三",":哈哈.jpg--------------------");
    }
}



c++


#include<iostream>
#include<map>
#include<ctime>
#include<sstream>
using namespace std;
class AbstractChatroom;
//抽象同事类
class Member {
protected :
    AbstractChatroom *chatroom;
    string name;
public:
    Member(string name) {
        this->name=name;
    }
    AbstractChatroom* getChatroom() {
        return chatroom;
    }
    void setChatroom(AbstractChatroom *chatroom) {
        this->chatroom = chatroom;
    }
    string getName() {
        return name;
    }
    void setName(string name) {
        this->name = name;
    }
    virtual void sendText(string to,string message)=0;
    virtual void sendImage(string to,string image)=0;
    void receiveText(string from,string message) {
        cout<<from<<"发送文本给"<<this->name<<",内容为:"<<message<<endl;
    }
    void receiveImage(string from,string image) {
        cout<<from<<"发送图片给"<<this->name<<",内容为:"<<image<<endl;
    }
};
string replace(string strSrc,const string &oldStr, const string &newStr,int count=-1)
{
    string strRet=strSrc;
    size_t pos = 0;
    int l_count=0;
    if(-1 == count) //替换所有
        count = strRet.size();
    while ((pos = strRet.find(oldStr, pos)) != string::npos)
    {
        strRet.replace(pos, oldStr.size(), newStr);
        if(++l_count >= count) break;
        pos += newStr.size();
    }
    return strRet;
}
//抽象中介者类
class AbstractChatroom {
public:
    virtual void registers(Member *member)=0;
    virtual void sendText(string from,string to,string message)=0;
    virtual void sendImage(string from,string to,string image)=0;
};
//具体中介者类
class ChatGroup: public AbstractChatroom{
private:
    map<string, Member*> members;
    map<string, Member*>::iterator iter;
public:
    void registers(Member *member) {
        iter=members.find(member->getName());
        if(iter==members.end()) {
            members.insert(map<string, Member*>::value_type(member->getName(),member));
            member->setChatroom(this);
        }
    }
    void sendText(string from, string to, string message) {
        Member *member=members.find(to)->second;
        string newMessage=message;
        newMessage=replace(message,"日", "*");
        member->receiveText(from, newMessage);
    }
    void sendImage(string from, string to, string image) {
        Member *member= members.find(to)->second;
        if(image.length()>5) {
           cout<<"图片太大,发送失败"<<endl;
        }
        else {
            member->receiveImage(from, image);
        }
    }
};
//具体中介者类
class NewChat:public AbstractChatroom{
private:
    map<string, Member*> members;
    map<string, Member*>::iterator iter;
public:
    void registers(Member *member) {
       iter=members.find(member->getName());
        if(iter==members.end()) {
            members.insert(map<string, Member*>::value_type(member->getName(),member));
            member->setChatroom(this);
        }
    }
    void sendText(string from, string to, string message) {
        Member *member=members.find(to)->second;
        string newMessage=message;
        newMessage=replace(message,"日", "*");
        if(message.length()>100) {
            cout<<"发送的文字长度不得超过100个字符,发送失败"<<endl;
        }
        else {
            member->receiveText(from, newMessage);
        }
    }
    void sendImage(string from, string to, string image) {
        Member *member=members.find(to)->second;
        if(image.length()>20) {
            cout<<"图片大小不得超过20M,发送失败"<<endl;
        }
        else {
            member->receiveImage(from, image);
        }
    }
};
//具体同事类
class CommonMember :public Member{
protected :
    string name;
public:
    CommonMember(string name):Member(name) {
        this->name=name;
    }
    void sendText(string to, string message) {
        cout<<"普通会员发送消息:"<<endl;
        chatroom->sendText(this->name, to, message);
    }
 
    void sendImage(string to, string image) {
        cout<<"普通会员不能发送图片!"<<endl;
    }
};
//具体同事类
class DiamondMember :public Member{
protected :
    string name;
public:
    DiamondMember(string name):Member(name){
        this->name=name;
        }
    void sendText(string to, string message) {
        cout<<"钻石会员发送消息:"<<endl;
        chatroom->sendText(name,to, message);
    }
    void sendImage(string to, string image) {
        cout<<"钻石会员发送图片:"<<endl;
        chatroom->sendImage(name, to, image);
    }
};
//具体同事类
class NewMember :public Member{
protected :
    string name;
public:
    NewMember(string name):Member(name){
        this->name=name;
    }
    void sendText(string to, string message) {
        time_t now = time(NULL);
        tm* t = localtime(&now);
        // 将信息输出到字符串流
        stringstream ss;
        stringstream ss2;
        ss << t->tm_year + 1900 << "." << t->tm_mon + 1 << "." <<
            t->tm_mday << ".." << t->tm_hour << "." << t->tm_min << "." << t->tm_sec;
        cout<<"新会员发送消息:"<<endl;
        chatroom->sendText(name,to,message+"  现在时间"+ss.str());
    }
    void sendImage(string to, string image) {
        cout<<"会员发送图片:"<<endl;
        chatroom->sendImage(name, to, image);
    }
};
//测试函数
int main(){
        AbstractChatroom *newChat=new NewChat();
        Member *member1,*member2,*member3,*member4;
        member1=new DiamondMember("张三");
        member2=new CommonMember("小李");
        member3=new NewMember("小芳");
        member4=new NewMember("小红");
 
        newChat->registers(member1);
        newChat->registers(member2);
        newChat->registers(member3);
        newChat->registers(member4);
 
        member1->sendText("小李","小李,你好!");
        member2->sendText("张三","张三,你好!");
        cout<<"---------------------------------------------"<<endl;
        member3->sendText("小红","小红你好。");
        member3->sendText("小红","你好小红!-----------------------------------------------------------------------------------------------------");
        member3->sendImage("小红","哈哈.jpg");
        member4->sendText("小芳","你好,小芳");
        member4->sendImage("小芳","哈哈.jpg--------------------");
}

标签:String,void,模式,name,Member,中介,public,string
From: https://www.cnblogs.com/lion999heart/p/17005202.html

相关文章

  • 迭代器模式
    Java//Student.javapackageshiyan18;publicclassStudent{   privateStringid;   privateStringname;   privateStringage;   publicStringget......
  • 备忘录模式
    java//Caretaker.javapackageshiyan20;publicclassCaretaker{   privateMemento[]array=null;   privateintindex;   privateMementomemento;  ......
  • 原型模式
    概述《设计模式》一书中对“原型模式”的意图描述如下:用原型实例指定创建对象的种类,并通过拷贝对象的原型创建新的对象貌似有点难懂,概括性地讲就是实现对不同类型对......
  • UML设计模式符号答疑
    在UML类图中,常见的有以下几种关系:泛化(Generalization),  实现(Realization),关联(Association),聚合(Aggregation),组合(Composition),依赖(Dependency) 1.泛化(Generalization)......
  • MongoDB - 模式设计
    注意事项模式设计,即在文档中表示数据的方式,对于数据表示来说时非常关键的。为MongoDB做模式设计时,在性能、可伸缩性和简单性方面是重中之重,也需要考虑一些特别的注意事......
  • 分布式事务方案 - SAGA模式
    本文目的是讲清楚SAGA这种分布式事务解决方案的实现思路,不包括具体实现代码,具体实现推荐使用阿里的Seata框架。内容包括:分布式事务问题描述SAGA-Choreography策......
  • 精通visual c++指纹模式识别系统算法及实现
    通过学习,掌握以下几个问题:1、核心算法,并且向GVF衍生;2、核心库封装的方法2016年11月16日06:52:51昨日实现了梯度场和频率场的计算。最大的感觉就是建立基础代码库的重要性。......
  • 【模式识别】判别函数和感知机算法
    给定两个类别的一共8个样本,通过感知机算法求解判别函数defbi_perception():X1=np.array([[0,0,0],[1,0,0],[1,0,1],......
  • CPU的实模式
    实模式是指CPU的寻址方式,寄存器大小,指令用法等,是用来反应CPU在该环境下如何工作的概念。CPU的工作原理:CPU大体分为三个部分,控制、运算、存储单元。控制单元是CPU的控制中......
  • 1个月连载30个设计模式真实案例,挑战年薪60W不是梦
    本文所有内容均节选自《设计模式就该这样学》本文自2021年10月29日起持续连载,请大家持续关注....序言DesignPatterns:ElementsofReusableObject-OrientedSoft......