标签:
定义:确保一个类只有一个实例,并提供一个全局访问点; 使用场景:有一些对象我们只需要一个,例如:线程池,缓存,对话框,注册表的对象,日志对象等,假如出现多个反而会出现问题; 与全局变量相比:首先你要明白第一点,单例模式既然能出现这么多年就说明它肯定有它的优势才对;另一方面:使用单例模式相比如全局变量,它既有全局变量的优点,提供一个全局访问点,同时没有全局变量所带来一开始就要求创建好对象的缺点。
public static MyClass getNewInstance()
{
return obj;
}
package com.singleton; public class SingletonTest { public static void main(String[] args) { Singleton singleton = Singleton.getNewInstance(); System.out.println(singleton.hashCode()); Singleton singleton1 = Singleton.getNewInstance(); System.out.println(singleton1.hashCode()); System.out.println(singleton == singleton1); } } class Singleton { private static Singleton singleton = new Singleton(); private Singleton() { } public static Singleton getNewInstance() { return singleton; } }
单例模式还有一种,可以将创建对象放到方法中:这样在多线程的环境下可能不止产生一个对象。
public static Singleton getNewInstance() { if (singleton == null) { singleton = new Singleton(); } return singleton; }
定义:定义了算法族,分别封装起来,让他们之间可以相互替换,此模式在于让算法的变化独立于使用算法的客户; 设计原则:找出应用中可能的变化之处,把它们独立起来,不要和那些不需要变化的代码混在一起;针对接口编程,不要针对实现编程;多用组合,少用继承。 使用场景:模拟鸭子–给鸭子添加fly(),重写quack()等。
package com.strategy; public interface StrategyI { public int calc(int a, int b); }
package com.strategy; public class AddStrategy implements StrategyI { @Override public int calc(int a, int b) { return a + b; } }
package com.strategy; public class Environment { public StrategyI strategy; public Environment(StrategyI s) { strategy = s; } public StrategyI getStrategy() { return strategy; } public void setStrategy(StrategyI s) { strategy = s; } public void perform() { System.out.println(strategy.calc(3, 4)); } }
package com.strategy;
public class Client
{
public static void main(String[] args)
{
StrategyI s1 = new AddStrategy();
Environment env1 = new Environment(s1);
env1.perform();
StrategyI s2 = new SubtractStrategy();
Environment env2 = new Environment(s2);
env2.perform();
env2.setStrategy(s1);
env2.perform();
}
}
观察者模式定义了一种一对多的依赖关系,让多个观察者同时监听某一个主题对象,当主题对象的状态情况发生变化时,他会通知所有的观察者对象,让他们能够改变自己。
jdk中使用了大量的观察者模式,它提供了Observer接口与Observable抽象类,我们一般诟病jdk的设计正是在于它把抽象主题角色定义为一个抽象类而不是使用接口。
抽象主题角色
package com.observer; public interface Watched { public void addWatcher(Watcher watcher); public void removeWatcher(Watcher watcher); public void notifyWatcher(String s); }
抽象观察者模式
package com.observer; public interface Watcher { public void update(String s); }
具体主题角色
package com.observer; import java.util.ArrayList; public class ConcreteWatched implements Watched { ArrayList<Watcher> list = new ArrayList(); @Override public void addWatcher(Watcher watcher) { list.add(watcher); } @Override public void removeWatcher(Watcher watcher) { list.remove(watcher); } @Override public void notifyWatcher(String s) { for(Watcher watcher : list) { watcher.update(s); } } }
具体观察者模式
package com.observer; public interface Watcher { public void update(String s); }
使用测试
package com.observer; public class Client { public static void main(String[] args) { ConcreteWatched girl = new ConcreteWatched(); ConcreteWatcher boy1 = new ConcreteWatcher(); ConcreteWatcher boy2 = new ConcreteWatcher(); ConcreteWatcher boy3 = new ConcreteWatcher(); girl.addWatcher(boy1); girl.addWatcher(boy2); girl.addWatcher(boy3); girl.notifyWatcher("我难过"); girl.removeWatcher(boy2); girl.notifyWatcher("happy"); } }
装饰者模式动态地将责任附加到对象上,若要扩展功能功能,装饰者提供了比继承更有弹性的替代文件
对扩展开放,对修改关闭
package com.decorator; public interface Component { public void saySomething(); }
package com.decorator; public class ConcreteCompoent implements Component { @Override public void saySomething() { System.out.println("ConcreteCompoent is running"); } }
package com.decorator; public class Decorator implements Component { public Component component; @Override public void saySomething() { component.saySomething(); } public void doThing() { System.out.println("Decorator haha"); } }
package com.decorator; public class ConcreteDecorator extends Decorator { public Component component; public ConcreteDecorator(Component component) { this.component = component; } @Override public void saySomething() { component.saySomething(); this.doThing(); } public void doThing() { System.out.println("ConcreteDecorator haha"); } }
package com.decorator; public class Client { public static void main(String[] args) { Component component = new ConcreteDecorator2(new ConcreteDecorator(new ConcreteCompoent())); component.saySomething(); } }
标签:
原文地址:http://www.cnblogs.com/dslover/p/4306113.html