初探设计模式

1.简单工厂模式:

所有产品由一个工厂创建

1.工厂模式:

父类中定义工厂方法,各子类实现具体的实例创建

工厂模式类图

      

 

代码演示

//人类
public interface Human {

    public void say();
}



//男人为人类实现类
public class Man implements Human {
    @Override
    public void say() {
        System.out.println("我是帅哥,我喜欢宝宝。。。。");
    }
}


//女人为人类实现类
public class Gril implements Human {
    @Override
    public void say() {
        System.out.println("我是宝宝,我喜欢勇勇。。。。");
    }
}


//将所有类加入到工厂中
public class SampleFactory {
    public static Human makeHuman(String type) {
        if (type.equals("man")) {
            Human man = new Man();
            return man;
        } else if (type.equals("gril")){
            Human gril = new Gril();
            return gril;
        } else {
            System.out.println("工厂中无此方法。。。");
            return null;
        }
    }
}



public class PlayGame {
    public static void main(String[] args) {

        Human man = SampleFactory.makeHuman("man");
        man.say();
        Human gril = SampleFactory.makeHuman("gril");
        gril.say();
        try {
            Human wxh = SampleFactory.makeHuman("wxh");
            wxh.say();
        } catch (NullPointerException e) {
            System.out.println("工厂中无此方法,NullPointerException");
        }

    }
}




输出:
我是帅哥,我喜欢宝宝。。。。
我是宝宝,我喜欢勇勇。。。。
工厂中无此方法。。。
工厂中无此方法,NullPointerException

 

3.抽象工厂模式:

       用女蜗造人阐述工厂模式:现在女娲要造人,她要造三种人:男人、女人、人妖。怎么造呢?先有造人工厂,这个工厂让她生产出不同的人种

工厂方法类图

//人类接口
public interface Human {
    public void sex();//人有不同性别
    public void talk();//人会说话
}


//人类实现
public class Girl implements Human {
    @Override
    public void sex() {
        System.out.println("我是女生,是公认的美女!!!");
    }

    @Override
    public void talk() {
        System.out.println("我喜欢勇勇!!");
    }
}

public class Man implements Human {
    @Override
    public void sex() {
        System.out.println("我是男生,是个帅哥!!!");
    }

    @Override
    public void talk() {
        System.out.println("我喜欢宝宝!!!");
    }
}

public class LadyBoy implements Human {
    @Override
    public void sex() {
        System.out.println("我是人妖!");
    }

    @Override
    public void talk() {
        System.out.println("来看我的表演吧!!");
    }
}


//抽象八卦炉
public abstract class AbstractHumanFactory {
    public abstract <T extends Human> T createHuman(Class<T> clazz);
}

//八卦炉实现
public class HumanFactory extends AbstractHumanFactory {
    @Override
    public <T extends Human> T createHuman(Class<T> clazz) {
        Human human = null;
        try {
            human = (Human) Class.forName(clazz.getName()).newInstance();
        } catch (Exception e) {
            System.out.println("不好意思!创建失败了!");
        }
        return (T) human;
    }
}


//造人游戏
public class PalyGame {

    public static void main(String[] args) {
        AbstractHumanFactory factory = new HumanFactory();
        Human girl = factory.createHuman(Girl.class);
        girl.sex();
        girl.talk();
        Human man = factory.createHuman(Man.class);
        man.sex();
        man.talk();
        Human ladyBoy = factory.createHuman(LadyBoy.class);
        ladyBoy.sex();
        ladyBoy.talk();

    }
}

输出:
我是女生,是公认的美女!!!
我喜欢勇勇!!
我是男生,是个帅哥!!!
我喜欢宝宝!!!
我是人妖!
来看我的表演吧!!

 

4.装饰者模式:

定义:以装饰的方式,动态地将责任附加到对象上。

装饰者类图:

/**
 * 共同的需装饰的行为定义接口
 */
public interface Component {
    String methodA();
    int methodB();
}


//被装饰者
public class ConcreteComponent implements Component {
    @Override
    public String methodA() {
        return "Concrete-object";
    }

    @Override
    public int methodB() {
        return 100;
    }
}


//装饰者:在被装饰对象的行为结果上附加逻辑
public class Decorator implements Component {
    protected  Component component;

    public Decorator(Component component) {
        super();
        this.component = component;
    }

    @Override
    public String methodA() {
        return this.component.methodA();
    }

    @Override
    public int methodB() {
        return this.component.methodB();
    }
}


//装饰者实现
public class DecoratorA extends Decorator {
    public DecoratorA(Component component) {
        super(component);
    }

    @Override
    public String methodA() {
        String src = this.component.methodA() + " +A";
        return src;
    }

    @Override
    public int methodB() {
        int i = this.component.methodB() + 10;
        return i;
    }
}



public class PlayGame {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        component = new DecoratorA(component);
        System.out.println(component.methodA());
        System.out.println(component.methodB());
    }
}


输出:
Concrete-object +A
110

 

5:代理模式

定义:为其他对象提供一种代理以控制对这个对象的访问

作用:不改变原类的代码,而增强原类对象的功能。可以选择前置、后置、环绕、异常处理增强

