面试常考设计模式
2017-10-08 本文已影响437人
_Kantin
(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();
}
}