`

Java设计模式

    博客分类:
  • Java
阅读更多

一、单例模式

 

package com.model.singletone;

public class SingleTone {

    private static SingleTone singleTone;

    private SingleTone() {

    }

    public static SingleTone getInstance() {

        if (singleTone == null) {
            singleTone = new SingleTone();
        }

        return singleTone;
    }
}


package com.model.singletone;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        SingleTone singleTone1 = SingleTone.getInstance();

        SingleTone singleTone2 = SingleTone.getInstance();

        System.out.println(singleTone1 == singleTone2);
    }

}
 

 二、观察者模式

 

package com.model.observer;

import static java.lang.String.format;

import java.util.Observable;

public class PersonObservable extends Observable {

    private String name;

    private Integer age;

    public PersonObservable(String name, Integer age) {

        this.name = name;
        this.age = age;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {

        this.name = name;
    }

    public Integer getAge() {

        return age;
    }

    public void setAge(Integer age) {

        this.age = age;
    }

    public void say(String message) {

        System.out.println(format("%s say %s!", name, message));
        setChanged();
        notifyObservers(message);
    }
}


package com.model.observer;

import static java.lang.System.out;

import java.util.Observable;
import java.util.Observer;

public class PersonObserver implements Observer {

    @Override
    public void update(Observable observable, Object message) {

        // PersonObservable personObservable = (PersonObservable) observable;
        // System.out.println(personObservable.getName());
        out.println(message);
    }

}


package com.model.observer;

public class Test {

    public static void main(String[] args) {

        PersonObservable person = new PersonObservable("LL", 11);
        person.addObserver(new PersonObserver());
        person.addObserver(new PersonObserver());
        person.addObserver(new PersonObserver());
        person.say("hello");
        person.say("world");
    }
}
 

三、代理模式

 

package com.model.invocationhandler;
public interface HelloWorld {

    void sayHelloWorld();

}


package com.model.invocationhandler;
public class HelloWorldImpl implements HelloWorld {

    @Override
    public void sayHelloWorld() {

        System.out.println("Hello World!");

    }

}


package com.model.invocationhandler;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class HelloWorldHandler implements InvocationHandler {

    // 要代理的原始对象

    private final Object objOriginal;

    /**
     * 
     * 构造函数。
     * 
     * @param obj
     *            要代理的原始对象。
     */

    public HelloWorldHandler(Object obj) {

        this.objOriginal = obj;

    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)

    throws Throwable {

        Object result;

        // 方法调用之前

        doBefore();

        // 调用原始对象的方法

        result = method.invoke(this.objOriginal, args);

        // 方法调用之后

        doAfter();

        return result;

    }

    private void doBefore() {

        System.out.println("before method invoke!");

    }

    private void doAfter() {

        System.out.println("after method invoke!");

    }

} 


package com.model.invocationhandler;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class Test {

    public static void main(String[] args) {

        HelloWorld hw = new HelloWorldImpl();

        InvocationHandler handler = new HelloWorldHandler(hw);

        HelloWorld proxy = (HelloWorld) Proxy.newProxyInstance(

        hw.getClass().getClassLoader(),

        hw.getClass().getInterfaces(),

        handler);

        proxy.sayHelloWorld();

    }

}
 

四、门面模式

 

package com.model.face;

/**
 * 购物中心
 * 
 * @author charles
 * 
 */
public class ShopingCenter {

    // 到购物中心的服饰店购买
    public void clothingStore() {

        Clothing clothing = new Clothing();
        clothing.getClothing(); // 购买到衣服
    }

    // 到购物中心的电器店购买
    public void EleStore() {

        Ele ele = new Ele();
        ele.getEle();// 购买到电器
    }

    // 到购物中心的首饰店购买
    public void JewelryStore() {

        Jewelry Jewelry = new Jewelry();
        Jewelry.getJewelry();// 购买到首饰
    }

    /**
     * 电器店
     * 
     * @author charles
     * 
     */
    class Ele {

        public void getEle() {

            System.out.println("购买到电器!");
        }
    }

    /**
     * 服装店
     * 
     * @author charles
     * 
     */
    class Clothing {

        public void getClothing() {

            System.out.println("购买到衣服!");
        }

    }

    /**
     * 首饰店
     * 
     * @author charles
     * 
     */
    class Jewelry {

        public void getJewelry() {

            System.out.println("购买到首饰!");
        }
    }

    public static void main(String[] args) {

        ShopingCenter shopingCenter = new ShopingCenter();
        shopingCenter.clothingStore();
        shopingCenter.EleStore();
        shopingCenter.JewelryStore();
    }
}
 

 

五、适配器模式

 

package com.model.adapter;

public class Client {

    public static void main(String[] args) {

        Client client = new Client();
        Target target = client.new Target();
        System.out.println(target.request());

        Adaptee adaptee = client.new Adaptee();
        Adapter adapter = client.new Adapter(adaptee);
        System.out.println(adapter.request());// 目的就是让适配器能做其他块的功能
    }

    class Target {

        public String request() {

            return "Graphic sender";
        }
    }

    class Adaptee {

        public String getData() {

            return "CPU DATA";
        }
    }

    class Adapter extends Target {

        private Adaptee adaptee = null;

        public Adapter(Adaptee adaptee) {

            this.adaptee = adaptee;
        }

        @Override
        public String request() {

            return adaptee.getData();
        }
    }
}
 

 

六、工厂模式

 

package com.model.factory;

/**
 * 单例工厂模式
 * 
 * @author charles
 * 
 */
public class ProductFactory {

    private static Product product;

    private static ProductFactory productFactory = new ProductFactory();

    public static Product createProduct() throws InstantiationException, IllegalAccessException {

        if (product == null) {
            product = productFactory.new Product();
        }

        return product;

    }

    class Product {

        public void doSomething() {

            System.out.println("hello world!");
        }
    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {

        Product product = ProductFactory.createProduct();
        product.doSomething();
    }
}
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics