您的位置:首页 > 编程语言 > Java开发

java23种设计模式代码

2017-12-27 14:09 706 查看
java中的设计模式有23种:

创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。

结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。

行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
其实还有两种:并发型模式和线程池模式。

接下来贴出每种设计模式的代码:

一、工厂模式

1、简单工厂模式

public interface Sender {
public void Send();
}
public class MailSender implements Sender {
@Override
public void Send() {
System.out.println("this is mailsender!");
}
}
public class SmsSender implements Sender {

@Override
public void Send() {
System.out.println("this is sms sender!");
}
}
建立工厂类:

public class SendFactory {

public Sender produce(String type) {
if ("mail".equals(type)) {
return new MailSender();
} else if ("sms".equals(type)) {
return new SmsSender();
} else {
System.out.println("请输入正确的类型!");
return null;
}
}
}
测试:

public class FactoryTest {
public static void main(String[] args) {
SendFactory factory = new SendFactory();
Sender sender = factory.produce("sms");
sender.Send();
}
}
2、工厂方法模式

把上面的SendFactory改动下:

public class SendFactory {
public Sender produceMail(){
return new MailSender();
}

public Sender produceSms(){
return new SmsSender();
}
}
测试:

public class FactoryTest {

public static void main(String[] args) {
SendFactory factory = new SendFactory();
Sender sender = factory.produceMail();
sender.Send();
}
}
3、抽象工厂模式

public interface Sender {
public void Send();
}
public class MailSender implements Sender {
@Override
public void Send() {
System.out.println("this is mailsender!");
}
}
public class SmsSender implements Sender {
@Override
public void Send() {
System.out.println("this is sms sender!");
}
}


接口:

public interface Provider {
public Sender produce();
}


两个工厂:

public class SendMailFactory implements Provider {

@Override
public Sender produce(){
return new MailSender();
}
}
public class SendSmsFactory implements Provider{

@Override
public Sender produce() {
return new SmsSender();
}
}
测试:
public class Test {
public static void main(String[] args) {
Provider provider = new SendMailFactory();
Sender sender = provider.produce();
sender.Send();
}
}
二、单例模式

1、懒汉式

public class Singleton {

/* 持有私有静态实例,防止被引用,此处赋值为null,目的是实现延迟加载 */
private static Singleton instance = null;

/* 私有构造方法,防止被实例化 */
private Singleton() {
}

/* 静态工程方法,创建实例 */
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
在多线程下加上锁:
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
但是,synchronized关键字锁住的是这个对象,这样的用法,在性能上会有所下降,因为每次调用getInstance(),都要对对象上锁,事实上,只有在第一次创建对象的时候需要加锁,之后就不需要了:

public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}


但是,在Java指令中创建对
4000
象和赋值操作是分开进行的,也就是说instance = new Singleton();语句是分两步执行的。但是JVM并不保证这两个操作的先后顺序,也就是说有可能JVM会为新的Singleton实例分配空间,然后直接赋值给instance成员,然后再去初始化这个Singleton实例。这样就可能出错了,我们以A、B两个线程为例:

a>A、B线程同时进入了第一个if判断

b>A首先进入synchronized块,由于instance为null,所以它执行instance = new Singleton();

c>由于JVM内部的优化机制,JVM先画出了一些分配给Singleton实例的空白内存,并赋值给instance成员(注意此时JVM没有开始初始化这个实例),然后A离开了synchronized块。

d>B进入synchronized块,由于instance此时不是null,因此它马上离开了synchronized块并将结果返回给调用该方法的程序。

e>此时B线程打算使用Singleton实例,却发现它没有被初始化,于是错误发生了。
进一步优化:

