码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3D & C# 设计模式--23

时间:2014-11-16 14:41:28      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:ugui   unity3d   脚本   设计模式   design patterns   

??

Unity3D & C#Design Patterns

23 design patterns.

Creational Patterns

1. Abstract Factory抽象工厂

创建几个相似的类的一个实例

2. Builder生成器

分离对象构造与它的表示

3. Factory Method工厂方法

创建几个派生类的一个实例

4. Prototype原型

要复制或克隆一个完全初始化的实例

5. Singleton单件

一个类只能运行一个实例可以存在

Structural Patterns

6. Adapter适配器

不同的类的接口相匹配

7. Bridge桥接

从其实现分离对象接口

8. Composite复合

简单和复合对象的树形结构

9 .Decorator装饰者

动态添加到对象的责任

10. Facade外观

一个表示整个子系统的单个类

11. Flyweight享元

细粒度的实例用于高效共享

12. Proxy代理服务器

一个表示另一个对象的对象

Behavioral Patterns

13. Chain of Resp.职责链模式

一连串的对象之间传递请求的一种方式

14. Command命令

将命令请求封装为一个对象

15. Interpreter解释器

方法包含程序中的语言元素

16. Iterator迭代器

按顺序访问集合中的元素

17. Mediator中介者

定义简化的类之间的通信

18. Memento备忘录

捕获和还原对象的内部状态

19. Observer观察者

一种方式通知到类数目的变化

20. State状态

在其状态改变时,改变一个对象的行为

21. Strategy策略

封装在类的内部算法

22. Template Method模板方法

推迟算法到子类的确切步骤

23. Visitor访问者

对一类没有改变定义新的操作

 

1 AbstractFactory抽象工厂

定义:

提供一个接口,而无需指定它们具体的类创建一系列相关或相互依赖对象。

使用频率:bubuko.com,布布扣 

UML 类图:

bubuko.com,布布扣

参与者:

    类和对象参加这种模式是:

  • AbstractFactory  (ContinentFactory)
    • declares an interface for operations that create abstract products
  • ConcreteFactory   (AfricaFactory, AmericaFactory)
    • implements the operations to create concrete product objects
  • AbstractProduct   (Herbivore, Carnivore)
    • declares an interface for a type of product object
  • Product  (Wildebeest, Lion, Bison, Wolf)
    • defines a product object to be created by the corresponding concrete factory
    • implements the AbstractProduct interface
  • Client  (AnimalWorld)
    • uses interfaces declared by AbstractFactory and AbstractProduct classes

C# 中的结构代码:

此结构的代码演示了创建对象的并行层次结构的抽象工厂模式。创建对象已经被抽象出来,并不需要在客户端代码中硬编码的类名称。

 

 

2 Builder生成器

定义:

分离复杂对象的构建与它的表示,同样的构建过程可以创建不同的表示。

使用频率:bubuko.com,布布扣    中低

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Builder  (VehicleBuilder)
    • specifies an abstract interface for creating parts of a Product object
  • ConcreteBuilder  (MotorCycleBuilder, CarBuilder, ScooterBuilder)
    • constructs and assembles parts of the product by implementing the Builder interface
    • defines and keeps track of the representation it creates
    • provides an interface for retrieving the product
  • Director  (Shop)
    • constructs an object using the Builder interface
  • Product  (Vehicle)
    • represents the complex object under construction. ConcreteBuilder builds the product‘s internal representation and defines the process by which it‘s assembled
    • includes classes that define the constituent parts, including interfaces for assembling the parts into the final result

 

C# 中的结构代码:

此结构的代码演示了分步的方式在其中创建复杂对象的生成器模式。施工过程可以创建不同的对象表示形式,并提供高水平的控制对象的程序集。

 

 

3 FactoryMethod工厂方法

定义:

定义一个接口用于创建对象,但是让子类决定实例化哪个类。工厂方法允许推迟到子类的实例化的类。

使用频率:  bubuko.com,布布扣  

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Product  (Page)
    • defines the interface of objects the factory method creates
  • ConcreteProduct  (SkillsPage, EducationPage, ExperiencePage)
    • implements the Product interface
  • Creator  (Document)
    • declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.
    • may call the factory method to create a Product object.
  • ConcreteCreator  (Report, Resume)
    • overrides the factory method to return an instance of a ConcreteProduct.

C# 中的结构代码:

此结构的代码演示了工厂方法提供极大的灵活性,在创建不同的对象。抽象类可以提供一个默认的对象,但每个子类可以实例化对象的扩展的版本。

 

 

