案例分析:设计模式与代码的结构特性

一、什么是设计模式(Design Pattern)?

设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。

设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的。

设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结。

使用设计模式是为了重用代码、让代码更容易被他人理解、保证代码可靠性。

毫无疑问,设计模式于己于他人于系统都是多赢的,设计模式使代码编制真正工程化,设计模式是软件工程的基石,如同大厦的一块块砖石一样。

项目中合理地运用设计模式可以完美地解决很多问题,每种模式在现实中都有相应的原理来与之对应,每种模式都描述了一个在我们周围不断重复发生的问题,以及该问题的核心解决方案,这也是设计模式能被广泛应用的原因。

二、设计模式有哪些?

1、创建型模式(Creational Pattern)

创建型模式(Creational Pattern)对类的实例化过程进行了抽象,能够将软件模块中对象的创建和对象的使用分离。为了使软件的结构更加清晰,外界对于这些对象只需要知道它们共同的接口,而不清楚其具体的实现细节,使整个系统的设计更加符合单一职责原则。

2、结构型模式(Structural Pattern)

结构型模式(Structural Pattern)描述如何将类或者对 象结合在一起形成更大的结构,就像搭积木,可以通过 简单积木的组合形成复杂的、功能更为强大的结构。

3、行为型模式(Behavioral Pattern)

 

行为型模式(Behavioral Pattern)是对在不同的对象之间划分责任和算法的抽象化。

 

行为型模式不仅仅关注类和对象的结构,而且重点关注它们之间的相互作用。

 

通过行为型模式,可以更加清晰地划分类与对象的职责,并研究系统在运行时实例对象 之间的交互。在系统运行时,对象并不是孤立的,它们可以通过相互通信与协作完成某些复杂功能,一个对象在运行时也将影响到其他对象的运行。

 

行为型模式分为类行为型模式和对象行为型。

 

三、代码示例分析

代码示例为工厂模式(Factory Pattern)

这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

在工厂模式中,我们在创建对象时不对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

 

工厂模式又分为:简单工厂模式、工厂方法模式和抽象工厂模式

简单工厂模式:通过定义一个类来负责创建其他类的实例,被创建的实例具有共同父类。

例如,文具店,首先是定义一个总体文具店,包含其各种行为。还要包含各种文具。 1 package pattern.factory;

 2  
 3 public abstract class Stationer {
 4  
 5     protected String name;
 6     public void open() {
 7         System.out.println("开店。。。");
 8     }
 9
10     public void Stock() {
11         System.out.println("购进" + this.name);
12     }
13     public void close(){
14         System.out.println("关店。。。");
15 } 16 }

 

 

1 public class Pen extends Stationer {
2  
3     public Pen() {
4         this.name = "笔";
5     }
6 }

 

1 public class Ruler extends Stationer {
2  
3     public Ruler() {
4         this.name = "尺子";
5     }
6 }
1 public class Pencil extends Stationer{
2  
3     public Pencil() {
4         this.name = "铅笔";
5     }
6 }

 

未使用简单工厂模式的文具店

 

 1 public class OldStationer {
 2   
 3      public Stationer sell(String type) {
 4          Stationer stuff = null;
 5          if (type.equals("pencil")) {
 6              stuff = new Pencil();
 7          } else if (type.equals("pen")) {
 8              stuff = new Pen();
 9          } else if (type.equals("ruler")) {
10             stuff = new Ruler();
11         }
12         
13         Stationer.stock();
14         Stationer.open();
15         return stuff;
16     }
17}

 

创建简单工厂

 1 public class SimpleStationerFactroy {
 2     
 3     public Stationer createStationer(String type) {
 4         Stationer stuff = null;
 5         if (type.equals("pen")) {
 6             stuff = new Pen();
 7         } else if (type.equals("Ruler")) {
 8             stuff = new Ruler();
 9         } else if (type.equals("pencil")) {
10             stuff = new Pencil();
11         }
12         return stuff;
13     }
14     
15 }

使用简单工厂之后的文具店

public class Stationer {
    private SimpleStationerFactroy factroy;
 
    public StationerStore(SimpleStationerFactroy factroy) {
        this.factroy = factroy;
    }
 
    public Stationer sellstuff(String type) {
        Stationer stationer = factroy.createStationer(type);
        stationer.stock();
        stationer.open();
        return stationer;
    }
}

用了简单工厂模式,以后添加文具,或者是删除文具都和Store无关,Store只负责开店进货关店。

 

 

 

 

 

 

posted on 2019-12-07 21:19  NiGo666  阅读(194)  评论(0)    收藏  举报

导航