Design Patterns

Where is the cost of software products from?

-Effort and time

Abstraction

  Abstraction is a process by which higher concepts are derived from the usage and classification of "real" or "concrete” concepts.

  It can help us to transfer and reuse our knowledge

 

Patterns

 

Pattern is a kind of abstractions

What patterns?

  Patterns in software development:

  A general reusable solution to a commonly occurring problem within a given context in software design.

  A design pattern is NOT a finished design that can be transformed directly into code.

  It is a description or template for how to solve a problem that can be used in many different situations.

 

Why do we need patterns?

  Design and development are challenging tasks:

  Balancing concerns, e.g. performance, adaptability, reliability.

  Defining components and their interrelationships.

  Thus experienced designers: Rarely start from first principles Look for similarities to problems solved in the past.

  Apply a working "handbook" of approaches Patterns can:

  Make expert knowledge widely available

  Supports focusing on the truly distinctive problems.

  Enable more efficient communication, and promote better understanding by providing a useful working vocabulary for design

  Save developers’ time/cost

Adapter pattern: 

  adapter中文為轉換器、轉接器,主要的目的就是將不相容的interface做轉換。

Factory pattern:

  工厂方法模式: 定义了一个创建对象的接口, 但由子类决定要实例化的类是哪一个. 工厂方法让类把实例化推迟到子类.

包括:

  创建者父类(creator), 包含创建对象的方法(代替new具体的对象, 通过参数创建不同的对象), 和一些基本的方法;

  具体创建者(concrete creator), 继承创建者父类, 实现创建对象的方法, 不同参数可以创建不同的对象;

  产品类父类(product), 包含产品的基本使用方法, 被创建者父类(creator)的基本方法使用;

  具体产品(concrete creator), 继承产品类父类, 对特定的产品进行定制, 供具体创建者(concrete creator)选择创建;

Singleton pattern:

  单例模式的特点:

  单例类只能有一个实例。

  单例类必须自己创建自己的唯一实例。

  单例类必须给所有其它对象提供这一实例。

单例模式应用:

  • 每台计算机可以有若干个打印机,但只能有一个Printer Spooler,避免两个打印作业同时输出到打印机。
  • 一个具有自动编号主键的表可以有多个用户同时使用,但数据库中只能有一个地方分配下一个主键编号。否则会出现主键重复。
  • Mediator pattern

  

 

 

 

Model View Controller (MVC) pattern:

Separate the UI from the model

  The single most important high-level structuring principle is to separate the UI from the model (business logic, problem domain, ...).

    The program structure is clearer

    UI knows nothing about model, and model knows nothing about UI

    Program enhancements and maintenance are much easier with this structure.

  A further separation is often proposed which additionally splits the user interface into a View (displaying information) and Controller (processing user interactions)

 

Model View Controller (MVC) Pattern

  Model: represents data and the rules that govern access to and updates of this data.

  View: renders the contents of a model. It specifies exactly how the model data should be presented. If the model data changes, the view must update its presentation as needed.

  Controller: translates the user's interactions with the view into actions that the model will perform. E.g., in a stand-alone GUI client, user interactions could be button clicks or menu selections

 

              

Observer and Observable

  Observer is very commonly used, e.g., a listener for a button.

  Observer is not limited to single components.

        

 

Model View Controller (MVC) Pattern

  Once a user interacts with the view, the following actions occur:

  1. The view recognizes that a user action has occurred

  2. The view generates an event, which implicitly invokes appropriate method in the controller

  3. The controller accesses the model. Possibly updating it with respect to the user's action

  4. If the model has been altered, it notifies interested listeners, such as the view, of the change.

  The controller may also update the view

Advantages of MVC:

  Code reusability Separation of concerns

  Easier task allocation

  Higher M&M modifiability and maintainability

 

 

 

Patterns may limit the developer in several ways:

  The amount of detail or the number of concepts that the developer is allowed to deal with may be limited

  The way elements are allowed to interact may be restricted

  A pattern may constrain the solution of some sub problems to be addressed in a less than optimal way

 

 

Keys to Using Patterns Effectively

  Recognize, search, instantiate:

    Recognize a problem as common.

    Recognize the key elements of the problem.

    Search a pattern catalog from proven solution templates.

    Instantiate the template for the specific problem.

    Recognition comes with experience

    Patterns are not always good!!!

    Combine Modify

posted @ 2017-12-19 01:00  CaiCongyu  阅读(142)  评论(0编辑  收藏  举报