private static class SingletonFactory{
private static Singleton instance = new Singleton();
}
public static Singleton getInstance(){
return SingletonFactory.instance;
}
单例模式使用内部类来维护单例的实现,JVM内部的机制能够保证当一个类被加载的时候,这个类的加载过程是线程互斥的。这样当我们第一次调用getInstance的时候,JVM能够帮我们保证instance只被创建一次,并且会保证把赋值给instance的内存初始化完毕,这样我们就不用担心上面的问题。同时该方法也只会在第一次调用的时候使用互斥机制,这样就解决了低性能问题。
但是如果在构造函数中抛出异常,实例将永远得不到创建,也会出错。
2、饿汉式

//饿汉式单例类.在类初始化时,已经自行实例化
public class Singleton1 {
private Singleton1() {}
private static final Singleton1 single = new Singleton1();
//静态工厂方法
public static Singleton1 getInstance() {
return single;
}
}
三、建造者模式

上面的一个Sender接口,两个实现类MailSender和SmsSender就不写了:

public class Builder {

private List<Sender> list = new ArrayList<Sender>();

public void produceMailSender(int count){
for(int i=0; i<count; i++){
list.add(new MailSender());
}
}

public void produceSmsSender(int count){
for(int i=0; i<count; i++){
list.add(new SmsSender());
}
}
}
测试:

public class Test {
public static void main(String[] args) {
Builder builder = new Builder();
builder.produceMailSender(10);
}
}
工厂模式关注的是创建单个产品,而建造者模式则关注创建符合对象,多个部分。

四、原型模式

class Prototype implements Cloneable {
public Prototype clone(){
Prototype prototype = null;
try{
prototype = (Prototype)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return prototype;
}
}
class ConcretePrototype extends Prototype{
public void show(){
System.out.println("原型模式实现类");
}
}
测试:

public class Client {
public static void main(String[] args){
ConcretePrototype cp = new ConcretePrototype();
for(int i=0; i< 10; i++){
ConcretePrototype clonecp = (ConcretePrototype)cp.clone();
clonecp.show();
}
}
}


浅复制:将一个对象复制后,基本数据类型的变量都会重新创建,而引用类型,指向的还是原对象所指向的。

深复制:将一个对象复制后,不论是基本数据类型还有引用类型,都是重新创建的。
例子:

public class Prototype implements Cloneable, Serializable {

private static final long serialVersionUID = 1L;
private String string;

private SerializableObject obj;

/* 浅复制 */
public Object clone() throws CloneNotSupportedException {
Prototype proto = (Prototype) super.clone();
return proto;
}

/* 深复制 */
public Object deepClone() throws IOException, ClassNotFoundException {

/* 写入当前对象的二进制流 */
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);

/* 读出二进制流产生的新对象 */
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
}

public String getString() {
return string;
}

public void setString(String string) {
this.string = string;
}

public SerializableObject getObj() {
return obj;
}

public void setObj(SerializableObject obj) {
this.obj = obj;
}

}

class SerializableObject implements Serializable {
private static final long serialVersionUID = 1L;
}
要实现深复制,需要采用流的形式读入当前对象的二进制输入,再写出二进制数据对应的对象。

还一种方法就是将引用类型另外拷贝,java提供的大部分的容器类都实现了Cloneable接口。

public class Prototype implements Cloneable {
private ArrayList list = new ArrayList();
public Prototype clone(){
Prototype prototype = null;
try{
prototype = (Prototype)super.clone();
prototype.list = (ArrayList) this.list.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return prototype;
}
}
五、适配器模式
1、类的适配器模式

public class Source {
public void method1() {
System.out.println("this is original method!");
}
}
public interface Targetable {
/* 与原类中的方法相同 */
public void method1();

/* 新类的方法 */
public void method2();
}
public class Adapter extends Source implements Targetable {
@Override
public void method2() {
System.out.println("this is the targetable method!");
}
}
测试:

public class AdapterTest {
public static void main(String[] args) {
Targetable target = new Adapter();
target.method1();
target.method2();
}
}
输出:

this is original method!
this is the targetable method!
2、对象的适配器模式

修改Adapter类的源码:

public class Wrapper implements Targetable {

private Source source;

public Wrapper(Source source){
super();
this.source = source;
}
@Override
public void method2() {
System.out.println("this is the targetable method!");
}

@Override
public void method1() {
source.method1();
}
}
测试:

public class AdapterTest {
public static void main(String[] args) {
Source source = new Source();
Targetable target = new Wrapper(source);
target.method1();
target.method2();
}
}
输出与第一种一样。

3、接口的适配器模式

public interface Sourceable {
public void method1();
public void method2();
}
public abstract class Wrapper2 implements Sourceable{
public void method1(){}
public void method2(){}
}
public class SourceSub1 extends Wrapper2 {
public void method1(){
System.out.println("the so
1b1e6
urceable interface's first Sub1!");
}
}
public class SourceSub2 extends Wrapper2 {
public void method2(){
System.out.println("the sourceable interface's second Sub2!");
}
}
测试:

public class WrapperTest {

public static void main(String[] args) {
Sourceable source1 = new SourceSub1();
Sourceable source2 = new SourceSub2();
source1.method1();
source1.method2();
source2.method1();
source2.method2();
}
}
输出:

the sourceable interface's first Sub1!
the sourceable interface's second Sub2!
当不希望实现一个接口中所有的方法时,可以创建一个抽象类Wrapper,实现所有方法,我们写别的类的时候,继承抽象类即可。

六、装饰器模式

public interface Sourceable {
public void method();
}
public class Source implements Sourceable {

@Override
public void method() {
System.out.println("the original method!");
}
}
public class Decorator implements Sourceable {

private Sourceable source;

public Decorator(Sourceable source){
super();
this.source = source;
}
@Override
public void method() {
System.out.println("before decorator!");
source.method();
System.out.println("after decorator!");
}
}
测试:

public class DecoratorTest {

public static void main(String[] args) {
Sourceable source = new Source();
Sourceable obj = new Decorator(source);
obj.method();
}
}
输出:

before decorator!
the original method!
after decorator!


装饰器模式与代理模式的区别:

装饰器模式关注于在一个对象上动态的添加方法,然而代理模式关注于控制对对象的访问。换句话 说,用代理模式,代理类(proxy
class)可以对它的客户隐藏一个对象的具体信息。因此,当使用代理模式的时候,我们常常在一个代理类中创建一个对象的实例。并且,当我们使用装饰器模 式的时候,我们通常的做法是将原始对象作为一个参数传给装饰者的构造器。
七、代理模式

1、静态代理

public interface IUserDao {
void save();
}
public class UserDao implements IUserDao {
public void save() {
System.out.println("----已经保存数据!----");
}
}
public class UserDaoProxy implements IUserDao{
//接收保存目标对象
private IUserDao target;
public UserDaoProxy(IUserDao target){
this.target=target;
}

public void save() {
System.out.println("开始事务...");
target.save();//执行目标对象的方法
System.out.println("提交事务...");
}
}
测试:

public class App {
public static void main(String[] args) {
//目标对象
UserDao target = new UserDao();

//代理对象,把目标对象传给代理对象,建立代理关系
UserDaoProxy proxy = new UserDaoProxy(target);

proxy.save();//执行的是代理的方法
}
}
2、动态代理

JDK的动态代理有一个限制,就是使用动态代理的对象必须实现一个或多个接口

JDK实现代理只需要使用newProxyInstance方法,但是该方法需要接收三个参数,完整的写法是:

static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,InvocationHandler h )
ClassLoader loader,:指定当前目标对象使用类加载器,获取加载器的方法是固定的
Class<?>[] interfaces,:目标对象实现的接口的类型,使用泛型方式确认类型
InvocationHandler h:事件处理,执行目标对象的方法时,会触发事件处理器的方法,会把当前执行目标对象的方法作为参数传入
public class ProxyFactory{

//维护一个目标对象
private Object target;
public ProxyFactory(Object target){
this.target=target;
}

//给目标对象生成代理对象
public Object getProxyInstance(){
return Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("开始事务2");
//执行目标对象方法
Object returnValue = method.invoke(target, args);
System.out.println("提交事务2");
return returnValue;
}
}
);
}

}
测试:

