面试常考设计模式

作者: _Kantin | 来源:发表于2017-10-08 16:31 被阅读437次

(1)一个线程安全且高效率的单利模式

//一个很好的单例模式
public class DesignModel1 {
    private static DesignModel1 instance;
    //把构造器私有化
    private DesignModel1(){};
    
    public static DesignModel1 getInstance() {
        if(instance ==null) {
            synchronized (DesignModel1.class) {
                if(instance==null) {
                    instance = new DesignModel1();
                }
            }
        }
        return instance;
    }
}

(2)工厂模式:通过对象构造工厂在构建相应的对象实例

interface Animal{
    public void say();
}

class   Cat implements Animal{
    @Override
    public void say() {
        System.out.println("I am a cat");
    }
    
}

class Dog implements Animal{

    @Override
    public void say() {
        System.out.println("I am a dog");
    }
    
}

class Factory{
    public static Animal getInstance(String className) {
        //通过equal方法在确定类的实例
        Animal ani = null;
        if("cat".equals(className)) {
            ani = new Cat();
        }
        if("dog".equals(className)) {
            ani = new Dog();
        }
        return ani;
    }
}

public class DesignModel2 {
    public static void main(String[] args) {   
        Animal a = null; // 定义接口对象  
        a = Factory.getInstance(args[0]); // 通过工厂获取实例  
        if (a != null) { // 判断对象是否为空  
            a.say(); // 调用方法   
        }   
    } 
}

(3)代理模式:对代理类进行动态的扩展

/**
 * 代理模式就是多一个代理类出来,替原对象进行一些操作
 * 例如买房子找中介,上法院找律师等等
 */

 interface Sourceable1 {  
    public void method();  
}  

 class Source1 implements Sourceable {  
      
    @Override  
    public void method() {  
        System.out.println("the original method!");  
    }  
} 
 //注意装饰器模式持有的是接口的实例,代理模式持有的是实现类的实例
 class Proxy1 implements Sourceable1{
     private Source1 source1;

    public Proxy1() {
        super();
        this.source1 = new Source1();
    }
    //重写Sourceable1的method()方法,并且持有的Source1实现类是实例也进行操作
    @Override
    public void method() {
        before();
        source1.method();
        after();
    }
    private void before() {  
        System.out.println("before proxy!");  
    }  
    private void after() {  
        System.out.println("after proxy!");  
    }  
 }
 
 
public class DesignModel7 {
    public static void main(String[] args) {
        Sourceable1 source = new Proxy1();
        source.method(); 
    }
}

(4)适配器模式

/**
 *适配器模式:一个接口可能有多个的实现方法,但是我们不需继承一个接口全部的实现它
 *而是用一个抽象类继承接口,然后我们选择性的继承即可 
 *
 */
interface Window {// 定义Window窗口接口,表示窗口操作  
    public void open();// 窗口打开  
  
    public void close();// 窗口关闭  
  
    public void iconified();// 窗口最小化  
  
    public void deiconified();// 窗口恢复  
  
    public void activated();// 窗口活动  
}   

//定义抽象类实现接口,在此类中覆写方法,但是所有的方法体为空   
abstract class WindowAdapter implements Window {   
 public void open() {   
 };// 窗口打开   

 public void close() {   
 };// 窗口关闭   

 public void iconified() {   
 };// 窗口最小化   

 public void deiconified() {   
 };// 窗口恢复   

 public void activated() {   
 };// 窗口活动   
}   

//子类继承WindowAdapter抽象类,选择性实现需要的方法   
class WindowImpl extends WindowAdapter {   
 public void open() {   
     System.out.println("窗口打开");// 实现open()方法  
 }   

 public void close() {   
     System.out.println("窗口关闭");// 实现close()方法  
 }   
}   
public class DesignModel4 {
    public static void main(String args[]) {   
        Window win = new WindowImpl(); // 实现接口对象  
        // 调用方法   
        win.open();   
        win.close();   
    }  
}

(5)构造者模式

/**
 * 
 *构造者模式
 */

interface Sender {  
    public void Send();  
}  

class MailSender implements Sender {  
    @Override  
    public void Send() {  
        System.out.println("this is mailsender!");  
    }  
}  

class SmsSender implements Sender { 
      
    @Override  
    public void Send() {  
        System.out.println("this is sms sender!");  
    }  
}  

//MailSender和SmsSender为Sender的接口实现类,所以可以在list中加入
//DesignModel5一个类管理着多个对象的实例
public class DesignModel5 {
    private List<Sender> list = new ArrayList<Sender>();  
    
