您的位置:首页 > 其它

常见设计模式分析

2018-01-10 15:19 176 查看
/**

 * @author shenqi

 * @date 2018/1/10.

 */

/**

 * 单例模式

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

 * 单例模式 懒汉式

 */

public class Singleton{

    private static Singleton singleton = null;

    private Singleton(){}

    public static Singleton getSingleton(){

        if (singleton == null){

            singleton = new Singleton();

        }

        return singleton;

    }

}

/**

 * 单例模式 双重线程检查模式

 */

public class Singleton{

    private static volatile Singleton sSingleton = null;

    private Singleton(){}

    public static Singleton getSingleton(){

        Singleton singleton = sSingleton;

        if (singleton == null){

            synchronized (Singleton.class){

                singleton = sSingleton;

                if (singleton == null){

                    singleton = new Singleton();

                    sSingleton = singleton;

                }

            }

        }

        return singleton;

    }

}

/**

 * 工厂方法模式

 */

//抽象产品角色

public interface MoveAble{

    void run();

}

//具体产品角色

public class Plane implements MoveAble{

    @Override

    public void run(){

        System.out.println("plane");

    }

}

//具体产品角色

public class Car implements MoveAble{

    @Override

    public void run(){

        System.out.println("car");

    }

}

//抽象工厂

public abstract class VehicleFactory{

    abstract MoveAble create();

}

//具体工厂

public class PlaneFactory extends VehicleFactory{

    public MoveAble create(){

        return new Plane();

    }

}

//具体工厂

public class CarFactory extends VehicleFactory{

    public MoveAble create(){

        return new Car();

    }

}

//测试

public class test{

    private static void main(String [] args){

        VehicleFactory factory = new Car();

        MoveAble m = factory.create();

        m.run();

    }

}

/**

 * 建造模式 Builder

 * 是一种对象构建的设计模式,它可以将复杂对象的建造过程抽象出来,使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。

 */

public interface Builder{

    Product getResult();

}

public class Director{

    private Builder builder;

    private Director(Builder builder){

        this.builder = builder;

    }

    private void construct(){

        builder.buildPartA();

        builder.buildPartB();

        builder.buildPartC();

    }

}

public class ConcreteBuilder implements Builder{

    Part partA,partB,partC;

    public void buildPartA(){

    }

    public void buildPartB{

    }

    public void buildPartC{

    }

    public Product getResult(){

    }

}

public interface Product{}

public interface Part{}

//调用Builder模式

//ConcreteBuilder builder = new ConcreteBuilder();

//Diretor diretor = new Director(builder);

//diretor.constuct();

//Product product = builder.getResult();

/**

 * 观察者模式

 * 该模式定义了一种一对多的依赖关系,让多个观察者对象同时监听同一主题对象,这个主题对象在状态发生变化时,会通知会通知所有观察者对象,使它们能够自动更新自己。观察者模式又叫发布-订阅(Publish/Subscribe)模式。

 */

//先创建一个Subject类

public interface Subject{

    public void attach(Observer observer);

    public void detach(Observer observer);

    public void notice();

}

//创建Observer类

public interface Observer{

    public void updte();

}

//创建ConcreteSubject类

public class Teacher implements Subject{

    private String phone;

    private List<String> students;

    public Teacher(){

        phone = "";

        students = new ArrayList<>();

    }

    @Override

    public void attach(Observer observer){

        students.add(observer);

    }

    @Override

    public void notice(){

        for(int i =0;i<students.size();i++){

            ((Observer)students.get(i).update());

        }

    }

    public String getPhone(){

        return phone;

    }

    public String setPhone(String phone){

        this.phone = phone;

        notice();

    }

}

//创建ConcreteObserver类

public class Students implements Observer{

    private String name;

    private String phone;

    private Teacher teacher;

    public Student(String name ,Teacher t){

        this.name = name;

        this.teacher=t;

    }

    @Override

    public void update(){

        phone = teacher.getPhone();

    }

}

/**

 * 适配器模式

 * 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。

 */

public interface Target{

    public void aaa();

    public void bbb();

}

public class Adaptee{

    public void aaa();

}

public class Adapter extends Adaptee implements Target{

    @Override

    public  void bbb();

}

/**

 * 装饰模式

 * 动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

 */

public interface Component{

    void show();

}

//创建一个具体的 ConcreteComponent 来实现 Component 接口:Person.java

public class Person implements Component{

    private String name;

    public String getName(){

        return name;

    }

    public void setName(String name){

        this.name = name;

    }

    @Override

    public void show(){

        System.out.print("zhuangban");

    }

}

//创建装饰类 Decorator 实现 Component 接口

public class Decorator implements Component{

    private Component component;

    public void decoratorObj(Component mcomponent){

        component= mcomponent;

    }

    @Override

    public void show(){

        if (component != null){

            component.show();

        }

    }

}

//分别创建具体的装饰类:Jeans.java , Pelisse.java, Sandal.java ...等等,分别继承 Decorator.java 类

public class Jeans extends Decorator{

    @Override

    public void show(){

        System.out.print("穿牛仔裤");

        super.show();

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: