抽象模板(实例)

当我们编写代码的时候会发现很多代码都是在重复的使用这样就会让代码的可读性变差所以我们可以运用模板设计模式简化代码:这里有个简单的实例:当我们不使用模块设计模式写代码的时候我们按正常的思路应该这么写:

​
public class Stringname {
    public static void main(String[] args) {
        Man jack = new Man("jack");
        jack.morning();
        jack.exercise();
        jack.afternoon();
        jack.evening();
        Woman nucy = new Woman("nucy");
        nucy.morning();
        nucy.afternoon();
        nucy.shop();
        nucy.evening();
    }
}

abstract class Persontype{
    private String name;

    public Persontype(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    abstract public void morning();
    abstract public void afternoon();
    abstract public void evening();
}


class Man extends Persontype{
    public Man(String name) {
        super(name);
    }

    @Override
    public void morning() {
        System.out.println(getName() + "正在吃早饭");
    }

    @Override
    public void afternoon() {
        System.out.println(getName() + "正在吃午饭");
    }
    public void exercise(){
        System.out.println(getName() + "去锻炼");
    }

    @Override
    public void evening() {
        System.out.println(getName() + "正在吃晚饭");
    }
}

class Woman extends Persontype{
    public Woman(String name) {
        super(name);
    }

    @Override
    public void morning() {
        System.out.println(getName() + "正在吃早饭");
    }

    @Override
    public void afternoon() {
        System.out.println(getName() + "正在吃午饭");
    }
    public void shop(){
        System.out.println(getName() + "去逛街");
    }

    @Override
    public void evening() {
        System.out.println(getName() + "正在吃晚饭");
    }
}

​

该代码我们发现十分的繁琐,有很多代码都是重复的,代码可读写很差这里就要用到我们的抽象类的模版设计:

public class Stringname {
    public static void main(String[] args) {
        Man jack = new Man("jack");
        jack.morning();
        jack.activity();
        jack.afternoon();
        jack.evening();
        Woman nucy = new Woman("nucy");
        nucy.morning();
        nucy.afternoon();
        nucy.activity();
        nucy.evening();
    }
}

abstract class Persontype{
    private String name;

    public Persontype(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void morning(){
        System.out.println(getName() + "正在吃早饭");
    }
    public void afternoon(){
        System.out.println(getName() + "正在吃午饭");
    }
    public abstract void activity();
    public void evening(){
        System.out.println(getName() + "正在吃晚饭");
    }
}


class Man extends Persontype{
    public Man(String name) {
        super(name);
    }

    @Override
    public void activity() {
        System.out.println(getName() + "去锻炼");
    }
}

class Woman extends Persontype{
    public Woman(String name) {
        super(name);
    }

    @Override
    public void activity() {
        System.out.println(getName() + "去购物");
    }
}

该代码就将公共的代码放在了父类中,不一样的方法在子类中进行重写,你会发现只有activity这个方法在两个子类中的表述不一样这也是我们模块的突破点,就可以在抽象类中将这个方法设置为抽象方法,然后进行重写,这样就能达到我们简化代码的目的。

你可能感兴趣的:(java,servlet,开发语言)