java设计模式之单例,工厂,代理模式

一、设计模式的分类

总体来说设计模式分为三大类:

创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。

结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。

行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访

问者模式、中介者模式、解释器模式。


1.工厂模式

1).普通工厂模式

 public interface Sender{
	 public void Send();
 }

public class MailSender implements Sender{

	@Override
	public void Send() {
		// TODO Auto-generated method stub
		System.out.println("我是Main");
	}

}


public class SmsSender implements Sender{

	@Override
	public void Send() {
		// TODO Auto-generated method stub
		System.out.println("我是Sms");
	}

}

public class Fractory {
public Sender product(String type) {
	// TODO Auto-generated method stub
	if("mail".equals(type)){
		return new MailSender();
	}else if("sms".contentEquals(type)){
		return new SmsSender();
	}else {
		System.out.println("输入正确的类型");
		return null;
		
	}
}

}

public static void main(String[] args){
	Fractory factory=new Fractory();
	Sender sender=factory.product("mail");
	sender.Send();
	
	}
	}


2).多个工厂方法模式

public class SendFactory{
	public Sender produceMail(){  
        return new MailSender();  
    }  
      
    public Sender produceSms(){  
        return new SmsSender();  
    }  
}  
}

public class FactoryTest {  
  
    public static void main(String[] args) {  
        SendFactory factory = new SendFactory();  
        Sender sender = factory.produceMail();  
        sender.Send();  
    }  
}  


3).静态工厂方法模式

public class SendFactory {  
      
    public static Sender produceMail(){  
        return new MailSender();  
    }  
      
    public static Sender produceSms(){  
        return new SmsSender();  
    }  
}  
public class FactoryTest {  
  
    public static void main(String[] args) {      
        Sender sender = SendFactory.produceMail();  
        sender.Send();  
    }  



2.单例模式

1).普通单例模式

public class Singleton {
	
	//普通单例模式
//	主要优点:
//	1、提供了对唯一实例的受控访问。
//	2、由于在系统内存中只存在一个对象,因此可以节约系统资源,对于一些需要频繁创建和销毁的对象单例模式无疑可以提高系统的性能。
//	3、允许可变数目的实例。
//	 
//	主要缺点:
//	1、由于单利模式中没有抽象层,因此单例类的扩展有很大的困难。
//	2、单例类的职责过重,在一定程度上违背了“单一职责原则”。
//	3、滥用单例将带来一些负面问题,如为了节省资源将数据库连接池对象设计为的单例类,可能会导致共享连接池对象的程序过多而出现连接池溢出;如果实例化的对象长时间不被利用,系统会认为是垃圾而被回收,这将导致对象状态的丢失。
   private static Singleton instance=null;
   private  Singleton(){
	   
   }
   //线程安全
   public static  Singleton Instance(){
	   if(instance ==null){
		   synchronized (instance) {
			   if(instance==null){
				   instance = new Singleton();
			   }
		   }
		   
	   }
	   return instance;
   }
   public Object readResolve(){
	   return instance;
   }
}


2).饿汉模式

public class Hungrymode {
	//饿汉模式
	private static Hungrymode singleton = new Hungrymode();
	private Hungrymode(){
		
	}
	public static Hungrymode getInstance(){
		return singleton;
	}
}


3).饱汉模式

public class Fullmode {
	//饱汉模式   锁
	private static Fullmode instance=null;
	   private  Fullmode(){
		   
	   }
	   //线程安全
	   public static  Fullmode Instance(){
		   if(instance ==null){
			   synchronized (instance) {
				   if(instance==null){
					   instance = new Fullmode();
				   }
			   }
			   
		   }
		   return instance;
	   }
}



3.代理模式

public interface Sourceable {
	public void method();

}

public class Source implements Sourceable {

	@Override
	public void method() {
		// TODO Auto-generated method stub
		System.err.println("我是中心");
	}

}


public class Proxy implements Sourceable {
	private Source source;
		public Proxy() {
			// TODO Auto-generated constructor stub
			//super();
			this.source=new Source();
			}
		@Override
		public void method() {
		// TODO Auto-generated method stub
//			System.err.println("为什么");
			after();
			source.method();
			before();
		
		}
		public void after(){
			System.out.println("在什么之后");
		}
		public void before(){
			System.out.println("在什么之前");
		}
		


}


public class ProxyText {
public static void main(String[] args){
	Sourceable source=new Proxy();
	source.method();
}
}














  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值