public class App {
public static void main(String[] args) {
// 目标对象
IUserDao target = new UserDao();
// 【原始的类型 class cn.itcast.b_dynamic.UserDao】
System.out.println(target.getClass());

// 给目标对象,创建代理对象
IUserDao proxy = (IUserDao) new ProxyFactory(target).getProxyInstance();
// class $Proxy0   内存中动态生成的代理对象
System.out.println(proxy.getClass());

// 执行方法   【代理对象】
proxy.save();
}
}
3、cglib代理

Cglib代理,也叫作子类代理,它是在内存中构建一个子类对象从而实现对目标对象功能的扩展

public class UserDao {

public void save() {
System.out.println("----已经保存数据!----");
}
}
public class ProxyFactory implements MethodInterceptor{
//维护目标对象
private Object target;

public ProxyFactory(Object target) {
this.target = target;
}

//给目标对象创建一个代理对象
public Object getProxyInstance(){
//1.工具类
Enhancer en = new Enhancer();
//2.设置父类
en.setSuperclass(target.getClass());
//3.设置回调函数
en.setCallback(this);
//4.创建子类(代理对象)
return en.create();

}

@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("开始事务...");

//执行目标对象的方法
Object returnValue = method.invoke(target, args);

System.out.println("提交事务...");

return returnValue;
}
}
测试:

public class App {

@Test
public void test(){
//目标对象
UserDao target = new UserDao();

//代理对象
UserDao proxy = (UserDao)new ProxyFactory(target).getProxyInstance();

//执行代理对象的方法
proxy.save();
}
}
八、外观模式

外观模式是为了解决类与类之间的依赖关系的。

public class CPU {

public void startup(){
System.out.println("cpu startup!");
}

public void shutdown(){
System.out.println("cpu shutdown!");
}
}
public class Memory {

public void startup(){
System.out.println("memory startup!");
}

public void shutdown(){
System.out.println("memory shutdown!");
}
}
public class Disk {

public void startup(){
System.out.println("disk startup!");
}

public void shutdown(){
System.out.println("disk shutdown!");
}
}
public class Computer {
private CPU cpu;
private Memory memory;
private Disk disk;

public Computer(){
cpu = new CPU();
memory = new Memory();
disk = new Disk();
}

public void startup(){
System.out.println("start the computer!");
cpu.startup();
memory.startup();
disk.startup();
System.out.println("start computer finished!");
}

public void shutdown(){
System.out.println("begin to close the computer!");
cpu.shutdown();
memory.shutdown();
disk.shutdown();
System.out.println("computer closed!");
}
}
测试:

public class User {

public static void main(String[] args) {
Computer computer = new Computer();
computer.startup();
computer.shutdown();
}
}
输出:

start the computer!
cpu startup!
memory startup!
disk startup!
start computer finished!
begin to close the computer!
cpu shutdown!
memory shutdown!
disk shutdown!
computer closed!
九、桥接模式

将抽象化与实现化解耦,使得二者可以独立变化,像我们常用的JDBC桥DriverManager一样,JDBC进行连接数据库的时候,在各个数据库之间进行切换,基本不需要动太多的代码,甚至丝毫不用动,原因就是JDBC提供统一接口,每个数据库提供各自的实现,用一个叫做数据库驱动的程序来桥接就行了。

public interface Sourceable {
public void method();
} 
public class SourceSub1 implements Sourceable {

@Override
public void method() {
System.out.println("this is the first sub!");
}
}
public class SourceSub2 implements Sourceable {

@Override
public void method() {
System.out.println("this is the second sub!");
}
}
定义一个桥:

public abstract class Bridge {
private Sourceable source;

public void method(){
source.method();
}

public Sourceable getSource() {
return source;
}

public void setSource(Sourceable source) {
this.source = source;
}
}
public class MyBridge extends Bridge {
public void method(){
getSource().method();
}
}
测试:

