
java设计模式
文章平均质量分 61
iteye_8208
这个作者很懒,什么都没留下…
展开
-
Flyweight模式
public class Flyweight { public static void main(String[] args) { IConnection con = ConnectionPool.getInstance().getConnection(); con.query("select * from user"); ConnectionPool.get...原创 2016-04-02 13:30:10 · 89 阅读 · 0 评论 -
Strategy模式
package com.jaeson.javastudy.designpattern;/** * 策略模式属于对象的行为模式。 * 其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。 * 策略模式使得算法可以在不影响到客户端的情况下发生变化。 * */public class StrategyPattern { ...原创 2016-10-08 11:33:37 · 128 阅读 · 0 评论 -
单例这种设计模式
随着我们编写代码的深入,我们或多或少都会接触到设计模式,其中单例(Singleton)模式应该是我们耳熟能详的一种模式。本文将比较特别的介绍一下Java设计模式中的单例模式。概念单例模式,又称单件模式或者单子模式,指的是一个类只有一个实例,并且提供一个全局访问点。实现思路在单例的类中设置一个private静态变量sInstance,sInstance类型为当前类,用来持有单例...原创 2016-10-05 17:38:12 · 78 阅读 · 0 评论 -
Observer模式
import java.util.*;interface ISubject { public void attach(IObserver observer); public void detach(IObserver observer); public void notifyObservers(); public void setState(int state);}...原创 2016-04-16 10:27:06 · 92 阅读 · 0 评论 -
Factory Method
interface IFactory { public IProduct produce();}interface IProduct { public void drive();}public class FactoryMethod { public static void main(String[] args) { IFactory carF...原创 2016-04-16 10:26:22 · 97 阅读 · 0 评论 -
builder模式
interface IBuilder { public void buildCPU(); public void buildScreen(); public void buildBattery(); public Phone getPhone();}public class Builder { public static void main(String[] a...原创 2016-04-16 10:25:57 · 106 阅读 · 0 评论 -
Proxy模式
interface IService { public void execute();}class ServiceImpl implements IService { public ServiceImpl() { System.out.println("ServiceImpl is create"); } @Override public void execute...原创 2016-04-16 10:25:23 · 143 阅读 · 0 评论 -
Facade模式
public class Facade { public static void main(String[] args) { Computer com = new Computer(); com.startup(); com.shutdown(); }}class Computer { private CPU cpu; private M...原创 2016-04-16 10:24:40 · 98 阅读 · 0 评论 -
adapter 模式
package com.jaeson.javastudy.designpattern;/** * java io 的字节/字符流 转换使用了adapter模式,InputStreamReader、OutputStreamWriter * * 将一个类的接口转换成客户希望的另外一个接口。Adapter 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。 * ...原创 2016-04-02 13:32:04 · 86 阅读 · 0 评论 -
Decorator模式
interface IService { public void execute();}public class Decorator implements IService { private IService service; public Decorator(IService service) { this.service = service; } ...原创 2016-04-02 13:31:50 · 126 阅读 · 0 评论 -
Composite模式
import java.util.Iterator;interface IComponent { public void operate(); public void add(IComponent component); public void remove(IComponent component); public Iterator<IComponent&...原创 2016-04-02 13:31:26 · 88 阅读 · 0 评论 -
bridge模式
import java.io.Serializable;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;interface IConnection { public void query(String sql);}interface IDriver { public DIALECT...原创 2016-04-02 13:30:31 · 78 阅读 · 0 评论 -
Java实现单例的难点
有简单又高效的方法可以实现单例模式,但没有一种方式能在任何情况下都确保单例的完整性。单例模式是指某个类只被实例化一次,用来表示全局或系统范围的组件。单例模式常用于日志记录、工厂、窗口管理器和平台组件管理等。我认为要尽量避免使用单例模式,因为一旦实现就很难改变或重载,而且会造成编写测试用例困难、代码结构糟糕等问题。另外,下面文章中的单例模式是不安全的。人们花大量的精力研究怎样更好地实现单...原创 2016-12-10 08:50:00 · 127 阅读 · 0 评论