【java标准库系列】重学设计模式

设计模式简介

设计模式分为三种类型,共23种:

  • 创建型模式
    单例模式、抽象工厂模式、建造者模式、工厂模式、原型模式。

  • 结构型模式
    适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。

  • 行为型模式
    模版方法模式、命令模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式、状态模式、策略模式、职责链模式(责任链模式)、访问者模式。

创建型模式

工厂方法模式

抽象产品类:

public abstract class Product {
public void method1(){
};
}

具体产品类1:

package com.jxdw.abstractfactory;

public class ConcreteProduct1 extends Product {
public void method2(){
System.out.println("-------------------ConcreteProduct1 method2----------------");
}
}

具体产品类2:

package com.jxdw.abstractfactory;

public class ConcreteProduct2 extends Product {
public void method2(){
System.out.println("ConcreteProduct2 method2");
}
}

抽象工厂类:

package com.jxdw.abstractfactory;

public abstract class Creator {
public abstract <T extends Product> T creator(Class<T> c);
}

具体工厂类:

package com.jxdw.abstractfactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ConcreteCreator extends Creator {

@Override
public <T extends Product> T creator(Class<T> c) {
Product product=null;
try {
product=(Product) Class.forName(c.getName()).newInstance();
Method method=c.getMethod("method2",null);
method.invoke(product,null);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return (T)product;
}
}

测试类:

package com.jxdw.abstractfactory;

public class Client {
public static void main(String[] args){
Creator creator=new ConcreteCreator();
Product product=creator.creator(ConcreteProduct1.class);
//((ConcreteProduct1) product).method2();
}
}

结构型模式

代理模式

接口:

package com.jxdw.Proxy;

public interface IGamePlayer {
public void login(String username,String password);
public void killBoss();
public void upgrade();
}

具体类:

package com.jxdw.Proxy;

public class GamePlayer implements IGamePlayer {
private String name="";

public GamePlayer(String name) {
this.name = name;
}

@Override
public void login(String username, String password) {
System.out.println("login username: "+username+",password: "+password);
}

@Override
public void killBoss() {
System.out.println("kill boss");
}

@Override
public void upgrade() {
System.out.println("upgrade game");
}
}

代理类

package com.jxdw.Proxy;

public class GamePlayerProxy {
private IGamePlayer iGamePlayer;

public GamePlayerProxy(IGamePlayer iGamePlayer) {
this.iGamePlayer = iGamePlayer;
}
public void killBoss(){
this.iGamePlayer.killBoss();
}
public void upgrade(){
this.iGamePlayer.upgrade();
}
public void login(String username,String password){
this.iGamePlayer.login(username,password);
}
}

测试类:

package com.jxdw.Proxy;

public class Client {
public static void main(String[] args) {
IGamePlayer gamePlayer=new GamePlayer("zhangsan");
GamePlayerProxy gamePlayerProxy=new GamePlayerProxy(gamePlayer);
gamePlayerProxy.login("zhangsan","password");
}
}

行为型模式

观察者模式

观察者接口:

package com.jxdw;

public interface IObserver {
void update(String message);
}

主题接口:

package com.jxdw;

public interface ISubject {
void addObserver(IObserver iObserver);
void deleteObserver(IObserver iObserver);
void inform();
}

观察者实现:

package com.jxdw;

public class CompleteObserver implements IObserver {
@Override
public void update(String message) {
System.out.println("observer receive "+message);
}
}

主题实现:

package com.jxdw;

import java.util.Vector;

public class CompleteSubject implements ISubject {
private Vector<IObserver> observerVector=new Vector<IObserver>();
@Override
public void addObserver(IObserver iObserver) {
observerVector.add(iObserver);
}

@Override
public void deleteObserver(IObserver iObserver) {
observerVector.removeElement(iObserver);
}

@Override
public void inform() {
System.out.println("message");
for (IObserver iObserver:observerVector){
iObserver.update("message");
}
}
}

测试类:

package com.jxdw;

public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
ISubject completeSubject=new CompleteSubject();
completeSubject.addObserver(new CompleteObserver());
completeSubject.inform();
}
}