public class BridgeTest {

public static void main(String[] args) {

Bridge bridge = new MyBridge();

/*调用第一个对象*/
Sourceable source1 = new SourceSub1();
bridge.setSource(source1);
bridge.method();

/*调用第二个对象*/
Sourceable source2 = new SourceSub2();
bridge.setSource(source2);
bridge.method();
}
}
输出:

this is the first sub!
this is the second sub!
十、组合模式

又叫部分-整体模式

public class TreeNode {

private String name;
private TreeNode parent;
private Vector<TreeNode> children = new Vector<TreeNode>();

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public TreeNode getParent() {
return parent;
}

public void setParent(TreeNode parent) {
this.parent = parent;
}

//添加孩子节点
public void add(TreeNode node){
children.add(node);
}

//删除孩子节点
public void remove(TreeNode node){
children.remove(node);
}

//取得孩子节点
public Enumeration<TreeNode> getChildren(){
return children.elements();
}
}
public class Tree {

TreeNode root = null;

public Tree(String name) {
root = new TreeNode(name);
}

public static void main(String[] args) {
Tree tree = new Tree("A");
TreeNode nodeB = new TreeNode("B");
TreeNode nodeC = new TreeNode("C");

nodeB.add(nodeC);
tree.root.add(nodeB);
System.out.println("build the tree finished!");
}
}
十一、享元模式

也就是说在一个系统中如果有多个相同的对象,那么只共享一份就可以了,不必每个都去实例化一个对象,通常与工厂模式一起使用。

public abstract class Flyweight{
 public abstract void operation();
}
public class ConcreteFlyweight extends Flyweight{
 private String string;
 public ConcreteFlyweight(String str){
  string = str;
 }
 public void operation()
 {
  System.out.println("Concrete---Flyweight : " + string);
 }
}
工厂方法类:

public class FlyweightFactory{
 private Hashtable flyweights = new Hashtable();//----------------------------1
 public FlyweightFactory(){}
 public Flyweight getFlyWeight(Object obj){
  Flyweight flyweight = (Flyweight) flyweights.get(obj);//----------------2
  if(flyweight == null){//---------------------------------------------------3
   //产生新的ConcreteFlyweight
   flyweight = new ConcreteFlyweight((String)obj);
   flyweights.put(obj, flyweight);//--------------------------------------5
  }
  return flyweight;//---------------------------------------------------------6
 }
 public int getFlyweightSize(){
  return flyweights.size();
 }
}
测试:

public class FlyweightPattern{
 FlyweightFactory factory = new FlyweightFactory();
 Flyweight fly1;
 Flyweight fly2;
 Flyweight fly3;
 Flyweight fly4;
 Flyweight fly5;
 Flyweight fly6;
 /** *//** Creates a new instance of FlyweightPattern */
 public FlyweightPattern(){
  fly1 = factory.getFlyWeight("Google");
  fly2 = factory.getFlyWeight("Qutr");
  fly3 = factory.getFlyWeight("Google");
  fly4 = factory.getFlyWeight("Google");
  fly5 = factory.getFlyWeight("Google");
  fly6 = factory.getFlyWeight("Google");
 }
 public void showFlyweight(){
  fly1.operation();
  fly2.operation();
  fly3.operation();
  fly4.operation();
  fly5.operation();
  fly6.operation();
  int objSize = factory.getFlyweightSize();
  System.out.println("objSize = " + objSize);
 }
 public static void main(String[] args){
  System.out.println("The FlyWeight Pattern!");
  FlyweightPattern fp = new FlyweightPattern();
  fp.showFlyweight();
 }
}
输出:

Concrete---Flyweight : Google
Concrete---Flyweight : Qutr
Concrete---Flyweight : Google
Concrete---Flyweight : Google
Concrete---Flyweight : Google
Concrete---Flyweight : Google
objSize = 2


十二、策略模式
定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法的变化不会影响到使用算法的客户。