5.1静态代理:

由程序员创建或由特定工具自动生成代理类源代码,再对其编译。在程序运行前,代理 的.class文件就已经存在了

//定义接口
public interface Girl {
    boolean dating(float length);
}

//接口实现
import com.sunyard.inf.Girl;

public class TeacherCang implements Girl {
    @Override
    public boolean dating(float length) {
        if (length >= 1.7f) {
            System.out.println("身高可以,可以约!");
            return true;
        }
        System.out.println("身高不可以,不可约!");
        return false;
    }
}


//静态代理类
public class Tony implements Girl {

    private Girl girl;

    public Girl getGirl() {
        return girl;
    }

    public void setGirl(Girl girl) {
        this.girl = girl;
    }

    @Override
    public boolean dating(float length) {
       //前置增强
        doSomethingBefore();
        boolean res = this.girl.dating(length);
        //后置增强
        doSomethingAfter();
        return res;
    }

    private void doSomethingBefore(){
        System.out.println("老板,这个我试过了,很不错,推荐给你!");
    }

    private void doSomethingAfter(){
        System.out.println("老板,你觉得怎么样,欢迎下次再约!");
    }
}


//外部访问代理
public class TuHao {

    private float length;

    public TuHao(float length) {
        this.length = length;
    }

    public void dathing(Girl g){
        g.dating(length);
    }
}

public class PlayGame {

    public static void main(String[] args) {
        Girl teacherCang = new TeacherCang();
        Tony tony = new Tony();
        TuHao tuHao = new TuHao(1.7f);
        tony.setGirl(teacherCang);
        tuHao.dathing(tony);
    }
}

输出:
老板,这个我试过了,很不错,推荐给你!
身高可以,可以约!
老板,你觉得怎么样,欢迎下次再约!


 

 

静态代理缺点:1.扩展能力差

                          2.可维护性差

5.2动态代理

在运行时,动态为不同类的对象创建代理,增强功能。灵活扩展,易维护!

实现方式:JDK动态代理:只可对接口创建代理

                  CGLIB动态代理:可对接口、类创建代理

public interface Boy {
	boolean dating(char cup);
	void show();
}

public interface Girl {
    boolean dating(float length);
}


//接口实现
public class TeacherChen implements Boy {

	public boolean dating(char cup) {
		if (cup == 'E') {
			System.out.println("这个女老板品德正好,可以约!");
			return true;
		}
		System.out.println("这个女老板品德不行,不可以约!");
		return false;
	}

	public void show() {
		System.out.println("开始进入拍摄模式。。。。。。。。");
	}

}


//接口实现
public class TeacherCang implements Girl {
    @Override
    public boolean dating(float length) {
        if (length >= 1.7f) {
            System.out.println("身高可以,可以约!");
            return true;
        }
        System.out.println("身高不可以,不可约!");
        return false;
    }
}


//实现InvocationHandler接口,环绕增强
public class MyInvationHandler implements InvocationHandler {

    private Object target;

    public MyInvationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
       //前置增强
        doSomethingBefore();
        // 调用被代理对象的方法
        Object res =  method.invoke(target,args);
        //后置增强
        doSomethingAfter();
        return res;
    }

    private void doSomethingBefore(){
        System.out.println("老板,这个我试过了,很不错,推荐给你!");
    }

    private void doSomethingAfter(){
        System.out.println("老板,你觉得怎么样,欢迎下次再约!");
    }

}


//实现动态代理
public class TonyCompany {
    public static Object proxy(Object target){
        return Proxy.newProxyInstance(target.getClass()
                .getClassLoader(),target.getClass()
                .getInterfaces(),new MyInvationHandler(target));
    }
}

//外部访问代理
public class TuHao {

    private float length;

    public TuHao(float length) {
        this.length = length;
    }

    public void dathing(Girl g){
        g.dating(length);
    }
}


//动态代理测试
public class PlayGame {

    public static void main(String[] args) {
        TuHao tuHao1 = new TuHao(1.7f);
        Girl teacherCang1 = new TeacherCang();
        Girl tony1 = (Girl) TonyCompany.proxy(teacherCang1);
        tuHao1.dathing(tony1);

        Boy teacherChen = new TeacherChen();
        Boy tony2 = (Boy) TonyCompany.proxy(teacherChen);
        tony2.dating('E');
        tony2.show();
    }
}


输出:
老板,这个我试过了,很不错,推荐给你!
身高可以,可以约!
老板,你觉得怎么样,欢迎下次再约!
老板,这个我试过了,很不错,推荐给你!
这个女老板品德正好,可以约!
老板,你觉得怎么样,欢迎下次再约!
老板,这个我试过了,很不错,推荐给你!
开始进入拍摄模式。。。。。。。。
老板,你觉得怎么样,欢迎下次再约!

 

6.代理模式-cglib动态代理

7.责任链模式

8.适配器模式

9.外观(门面)模式

10.观察者模式也称监听模式、发布订阅模式

11.命令模式

12.状态模式

13.桥接模式

14.单例模式

15.模板方法模式

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值