所有示例均在gihub(ssh_base)中====>https://github.com/chengyangyang

java 中设计模式

 

  1. 单例模式(一个类只有一个实例)

package ch.test.notes.designmodel;

/**
 * Description: 单例模式 (饿汉模式 线程安全的)
 *
 * @author cy
 * @date 2019年05月10日 9:33
 * version 1.0
 */
public class Singelon {

    private static Singelon si = new Singelon();

    public static Singelon getInstance() {
        return si;
    }

    private Singelon() { // 将构造方法封装为私有化
    }

    public void print(){
        System.out.println("打印--------->");
    }
}

 

package ch.test.notes.designmodel;

/**
 * Description: 单例模式 (懒汉模式)线程不安全
 *
 * @author cy
 * @date 2019年05月10日 9:33
 * version 1.0
 */
public class Singelon {

    private static Singelon si = null;

    private Singelon() { // 将构造方法封装为私有化
    }

    public static Singelon getInstance() {
        if(si == null){
            si = new Singelon();
        }
        return si;
    }

    public void print(){
        System.out.println("打印--------->");
    }
}

  2. 工厂模式(多个子类实现一个接口,根据不同子类复写父类方法,根据业务产生不同的实例)

package ch.test.notes.designmodel;

/**
 * Description:定义一个人的接口
 *
 * @author cy
 * @date 2019年05月10日 9:50
 * version 1.0
 */
public interface Person1 {

    // 人的身高
    String getHeight();

    // 职位
    String getWork();

}

创建两个实现类

package ch.test.notes.designmodel;

/**
 * Description: 实现类
 *
 * @author cy
 * @date 2019年05月10日 9:52
 * version 1.0
 */
public class Student implements Person1 {

    @Override
    public String getHeight() {
        return "12";
    }

    @Override
    public String getWork() {
        return "我的职位是学生!";
    }
}
package ch.test.notes.designmodel;

/**
 * Description:实现类
 *
 * @author cy
 * @date 2019年05月10日 9:53
 * version 1.0
 */
public class Tearch implements Person1 {

    @Override
    public String getHeight() {
        return "150";
    }

    @Override
    public String getWork() {
        return "我的职位是教师!";
    }
}

创建一个工厂,根据类型不同创建不同的对象

package ch.test.notes.designmodel;

/**
 * Description: 人员工厂
 *
 * @author cy
 * @date 2019年05月10日 9:55
 * version 1.0
 */
public class Factory {

    public static Person1 getInstance(String type){
        Person1 person = null;
        if("1".equals(type)){
            person = new Student();
        }else {
            person = new Tearch();
        }
        return person;
    }
}

测试(传入不同的类型,得到不同的对象内容)

package ch.test.notes.designmodel;

/**
 * Description: // 设计模式
 *
 * @author cy
 * @date 2019年05月10日 9:16
 * version 1.0
 */
public class main {

    public static void main(String[] args) {
        Person1 instance = Factory.getInstance("0");
        String work = instance.getWork();
        System.out.println(work);


    }

}

 3. 代理模式 (由一个主题来操作一个真实主题,代理服务器来完成一个真实业务以外的业务)

  角色(要做的东西,    假设只有vip 能做, 代理就是vip   普通人员不是,所以需要代理人代理)

package ch.test.notes.designmodel;

/**
 * Description: 商店
 *
 * @author cy
 * @date 2019年05月10日 10:32
 * version 1.0
 */
public interface Store {

    // 买东西
    String getGoods(boolean vip);

}
package ch.test.notes.designmodel;

/**
 * Description:
 *
 * @author cy
 * @date 2019年05月10日 10:35
 * version 1.0
 */
public class StoreImpl implements Store {

    private int num = 10;

    @Override
    public String getGoods(boolean vip) {
        if(!vip){
            System.out.println("只有VIP才能购买商品!");
            return "只有VIP才能购买商品!";
        }
        num --;
        System.out.println("购买了货物,剩余数量为"+ num);
        return "购买了货物,剩余数量为"+ num;
    }
}
package ch.test.notes.designmodel;

/**
 * Description: 代理
 *
 * @author cy
 * @date 2019年05月10日 10:34
 * version 1.0
 */
public class Proxy{

    private Store store;
    private Boolean vip = true;

    public Proxy(Store store){
        this.store = store;
    }

    // 购买商品
    public String getGoods() {
        // 这里也可以做一些校验
        // 检查身份证
        return store.getGoods(this.vip);
    }
}
package ch.test.notes.designmodel;

/**
 * Description: // 设计模式
 *
 * @author cy
 * @date 2019年05月10日 9:16
 * version 1.0
 */
public class main {

    public static void main(String[] args) {
        StoreImpl store = new StoreImpl();
        store.getGoods(false);
        new Proxy(store).getGoods();
    }

}

结果:

只有VIP才能购买商品!
购买了货物,剩余数量为9

   4. 委派模式(相当于领导给项目经理下达任务,项目经理将不同的任务,下达到不同的人上面,和上面的区别是注重的是结果,不需要过程)

    (角色 做任务    员工A  员工B  组长         组长接到任务后,分配给A和B)

package ch.test.notes.designmodel;

/**
 * Description: 老板需要做的任务
 *
 * @author cy
 * @date 2019年05月10日 11:08
 * version 1.0
 */
public interface ITarget {

    public void doSomething(String command);
}
package ch.test.notes.designmodel;

/**
 * Description:
 *
 * @author cy
 * @date 2019年05月10日 11:10
 * version 1.0
 */