4 Prototype原型

定义:

指定要使用一个典型的实例,创建的对象的类型,通过拷贝这些原型创建新的对象。

使用频率:bubuko.com,布布扣  介质

UML 类图:

bubuko.com,布布扣

 

参与者:

    Theclasses and objects participating in this pattern are:

  • Prototype  (ColorPrototype)
    • declares an interface for cloning itself
  • ConcretePrototype  (Color)
    • implements an operation for cloning itself
  • Client  (ColorManager)
    • creates a new object by asking a prototype to clone itself

C# 中的结构代码: 

此结构的代码演示了在其中创建新对象通过复制已存在的对象(原型) 在同一类的原型模式。

 

 

5 Singleton单件

定义:

确保一个类只有一个实例,并提供一个全局访问点访问它。

使用频率:bubuko.com,布布扣  中高

UML 类图

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Singleton   (LoadBalancer)
    • defines an Instance operation that lets clients access its unique instance. Instance is a class operation.
    • responsible for creating and maintaining its own unique instance.

C# 中的结构代码:

此结构的代码演示可以创建单例模式确保了只有单个实例 (单身人士) 的类。

 

 

6 Adapter适配器

定义:

将一个类的接口转换成客户希望的另外一个接口。适配器让各类共同工作,否则无法由于不兼容的接口。

使用频率:bubuko.com,布布扣 中高

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Target   (ChemicalCompound)
    • defines the domain-specific interface that Client uses.
  • Adapter   (Compound)
    • adapts the interface Adaptee to the Target interface.
  • Adaptee   (ChemicalDatabank)
    • defines an existing interface that needs adapting.
  • Client   (AdapterApp)
    • collaborates with objects conforming to the Target interface.

C# 中的结构代码:

此结构的代码演示了适配器模式映射到另一个类的接口,以便他们可以一起工作。这些不兼容的类可能来自不同的库或框架。

 

 

 

7 Bridge桥接

定义:

所以,这两个可以独立的变化,解耦及其实现的一种抽象。

使用频率:bubuko.com,布布扣 介质

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Abstraction   (BusinessObject)
    • defines the abstraction‘s interface.
    • maintains a reference to an object of type Implementor.
  • RefinedAbstraction   (CustomersBusinessObject)
    • extends the interface defined by Abstraction.
  • Implementor   (DataObject)
    • defines the interface for implementation classes. This interface doesn‘t have to correspond exactly to Abstraction‘s interface; in fact the two interfaces can be quite different. Typically the Implementation interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives.
  • ConcreteImplementor   (CustomersDataObject)
    • implements the Implementor interface and defines its concrete implementation.

C# 中的结构代码:

此结构的代码演示了桥接模式分离(分离) 从其实现的接口。执行可以进化而不更改客户端所使用的对象的抽象。

 

 

8  Composite组合

定义:

组合成树状结构来表示部分整体层次结构的对象。复合允许客户端均匀地看待单个对象和对象的组合。

使用频率:bubuko.com,布布扣 中高

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Component   (DrawingElement)
    • declares the interface for objects in the composition.
    • implements default behavior for the interface common to all classes, as appropriate.
    • declares an interface for accessing and managing its child components.
    • (optional) defines an interface for accessing a component‘s parent in the recursive structure, and implements it if that‘s appropriate.
  • Leaf   (PrimitiveElement)
    • represents leaf objects in the composition. A leaf has no children.
    • defines behavior for primitive objects in the composition.
  • Composite   (CompositeElement)
    • defines behavior for components having children.
    • stores child components.
    • implements child-related operations in the Component interface.
  • Client  (CompositeApp)
    • manipulates objects in the composition through the Component interface.

C# 中的结构代码:

此结构的代码演示了所允许的树状结构,各个节点均匀访问,无论他们是叶节点或分支(复合) 节点创建的复合模式。

 

 

9  Decorator装饰者模式

定义:

动态地将责任附加到对象的附加。为扩展功能,装饰者提供了比继承更灵活的替代方案。

使用频率:bubuko.com,布布扣  介质

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Component   (LibraryItem)
    • defines the interface for objects that can have responsibilities added to them dynamically.
  • ConcreteComponent   (Book, Video)
    • defines an object to which additional responsibilities can be attached.
  • Decorator   (Decorator)
    • maintains a reference to a Component object and defines an interface that conforms to Component‘s interface.
  • ConcreteDecorator   (Borrowable)
    • adds responsibilities to the component.

