软件构造回顾(二)

在学习软件构造这门课时,我们接触到了设计模式这个概念,在最开始的学习过程中,由于没有用到过这些模式,所以并不能接受这些模式中的各种关系和其本身的意义,但是,在经过了学习与实践之后,我就能够浅略地了解这些模式的内容了。

适配器模式

这是最先讲的模式,可以采用一个类比的思想去理解这个模式的含义:在生活中不同国家的用电器的工作电压是不同的,好比日本和中国的就不一样,所以,如果想在中国使用日本的电器,那么一定要把这个电器先连在一个变压器上,再用变压器插在插座上,这样才能使得日本的电器在中国使用。

而适配器模式也是这个含义,他将一个类的接口转换成用户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。这就充当了“变压器”的角色。

代码如下:

interface Target
{
    public void request();
}
class Adapter
{
    public void specificRequest()
    {       
        System.out.println("适配器试用");
    }
}
class ClassAdapter extends Adapter implements Target
{
    public void request()
    {
        specificRequest();
    }
}
public class Adaptertest
{
    public static void main(String[] args)
    {
        Target target = new ClassAdapter();
        target.request();
    }
}

结果:
在这里插入图片描述

装饰器模式

装饰器模式:顾名思义,装饰吗,就像包装纸一样,如果一个物品,它被层层包装纸包装上,那么它不就具有了这些包装纸带来的色彩了吗?所以,当需要某些功能的组合的时候,我们也可以像加包装纸那样将各种功能进行组合。
如图:在这里插入图片描述
首先要有一个具体物件来实现共性的功能,之后由一个抽象类来实现包装纸的叠加。
实例:

public class Decoratortest
{
    public static void main(String[] args)
    {
        Component p=new Component1();
        p.operation();
        System.out.println("---------------------------------");
        Component d=new Decorator1(p);
        d.operation();
    }
}
interface  Component
{
    public void operation();
}
class Component1 implements Component
{
    public Component1()
    {
        System.out.println("创建具体构件角色");       
    }   
    public void operation()
    {
        System.out.println("具有Operation");           
    }
}
class Decorator implements Component
{
    private Component component;   
    public  Decorator(Component component)
    {
        this.component=component;
    }   
    public void operation()
    {
        component.operation();
    }
}
class Decorator1 extends Decorator
{
    public Decorator1(Component component)
    {
        super(component);
    }   
    public void operation()
    {
        super.operation();
        addedFunction();
    }
    public void addedFunction()
    {
        System.out.println("已增加功能");           
    }
}

结果:
在这里插入图片描述

外观模式

当我们为了达到某一个目标需要完成一系列的事情的时候,我们就像需要在不同的部门之间来回跑,所以,这个时候我们可以设想一下,如果有一个部门,它可以在本部门内完成这个目标,这是不是就能大大减小我们的花费?

外观模式的含义大致就是这个意思,它是一种通过为多个复杂的子模块提供一个一致的接口,而使这些子模块更加容易被访问的模式。

策略模式

策略模式的内容也很好理解,就是:为了完成一个目标,我们拥有不止一种方法去实现,那么,我么就得在其中挑选出一种最适合我们现状的方法。那么,怎么选择方法呢?这就不能硬选,不能再开发的时候写进去,为了更能适应现实,我们应该让系统在运行的时候自己进行挑选,所以,这就需要用到delegation。
实例:

interface Strategy
{   
    public void strategyMethod(); 
}
class StrategyA implements Strategy
{
    public void strategyMethod()
    {
        System.out.println("A的方法");
    }
}
class StrategyB implements Strategy
{
  public void strategyMethod()
  {
      System.out.println("B的方法");
  }
}
class Context
{
    private Strategy strategy;
    public Strategy getStrategy()
    {
        return strategy;
    }
    public void setStrategy(Strategy strategy)
    {
        this.strategy=strategy;
    }
    public void strategyMethod()
    {
        strategy.strategyMethod();
    }
}
public class Strategyce
{
    public static void main(String[] args)
    {
        Context c=new Context();
        Strategy s1=new StrategyA();
        c.setStrategy(s1);
        c.strategyMethod();
        Strategy s2=new StrategyB();
        c.setStrategy(s2);
        c.strategyMethod();
    }
}

模板模式

在生活中,我们遇到过一个脑筋急转弯:把大象放冰箱需要几步?答:三步:把冰箱门打开;把大象放进去;把冰箱门关上。这个脑筋急转弯充分体现了这个模式的思想,也许每个人的方法具体的实现方式都不一样,但是,他们的方法肯定可以这么分。所以,针对于完成一个目标,有着固定的方法顺序,但是实现方法的方式或细节可能不同,所以这个时候就需要用到这个模式了。

它定义一个操作中的算法骨架,而将算法的一些步骤放到到子类中,使得子类可以不改变该算法结构的情况下重定义该算法的某些特定步骤。这样就可以满足方法步骤大致相同却实现方式不同的需求。
例子:

public class Template
{
    public static void main(String[] args)
    {
    	AbstractTemplate tm=new mod();
        tm.TemplateMethod();
    }
}
abstract class AbstractTemplate
{
    public void TemplateMethod()
    {
        System.out.println("调用方法");
        abstractMethod1();          
        abstractMethod2();
    }   
    public abstract void abstractMethod1(); 
    public abstract void abstractMethod2(); 
}
class mod extends AbstractTemplate
{
    public void abstractMethod1()
    {
        System.out.println("方法一");
    }   
    public void abstractMethod2()
    {
        System.out.println("方法二");
    }
}

结果:
在这里插入图片描述

迭代器模式

这应该是最先接触到的模式,以至于在接触到的时候都没有注意。老师在一次实验课介绍的时候说了Iterator的用法,但是后来,我们才知道,这也是一种设计模式。
迭代器模式:它在客户访问类与聚合类之间插入一个迭代器,这分离了聚合对象与其遍历行为,对客户端也隐藏了其内部细节,这也满足“单一职责原则”和“开闭原则”,防止信息泄露。

实现方式就是:让自己的集合类实现Iterable接口,并实现自己的独特Iterator类完成迭代器(三种方法:hasNext, next, remove)的设计,允许客户端利用这个迭代器进行显式或隐式的迭代遍历:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值