【Java基础之设计模式】代码库(三)

抽象工厂模式.java

//抽象工厂
public abstract class AbstractFactory {
    public abstract Vehicle create();
    public abstract Weapon create();
    public abstract Food create();
}

//工厂
public class Factory extends AbstractFactory {
    @Override
    public Vehicle create() {
        return new RangeRover();
    }

    @Override
    public Weapon create() {
        return new AK47();
    }

    @Override 
    public Food create() {
        return new Apple();
    }
}

//测试
public class Test {
    public static void main(String[] args) {
        AbstractFactory abf = new Factory();

        //产品一
        Vehicle v = abf.create();
        v.run();

        //产品二
        Weapon w = abf.create();
        w.fire();

        //产品三
        Food f = abf.create();
        f.price();
    }
}

单例模式.java

//懒汉式
public class Singleton {
    private static Singleton instance = null;

    //私有构造函数
    private Singleton() {

    }

    public static synchronized getInstance() {
        if (instance == null) 
            instance = new Singleton();

        return instance;
    }
}



//饿汉式
public class Singleton {
    private static final Singleton instance = new Singleton();

    private Singleton() {

    }

    public static Singleton getInstance() {
        return instance;
    }
}

工厂方法模式.java

//抽象产品角色
public interface Moveable {
    void run();
}

//具体产品角色
public class Plane implements Moveable {
    @Override
    public void run() {
        System.out.println("飞机飞起来了!");
    }
}

public class Car implements Moveable {
    @Override
    public void run() {
        System.out.println("汽车开起来了!");
    }
}

//抽象工厂
public abstract class Factory {
    abstract Moveable create();
}

//工厂
public class PlaneFactory extends Factory {
    public Moveable create() {
        return new Plane();
    }
}

public class CarFactory extends Factory {
    public Moveable create() {
        return new Car();
    }
}

//测试
public class Test {
    public static void main(String[] args) {
        Factory fac = new CarFactory();
        Moveable car = fac.create();
        car.run();
    }
}

观察者模式.java

//抽象观察者
public interface Watcher {
    public void update(String str);
}

//抽象主题角色(被观察者)
public interface Watched {
    public void addWatcher(Watcher watcher);

    public void removeWatcher(Watcher watcher);

    public void notifyWatchers(String str);
}

//观察者
public class ConcreteWatcher implements Watcher {
    @Override 
    public void update(String str) {
        System.out.println(str);
    }
}

//主题角色
public class ConcreteWatched implements Watched {
    private List<Watcher> list = new ArrayList<>();

    @Override
    public void addWatcher(Watcher watcher) {
        list.add(watcher);
    }

    @Override
    public void removeWatcher(Watcher watcher) {
        list.remove(watcher);
    }

    @Override
    public void notifyWatchers(String str) {
        for (Watcher watcher : list) {
            watcher.update(str);
        }
    }
}



//测试
public class Test {
    public static void main(String[] args) {
        Watched girl = new ConcreteWatched();

        Watcher boy1 = new ConcreteWatcher();
        Watcher boy2 = new ConcreteWatcher();
        Watched boy3 = new ConcreteWatcher();

        //注册
        girl.addWatcher(boy1);
        girl.addWatcher(boy2);
        girl.addWatcher(boy3);

        girl.notifyWatchers("我美不美?");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值