package com.design.model.strategy;
import java.util.Date;
interface IUser {
public String getUserInfo();
public void sendMsg(String msg);
}
class Person implements IUser{
@Override
public String getUserInfo() {
// TODO Auto-generated method stub
return "我是人!";
}
@Override
public void sendMsg(String msg) {
// TODO Auto-generated method stub
System.out.println(new Date()+"人发送了一则消息:"+msg);
}
}
class Man extends Person{
@Override
public String getUserInfo() {
// TODO Auto-generated method stub
return "我是男人!";
}
@Override
public void sendMsg(String msg) {
// TODO Auto-generated method stub
System.out.println(new Date()+"男人发送了一则消息:"+msg);
}
}
class WoMan extends Person{
@Override
public String getUserInfo() {
// TODO Auto-generated method stub
return "我是女人!";
}
@Override
public void sendMsg(String msg) {
// TODO Auto-generated method stub
System.out.println(new Date()+"女人发送了一则消息 :"+msg);
}
}
class Work {
private IUser iuser;
public Work(IUser iuser){
this.iuser=iuser;
}
public void sendMsg(String msg){
iuser.sendMsg(msg);
}
public String getInfo(){
return iuser.getUserInfo();
}
public static void main(String[] args) {
//面向接口编程
//男人工作
Work work =new Work(new Man());
System.out.println(work.getInfo());
work.sendMsg("hello");
//女人工作
Work work0 =new Work(new WoMan());
System.out.println(work0.getInfo());
work0.sendMsg("hello");
//默认的人工作
Work work1 =new Work(new Person());
System.out.println(work1.getInfo());
work1.sendMsg("hello");
}
}