设计模式

 抽象工厂模式factory:

public interface Send {
	public void send();
}
public class SmsSendImpl implements Send {
	@Override
	public void send() {
		// TODO Auto-generated method stub
		System.out.println("sms send");
	}
}
public class MailSendImpl implements Send {
	@Override
	public void send() {
		// TODO Auto-generated method stub
		System.out.println("mail send");
	}
}
public class FactoryCreate {
	public Send produce(String type){
		if("mail".equals(type)){
			return new MailSendImpl();
		}else if("sms".equals(type)){
			return new SmsSendImpl();
		}else{
			return null;
		}
	}
}
public static void main(String[] args) {
		// TODO Auto-generated method stub
		FactoryCreate   fc = new FactoryCreate();
		Send send = fc.produce("sms");
		send.send();
	}  

单例模式Singleton

public class SingleTon {
	private static SingleTon  singleton = null ;
	public static SingleTon getInstance(){
		if(singleton == null){
			singleton = new SingleTon();
		}
		return singleton;
	}
}
public static void main(String[] args) {
		// TODO Auto-generated method stub
		SingleTon s = SingleTon.getInstance();
		System.out.println("--"+s);
	}

原型模式prototype:

public abstract class AbstractSpoon implements Cloneable {
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}	
}
public class SoupSpoon extends AbstractSpoon {
	public SoupSpoon() {
		super();
		// TODO Auto-generated constructor stub
		this.setName("aaa");
	}
}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		AbstractSpoon ss = new SoupSpoon();
		String name = ss.getName();
		System.out.println("--"+name);
	}

  

 

  

 

 

转载于:https://www.cnblogs.com/estellez/p/4150765.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值