public class TargetA implements  ITarget{

    @Override
    public void doSomething(String command) {
        System.out.println("我是员工A,开始做"+command+"工作");
    }
}
package ch.test.notes.designmodel;

/**
 * Description:
 *
 * @author cy
 * @date 2019年05月10日 11:10
 * version 1.0
 */
public class TargetB implements  ITarget{

    @Override
    public void doSomething(String command) {
        System.out.println("我是员工B,开始做"+command+"工作");
    }
}
package ch.test.notes.designmodel;

import java.util.HashMap;
import java.util.Map;

/**
 * Description: 组长
 *
 * @author cy
 * @date 2019年05月10日 11:11
 * version 1.0
 */
public class Leader  implements ITarget  {

    private Map<String,ITarget> map = new HashMap<>();

    public Leader(){
        map.put("打扫",new TargetA());
        map.put("吃饭",new TargetB());
    }


    @Override
    public void doSomething(String command) {
        map.get(command).doSomething(command);
    }

    public static void main(String[] args) {
        new Leader().doSomething("吃饭");
        new Leader().doSomething("打扫");
    }
}

   5. 策略模式

  背景:在软件开发过程中,要实现一种功能,需要多种算法或者策略,我们可以根据应用场景不同,选择不同算法和策略和实现该功能。比如一系列算法,把每个算法封装

  起来,并且使他们可以相互替换,这就是策略模式。

  假如我们有一个鸭子的抽象类,包含行为所有的鸭子都会游泳,属性鸭子的信息,如果我们增加一个飞行的功能,但是有些鸭子没有飞行的能力,所以我们不能再抽象类

  中加入飞行的方法。这时候,就需要我们抽象一个飞的接口。

  1.首先抽象一个策略接口

package ch.test.notes.designmodel;

/**
 * Description:策略接口
 *
 * @author cy
 * @date 2019年05月10日 12:37
 * version 1.0
 */
public interface CarFunction {

    void run();        //每辆车有不同的行驶方法
}

  2.具体策略父类

package ch.test.notes.designmodel;

/**
 * Description:每个车都具有的相同的属性和行为
 *
 * @author cy
 * @date 2019年05月10日 12:38
 * version 1.0
 */
public class Car implements CarFunction {

    protected String name;            //车名字
    protected String color;            //车颜色

    public Car(String name, String color) {
                 this.name = name;
                 this.color = color;
    }

    @Override
    public void run() {
        System.out.println(color +" " + name  +"在行驶。。。");
    }
}

  具体策略实现子类

package ch.test.notes.designmodel;

/**
 * Description:
 *
 * @author cy
 * @date 2019年05月10日 13:09
 * version 1.0
 */
public class SmallCar extends Car {
    public SmallCar(String name, String color) {
        super(name, color);
    }

    @Override
    public void run() {
        System.out.println(color +" " + name  +"在高速的行驶。。。");
    }
}
package ch.test.notes.designmodel;

/**
 * Description:
 *
 * @author cy
 * @date 2019年05月10日 13:11
 * version 1.0
 */
public class BussCar extends Car {

    public BussCar(String name, String color) {
        super(name, color);
    }

    @Override
    public void run() {
        System.out.println(color +" " + name  +"在缓慢的行驶。。。");
    }


}

应用场景

package com.design.strategy;
/**
 * 
 * @ClassName   : Person 
 * @Description : 应用场景类
 *
 */
public class Person {
    private String name;    //姓名
    private Integer age;    //年龄
    private Car car;        //拥有车
    
    public void driver(Car car){
        System.out.print(name +"  "+ age+""+" 开着");
        car.run();
    }

    public Person(String name,Integer age) {
        this.name=name;
        this.age=age;
    }

}

运行环境

package com.design.strategy;
/**
 * 
 * @ClassName   : Strategy 
 * @Description : 运行环境类:Strategy    
 * @date        : 2017年12月9日 上午11:43:58
 *
 */
public class Strategy {
    public static void main(String[] args) {
        Car smallCar = new SmallCar("路虎","黑色");
        Car bussCar = new BussCar("公交车","白色");
        Person p1 = new Person("小明", 20);
        p1.driver(smallCar);
        p1.driver(bussCar);
    }
}

运行结果:
小明  20 岁  开着黑色 路虎在高速的行驶。。。
小明  20 岁  开着白色 公交车在缓慢的行驶。。。

   6.原型模式

  就是对对象的拷贝,深拷贝、浅拷贝生成新的对象。

 

  7.适配器模式  (由源到目标的适配)

  目标

public interface Robot
{
    public void cry();
    public void move();
}

  

public class Dog
{
    public void shout()
    {
        System.out.println("狗可以汪汪叫!");
    }
    public void run()
    {
        System.out.println("狗可以跑!");
    }
}

进行适配

public class DogAdapter extends Dog implements Robot
{
    public void cry(){
        System.out.print("机器人模仿:");
        super.shout();
    }
    public void move(){
        System.out.print("机器人模仿:");
        super.run();
    }
}

 如果想要实现一个接口,但是又不想实现所有的接口,我们可以抽象出来一个方法,然后再去继承这个类,进行方法的复写,这也是适配模式

  

 

  

 

 

 

  

 

 

  

posted @ 2019-05-10 09:42  ☞书香门第☜  阅读(160)  评论(0编辑  收藏  举报
http://count.knowsky.com/count1/count.asp?id=434520&sx=1&ys=64"