标签:blog http io os ar 使用 java sp strong
1.单例设计模式
所谓单例设计模式简单说就是无论程序如何运行,采用单例设计模式的类(Singleton类)永远只会有一个实例化对象产生。具体实现步骤如下:
(1) 将采用单例设计模式的类的构造方法私有化(采用private修饰)。
(2) 在其内部产生该类的实例化对象,并将其封装成private static类型。
(3) 定义一个静态方法返回该类的实例。
示例代码如下:
- class Singleton {
- private static Singleton instance = new Singleton();
-
- public static Singleton getInstance() {
- return instance;
- }
-
- private Singleton() {
- }
-
- public void print() {
- System.out.println("Hello World!!!");
- }
- }
-
- public class SingletonDemo {
- public static void main(String args[]) {
- Singleton s1 = null;
- Singleton s2 = null;
- Singleton s3 = null;
- s1 = Singleton.getInstance();
- s2 = Singleton.getInstance();
- s3 = Singleton.getInstance();
- s1.print();
- s2.print();
- s3.print();
- }
- }
单例模式的实现
-
- public class SingletonTest {
-
- private SingletonTest() {
- }
-
- private static final SingletonTest instance = new SingletonTest();
-
- public static SingletonTest getInstancei() {
- return instance;
- }
-
- }
- public class SingletonTest {
- private SingletonTest() {
- }
-
- private static SingletonTest instance;
-
- public static SingletonTest getInstance() {
- if (instance == null)
- instance = new SingletonTest();
- return instance;
- }
- }
- public class SingletonTest {
- private SingletonTest() {
- }
-
- private static SingletonTest instance;
-
- public static synchronized SingletonTest getInstance() {
- if (instance == null)
- instance = new SingletonTest();
- return instance;
- }
- }
- public class SingletonTest {
- private static SingletonTest instance;
-
- private SingletonTest() {
- }
-
- public static SingletonTest getIstance() {
- if (instance == null) {
- synchronized (SingletonTest.class) {
- if (instance == null) {
- instance = new SingletonTest();
- }
- }
- }
- return instance;
- }
- }
2.工厂设计模式
程序在接口和子类之间加入了一个过渡端,通过此过渡端可以动态取得实现了共同接口的子类实例化对象。
示例代码如下:
- interface Animal {
- public void say();
- }
-
- class Cat implements Animal {
- @Override
- public void say() {
- System.out.println("我是猫咪,喵呜!");
- }
- }
-
- class Dog implements Animal {
-
- @Override
- public void say() {
- System.out.println("我是小狗,汪汪!");
- }
- }
-
- class Factory {
- public static Animal getInstance(String className) {
- Animal a = null;
- if ("Cat".equals(className)) {
- a = new Cat();
- }
- if ("Dog".equals(className)) {
- a = new Dog();
- }
- return a;
- }
- }
-
- public class FactoryDemo {
-
- public static void main(String[] args) {
- Animal a = null;
- a = Factory.getInstance(args[0]);
- if (a != null) {
- a.say();
- }
- }
- }
3.代理设计模式
指由一个代理主题来操作真实主题,真实主题执行具体的业务操作,而代理主题负责其他相关业务的处理。比如生活中的通过代理访问网络,客户通过网络代理连接网络(具体业务),由代理服务器完成用户权限和访问限制等与上网相关的其他操作(相关业务)。
示例代码如下:
- interface Network {
- public void browse();
- }
-
- class Real implements Network {
- public void browse() {
- System.out.println("上网浏览信息!");
- }
- }
-
- class Proxy implements Network {
- private Network network;
-
- public Proxy(Network network) {
- this.network = network;
- }
-
- public void check() {
- System.out.println("检查用户是否合法!");
- }
-
- public void browse() {
- this.check();
- this.network.browse();
- }
- }
-
- public class ProxyDemo {
- public static void main(String args[]) {
- Network net = null;
- net = new Proxy(new Real());
- net.browse();
- }
- }
4.观察者设计模式
所谓观察者模式,举个例子现在许多购房者都密切观察者房价的变化,当房价变化时,所有购房者都能观察到,以上的购房者属于观察者,这便是观察者模式。
java中可以借助Observable类和Observer接口轻松实现以上功能。当然此种模式的实现也不仅仅局限于采用这两个类。
示例代码如下:
- import java.util.Observable;
- import java.util.Observer;
-
- class House extends Observable {
- private float price;
-
- public void setPrice(float price) {
- this.setChanged();
- this.notifyObservers(price);
- this.price = price;
- }
-
- public float getPrice() {
- return this.price;
- }
-
- public House(float price) {
- this.price = price;
- }
-
- public String toString() {
- return "房子价格为: " + this.price;
- }
- }
-
- class HousePriceObserver implements Observer {
- private String name;
-
- public HousePriceObserver(String name) {
- super();
- this.name = name;
- }
-
- @Override
- public void update(Observable o, Object arg) {
- if (arg instanceof Float) {
- System.out.println(this.name + "观察的价格更改为:"
- + ((Float) arg).floatValue());
- }
-
- }
-
- }
-
- public class ObserDeom {
- public static void main(String[] args) {
- House h = new House(1000000);
- HousePriceObserver hpo1 = new HousePriceObserver("购房者A");
- HousePriceObserver hpo2 = new HousePriceObserver("购房者B");
- HousePriceObserver hpo3 = new HousePriceObserver("购房者C");
- h.addObserver(hpo1);
- h.addObserver(hpo2);
- h.addObserver(hpo3);
- System.out.println(h);
-
- h.setPrice(2222222);
- System.out.println(h);
- }
- }
5.适配器模式
如果一个类要实现一个具有很多抽象方法的接口,但是本身只需要实现接口中的部分方法便可以达成目的,所以此时就需要一个中间的过渡类,但此过渡类又不希望直接使用,所以将此类定义为抽象类最为合适,再让以后的子类直接继承该抽象类便可选择性的覆写所需要的方法,而此抽象类便是适配器类。
示例代码如下:
- interface Window {
- public void open();
-
- public void close();
-
- public void iconified();
-
- public void deiconified();
-
- public void activated();
- }
-
- abstract class WindowAdapter implements Window {
- public void open() {
- };
-
- public void close() {
- };
-
- public void iconified() {
- };
-
- public void deiconified() {
- };
-
- public void activated() {
- };
- }
-
- class WindowImpl extends WindowAdapter {
- public void open() {
- System.out.println("窗口打开");
- }
-
- public void close() {
- System.out.println("窗口关闭");
- }
- }
-
- public class AdapterDemo {
- public static void main(String args[]) {
- Window win = new WindowImpl();
-
- win.open();
- win.close();
- }
- }
http://blog.csdn.net/haoxingfeng/article/details/9191619
java几种常用设计模式简单示例
标签:blog http io os ar 使用 java sp strong
原文地址:http://www.cnblogs.com/wikiki/p/4060889.html