设计模式与框架——课程总结

DPF课程的考前总结,备份一下,图片后面补充。

Behavioral Variability Patterns

  • Template Method: Define the skeleton of algorithm in template method and redefine some primitive operations in subclasses (without changing the structure of the algorithm). In Template Method pattern, template and hook are implemented with the same class but different methods. (Role Model的视角看,Caller和Callee是equivalence关系。)
  • Objectifier: Hook method can be split out of the class and put into a seperate object.
  • Template Class: Combine Template Method and Objectifier. Hook objects can be exchanged at run time to exchange a set of methods.
  • Strategy: Talks about the exchange of algorithm.
  • Generic Template Class: Use static generic expansion to realize template class, which provides more type safety and less runtime dispatch.
  • Dimensional Class Hierarchy: Vary the template class in a class hierarchy. Concrete template classes can change the template algorithm.
  • Bridge: A variant of DCH with different incentive. The intention is to seperate the abstraction and implementation so that they can vary independently.
  • Visitor: Also a variant of DCH. One can define a new operation without changing the classes of the elements on which it operates. Visitor is used when many distinct and unrelated operations need to be performed on objects in an object structure, where the objects rarely change.
  • Facet Classification: Seperate concerns into different facets. These facets are orthogonal dimensions of the model.
  • Layered Objects: If dimensions in a Bridge have dependencies, then a contract is needed between dimensions. Layered objects shows the dimension dependencies. Upper layers depend on information of lower layers.

Comparison

  • Template Method vs. Bridge:

    Common: They define abstract methods in super class and implement them in subclass.

    Difference: 1. The Bridge hides how abstract methods are implemented because interface and implementation are seperated. 2. And in Bridge pattern, abstraction and implementation can be refined seperatly.

  • Template Method vs. Strategy:

    1. They both provide variation of method. Template Method is used when parts of the algorithm should be varied. With Strategy, the whole algorithm is varied.
    2. Template Method can do type checking during compile time. In Strategy, problems occurr at run time can’t be found statically.
  • Objectifier vs. Template Class:

    Objectifier is more abstract. Requests are delegated to the objectifier.

    Different role model.

  • Template Class vs. Generic Template Class:

    1. Template Class uses polymorphism to dispatch to the concrete hook classes. In Generic Template Class, the polymorphic dispatch is expanded at compile time, by the generic expansion. (type safe)
    2. The design pattern Generic Template Class flattens the inheritance hierarchy of the hook classes.

Creational Variability Patterns

  • Factory Method: Subclasses specialize the constructor and enrich with more behavior.
  • Abstract Factory
    • Prototyping Factory
    • Interpretive Factory
  • Builder

Abstract Factory

Given many different families of products. Abstract factory helps switching between classes of different product families in a consistent way.

Builder

Data structure defines a protocol.

Use director to traverse the data structure and call different build methods for corresponding parts.

The Builder hides:

  • the protocol
  • the current status
  • the implementation of the data structure

Comparison

  • Template Method vs. Factory Method:

    Same structure. But template method is a behavioral pattern and factory method is a creational pattern. For behavioral patterns, the abstract class defines an abstract method and the behavior is implemented in the subclass. For creational patterns, they use the behavior to create objects (products).

Extensibility Patterns