public interface ICalculator {
public int calculate(String exp);
}
辅助类:

public abstract class AbstractCalculator {

public int[] split(String exp,String opt){
String array[] = exp.split(opt);
int arrayInt[] = new int[2];
arrayInt[0] = Integer.parseInt(array[0]);
arrayInt[1] = Integer.parseInt(array[1]);
return arrayInt;
}
}
实现类:

public class Plus extends AbstractCalculator implements ICalculator {

@Override
public int calculate(String exp) {
int arrayInt[] = split(exp,"\\+");
return arrayInt[0]+arrayInt[1];
}
}
public class Minus extends AbstractCalculator implements ICalculator {

@Override
public int calculate(String exp) {
int arrayInt[] = split(exp,"-");
return arrayInt[0]-arrayInt[1];
}

}
public class Multiply extends AbstractCalculator implements ICalculator {

@Override
public int calculate(String exp) {
int arrayInt[] = split(exp,"\\*");
return arrayInt[0]*arrayInt[1];
}
}
测试:

public class StrategyTest {

public static void main(String[] args) {
String exp = "2+8";
ICalculator cal = new Plus();
int result = cal.calculate(exp);
System.out.println(result);
}
}
输出:

10
十三、模板方法模式

public abstract class AbstractCalculator {

/*主方法,实现对本类其它方法的调用*/
public final int calculate(String exp,String opt){
int array[] = split(exp,opt);
return calculate(array[0],array[1]);
}

/*被子类重写的方法*/
abstract public int calculate(int num1,int num2);

public int[] split(String exp,String opt){
String array[] = exp.split(opt);
int arrayInt[] = new int[2];
arrayInt[0] = Integer.parseInt(array[0]);
arrayInt[1] = Integer.parseInt(array[1]);
return arrayInt;
}
}
public class Plus extends AbstractCalculator {

@Override
public int calculate(int num1,int num2) {
return num1 + num2;
}
}
测试:

public class StrategyTest {

public static void main(String[] args) {
String exp = "8+8";
AbstractCalculator cal = new Plus();
int result = cal.calculate(exp, "\\+");
System.out.println(result);
}
}
十四、观察者模式

当一个对象变化时,其它依赖该对象的对象都会收到通知,并且随着变化!

public interface Observer {
public void update();
}
public class Observer1 implements Observer {

@Override
public void update() {
System.out.println("observer1 has received!");
}
}
public class Observer2 implements Observer {

@Override
public void update() {
System.out.println("observer2 has received!");
}
}
被观察主体:

public interface Subject {

/*增加观察者*/
public void add(Observer observer);

/*删除观察者*/
public void del(Observer observer);

/*通知所有的观察者*/
public void notifyObservers();

/*自身的操作*/
public void operation();
}
public abstract class AbstractSubject implements Subject {

private Vector<Observer> vector = new Vector<Observer>();
@Override
public void add(Observer observer) {
vector.add(observer);
}

@Override
public void del(Observer observer) {
vector.remove(observer);
}

@Override
public void notifyObservers() {
Enumeration<Observer> enumo = vector.elements();
while(enumo.hasMoreElements()){
enumo.nextElement().update();
}
}
}
public class MySubject extends AbstractSubject {

@Override
public void operation() {
System.out.println("update self!");
notifyObservers();
}
}
测试:

public class ObserverTest {

public static void main(String[] args) {
Subject sub = new MySubject();
sub.add(new Observer1());
sub.add(new Observer2());

sub.operation();
}
}
输出:

update self!
observer1 has received!
observer2 has received!
十五、迭代器模式

迭代器模式就是顺序访问聚集中的对象

public interface Collection {

public Iterator iterator();

/*取得集合元素*/
public Object get(int i);

/*取得集合大小*/
public int size();
}
public interface Iterator {
//前移
public Object previous();

//后移
public Object next();
public boolean hasNext();

//取得第一个元素
public Object first();
}
两个实现:

