写一个单态设计模式、一个工厂设计模式、代理设计模式
单态:
package TKaoShi;
public class DanTaiPerson {
private static DanTaiPerson dtp;
private String name;
private char sex;
private DanTaiPerson(){
}
private DanTaiPerson(String name,char sex){
this.name=name;
this.sex=sex;
}
public static DanTaiPerson diaoYong(){
dtp=new DanTaiPerson("luchangyin", '男');
return dtp;
}
public void printDanTaiPerson(){
System.out.println("姓名:"+name+"\n性别:"+sex);
}
}
package TKaoShi;
public class DanTaiMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
DanTaiPerson dp =DanTaiPerson.diaoYong();
dp.printDanTaiPerson();
}
}
工厂设计:
package TKaoShi;
public abstract class Computer {
public abstract String nameComputer();
}
package TKaoShi;
public class DestTopComputer extends Computer {
@Override
public String nameComputer() {
// TODO Auto-generated method stub
return "这是一个台式电脑";
}
}
package TKaoShi;
public class TopComputer extends Computer{
public TopComputer(){
}
@Override
public String nameComputer() {
// TODO Auto-generated method stub
return "这是一个笔记本";
}
}
package TKaoShi;
public class Factory {
public static Computer getComputer(String name){
if(name==null||"".equals(name))
return null;
else if(name.equals("TomComputer"))
return new TopComputer();
else if(name.equals("DestTopComputer"))
return new DestTopComputer();
return null;
}
public static Computer getTopComputer(){
return new TopComputer();
}
public static Computer getDeTopConputer(){
return new DestTopComputer();
}
}
package TKaoShi;
public class TestComputerMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
Factory fa=new Factory();
TopComputer tc=(TopComputer) fa.getTopComputer();
System.out.println(tc.nameComputer());
DestTopComputer dc=(DestTopComputer)fa.getDeTopConputer();
System.out.println(dc.nameComputer());
System.out.println("-----------------");
TopComputer c=(TopComputer) fa.getComputer("TomComputer");
System.out.println(c.nameComputer());
DestTopComputer d=(DestTopComputer)fa.getComputer("DestTopComputer");
System.out.println(d.nameComputer());
}
}
代理设计:
package TKaoShi;
public interface Internet {
public abstract void lianWang();
public abstract void shangWang();
public abstract void duanWang();
}
package TKaoShi;
public class WangGuan implements Internet {
@Override
public void lianWang() {
// TODO Auto-generated method stub
System.out.println("已经联网了!");
}
@Override
public void shangWang() {
// TODO Auto-generated method stub
System.out.println("现在可以上网了!");
}
@Override
public void duanWang() {
// TODO Auto-generated method stub
System.out.println("大家注意了,准备要断网咯!");
}
}
package TKaoShi;
public class WangMin implements Internet{
private WangGuan wg;
public void setWang(WangGuan wg){
this.wg=wg;
}
@Override
public void lianWang() {
// TODO Auto-generated method stub
wg.lianWang();
}
@Override
public void shangWang() {
// TODO Auto-generated method stub
wg.shangWang();
}
@Override
public void duanWang() {
// TODO Auto-generated method stub
wg.duanWang();
}
}
package TKaoShi;
public class TestInternetMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
WangGuan wg=new WangGuan();
WangMin wm=new WangMin();
wm.setWang(wg);
wm.lianWang();
wm.shangWang();
wm.duanWang();
}
}