Recursive Extension:

  • Object Recursion (1/*-1/*): Terminator and recurser can be treated uniformly under one abstract super class.
  • Composite (1-*): Dynamically add and remove nodes in a tree. The iterator does not need to know whether it works on a leaf or an inner node.
  • Decorator (1-1): Dynamically add responsibilities to the core object.
  • Chain of Responsibility (recursing on the abstract super class): Delegate an action to a list of delegatees that attempt to solve the problem one after another.

Flat Extension: Flat means it has a direct pointer to the sister class

  • Proxy: Hide the access to a real subject by representative
  • *-Bridge: Add more hook object at run time
  • Observer: When something in the subject changes, it notifies the observer to make change correspondingly

Architectural Glue Patterns

Mismatch

Different assumptions about:

  • components
  • connections
  • architecture
  • building procedure

Patterns

  • Adapter: Used to solve architechtural mismatch
    • Object adapter: Adapter has a reference to the adaptee
    • Class adapter: Use multiple inheritance to implement class adapter
  • Facade: Provide a unified interface to a set of interfaces in a subsystem
  • Mediator: Coordinate a set of objects interact. Hides all communication partners. Every partner uses mediator object as proxy.

Role-based Design

Role, Role Types and Role Model

  • A role is a dynamic view onto an object, which can change dynamically.

  • A role type is a dynamic view type onto an object.

  • A role model is a set of object collaborations described by a set of role types.

设计模式的概念中每个类都是一个角色类型,他们在实际应用中所对应的类或者对象扮演这个角色。

Every class in the design pattern is a role type. The matched class of the application plays the role. Or the object plays the role.

Role model中描述的类和对象的协作方式由role constraints确定。

Role Model + Class Model => Class-role Model

Role Constraints

role-use, role-implication, role-equivalence, role-implication-inheritance, role-association, role-prohibition

画图,意义

Benefits of Role-based Design

  • Roles split classes into smaller pieces, which improves cohesion and reduces coupling.
  • Roles emphasize the context-dependent parts of classes.
  • Roles seperate concerns.
  • Role models can be reused independently of classes.

Role Mapping in MDA (Model-driven Architecture)

  • Requirements Model
  • Role Models
  • Merge Role Models
  • Class Model
  • Code

Implementation of Role

  • Interface

    Cons: Code for the interfaces must be written by hand. By reuse of same roles, interfaces needed to be implemented again.

  • Multiple-inheritance

    Cons: A standard class can inherit from a role class only once.

  • Mixin classes

    Pros: no schizophrenia

    Cons: only static extensibility

  • Role-object pattern (Delegation)

    Pros: Solves the problem of object schizophrenia.

    Cons: All the code about role management and reflection needs to be implemented manually.

  • EOP/Decorator…

Formal Model of Role Type

Role types are:

  • non-rigid: An object could stop being of the type, and still keeps its identity. E.g. Reader can stop reading.
  • founded: A role type always needs a counter role type. (An object of the type is always in collaboration with another object).

Rigid, founded: compartment types/relationship types

Rigid, non-founded: natural types

non-rigid, non-founded: phase types

Instances of a founded type

  • always have a counter role
  • always have a context (collaboration instance)

Design Patterns as Role Models

Role Models not mentioned:

  • Visitor:

    Participants: Visitor, Element, Object Structure

  • Builder

    Participants: Director, Builder

  • Proxy

    Participants: Subject, Proxy

    Proxy Subject

  • Facade

    Participants: Facade, Subsystem

    Facade Subsystem

Role Object Pattern

Object Schizophrenia

When spliting a conceptual object into several physical objects, different objects have different identities.

But in fact, all the physical objects should only have one same identity, which is the identity of their conceptual object.

Role Object Pattern

Used to implement roles in role-oriented programming.

结构图

Core object represents the identity. In order to solve object schizophrenia, clients always have to ask the core object for the identity. If the role object is being asked, it forwards the request to its core object. Hence the role object must know the core object. (reference in UML)

The core object delegate all the call to the respective role. The role objects implement different methods.

Framework Variability Pattern

Framework

Framework is a class model with open roles.

A framework can be instantiated by binding open roles to classes.

Intergration repertoire: a set of open role types, by which the framework can be integrated into an application.

Repertoire: a set of role types of a class. 指一个类的角色类型集合

而integration repertoir指一个框架的开放角色类型集合

Framework Hook Patterns

Attributes

Framework Hook Patterns describe the roles of classes on the border of the framework using design patterns.

Framework variation point is characterized with Template & Hook Role Model (or called Template & Hook Metapattern [Pree]).

当然Framework Hook Pattern也根据目的分为Variation和Extension两种,这一节讨论的是Variation。

In Framework Hook Patterns, the template part of the role model is fixed. Hook class can be exchanged.

Template & Hook Role Model + standard design pattern => Framework Hook Pattern

Template & Hook Role Model adds extra constraints on standard design patterns:

  • Template part cannot be exchaned.
  • Only defined on the border of the framework.

Types of Framework Hook Patterns

  • Delegation-based (Blackbox Framework)

    1-T–H: dynamic variability (Template Class, Strategy, Dimensional Class Hierarchy, Bridge)

    n-T–H: dynamic flat extensibility (Observer, Multi-bridge)

  • Recursion-based (between Blackbox and Whitebox)

    1-H<=T: (1-object Recursion, Decorator)

    n-H<=T: (n-object Recursion, Composite)

  • Unification-based

    TH

    1-TH: (Chain of Responsibility)

    n-TH

  • Inheritance-based (Whitebox Framework)

    H<T: (Template Method)

Framework Extensibility Pattern

Extension Objects Pattern

结构图

Pros:

  • Extensions can be added dynamically and unforeseen
  • Complex objects can be split into simpler parts
  • Extensions can model roles of objects

Cons:

  • Clients will become complicated because they have to manage extension objects themselves
  • Object schizophrenia

EOP as Framework Hook Pattern

  • 1-H=T
  • n-H=T

Role Object Pattern (Layers)

ROP is both a variability pattern and an extensibility pattern.

Core and roles conceptually belong to the conceptual object, but distributed over the layers.

  • Deep Roles: roles play roles

  • Flat Roles: core plays roles

  • Trader: interprets a service request based on a service description

    在所有类中添加一个方法accept(ServiceDescription)来接受服务描述,在role map中查找服务并调用

    Traded call adds services

几个ROP的结构图

Layer Pattern as Framework Hook Pattern

  • n-TrH

GenVoca Pattern

Mixin

Mixin class adds new features for mixin-base class by parameterizing it.

Mixin-base class is a class with generic super class.

Mixin class为mixin-base添加新功能

GenVoca Pattern

GenVoca is a nested mixin parameterization.

Each mixin represents a role.

Pros: merge all roles into one physical object so there’s no object schizophrenia

Cons: only provides static extensibility

Mixin Layer Pattern

GenVoca is a single stack of parameterization.

Mixin Layer Pattern groups all roles of an abstraction layer together and compose entire layers.

The whole layer can be dynamically exchanged.

But! Only provides static extensibility.

设计模式 GenVoca Pattern Role Object Pattern
分层 Abstraction Layer Role Layer
基础 Mixin Role Object
分层框架 Framework with Mixin Layer Pattern Layer Role Object Framework

TAM Architechtural Style and Pattern Language

TAM metaphore: People use tools to work on materials. The environment is the workshop or desktop.

Materials: passive entities

  • Values
  • Objects

Tools: active entities

Tools can be split into two parts:

  • IP (interaction part): interact with user (GUI)
  • FP (functional part): work on materials (business logic)

IP-FP refines MVC:

  • IP: View
  • FP: Controller and managing part of the material
  • Material except for managing part: Model

Environment:

  • Tool coordinator: manage dependencies between tools.
  • Material container: manage dependencies between materials using container and constraint.

TAM as Layered Framework:

  • Interaction Part
  • Functional Part
  • Material Containers
  • Material

发表回复

您的电子邮箱地址不会被公开。