    public void produceMailSender(int count){  
        for(int i=0; i<count; i++){  
            list.add(new MailSender());  
        }  
    }  
      
    public void produceSmsSender(int count){  
        for(int i=0; i<count; i++){  
            list.add(new SmsSender());  
        }  
    }  
    
    public static void main(String[] args) {  
        DesignModel5 builder = new DesignModel5();  
        builder.produceMailSender(10);  
    }  
    
}

(6)装饰模式

//装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例
interface Sourceable {  
    public void method();  
}  

//本来Source方法继承了Sourceable实现了method()方法
class Source implements Sourceable {  
      
    @Override  
    public void method() {  
        System.out.println("the original method!");  
    }  
}  

//Decorator持有Sourceable的属性,对其进行装饰
class Decorator implements Sourceable {  
      
    private Sourceable source;  
      
    public Decorator(Sourceable source){  
        super();  
        this.source = source;  
    }  
    @Override  
    public void method() {  
        System.out.println("before decorator!");  
        source.method();  
        System.out.println("after decorator!");  
    }  
}  
public class DesignModel6 {
    public static void main(String[] args) {  
        //接口不能单独的实例化,必须通过实现类来帮助实例化
        Sourceable source = new Source();  
        Sourceable obj = new Decorator(source);  
        obj.method();  
    }  
}

(7)观察者模式

interface Observer{
    public void updata();
}

class Observer1 implements Observer{

    @Override
    public void updata() {
        System.out.println("observer1 has received!");  
    }
    
}

class Observer2 implements Observer{

    @Override
    public void updata() {
        System.out.println("observer2 has received!");  
    }
    
}

interface Subject{
     /*增加观察者*/  
    public void add(Observer observer);
     /*删除观察者*/  
    public void del(Observer observer);
     /*通知所有的观察者*/  
    public void notifyObservers();
    /*自身的操作*/  
    public void operation();
}

abstract class AbstractSubject implements Subject{
    private List<Observer> observers = new ArrayList<>();

    @Override
    public void add(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void del(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        Iterator<Observer> ite = observers.iterator();
        while(ite.hasNext()) {
            ite.next().updata();
        }
    }
    
    
    
}

class MySubject extends AbstractSubject{

    @Override
    public void operation() {
        System.out.println("udpate self");
        notifyObservers();
    }
    
}



public class DesignModel8 {
    public static void main(String[] args) {  
        Subject sub = new MySubject();  
        sub.add(new Observer1());  
        sub.add(new Observer2());  
        sub.operation();  
    }  
}

相关文章

  • 面试常考设计模式

    (1)一个线程安全且高效率的单利模式 (2)工厂模式:通过对象构造工厂在构建相应的对象实例 (3)代理模式:对代理...

  • Spring 设计模式

    设计模式 Java面试的过程中, 设计模式是常考的一项。我曾经多次面试Java程序员时也常问到,例如谈谈自己对sp...

  • Spring中使用了哪些设计模式?(再也不怕面试官提问了)

    设计模式 Java面试的过程中, 设计模式是常考的一项。 今天,我们就设计模式的内在价值做一番探讨,并以sprin...

  • [转载] 如何正确地写出单例模式

    单例模式算是设计模式中最容易理解,也是最容易手写代码的模式了吧。但是其中的坑却不少,所以也常作为面试题来考。本文主...

  • 如何正确地写出单例模式

    单例模式算是设计模式中最容易理解,也是最容易手写代码的模式了吧。但是其中的坑却不少,所以也常作为面试题来考。本文主...

  • 如何写出正确的单例模式

    单例模式算是设计模式中最容易理解,也是最容易手写代码的模式了吧。但是其中的坑却不少,所以也常作为面试题来考。本文主...

  • 面试之设计模式2018-12-20

    面试常问的面试题,设计模式,差不多我们项目开发中也常会用到这些: 简单工厂模式,策略模式、装饰模式、代理模式、工厂...

  • 图解Java设计模式之设计模式面试题

    图解Java设计模式之设计模式面试题 1.1 Java设计模式内容介绍 1.1.1 先看几个经典的面试题 1.1....

  • 设计模式面试题目录

    更新时间 2017-2-14 设计模式大集锦 程序员面试全攻略 两道设计模式的面试题 20个设计模式和软件设计面试问题

  • 常见面试题之设计模式

    title: 常见面试题之设计模式 categories: [设计模式] tags: [面试题] date: 20...

网友评论

    本文标题:面试常考设计模式

    本文链接:https://www.haomeiwen.com/subject/iytyyxtx.html