C# 中的结构代码:

此结构的代码演示了装饰者模式的动态地将额外的功能添加到现有的对象。

 

 

 

 

10 Facade外观模式

定义:

提供一组在一个子系统接口统一的接口。外观定义了一个更高级别的接口使子系统更易于使用。

使用频率:bubuko.com,布布扣

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Facade   (MortgageApplication)
    • knows which subsystem classes are responsible for a request.
    • delegates client requests to appropriate subsystem objects.
  • Subsystem classes   (Bank, Credit, Loan)
    • implement subsystem functionality.
    • handle work assigned by the Facade object.
    • have no knowledge of the facade and keep no reference to it.

C# 中的结构代码:

此结构的代码演示 Facade 模式,提供了一个简化和统一到一个大型的子系统的类接口。

 

11 Flyweight享元模式

定义:

使用共享来有效地支持大量细粒度的对象。

使用频率:bubuko.com,布布扣

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Flyweight   (Character)
    • declares an interface through which flyweights can receive and act on extrinsic state.
  • ConcreteFlyweight   (CharacterA, CharacterB, ..., CharacterZ)
    • implements the Flyweight interface and adds storage for intrinsic state, if any. A ConcreteFlyweight object must be sharable. Any state it stores must be intrinsic, that is, it must be independent of the ConcreteFlyweight object‘s context.
  • UnsharedConcreteFlyweight   ( not used )
    • not all Flyweight subclasses need to be shared. The Flyweight interface enables sharing, but it doesn‘t enforce it. It is common for UnsharedConcreteFlyweight objects to have ConcreteFlyweight objects as children at some level in the flyweight object structure (as the Row and Column classes have).
  • FlyweightFactory   (CharacterFactory)
    • creates and manages flyweight objects
    • ensures that flyweight are shared properly. When a client requests a flyweight, the FlyweightFactory objects assets an existing instance or creates one, if none exists.
  • Client   (FlyweightApp)
    • maintains a reference to flyweight(s).
    • computes or stores the extrinsic state of flyweight(s).

C# 中的结构代码:

此结构的代码演示了在其中为数较少的对象由不同的客户端共享多次的Flyweight模式。

 

 

 

12  Proxy代理模式

定义:

提供代理项或另一个对象的占位符以控制对它的访问。

使用频率:bubuko.com,布布扣 中高

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Proxy   (MathProxy)
    • maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.
    • provides an interface identical to Subject‘s so that a proxy can be substituted for for the real subject.
    • controls access to the real subject and may be responsible for creating and deleting it.
    • other responsibilites depend on the kind of proxy:
      • remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.
      • virtual proxies may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real images‘s extent.
      • protection proxies check that the caller has the access permissions required to perform a request.
  • Subject   (IMath)
    • defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.
  • RealSubject   (Math)
    • defines the real object that the proxy represents.

C# 中的结构代码:

此结构的代码演示了代理模式提供一个代表性的对象(代理) 控制着通往另一个类似的对象。

 

 

13  Chain of Responsibility职责链模式

定义:

避免耦合的请求发件人对它的接收器,让多个对象有机会来处理该请求。链的接收对象并传递链的请求,直到一个对象处理它。

使用频率:bubuko.com,布布扣 中低

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Handler   (Approver)
    • defines an interface for handling the requests
    • (optional) implements the successor link
  • ConcreteHandler   (Director, VicePresident, President)
    • handles requests it is responsible for
    • can access its successor
    • if the ConcreteHandler can handle the request, it does so; otherwise it forwards the request to its successor
  • Client   (ChainApp)
    • initiates the request to a ConcreteHandler object on the chain

C# 中的结构代码:

此结构的代码演示了几个链接的对象(链) 提供对请求作出回应或转交给行中的下一个对象的机会的责任链模式。

 

 

14  Command命令模式

定义:

将一个请求封装为一个对象,从而让您将参数化客户提供不同的请求、队列或日志请求,以及支持可撤销的操作。

使用频率:bubuko.com,布布扣 中高

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Command  (Command)
    • declares an interface for executing an operation
  • ConcreteCommand  (CalculatorCommand)
    • defines a binding between a Receiver object and an action
    • implements Execute by invoking the corresponding operation(s) on Receiver
  • Client  (CommandApp)
    • creates a ConcreteCommand object and sets its receiver
  • Invoker  (User)
    • asks the command to carry out the request
  • Receiver  (Calculator)
    • knows how to perform the operations associated with carrying out the request.

