3、设计模式—单例、工厂、代理、适配器、观察者

 

1、  设计模式——单例模式

 

单例模式负责创建自己的对象,同时确保只有一个对象被创建。

比如,我们要统计网站的访问用户数,在任何时候都只有一个对象在记录用户数,否则就可能出现重复写入的问题,这是单例模式应用的一个场景

实现如下

class Person{

    //私有构造函数,组织对象实例化,从而阻止创建多个对象

    private Person(){}

    //私有对象变量

    privatestatic Person p;

    //main函数执行前执行,产生一个实例对象

    static{

        p =new Person();

    }

    //获取单例对象

    publicstatic Person getPerson(){

        if(p ==null){

            p =new Person();

        }

        System.out.println("get single person object");

        return p;

    }

}

 

publicclass hello{

    publicstaticvoid main(String args[]){

        //获取单例对象,并比较是否是同一个对象

        Person p1 = Person.getPerson();

        Person p2 = Person.getPerson();

        if(p1 == p2){

            System.out.println("is the same object");

        }else{

            System.out.println("is not the same object");

        }

       

    }

}

 

 

2、  设计模式——工厂模式

 

首先看下面代码,Apple和Orange实现了Fruit接口,程序要调用Apple的eat方法的话,要先在代码中显示的实例化Apple类,才能调用

现在的问题是,如果我想要调用Orange的方法呢,这时就需要修改程序代码才能实现调用,这会非常麻烦,因为程序已经发布出去,你必须修改代码,再从新部署等等

interface Fruit{

    publicvoid eat();

}

class Apple implements Fruit{

    publicvoid eat(){

        System.out.println("apple has been eat");

    }

}

class Orange implements Fruit{

    publicvoid eat(){

        System.out.println("orange has been eat");

    }

}

 

publicclass hello{

    publicstaticvoid main(String args[]){

        Fruit fruit =new Apple();

        fruit.eat();

    }

}

 

那有没有什么更简单的方法呢?有,通过工厂模式,我们可以由类名来自动生成类的实例,再通过接口来调用实例中的方法即可。这样如果我们要调用其它的类对象的方法,通过传递不同的类名作为参数就可以了,是不是简单了很多,因为不要修改代码,也不需要重新部署等等,如下

interface Fruit{

    publicvoid eat();

}

class Apple implements Fruit{

    publicvoid eat(){

        System.out.println("apple has been eat");

    }

}

class Orange implements Fruit{

    publicvoid eat(){

        System.out.println("orange has been eat");

    }

}

class Factory{

    static Fruit getInstance(String className){

        if("Apple".equals(className)){

            returnnew Apple();

        }

        elseif("Orange".equals(className)){

            returnnew Orange();

        }

        else{

            returnnull;

        }

    }

}

 

publicclass hello{

    publicstaticvoid main(String args[]){

        Fruit fruit = Factory.getInstance("Orange");

        fruit.eat();

    }

}

 

 

3、  设计模式——代理模式

 

所谓代理模式,是指客户端不直接调用实际的对象,而是通过调用代理对象来间接的调用实际的对象。

比如我要打官司,实际是我在打官司,但是现实中会通过律师来代理我来打官司,因为律师可以为我完成我不会或者无法完成的一些操作,比如填写诉状、起诉等操作

//起诉人接口

interface Plaintiff{

    publicvoid sue();

}

//我是起诉人,要起诉xxx

class Me implements Plaintiff{

    publicvoid sue(){

        System.out.println("i will sue xxx");

    }

}

//代理律师,起诉人通过代理律师来起诉,并且由代理律师填写起诉书

class ProxyLayer implements Plaintiff{

    private Plaintiff p;

    //代理律师接受某起诉人的委托

    public ProxyLayer(Plaintiff p){

        this.p = p;

    }

    //律师填写起诉书

    publicvoid fillPlaint(){

        System.out.println("the layer filled the plaint");

    }

    //代为起诉

    publicvoid sue(){

        this.fillPlaint();  //填写起诉书

        this.p.sue();   //代起诉人起诉

    }

}

 

publicclass hello{

    publicstaticvoid main(String args[]){

        //我,委托代理律师

        Plaintiff p =new ProxyLayer(new Me());

        //进行起诉操作

        p.sue();

    }

}

 

 

4、  设计模式——适配器模式

 

如果一个类要实现一个接口,则必须实现这个接口的所有方法,但是有时候一个接口定义了非常多的方法,而要实现的类又没必要实现这么多方法的时候怎么办呢?

这时候就可以使用适配器模式,通过适配器,我们可以将复杂的接口转换成适合当前类的接口。

通过一个抽象类abstruct,先实现接口所有的方法(方法的实现为空),再由类来覆写这个抽象类,这样就实现了接口和类的适配

//汽车接口,有三个方法

interface Car{

    publicvoid run();

    publicvoid addOil();

    publicvoid carry();

}

//汽车接口适配器,实现了所有的汽车接口

abstractclass CarAdapter implements Car{

    publicvoid run(){}

    publicvoid addOil(){}

    publicvoid carry(){}

}

//跑车没有运载功能,通过适配器实现了对car接口的实现

class SportsCar extends CarAdapter{

    publicvoid run(){

        System.out.println("my car is running");

    }

    publicvoid addOil(){

        System.out.println("my car is adding oil");

    }

}

 

publicclass hello{

    publicstaticvoid main(String args[]){

        //实现了car接口的SportCar

        Car c =new SportsCar();

        c.run();

        c.addOil();

    }

}

 

 

5、  设计模式——观察者模式

通过观察者模式,我们可以监控被观察对象的变化,达到及时监督的目的。

import java.util.Observable;

import java.util.Observer;

 

//房屋(被观察对象),需继承Obserable

class House extends Observable{

    privatefloat price;

    public House(float price){this.price = price;}

    publicfloat getPrice(){returnthis.price;}

    publicvoid setPrice(float price){

        //设置观察点

        super.setChanged();

        //通知观察者

        super.notifyObservers(price);

        this.price = price;

    }

    public String toString(){

        return"house's price is "+this.price;

    }

}

 

//观察者,需实现Observer接口,接口中只有update方法需要实现

class HouseObserver implements Observer{

    private String name;

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

    //观察者观察到的对象,和观察到的改变了的参数

    publicvoid update(Observable obj, Object arg){

        if(arg instanceofFloat){

            System.out.println(this.name +" observ the price changed to : "+(float)arg);

        }

    }

}

 

publicclass hello{

    publicstaticvoid main(String args[])throws Exception{

        //观察对象

        House house =new House(10000);

        //三个观察者

        HouseObserver ho1 =new HouseObserver("kaka");

        HouseObserver ho2 =new HouseObserver("fedy");

        HouseObserver ho3 =new HouseObserver("tony");

        //观察者关注观察对象

        house.addObserver(ho1);

        house.addObserver(ho2);

        house.addObserver(ho3);

        //输出房屋价格,修改房屋价格,输出修改后的服务价格

        //查看观察者是否观察到房屋价格的变化

        System.out.println(house);

        house.setPrice(12888);

        System.out.println(house);

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值