设计模式系列:迭代器和单例模式

场景

这个场景好像不用太多说,主要是减少new的数量,确保实例唯一。

代码

//version1
public class SingleFactory {
    private static SingleFactory singleFactory = new SingleFactory();
    private SingleFactory(){
        System.out.println("init method");
    }
    public static SingleFactory getInstance(){
        return singleFactory;
    }
}

public class HungrySingleFactory {
    private static HungrySingleFactory hungrySingleFactory;
    private HungrySingleFactory(){
        System.out.println("init");
    }
    public static HungrySingleFactory getInstance(){
        if(hungrySingleFactory == null) {
            synchronized (HungrySingleFactory.class) {
                if(hungrySingleFactory == null) {
                    hungrySingleFactory = new HungrySingleFactory();
                }
            }
        }
        return hungrySingleFactory;
    }
}

定义

保证一个类只有一个实例,并提供一个访问它的全局访问点

类图

设计模式系列:迭代器和单例模式_第1张图片
单例模式.gif

场景

现在公交车都是上车刷卡。但是在90年代初都是在每辆公交车上有收费员跟随公交车,每站上车来的人需要和收费员买票

代码

public class Ticket {
    private String startStation;
    private String endStation;
    private int stationNum;
    private double money;
}

public class Passenger {
    private Ticket ticket;
    public Ticket getTicket() {
        return ticket;
    }
    public void setTicket(Ticket ticket) {
        this.ticket = ticket;
    }
}

public class Bus {
    private List passengerList = new ArrayList();
    public void addPassenger(Passenger passenger) {
        passengerList.add(passenger);
    }
    public void removePassenger(Passenger passenger) {
        passengerList.remove(passenger);
    }
    public int getPassengerNum() {
        return passengerList.size();
    }
    public Passenger getPassenger(int index) {
        return passengerList.get(index);
    }
}

public interface Iterator {
    public boolean hasNext();
    public Object next();
}

public class Cashier implements Iterator {
    private Bus bus;
    public Cashier(Bus bus) {
        this.bus = bus;
    }
    int num = 1;
    public void reset(){
        num = 1;
    }
    public boolean hasNext() {
        return num < bus.getPassengerNum();
    }
    public Passenger next() {
        Passenger passenger = bus.getPassenger(num);
        num++;
        return passenger;
    }
    public void iterator() {
        reset();
        while(hasNext()){
            Passenger passenger = next();
            Ticket pTicket = passenger.getTicket();
            if(pTicket == null) {
                System.out.println("请你立刻买票");
            }
        }
    }
    public Ticket pushTicket(String startStation, String endStation, int stationNum, int money) {
        Ticket ticket = new Ticket();
        ticket.setStartStation(startStation);
        ticket.setEndStation(endStation);
        ticket.setStationNum(stationNum);
        ticket.setMoney(money);
        return ticket;
    }
}

public class Tester {
    public static void main(String[] args) {
        Bus bus = new Bus();
        Cashier cashier = new Cashier(bus);
        Passenger p1 = new Passenger();
        bus.addPassenger(p1);
        Passenger p2 = new Passenger();
        Ticket ticket = cashier.pushTicket("上海","婺源",3, 10);
        p2.setTicket(ticket);
        bus.addPassenger(p2);
        Passenger p3 = new Passenger();
        bus.addPassenger(p3);
        cashier.iterator();
      
        Passenger p4 = new Passenger();
        bus.addPassenger(p4);
        bus.removePassenger(p2);
        cashier.iterator();
    }
}

定义

提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示。

类图

设计模式系列:迭代器和单例模式_第2张图片
Iterator.gif

你可能感兴趣的:(设计模式系列:迭代器和单例模式)