C# 中的结构代码:

此结构的代码演示了将请求存储为对象允许客户端执行或播放请求的命令模式。

 

 

15  Interpreter解释器模式

定义:

给定一个语言,定义和解释器使用该表示来解释语言中的句子,其语法的表示法。

使用频率:bubuko.com,布布扣

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • AbstractExpression  (Expression)
    • declares an interface for executing an operation
  • TerminalExpression  ( ThousandExpression, HundredExpression, TenExpression, OneExpression )
    • implements an Interpret operation associated with terminal symbols in the grammar.
    • an instance is required for every terminal symbol in the sentence.
  • NonterminalExpression  ( not used )
    • one such class is required for every rule R ::= R1R2...Rn in the grammar
    • maintains instance variables of type AbstractExpression for each of the symbols R1 through Rn.
    • implements an Interpret operation for nonterminal symbols in the grammar. Interpret typically calls itself recursively on the variables representing R1 through Rn.
  • Context  (Context)
    • contains information that is global to the interpreter
  • Client  (InterpreterApp)
    • builds (or is given) an abstract syntax tree representing a particular sentence in the language that the grammar defines. The abstract syntax tree is assembled from instances of the NonterminalExpression and TerminalExpression classes
    • invokes the Interpret operation

C# 中的结构代码:

此结构的代码演示的翻译模式,利用定义的语法,提供口译员那过程解析语句。

 

16  Iterator迭代器

定义:

提供方法顺序访问一个聚合对象元素,而又不暴露其内部表示。

使用频率:bubuko.com,布布扣

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Iterator  (AbstractIterator)
    • defines an interface for accessing and traversing elements.
  • ConcreteIterator  (Iterator)
    • implements the Iterator interface.
    • keeps track of the current position in the traversal of the aggregate.
  • Aggregate  (AbstractCollection)
    • defines an interface for creating an Iterator object
  • ConcreteAggregate  (Collection)
    • implements the Iterator creation interface to return an instance of the proper ConcreteIterator

 

C# 中的结构代码:

此结构的代码演示了所提供的方法来遍历的迭代器模式(迭代) 的项的集合未作详细说明的基础结构的集合。

 

17  Mediator中介者模式

定义:

定义对象来封装一组对象的交互。调解员保持对象被明确地说,谈到彼此促进松散耦合,它让你独立地改变它们之间的交互。

使用频率:bubuko.com,布布扣 中低

UML 类图:

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Mediator  (IChatroom)
    • defines an interface for communicating with Colleague objects
  • ConcreteMediator  (Chatroom)
    • implements cooperative behavior by coordinating Colleague objects
    • knows and maintains its colleagues
  • Colleague classes  (Participant)
    • each Colleague class knows its Mediator object
    • each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague

C# 中的结构代码:

此结构的代码演示了中介者模式促进松散耦合不同的对象和对象类型之间的通信。调解员是通过它的所有交互都必须都发生的中心枢纽。

 

 

18  Memento备忘录模式

定义:

没有违反封装,捕获和外部对象的内部状态,以便对象可以以后恢复到此状态。

使用频率:bubuko.com,布布扣

UML 类图

bubuko.com,布布扣

参与者

    Theclasses and objects participating in this pattern are:

  • Memento  (Memento)
    • stores internal state of the Originator object. The memento may store as much or as little of the originator‘s internal state as necessary at its originator‘s discretion.
    • protect against access by objects of other than the originator. Mementos have effectively two interfaces. Caretaker sees a narrow interface to the Memento -- it can only pass the memento to the other objects. Originator, in contrast, sees a wide interface, one that lets it access all the data necessary to restore itself to its previous state. Ideally, only the originator that produces the memento would be permitted to access the memento‘s internal state.
  • Originator  (SalesProspect)
    • creates a memento containing a snapshot of its current internal state.
    • uses the memento to restore its internal state
  • Caretaker  (Caretaker)
    • is responsible for the memento‘s safekeeping
    • never operates on or examines the contents of a memento.

C# 中的结构代码

此结构的代码演示了纪念品模式的临时保存和还原另一个对象的内部状态。

 

 

 

19  Observer观察者模式

定义

定义对象之间的一个一对多依赖性,以便当一个对象改变状态,其所有依赖项都通知和自动更新。

使用频率:bubuko.com,布布扣