public class MyCollection implements Collection {

public String string[] = {"A","B","C","D","E"};
@Override
public Iterator iterator() {
return new MyIterator(this);
}

@Override
public Object get(int i) {
return string[i];
}

@Override
public int size() {
return string.length;
}
}
public class MyIterator implements Iterator {

private Collection collection;
private int pos = -1;

public MyIterator(Collection collection){
this.collection = collection;
}

@Override
public Object previous() {
if(pos > 0){
pos--;
}
return collection.get(pos);
}

@Override
public Object next() {
if(pos<collection.size()-1){
pos++;
}
return collection.get(pos);
}

@Override
public boolean hasNext() {
if(pos<collection.size()-1){
return true;
}else{
return false;
}
}

@Override
public Object first() {
pos = 0;
return collection.get(pos);
}

}
测试:

public class Test {

public static void main(String[] args) {
Collection collection = new MyCollection();
Iterator it = collection.iterator();

while(it.hasNext()){
System.out.println(it.next());
}
}
}
输出:

A B C D E
十六、责任链模式

public interface Handler {
public void operator();
}
public abstract class AbstractHandler {

private Handler handler;

public Handler getHandler() {
return handler;
}

public void setHandler(Handler handler) {
this.handler = handler;
}
}
public class MyHandler extends AbstractHandler implements Handler {

private String name;

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

@Override
public void operator() {
System.out.println(name+"deal!");
if(getHandler()!=null){
getHandler().operator();
}
}
}
测试:

public class Test {

public static void main(String[] args) {
MyHandler h1 = new MyHandler("h1");
MyHandler h2 = new MyHandler("h2");
MyHandler h3 = new MyHandler("h3");

h1.setHandler(h2);
h2.setHandler(h3);

h1.operator();
}
}
输出:

h1deal!
h2deal!
h3deal!
十七、命令模式

Invoker是调用者(司令员),Receiver是被调用者(士兵),MyCommand是命令,实现了Command接口,持有接收对象

public interface Command {
public void exe();
}
public class MyCommand implements Command {

private Receiver receiver;

public MyCommand(Receiver receiver) {
this.receiver = receiver;
}

@Override
public void exe() {
receiver.action();
}
}
public class Receiver {
public void action(){
System.out.println("command received!");
}
}
public class Invoker {

private Command command;

public Invoker(Command command) {
this.command = command;
}

public void action(){
command.exe();
}
}
测试:

public class Test {

public static void main(String[] args) {
Receiver receiver = new Receiver();
Command cmd = new MyCommand(receiver);
Invoker invoker = new Invoker(cmd);
invoker.action();
}
}
输出:

command received!
命令模式的目的就是达到命令的发出者和执行者之间解耦,实现请求和执行分开,Struts其实就是一种将请求和呈现分离的技术。
十八、备忘录模式

主要目的是保存一个对象的某个状态,以便在适当的时候恢复对象。

Original类是原始类,里面有需要保存的属性value及创建一个备忘录类,用来保存value值。Memento类是备忘录类,Storage类是存储备忘录的类,持有Memento类的实例。

