几种设计模式

1.单例模式

  • 此类需要使用一个静态属性来保存曾经创建的实例,且该属性需要被静态方法访问,所以该属性也应该为static修饰。
class Singleton {
    private static Singleton sing;
    private Singleton(){}
    public static Singleton getSing(){
        if (sing==null){
            sing=new Singleton();
        }
        return sing;
    }
}

2. 简单工厂模式

Person.java

public class Person {
    private Axe axe;

    public void setAxe(Axe axe) {
        this.axe = axe;
    }

    public Axe getAxe() {
        return axe;
    }

    public void useAxe(){
        System.out.println("gagasg");
        getAxe().mop();
    }
}

Axe.java

public class Axe {
    public void mop(){
        System.out.println("jgg");
    }
}

Text.java

public class Text {
    public static void main(String[] args) {
        Person p =new Person();
        p.useAxe();
    }
}

这样写是不允许的,会发生NullPointException
原因是在Text.java中间接调用Axe,但是没有Axe这个对象,除非改成这样。

public class Text {
    public static void main(String[] args) {
        Axe axe =new Axe();
        Person p = new Person();
        p.setAxe(axe);
        p.useAxe();
    }
}

但是经过spring的配置之后即可这样使用。
配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="cm.Person">
    <property name="axe" ref="axe" />
</bean>
    <bean id="axe" class="cm.Axe" />
</beans>

Text

public class Text {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
        Person p = ctx.getBean("person",Person.class);
        p.useAxe();
    }
}
  • 工厂模式:就是说在原始社会中人需要斧头只能自己生产,但是现在只要去工厂里买生产好的就行了。

3.代理模式

  • 总结起来说就是,只要客户端代码不能或不想直接访问被调用对象–这种情况有很多原因,比如需要创建一个开销很大的对象,或者被调用对象在远程主机上,或者目标对象的功能还不足以满足需求,而是额外创建一个代理对象返回给客户端使用,那么这种设计方式就是代理模式。

  • Image.java

public interface Image {
    void show();
}
  • BigImage.java
public class BigImage implements Image {
    public BigImage(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void show() {
        System.out.println("绘制实际的大图片");
    }
}
  • ImageProxy.java
public class ImageProxy implements Image {
    private Image image;//创建一个image实例,作为被代理对象。
    public ImageProxy(Image image){
        this.image=image;

    }


    @Override
    public void show() {
        if (image==null){//若image为空,则创建被代理对象
            //意思为该类为代理类,BigImage为被代理类。
            image=new BigImage();
        }
        image.show();
    }
}
  • BigImageTest.java
public class BigImageTest {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        Image image= new ImageProxy(null);
        System.out.println("系统得到Image对象的时间开销"+(System.currentTimeMillis()-start));
        image.show();//只有当实际调用image代理的show()方法时,程序才会创建真正的被代理的对象。
    }
}

4.命令模式

模式背景:某个方法需要完成某一个功能,完成这个功能的大部分步骤已经确定了,单可能有少量具体步骤无法确定,必须要等到执行的时候才能确定。

  • Command.java
public interface Command {
    void process(int[] target);
}
  • ProcessArray.java
public class ProcessArray {
    public void each(int[] target,Command command){
        command.process(target);
    }
}
  • CommandTest.java
public class CommandTest {
    public static void main(String[] args) {
        ProcessArray pa = new ProcessArray();
        int[] target={3,-7,6,4};
        pa.each(target,new Command() {
            @Override
            public void process(int[] target) {
                for (int tmp:target){
                    System.out.println("迭代输出目标组的元素"+tmp);
                }
            }
        });
        System.out.println("----------------");
        pa.each(target,new Command() {
            @Override
            public void process(int[] target) {
                int sum=0;
                for (int tmp:target){
                    sum+=tmp;
                }
                System.out.println("数组和是"+sum);
            }
        });
    }
}
  • 过程是,创建了一个数组和ProcessArray对象->执行pa.each方法并重写Command的process方法。->target数组先传到each方法,再传到process方法中进行操作。
  • 结果是,
迭代输出目标组的元素3
迭代输出目标组的元素-7
迭代输出目标组的元素6
迭代输出目标组的元素4
----------------
数组和是6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值