UML 类图

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Subject  (Stock)
    • knows its observers. Any number of Observer objects may observe a subject
    • provides an interface for attaching and detaching Observer objects.
  • ConcreteSubject  (IBM)
    • stores state of interest to ConcreteObserver
    • sends a notification to its observers when its state changes
  • Observer  (IInvestor)
    • defines an updating interface for objects that should be notified of changes in a subject.
  • ConcreteObserver  (Investor)
    • maintains a reference to a ConcreteSubject object
    • stores state that should stay consistent with the subject‘s
    • implements the Observer updating interface to keep its state consistent with the subject‘s

 

C# 中的结构代码:

此结构的代码演示了观察者模式已注册的对象的通知和更新状态更改。

 

 

20  State状态模式

定义:

允许对象在其内部状态改变时改变它的行为。该对象将会出现改变它的类。

使用频率:bubuko.com,布布扣 介质

UML 类图

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Context  (Account)
    • defines the interface of interest to clients
    • maintains an instance of a ConcreteState subclass that defines the current state.
  • State  (State)
    • defines an interface for encapsulating the behavior associated with a particular state of the Context.
  • Concrete State  (RedState, SilverState, GoldState)
    • each subclass implements a behavior associated with a state of Context

C# 中的结构代码:

此结构的代码演示了允许一个对象的行为以不同的方式取决于其内部状态的状态模式。行为上的差异被委派给代表此状态的对象。

 

21  Strategy策略模式

定义:

定义一系列算法,封装每个,让他们可以互换。策略使得算法可独立于使用它的客户而变化。

使用频率:bubuko.com,布布扣 中高

UML 类图

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • Strategy  (SortStrategy)
    • declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy
  • ConcreteStrategy  (QuickSort, ShellSort, MergeSort)
    • implements the algorithm using the Strategy interface
  • Context  (SortedList)
    • is configured with a ConcreteStrategy object
    • maintains a reference to a Strategy object
    • may define an interface that lets Strategy access its data.

 

C# 中的结构代码:

此结构的代码演示了战略模式的封装形式的对象的功能。这允许客户端动态改变算法的策略。

 

22  Template Method模板方法模式

定义:

在操作中,将一些步骤交给子类定义骨架的一种算法。模板方法允许子类而无需更改该算法结构重新定义算法的某些步骤。

使用频率:bubuko.com,布布扣 介质

UML 类图

bubuko.com,布布扣

参与者:

    Theclasses and objects participating in this pattern are:

  • AbstractClass  (DataObject)
    • defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm
    • implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
  • ConcreteClass  (CustomerDataObject)
    • implements the primitive operations ot carry out subclass-specific steps of the algorithm

 

C# 中的结构代码:

此结构的代码演示模板方法提供骨架的调用序列的方法。一个或多个步骤可以推迟到子类,而不改变整体的调用顺序执行这些步骤。

 

 

23  Visitor访问者模式

定义:

代表结构元素的对象执行的操作。访问者允许您定义新的操作,而无需更改它所操作的元素的类。

使用频率:bubuko.com,布布扣

UML 类图

bubuko.com,布布扣

参与者

    Theclasses and objects participating in this pattern are:

  • Visitor  (Visitor)
    • declares a Visit operation for each class of ConcreteElement in the object structure. The operation‘s name and signature identifies the class that sends the Visit request to the visitor. That lets the visitor determine the concrete class of the element being visited. Then the visitor can access the elements directly through its particular interface
  • ConcreteVisitor  (IncomeVisitor, VacationVisitor)
    • implements each operation declared by Visitor. Each operation implements a fragment of the algorithm defined for the corresponding class or object in the structure. ConcreteVisitor provides the context for the algorithm and stores its local state. This state often accumulates results during the traversal of the structure.
  • Element  (Element)
    • defines an Accept operation that takes a visitor as an argument.
  • ConcreteElement  (Employee)
    • implements an Accept operation that takes a visitor as an argument
  • ObjectStructure  (Employees)
    • can enumerate its elements
    • may provide a high-level interface to allow the visitor to visit its elements
    • may either be a Composite (pattern) or a collection such as a list or a set

C# 中的结构代码

此结构的代码演示如何访问者模式,对象遍历对象结构,并在此结构中执行相同的操作,每个节点上。不同的访客对象定义不同的操作。

Unity3D & C# 设计模式--23

标签:ugui   unity3d   脚本   设计模式   design patterns   

原文地址:http://blog.csdn.net/u010019717/article/details/41172783

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!