标签:String 每日 System 博客 public println void out
实验19:中介者模式
package zhongjiezhe;
import java.util.*;
public class Group extends Abcharroom{
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);
}
}
}
package zhontjiezhe;
import java.util.*;
public class Group extends Abcharroom{
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);
}
}
}
package zhontjiezhe;
public abstract class Abcharroom {
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);
}
package zhontjiezhe;
public class main {
public static void main(String args[]){
Abcharroom r1=new Room();
Member m1,m2,m3;
m1=new Xin("李华");
m2=new Xin("赵芳");
m3=new Xin("王帅");
r1.register(m1);
r1.register(m2);
r1.register(m3);
m1.sendtext("赵芳",":你好啊");
m2.sendtext("李华",":你好");
m2.sendtext("李华",":今天天气不错");
m1.sendtext("赵芳","呵呵");
m3.sendimage("李华",":再见");
m3.sendimage("赵芳",":再见");
}
}
package zhontjiezhe;
public class Pu extends Member{
public Pu(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("普通会员不能发送图片");
}
}
package zhontjiezhe;
public class Xin extends Member{
public Xin(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);
}
}
实验 20:备忘录模式
package beiwanglu;
import java.util.ArrayList;
public class Caretaker
{
private Memento memento;
private ArrayList mementolist = new ArrayList();
public Memento getMemento(int i) {
return (Memento)mementolist.get(i);
}
public void setMemento(Memento memento) {
mementolist.add(memento);
}
}
package beiwanglu;
public class Client
{
public static void main(String a[])
{
UserInfoDTO user=new UserInfoDTO();
Caretaker c=new Caretaker();
int index=0;
user.setAccount("lihua");
user.setPassword("123456");
user.setTelNo("13000000000");
System.out.println("状态一:");
user.show();
c.setMemento(user.saveMemento());//保存备忘录
System.out.println("---------------------------");
index++;
user.setPassword("100000");
user.setTelNo("13100000001");
System.out.println("状态二:");
user.show();
c.setMemento(user.saveMemento());//保存备忘录
System.out.println("---------------------------");
index++;
user.setPassword("999999");
user.setTelNo("13100009999");
System.out.println("状态三:");
user.show();
System.out.println("---------------------------");
for(int i=index-1;i>=0;i--)
{
int j=i+1;
user.restoreMemento(c.getMemento(i));//从备忘录中恢复
System.out.println("返回状态:"+j);
user.show();
System.out.println("---------------------------");
}
}
}
package beiwanglu;
class Memento
{
private String account;
private String password;
private String telNo;
public Memento()
{
}
public Memento(String account,String password,String telNo)
{
this.account=account;
this.password=password;
this.telNo=telNo;
}
public String getAccount()
{
return account;
}
public void setAccount(String account)
{
this.account=account;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password=password;
}
public String getTelNo()
{
return telNo;
}
public void setTelNo(String telNo)
{
this.telNo=telNo;
}
}
package beiwanglu;
public class UserInfoDTO
{
private String account;
private String password;
private String telNo;
public String getAccount()
{
return account;
}
public void setAccount(String account)
{
this.account=account;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password=password;
}
public String getTelNo()
{
return telNo;
}
public void setTelNo(String telNo)
{
this.telNo=telNo;
}
public Memento saveMemento()
{
return new Memento(account,password,telNo);
}
public void restoreMemento(Memento memento)
{
this.account=memento.getAccount();
this.password=memento.getPassword();
this.telNo=memento.getTelNo();
}
public void show()
{
System.out.println("Account:" + this.account);
System.out.println("Password:" + this.password);
System.out.println("TelNo:" + this.telNo);
}
}
标签:String,
每日,
System,
博客,
public,
println,
void,
out
From: https://www.cnblogs.com/LiuLyuancn/p/17852597.html