public class Original {

private String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public Original(String value) {
this.value = value;
}

public Memento createMemento(){
return new Memento(value);
}

public void restoreMemento(Memento memento){
this.value = memento.getValue();
}
}
public class Memento {

private String value;

public Memento(String value) {
this.value = value;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
public class Storage {

private Memento memento;

public Storage(Memento memento) {
this.memento = memento;
}

public Memento getMemento() {
return memento;
}

public void setMemento(Memento memento) {
this.memento = memento;
}
}
测试:

public class Test {

public static void main(String[] args) {

// 创建原始类
Original origi = new Original("egg");

// 创建备忘录
Storage storage = new Storage(origi.createMemento());

// 修改原始类的状态
System.out.println("初始化状态为:" + origi.getValue());
origi.setValue("niu");
System.out.println("修改后的状态为:" + origi.getValue());

// 回复原始类的状态
origi.restoreMemento(storage.getMemento());
System.out.println("恢复后的状态为:" + origi.getValue());
}
}
输出:

初始化状态为:egg
修改后的状态为:niu
恢复后的状态为:egg
十九、状态模式
当对象的状态改变时,同时改变其行为
public class State {

private String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public void method1(){
System.out.println("execute the first opt!");
}

public void method2(){
System.out.println("execute the second opt!");
}
}
public class Context {

private State state;

public Context(State state) {
this.state = state;
}

public State getState() {
return state;
}

public void setState(State state) {
this.state = state;
}

public void method() {
if (state.getValue().equals("state1")) {
state.method1();
} else if (state.getValue().equals("state2")) {
state.method2();
}
}
}
测试:

public class Test {

public static void main(String[] args) {

State state = new State();
Context context = new Context(state);

//设置第一种状态
state.setValue("state1");
context.method();

//设置第二种状态
state.setValue("state2");
context.method();
}
}
输出:

execute the first opt!
execute the second opt!
二十、访问者模式

访问者模式把数据结构和作用于结构上的操作解耦合

public interface Visitor {
public void visit(Subject sub);
}
public class MyVisitor implements Visitor {

@Override
public void visit(Subject sub) {
System.out.println("visit the subject:"+sub.getSubject());
}
}
Subject类,accept方法,接受将要访问它的对象,getSubject()获取将要被访问的属性

public interface Subject {
public void accept(Visitor visitor);
public String getSubject();
}
public class MySubject implements Subject {

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}

@Override
public String getSubject() {
return "love";
}
}
测试:

public class Test {

public static void main(String[] args) {

Visitor visitor = new MyVisitor();
Subject sub = new MySubject();
sub.accept(visitor);
}
}
输出:

visit the subject:love
二十一、中介者模式

中介者模式也是用来降低类类之间的耦合的,因为如果类类之间有依赖关系的话,不利于功能的拓展和维护,因为只要修改一个对象,其它关联的对象都得进行修改。

public interface Mediator {
public void createMediator();
public void workAll();
}
public class MyMediator implements Mediator {

private User user1;
private User user2;

public User getUser1() {
return user1;
}

public User getUser2() {
return user2;
}

@Override
public void createMediator() {
user1 = new User1(this);
user2 = new User2(this);
}

@Override
public void workAll() {
user1.work();
user2.work();
}
}
public abstract class User {

private Mediator mediator;

public Mediator getMediator(){
return mediator;
}

public User(Mediator mediator) {
this.mediator = mediator;
}

public abstract void work();
}
public class User1 extends User {

public User1(Mediator mediator){
super(mediator);
}

@Override
public void work() {
System.out.println("user1 exe!");
}
}
public class User2 extends User {

public User2(Mediator mediator){
super(mediator);
}

@Override
public void work() {
System.out.println("user2 exe!");
}
}
测试:

public class Test {

public static void main(String[] args) {
Mediator mediator = new MyMediator();
mediator.createMediator();
mediator.workAll();
}
}
输出:

user1 exe!
user2 exe!
二十二、解释器模式

解释器模式用来做各种各样的解释器,如正则表达式等的解释器等等

public interface Expression {
public int interpret(Context context);
}
public class Plus implements Expression {

@Override
public int interpret(Context context) {
return context.getNum1()+context.getNum2();
}
}
public class Minus implements Expression {

@Override
public int interpret(Context context) {
return context.getNum1()-context.getNum2();
}
}
public class Context {

private int num1;
private int num2;

public Context(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}

public int getNum1() {
return num1;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
this.num2 = num2;
}
}
测试:

public class Test {

public static void main(String[] args) {

// 计算9+2-8的值
int result = new Minus().interpret((new Context(new Plus()
.interpret(new Context(9, 2)), 8)));
System.out.println(result);
}
}
输出:

3


由于我把工厂模式整合到一块了,所以看上去只有22中,实际是有23种的。
java的23中设计模式到此就讲完了,如果有错误或者不完善的地方还请不吝赐教!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 设计模式