zoukankan      html  css  js  c++  java
  • 设计模式

     抽象工厂模式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);
    	}
    

      

     

      

     

  • 相关阅读:
    PHP之十六个魔术方法详解
    PHP之十六个魔术方法详解
    让Docker容器使用静态独立的外部IP(便于集群组建)
    桥接和nat模式区别
    使用 GitHub / GitLab 的 Webhooks 进行网站自动化部署
    docker gitlab
    docker gitlab安装
    docker ui管理工具
    docker 批量删除容器
    Scrapy设置代理
  • 原文地址:https://www.cnblogs.com/estellez/p/4150765.html
Copyright © 2011-2022 走看看