本篇文章给大家谈谈23种设计模式UML图相似性分类3of5:左右聚合,以及uml中的交互图有两种的知识点,同时本文还将给你拓展(转载)23种设计模式的uml图表示及通俗介绍、23种设计模式、23种设计
本篇文章给大家谈谈23种设计模式UML图相似性分类 3 of 5:左右聚合,以及uml中的交互图有两种的知识点,同时本文还将给你拓展(转载)23种设计模式的uml图表示及通俗介绍、23种设计模式、23种设计模式-依赖、关联、聚合和组合之间区别的理解、23种设计模式C++源码与UML实现--建造者模式等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- 23种设计模式UML图相似性分类 3 of 5:左右聚合(uml中的交互图有两种)
- (转载)23种设计模式的uml图表示及通俗介绍
- 23种设计模式
- 23种设计模式-依赖、关联、聚合和组合之间区别的理解
- 23种设计模式C++源码与UML实现--建造者模式
23种设计模式UML图相似性分类 3 of 5:左右聚合(uml中的交互图有两种)
23种设计模式UML图相似性分类 3 of 5:左右聚合
(转载)23种设计模式的uml图表示及通俗介绍
<div>
<p> 转载自: https://www.cnblogs.com/ningskyer/articles/3615312.html </p>
</br> <h1>0.分类</h1> <p><a><strong>创建型模式</strong> <br><br>1、FACTORY </a><a>2、BUILDER </a>3、FACTORY METHOD <em id="__mceDel">4、PROTOTYPE </em><em id="__mceDel">5、SINGLETON</em></p> <p><em id="__mceDel"><br><strong>结构型模式</strong> <br><br>6、ADAPTER 7、BRIDGE 8、COMPOSITE 9、DECORATOR 10、FACADE 11、FLYWEIGHT 12、PROXY<br><br><strong>行为模式</strong> <br><br>13、CHAIN OF RESPONSIBLEITY 14、COMMAND 15、INTERPRETER 16、ITERATOR 17、MEDIATOR<br><br>18、MEMENTO 19、OBSERVER 20、STATE 21、STRATEGY 22、TEMPLATE METHOD 23、VISITOR</em></p> <h1><strong>1.图示版</strong></h1> <p><em> </em></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/2014040351.jpg" alt="" border="0"></p> <palign="center"><span>责任链模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/2014040352.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Gives more than one object an opportunity to handle a request by linking receiving objects together.</p> <h4>Use When</h4> <ul> <li>Multiple objects may handle a request and the handler doesn''t have to be a specific object.</li> <li>A set of objects should be able to handle a request with the handler determined at runtime.</li> <li>A request not being handled is an acceptable potential outcome.</li> </ul> <h4>Example</h4> <p>Exception handling in some languages implements this pattern. When an exception is thrown in a method the runtime checks to see if the method has a mechanism to handle the exception or if it should be passed up the call stack. When passed up the call stack the process repeats until code to handle the exception is encountered or until there are no more parent objects to hand the request to.</p> <p><span><strong>命令模式</strong></span></p> <p><img src="http://www.uml.org.cn/oobject/images/2014040353.jpg" alt="" border="0"></p> <p><span>解释器模式</span></p> <p><img src="http://www.uml.org.cn/oobject/images/2014040354.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Defines a representation for a grammar as well as a mechanism to understand and act upon the grammar.</p> <h4>Use When</h4> <ul> <li>There is grammar to interpret that can be represented as large syntax trees.</li> <li>The grammar is simple.</li> <li>Efficiency is not important.</li> <li>Decoupling grammar from underlying expressions is desired.</li> </ul> <h4>Example</h4> <p>Text based adventures, wildly popular in the 1980''s, provide a good example of this. Many had simple commands, such as "step down" that allowed traversal of the game. These commands could be nested such that it altered their meaning. For example, "go in" would result in a different outcome than "go up". By creating a hierarchy of commands based upon the command and the qualifier (non-terminal and terminal expressions) the application could easily map many command variations to a relating tree of actions.</p> <palign="center"><span>迭代器模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/2014040355.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Allows for access to the elements of an aggregate object without allowing access to its underlying representation.</p> <h4>Use When</h4> <ul> <li>Access to elements is needed without access to the entire representation.</li> <li>Multiple or concurrent traversals of the elements are needed.</li> <li>A uniform interface for traversal is needed.</li> <li>Subtle differences exist between the implementation details of various iterators.</li> </ul> <h4>Example</h4> <p>The Java implementation of the iterator pattern allows users to traverse various types of data sets without worrying about the underlying implementation of the collection. Since clients simply interact with the iterator interface, collections are left to define the appropriate iterator for themselves. Some will allow full access to the underlying data set while others may restrict certain functionalities, such as removing items.</p> <p><span>中介者模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/2014040356.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Allows loose coupling by encapsulating the way disparate sets of objects interact and communicate with each other. Allows for the actions of each object set to vary independently of one another.</p> <h4>Use When</h4> <ul> <li>Communication between sets of objects is well defined and complex.</li> <li>Too many relationships exist and common point of control or communication is needed.</li> </ul> <h4>Example</h4> <p>Mailing list software keeps track of who is signed up to the mailing list and provides a single point of access through which any one person can communicate with the entire list. Without a mediator implementation a person wanting to send a message to the group would have to constantly keep track of who was signed up and who was not. By implementing the mediator pattern the system is able to receive messages from any point then determine which recipients to forward the message on to, without the sender of the message having to be concerned with the actual recipient list.</p> <p><span>备忘录模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/2014040357.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Allows for capturing and externalizing an object''s internal state so that it can be restored later, all without violating encapsulation.</p> <h4>Use When</h4> <ul> <li>The internal state of an object must be saved and restored at a later time.</li> <li>Internal state cannot be exposed by interfaces without exposing implementation.</li> <li>Encapsulation boundaries must be preserved.</li> </ul> <h4>Example</h4> <p>Undo functionality can nicely be implemented using the memento pattern. By serializing and deserializing the state of an object before the change occurs we can preserve a snapshot of it that can later be restored should the user choose to undo the operation.</p> <palign="center"><span>观察者模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/2014040358.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Lets one or more objects be notified of state changes in other objects within the system.</p> <h4>Use When</h4> <ul> <li>State changes in one or more objects should trigger behavior in other objects</li> <li>Broadcasting capabilities are required.</li> <li>An understanding exists that objects will be blind to the expense of notification.</li> </ul> <h4>Example</h4> <p>This pattern can be found in almost every GUI environment. When buttons, text, and other fields are placed in applications the application typically registers as a listener for those controls. When a user triggers an event, such as clicking a button, the control iterates through its registered observers and sends a notification to each.</p> <palign="center"><span>状态模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/2014040359.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Ties object circumstances to its behavior, allowing the object to behave in different ways based upon its internal state.</p> <h4>Use When</h4> <ul> <li>The behavior of an object should be influenced by its state.</li> <li>Complex conditions tie object behavior to its state.</li> <li>Transitions between states need to be explicit.</li> </ul> <h4>Example</h4> <p>An email object can have various states, all of which will change how the object handles different functions. If the state is "not sent" then the call to send() is going to send the message while a call to recallMessage() will either throw an error or do nothing. However, if the state is "sent" then the call to send() would either throw an error or do nothing while the call to recallMessage() would attempt to send a recall notification to recipients. To avoid conditional statements in most or all methods there would be multiple state objects that handle the implementation with respect to their particular state. The calls within the Email object would then be delegated down to the appropriate state object for handling.</p> <palign="center"><span>策略模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403510.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Defines a set of encapsulated algorithms that can be swapped to carry out a specific behavior.</p> <h4>Use When</h4> <ul> <li>The only difference between many related classes is their behavior.</li> <li>Multiple versions or variations of an algorithm are required.</li> <li>Algorithms access or utilize data that calling code shouldn''t be exposed to.</li> <li>The behavior of a class should be defined at runtime.</li> <li>Conditional statements are complex and hard to maintain.</li> </ul> <h4>Example</h4> <p>When importing data into a new system different validation algorithms may be run based on the data set. By configuring the import to utilize strategies the conditional logic to determine what validation set to run can be removed and the import can be decoupled from the actual validation code. This will allow us to dynamically call one or more strategies during the import.</p> <palign="center">模板方法模式</p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403511.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Identifies the framework of an algorithm, allowing implementing classes to define the actual behavior.</p> <h4>Use When</h4> <ul> <li>A single abstract implementation of an algorithm is needed.</li> <li>Common behavior among subclasses should be localized to a common class.</li> <li>Parent classes should be able to uniformly invoke behavior in their subclasses.</li> <li>Most or all subclasses need to implement the behavior.</li> </ul> <h4>Example</h4> <p>A parent class, InstantMessage, will likely have all the methods required to handle sending a message. However, the actual serialization of the data to send may vary depending on the implementation. A video message and a plain text message will require different algorithms in order to serialize the data correctly. Subclasses of InstantMessage can provide their own implementation of the serialization method, allowing the parent class to work with them without understanding their implementation details.</p> <palign="center"><span>访问者模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403512.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Allows for one or more operations to be applied to a set of objects at runtime, decoupling the operations from the object structure.</p> <h4>Use When</h4> <ul> <li>An object structure must have many unrelated operations performed upon it.</li> <li>The object structure can''t change but operations performed on it can.</li> <li>Operations must be performed on the concrete classes of an object structure.</li> <li>Exposing internal state or operations of the object structure is acceptable.</li> <li>Operations should be able to operate on multiple object structures that implement the same interface sets.</li> </ul> <h4>Example</h4> <p>Calculating taxes in different regions on sets of invoices would require many different variations of calculation logic. Implementing a visitor allows the logic to be decoupled from the invoices and line items. This allows the hierarchy of items to be visited by calculation code that can then apply the proper rates for the region. Changing regions is as simple as substituting a different visitor.</p> <p><span>适配器模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403513.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Permits classes with disparate interfaces to work together by creating a common object by which they may communicate and interact.</p> <h4>Use When</h4> <ul> <li>A class to be used doesn''t meet interface requirements.</li> <li>Complex conditions tie object behavior to its state.</li> <li>Transitions between states need to be explicit.</li> </ul> <h4>Example</h4> <p>A billing application needs to interface with an HR application in order to exchange employee data, however each has its own interface and implementation for the Employee object. In addition, the SSN is stored in different formats by each system. By creating an adapter we can create a common interface between the two applications that allows them to communicate using their native objects and is able to transform the SSN format in the process.</p> <palign="center"><span>桥接模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403514.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Defines an abstract object structure independently of the implementation object structure in order to limit coupling.</p> <h4>Use When</h4> <ul> <li>Abstractions and implementations should not be bound at compile time.</li> <li>Abstractions and implementations should be independently extensible.</li> <li>Changes in the implementation of an abstraction should have no impact on clients.</li> <li>Implementation details should be hidden from the client.</li> </ul> <h4>Example</h4> <p>The Java Virtual Machine (JVM) has its own native set of functions that abstract the use of windowing, system logging, and byte code execution but the actual implementation of these functions is delegated to the operating system the JVM is running on. When an application instructs the JVM to render a window it delegates the rendering call to the concrete implementation of the JVM that knows how to communicate with the operating system in order to render the window.</p> <p><span>组合模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403515.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Facilitates the creation of object hierarchies where each object can be treated independently or as a set of nested objects through the same interface.</p> <h4>Use When</h4> <ul> <li>Hierarchical representations of objects are needed.</li> <li>Objects and compositions of objects should be treated uniformly.</li> </ul> <h4>Example</h4> <p>Sometimes the information displayed in a shopping cart is the product of a single item while other times it is an aggregation of multiple items. By implementing items as composites we can treat the aggregates and the items in the same way, allowing us to simply iterate over the tree and invoke functionality on each item. By calling the getCost() method on any given node we would get the cost of that item plus the cost of all child items, allowing items to be uniformly treated whether they were single items or groups of items.</p> <p><span>装饰者模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403516.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Allows for the dynamic wrapping of objects in order to modify their existing responsibilities and behaviors.</p> <h4>Use When</h4> <ul> <li>Object responsibilities and behaviors should be dynamically modifiable.</li> <li>Concrete implementations should be decoupled from responsibilities and behaviors.</li> <li>Subclassing to achieve modification is impractical or impossible.</li> <li>Specific functionality should not reside high in the object hierarchy.</li> <li>A lot of little objects surrounding a concrete implementation is acceptable.</li> </ul> <h4>Example</h4> <p>Many businesses set up their mail systems to take advantage of decorators. When messages are sent from someone in the company to an external address the mail server decorates the original message with copyright and confidentiality information. As long as the message remains internal the information is not attached. This decoration allows the message itself to remain unchanged until a runtime decision is made to wrap the message with additional information.</p> <p><span> 门面模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403517.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Supplies a single interface to a set of interfaces within a system.</p> <h4>Use When</h4> <ul> <li>A simple interface is needed to provide access to a complex system.</li> <li>There are many dependencies between system implementations and clients.</li> <li>Systems and subsystems should be layered.</li> </ul> <h4>Example</h4> <p>By exposing a set of functionalities through a web service the client code needs to only worry about the simple interface being exposed to them and not the complex relationships that may or may not exist behind the web service layer. A single web service call to update a system with new data may actually involve communication with a number of databases and systems, however this detail is hidden due to the implementation of the façade pattern.</p> <p><span>享元模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403518.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Facilitates the reuse of many fine grained objects, making the utilization of large numbers of objects more efficient.</p> <h4>Use When</h4> <ul> <li>Many like objects are used and storage cost is high.</li> <li>The majority of each object''s state can be made extrinsic.</li> <li>A few shared objects can replace many unshared ones.</li> <li>The identity of each object does not matter.</li> </ul> <h4>Example</h4> <p>Systems that allow users to define their own application flows and layouts often have a need to keep track of large numbers of fields, pages, and other items that are almost identical to each other. By making these items into flyweights all instances of each object can share the intrinsic state while keeping the extrinsic state separate. The intrinsic state would store the shared properties, such as how a textbox looks, how much data it can hold, and what events it exposes. The extrinsic state would store the unshared properties, such as where the item belongs, how to react to a user click, and how to handle events.</p> <palign="center"><span>代理模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403519.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Allows for object level access control by acting as a pass through entity or a placeholder object.</p> <h4>Use When</h4> <ul> <li>The object being represented is external to the system.</li> <li>Objects need to be created on demand.</li> <li>Access control for the original object is required.</li> <li>Added functionality is required when an object is accessed.</li> </ul> <h4>Example</h4> <p>Ledger applications often provide a way for users to reconcile their bank statements with their ledger data on demand, automating much of the process. The actual operation of communicating with a third party is a relatively expensive operation that should be limited. By using a proxy to represent the communications object we can limit the number of times or the intervals the communication is invoked. In addition, we can wrap the complex instantiation of the communication object inside the proxy class, decoupling calling code from the implementation details.</p> <p><span>抽象工厂模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403520.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Provide an interface that delegates creation calls to one or more concrete classes in order to deliver specific objects.</p> <h4>Use When</h4> <ul> <li>The creation of objects should be independent of the system utilizing them.</li> <li>Systems should be capable of using multiple families of objects.</li> <li>Families of objects must be used together.</li> <li>Libraries must be published without exposing implementation details.</li> <li>Concrete classes should be decoupled from clients.</li> </ul> <h4>Example</h4> <p>Email editors will allow for editing in multiple formats including plain text, rich text, and<abbr title="Hypertext Markup Language">HTML</abbr> Depending on the format being used, different objects will need to be created. If the message is plain text then there could be a body object that represented just plain text and an attachment object that simply encrypted the attachment into Base64. If the message is HTML then the body object would represent HTML encoded text and the attachment object would allow for inline representation and a standard attachment. By utilizing an abstract factory for creation we can then ensure that the appropriate object sets are created based upon the style of email that is being sent.</p> <palign="center"><span>建造模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403521.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Allows for the dynamic creation of objects based upon easily interchangeable algorithms.</p> <h4>Use When</h4> <ul> <li>Object creation algorithms should be decoupled from the system.</li> <li>Multiple representations of creation algorithms are required.</li> <li>The addition of new creation functionality without changing the core code is necessary.</li> <li>Runtime control over the creation process is required.</li> </ul> <h4>Example</h4> <p>A file transfer application could possibly use many different protocols to send files and the actual transfer object that will be created will be directly dependent on the chosen protocol. Using a builder we can determine the right builder to use to instantiate the right object. If the setting is <abbr title="File Transfer Protocol">FTP</abbr> then the FTP builder would be used when creating the object.</p> <palign="center"><span>工厂方法模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403522.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Exposes a method for creating objects, allowing subclasses to control the actual creation process.</p> <h4>Use When</h4> <ul> <li>A class will not know what classes it will be required to create.</li> <li>Subclasses may specify what objects should be created.</li> <li>Parent classes wish to defer creation to their subclasses.</li> </ul> <h4>Example</h4> <p>Many applications have some form of user and group structure for security. When the application needs to create a user it will typically delegate the creation of the user to multiple user implementations. The parent user object will handle most operations for each user but the subclasses will define the factory method that handles the distinctions in the creation of each type of user. A system may have AdminUser and StandardUser objects each of which extend the User object. The AdminUser object may perform some extra tasks to ensure access while the StandardUser may do the same to limit access.</p> <palign="center"><span>原型模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403523.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Create objects based upon a template of an existing objects through cloning.</p> <h4>Use When</h4> <ul> <li>Composition, creation, and representation of objects should be decoupled from a system.</li> <li>Classes to be created are specified at runtime.</li> <li>A limited number of state combinations exist in an object.</li> <li>Objects or object structures are required that are identical or closely resemble other existing objects or object structures.</li> <li>The initial creation of each object is an expensive operation.</li> </ul> <h4>Example</h4> <p>Rates processing engines often require the lookup of many different configuration values, making the initialization of the engine a relatively expensive process. When multiple instances of the engine is needed, say for importing data in a multi-threaded manner, the expense of initializing many engines is high. By utilizing the prototype pattern we can ensure that only a single copy of the engine has to be initialized then simply clone the engine to create a duplicate of the already initialized object. The added benefit of this is that the clones can be streamlined to only include relevant data for their situation.</p> <p><span>单例模式</span></p> <palign="center"><img src="http://www.uml.org.cn/oobject/images/20140403524.jpg" alt="" border="0"></p> <h3>Purpose</h3> <p>Ensures that only one instance of a class is allowed within a system.</p> <h4>Use When</h4> <ul> <li>Exactly one instance of a class is required.</li> <li>Controlled access to a single object is necessary.</li> </ul> <h4>Example</h4> <p>Most languages provide some sort of system or environment object that allows the language to interact with the native operating system. Since the application is physically running on only one operating system there is only ever a need for a single instance of this system object. The singleton pattern would be implemented by the language runtime to ensure that only a single copy of the system object is created and to ensure only appropriate processes are allowed access to it.</p> <h1>2.大话版</h1> <h2>创建型模式</h2> <p>1、FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说“来四个鸡翅”就行了。麦当劳和肯德基就是生产鸡翅的Factory</p> <p><span>工厂模式</span>:客户类和工厂类分开。消费者任何时候需要某种产品,只需向工厂请求即可。消费者无须修改就可以接纳新产品。缺点是当产品修改时,工厂类也要做相应的修改。如:如何创建及如何向客户端提供。</p> <p>2、BUILDER—MM最爱听的就是“我爱你”这句话了,见到不同地方的MM,要能够用她们的方言跟她说这句话哦,我有一个多种语言翻译机,上面每种语言都有一个按键,见到MM我只要按对应的键,它就能够用相应的语言说出“我爱你”这句话了,国外的MM也可以轻松搞掂,这就是我的“我爱你”builder。(这一定比美军在伊拉克用的翻译机好卖)</p> <p><span>建造模式</span>:将产品的内部表象和产品的生成过程分割开来,从而使一个建造过程生成具有不同的内部表象的产品对象。建造模式使得产品内部表象可以独立的变化,客户不必知道产品内部组成的细节。建造模式可以强制实行一种分步骤进行的建造过程。</p> <p>3、FACTORY METHOD—请MM去麦当劳吃汉堡,不同的MM有不同的口味,要每个都记住是一件烦人的事情,我一般采用Factory Method模式,带着MM到服务员那儿,说“要一个汉堡”,具体要什么样的汉堡呢,让MM直接跟服务员说就行了。</p> <p><span>工厂方法模式</span>:核心工厂类不再负责所有产品的创建,而是将具体创建的工作交给子类去做,成为一个抽象工厂角色,仅负责给出具体工厂类必须实现的接口,而不接触哪一个产品类应当被实例化这种细节。</p> <p>4、PROTOTYPE—跟MM用QQ聊天,一定要说些深情的话语了,我搜集了好多肉麻的情话,需要时只要copy出来放到QQ里面就行了,这就是我的情话prototype了。(100块钱一份,你要不要)</p> <p><span>原型模式</span>:通过给出一个原型对象来指明所要创建的对象的类型,然后用复制这个原型对象的方法创建出更多同类型的对象。原始模型模式允许动态的增加或减少产品类,产品类不需要非得有任何事先确定的等级结构,原始模型模式适用于任何的等级结构。缺点是每一个类都必须配备一个克隆方法。</p> <p>5、SINGLETON—俺有6个漂亮的老婆,她们的老公都是我,我就是我们家里的老公Sigleton,她们只要说道“老公”,都是指的同一个人,那就是我(刚才做了个梦啦,哪有这么好的事)</p> <p><span>单例模式</span>:单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例单例模式。单例模式只应在有真正的“单一实例”的需求时才可使用。</p> <h2>结构型模式</h2> <p>6、ADAPTER—在朋友聚会上碰到了一个美女Sarah,从香港来的,可我不会说粤语,她不会说普通话,只好求助于我的朋友kent了,他作为我和Sarah之间的Adapter,让我和Sarah可以相互交谈了(也不知道他会不会耍我)</p> <p><span>适配器模式</span>:把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口原因不匹配而无法一起工作的两个类能够一起工作。适配类可以根据参数返还一个合适的实例给客户端。</p> <p>7、BRIDGE—早上碰到MM,要说早上好,晚上碰到MM,要说晚上好;碰到MM穿了件新衣服,要说你的衣服好漂亮哦,碰到MM新做的发型,要说你的头发好漂亮哦。不要问我“早上碰到MM新做了个发型怎么说”这种问题,自己用BRIDGE组合一下不就行了</p> <p><span>桥梁模式:</span>将抽象化与实现化脱耦,使得二者可以独立的变化,也就是说将他们之间的强关联变成弱关联,也就是指在一个软件系统的抽象化和实现化之间使用组合/聚合关系而不是继承关系,从而使两者可以独立的变化。</p> <p>8、COMPOSITE—Mary今天过生日。“我过生日,你要送我一件礼物。”“嗯,好吧,去商店,你自己挑。”“这件T恤挺漂亮,买,这条裙子好看,买,这个包也不错,买。”“喂,买了三件了呀,我只答应送一件礼物的哦。”“什么呀,T恤加裙子加包包,正好配成一套呀,小姐,麻烦你包起来。”“……”,MM都会用Composite模式了,你会了没有?</p> <p><span>合成模式</span>:合成模式将对象组织到树结构中,可以用来描述整体与部分的关系。合成模式就是一个处理对象的树结构的模式。合成模式把部分与整体的关系用树结构表示出来。合成模式使得客户端把一个个单独的成分对象和由他们复合而成的合成对象同等看待。</p> <p>9、DECORATOR—Mary过完轮到Sarly过生日,还是不要叫她自己挑了,不然这个月伙食费肯定玩完,拿出我去年在华山顶上照的照片,在背面写上“最好的的礼物,就是爱你的Fita”,再到街上礼品店买了个像框(卖礼品的MM也很漂亮哦),再找隔壁搞美术设计的Mike设计了一个漂亮的盒子装起来……,我们都是Decorator,最终都在修饰我这个人呀,怎么样,看懂了吗?</p> <p><span>装饰模式</span>:装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案,提供比继承更多的灵活性。动态给一个对象增加功能,这些功能可以再动态的撤消。增加由一些基本功能的排列组合而产生的非常大量的功能。</p> <p>10、FACADE—我有一个专业的Nikon相机,我就喜欢自己手动调光圈、快门,这样照出来的照片才专业,但MM可不懂这些,教了半天也不会。幸好相机有Facade设计模式,把相机调整到自动档,只要对准目标按快门就行了,一切由相机自动调整,这样MM也可以用这个相机给我拍张照片了。</p> <p><span>门面模式</span>:外部与一个子系统的通信必须通过一个统一的门面对象进行。门面模式提供一个高层次的接口,使得子系统更易于使用。每一个子系统只有一个门面类,而且此门面类只有一个实例,也就是说它是一个单例模式。但整个系统可以有多个门面类。</p> <p>11、FLYWEIGHT—每天跟MM发短信,手指都累死了,最近买了个新手机,可以把一些常用的句子存在手机里,要用的时候,直接拿出来,在前面加上MM的名字就可以发送了,再不用一个字一个字敲了。共享的句子就是Flyweight,MM的名字就是提取出来的外部特征,根据上下文情况使用。</p> <p><span>享元模式</span>:FLYWEIGHT在拳击比赛中指最轻量级。享元模式以共享的方式高效的支持大量的细粒度对象。享元模式能做到共享的关键是区分内蕴状态和外蕴状态。内蕴状态存储在享元内部,不会随环境的改变而有所不同。外蕴状态是随环境的改变而改变的。外蕴状态不能影响内蕴状态,它们是相互独立的。将可以共享的状态和不可以共享的状态从常规类中区分开来,将不可以共享的状态从类里剔除出去。客户端不可以直接创建被共享的对象,而应当使用一个工厂对象负责创建被共享的对象。享元模式大幅度的降低内存中对象的数量。</p> <p>12、PROXY—跟MM在网上聊天,一开头总是“hi,你好”,“你从哪儿来呀?”“你多大了?”“身高多少呀?”这些话,真烦人,写个程序做为我的Proxy吧,凡是接收到这些话都设置好了自动的回答,接收到其他的话时再通知我回答,怎么样,酷吧。</p> <p><span>代理模式</span>:代理模式给某一个对象提供一个代理对象,并由代理对象控制对源对象的引用。代理就是一个人或一个机构代表另一个人或者一个机构采取行动。某些情况下,客户不想或者不能够直接引用一个对象,代理对象可以在客户和目标对象直接起到中介的作用。客户端分辨不出代理主题对象与真实主题对象。代理模式可以并不知道真正的被代理对象,而仅仅持有一个被代理对象的接口,这时候代理对象不能够创建被代理对象,被代理对象必须有系统的其他角色代为创建并传入。</p> <h2>行为模式</h2> <p>13、CHAIN OF RESPONSIBLEITY—晚上去上英语课,为了好开溜坐到了最后一排,哇,前面坐了好几个漂亮的MM哎,找张纸条,写上“Hi,可以做我的女朋友吗?如果不愿意请向前传”,纸条就一个接一个的传上去了,糟糕,传到第一排的MM把纸条传给老师了,听说是个老处女呀,快跑!</p> <p><span>责任链模式</span>:在责任链模式中,很多对象由每一个对象对其下家的引用而接<em id="__mceDel">起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。客户并不知道链上的哪一个对象最终处理这个请求,系统可以在不影响客户端的情况下动态的重新组织链和分配责任。处理者有两个选择:承担责任或者把责任推给下家。一个请求可以最终不被任何接收端对象所接受。</em></p> <p>14、COMMAND—俺有一个MM家里管得特别严,没法见面,只好借助于她弟弟在我们俩之间传送信息,她对我有什么指示,就写一张纸条让她弟弟带给我。这不,她弟弟又传送过来一个COMMAND,为了感谢他,我请他吃了碗杂酱面,哪知道他说:“我同时给我姐姐三个男朋友送COMMAND,就数你最小气,才请我吃面。”,:-(</p> <p><span>命令模式</span>:命令模式把一个请求或者操作封装到一个对象中。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。命令模式允许请求的一方和发送的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否执行,何时被执行以及是怎么被执行的。系统支持命令的撤消。</p> <p>15、INTERPRETER—俺有一个《泡MM真经》,上面有各种泡MM的攻略,比如说去吃西餐的步骤、去看电影的方法等等,跟MM约会时,只要做一个Interpreter,照着上面的脚本执行就可以了。</p> <p><span>解释器模式</span>:给定一个语言后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器。客户端可以使用这个解释器来解释这个语言中的句子。解释器模式将描述怎样在有了一个简单的文法后,使用模式设计解释这些语句。在解释器模式里面提到的语言是指任何解释器对象能够解释的任何组合。在解释器模式中需要定义一个代表文法的命令类的等级结构,也就是一系列的组合规则。每一个命令对象都有一个解释方法,代表对命令对象的解释。命令对象的等级结构中的对象的任何排列组合都是一个语言。</p> <p>16、ITERATOR—我爱上了Mary,不顾一切的向她求婚。</p> <p>Mary:“想要我跟你结婚,得答应我的条件”</p> <p>我:“什么条件我都答应,你说吧”</p> <p>Mary:“我看上了那个一克拉的钻石”</p> <p>我:“我买,我买,还有吗?”</p> <p>Mary:“我看上了湖边的那栋别墅”</p> <p>我:“我买,我买,还有吗?”</p> <p>Mary:“你的小弟弟必须要有50cm长”</p> <p>我脑袋嗡的一声,坐在椅子上,一咬牙:“我剪,我剪,还有吗?”</p> <p>……</p> <p><span>迭代子模式</span>:迭代子模式可以顺序访问一个聚集中的元素而不必暴露聚集的内部表象。多个对象聚在一起形成的总体称之为聚集,聚集对象是能够包容一组对象的容器对象。迭代子模式将迭代逻辑封装到一个独立的子对象中,从而与聚集本身隔开。迭代子模式简化了聚集的界面。每一个聚集对象都可以有一个或一个以上的迭代子对象,每一个迭代子的迭代状态可以是彼此独立的。迭代算法可以独立于聚集角色变化。</p> <p>17、MEDIATOR—四个MM打麻将,相互之间谁应该给谁多少钱算不清楚了,幸亏当时我在旁边,按照各自的筹码数算钱,赚了钱的从我这里拿,赔了钱的也付给我,一切就OK啦,俺得到了四个MM的电话。</p> <p><span>调停者模式</span>:调停者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显作用。从而使他们可以松散偶合。当某些对象之间的作用发生改变时,不会立即影响其他的一些对象之间的作用。保证这些作用可以彼此独立的变化。调停者模式将多对多的相互作用转化为一对多的相互作用。调停者模式将对象的行为和协作抽象化,把对象在小尺度的行为上与其他对象的相互作用分开处理。</p> <p>18、MEMENTO—同时跟几个MM聊天时,一定要记清楚刚才跟MM说了些什么话,不然MM发现了会不高兴的哦,幸亏我有个备忘录,刚才与哪个MM说了什么话我都拷贝一份放到备忘录里面保存,这样可以随时察看以前的记录啦。</p> <p><span>备忘录模式</span>:备忘录对象是一个用来存储另外一个对象内部状态的快照的对象。备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捉住,并外部化,存储起来,从而可以在将来合适的时候把这个对象还原到存储起来的状态。</p> <p>19、OBSERVER—想知道咱们公司最新MM情报吗?加入公司的MM情报邮件组就行了,tom负责搜集情报,他发现的新情报不用一个一个通知我们,直接发布给邮件组,我们作为订阅者(观察者)就可以及时收到情报啦</p> <p><span>观察者模式</span>:观察者模式定义了一种一队多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使他们能够自动更新自己。</p> <p>20、STATE—跟MM交往时,一定要注意她的状态哦,在不同的状态时她的行为会有不同,比如你约她今天晚上去看电影,对你没兴趣的MM就会说“有事情啦”,对你不讨厌但还没喜欢上的MM就会说“好啊,不过可以带上我同事么?”,已经喜欢上你的MM就会说“几点钟?看完电影再去泡吧怎么样?”,当然你看电影过程中表现良好的话,也可以把MM的状态从不讨厌不喜欢变成喜欢哦。</p> <p><span>状态模式</span>:状态模式允许一个对象在其内部状态改变的时候改变行为。这个对象看上去象是改变了它的类一样。状态模式把所研究的对象的行为包装在不同的状态对象里,每一个状态对象都属于一个抽象状态类的一个子类。状态模式的意图是让一个对象在其内部状态改变的时候,其行为也随之改变。状态模式需要对每一个系统可能取得的状态创立一个状态类的子类。当系统的状态变化时,系统便改变所选的子类。</p> <p>21、STRATEGY—跟不同类型的MM约会,要用不同的策略,有的请电影比较好,有的则去吃小吃效果不错,有的去海边浪漫最合适,单目的都是为了得到MM的芳心,我的追MM锦囊中有好多Strategy哦。</p> <p><span>策略模式</span>:策略模式针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。策略模式把行为和环境分开。环境类负责维持和查询行为类,各种算法在具体的策略类中提供。由于算法和环境独立开来,算法的增减,修改都不会影响到环境和客户端。</p> <p>22、TEMPLATE METHOD——看过《如何说服女生上床》这部经典文章吗?女生从认识到上床的不变的步骤分为巧遇、打破僵局、展开追求、接吻、前戏、动手、爱抚、进去八大步骤(Template method),但每个步骤针对不同的情况,都有不一样的做法,这就要看你随机应变啦(具体实现);</p> <p><span>模板方法模式</span>:模板方法模式准备一个抽象类,将部分逻辑以具体方法以及具体构造子的形式实现,然后声明一些抽象方法来迫使子类实现剩余的逻辑。不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的逻辑有不同的实现。先制定一个顶级逻辑框架,而将逻辑的细节留给具体的子类去实现。</p> <p>23、<strong>VISITOR</strong>—情人节到了,要给每个MM送一束鲜花和一张卡片,可是每个MM送的花都要针对她个人的特点,每张卡片也要根据个人的特点来挑,我一个人哪搞得清楚,还是找花店老板和礼品店老板做一下Visitor,让花店老板根据MM的特点选一束花,让礼品店老板也根据每个人特点选一张卡,这样就轻松多了;</p> <p><span><strong>访问者模式</strong></span>:访问者模式的目的是封装一些施加于某种数据结构元素之上的操作。一旦这些操作需要修改的话,接受这个操作的数据结构可以保持不变。访问者模式适用于数据结构相对未定的系统,它把数据结构和作用于结构上的操作之间的耦合解脱开,使得操作集合可以相对自由的演化。访问者模式使得增加新的操作变的很容易,就是增加一个新的访问者类。访问者模式将有关的行为集中到一个访问者对象中,而不是分散到一个个的节点类中。当使用访问者模式时,要将尽可能多的对象浏览逻辑放在访问者类中,而不是放到它的子类中。访问者模式可以跨过几个类的等级结构访问属于不同的等级结构的成员类。</p> <p> </p></div><div id="MySignature"><p>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</p>
23种设计模式
策略模式属于对象的行为模式。其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。
策略模式的结构
策略模式是对算法的包装,是把使用算法的责任和算法本身分割开来,委派给不同的对象管理。策略模式通常把一个系列的算法包装到一系列的策略类里面,作为一个抽象策略类的子类。用一句话来说,就是:“准备一组算法,并将每一个算法封装起来,使得它们可以互换”。下面就以一个示意性的实现讲解策略模式实例的结构。
这个模式涉及到三个角色:
● 环境(Context)角色:持有一个Strategy的引用。
● 抽象策略(Strategy)角色:这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口。
● 具体策略(ConcreteStrategy)角色:包装了相关的算法或行为。
源代码
1. 抽象策略角色
2. 具体策略角色
3. 环境角色
4. 测试类
认识策略模式
策略模式的重心
策略模式的重心不是如何实现算法,而是如何组织、调用这些算法,从而让程序结构更灵活,具有更好的维护性和扩展性。
算法的平等性
策略模式一个很大的特点就是各个策略算法的平等性。对于一系列具体的策略算法,大家的地位是完全一样的,正因为这个平等性,才能实现算法之间可以相互替换。所有的策略算法在实现上也是相互独立的,相互之间是没有依赖的。
所以可以这样描述这一系列策略算法:策略算法是相同行为的不同实现。
运行时策略的唯一性
运行期间,策略模式在每一个时刻只能使用一个具体的策略实现对象,虽然可以动态地在不同的策略实现中切换,但是同时只能使用一个。
公有的行为
经常见到的是,所有的具体策略类都有一些公有的行为。这时候,就应当把这些公有的行为放到共同的抽象策略角色Strategy类里面。当然这时候抽象策略角色必须要用Java抽象类实现,而不能使用接口。
这其实也是典型的将代码向继承等级结构的上方集中的标准做法。
策略模式的优点
(1)策略模式提供了管理相关的算法族的办法。策略类的等级结构定义了一个算法或行为族。恰当使用继承可以把公共的代码移到父类里面,从而避免代码重复。
(2)使用策略模式可以避免使用多重条件(if-else)语句。多重条件语句不易维护,它把采取哪一种算法或采取哪一种行为的逻辑与算法或行为的逻辑混合在一起,统统列在一个多重条件语句里面,比使用继承的办法还要原始和落后。
策略模式的缺点
(1)客户端必须知道所有的策略类,并自行决定使用哪一个策略类。这就意味着客户端必须理解这些算法的区别,以便适时选择恰当的算法类。换言之,策略模式只适用于客户端知道算法或行为的情况。
(2)由于策略模式把每个具体的策略实现都单独封装成为类,如果备选的策略很多的话,那么对象的数目就会很可观。
23种设计模式-依赖、关联、聚合和组合之间区别的理解
在学习面向对象设计对象关系时,依赖、关联、聚合和组合这四种关系之间区别比较容易混淆。特别是后三种,仅仅是在语义上有所区别,所谓语义就是指上下文环境、特定情景等。他们在编程语言中的体现却是基本相同的,但是基本相同并不等于完全相同,这一点在我的前一篇博文《设计模式中类的关系》中已经有所提及,下面就来详细的论述一下在java中如何准确的体现依赖、关联、聚合和组合。
首先看一看书上对这四种关系的定义:
- 依赖(Dependency)关系是类与类之间的联接。依赖关系表示一个类依赖于另一个类的定义。例如,一个人(Person)可以买车(car)和房子(House),Person类依赖于Car类和House类的定义,因为Person类引用了Car和House。与关联不同的是,Person类里并没有Car和House类型的属性,Car和House的实例是以参量的方式传入到buy()方法中去的。一般而言,依赖关系在Java语言中体现为局域变量、方法的形参,或者对静态方法的调用。
- 关联(Association)关系是类与类之间的联接,它使一个类知道另一个类的属性和方法。关联可以是双向的,也可以是单向的。在Java语言中,关联关系一般使用成员变量来实现。
- 聚合(Aggregation) 关系是关联关系的一种,是强的关联关系。聚合是整体和个体之间的关系。例如,汽车类与引擎类、轮胎类,以及其它的零件类之间的关系便整体和个体的关系。与关联关系一样,聚合关系也是通过实例变量实现的。但是关联关系所涉及的两个类是处在同一层次上的,而在聚合关系中,两个类是处在不平等层次上的,一个代表整体,另一个代表部分。
- 组合(Composition) 关系是关联关系的一种,是比聚合关系强的关系。它要求普通的聚合关系中代表整体的对象负责代表部分对象的生命周期,组合关系是不能共享的。代表整体的对象需要负责保持部分对象和存活,在一些情况下将负责代表部分的对象湮灭掉。代表整体的对象可以将代表部分的对象传递给另一个对象,由后者负责此对象的生命周期。换言之,代表部分的对象在每一个时刻只能与一个对象发生组合关系,由后者排他地负责生命周期。部分和整体的生命周期一样。
——摘自《Java面向对象编程》,作者:孙卫琴
以上关系的耦合度依次增强(关于耦合度的概念将在以后具体讨论,这里可以暂时理解为当一个类发生变更时,对其他类造成的影响程度,影响越小则耦合度越弱,影响越大耦合度越强)。由定义我们已经知道,依赖关系实际上是一种比较弱的关联,聚合是一种比较强的关联,而组合则是一种更强的关联,所以笼统的来区分的话,实际上这四种关系、都是关联关系。
依赖关系比较好区分,它是耦合度最弱的一种,在java中表现为局域变量、方法的形参,或者对静态方法的调用,如下面的例子:Driver类依赖于Car类,Driver的三个方法分别演示了依赖关系的三种不同形式。
- classCar{
- publicstaticvoidrun(){
- System.out.println("汽车在奔跑");
- }
- }
- @H_301_84@classDriver{
- //使用形参方式发生依赖关系 @H_301_84@voiddrive1(Carcar){
- car.run();
- //使用局部变量发生依赖关系 @H_301_84@voiddrive2(){
- Carcar=newCar();
- car.run();
- //使用静态变量发生依赖关系
- voiddrive3(){
- Car.run();
- }
关联关系在java中一般使用成员变量来实现,有时也用方法形参的形式实现。依然使用Driver和Car的例子,使用方法参数形式可以表示依赖关系,也可以表示关联关系,毕竟我们无法在程序中太准确的表达语义。在本例中,使用成员变量表达这个意思:车是我自己的车,我“拥有”这个车。使用方法参数表达:车不是我的,我只是个司机,别人给我什么车我就开什么车,我使用这个车。
23种设计模式C++源码与UML实现--建造者模式
建造者模式
代码仓库地址:建造者模式
Builder
模式也叫建造者模式或者生成器模式,是由GoF
提出的23种设计模式中的一种。Builder
模式是一种对象创建模式之一,用来隐藏复合对象的创建过程。他把复合对象的创建过程加以抽象,通过子类继承和重载的方式,动态地创建具有复合属性的对象。
对象的创建:Builder
模式是为了对象的创建而设计的模式-创建的是一个复合对象,被创建的对象为一个具有复合属性的复合对象,关注对象创建的各个部分的创建过程,不同工厂(Builder
)对产品属性有不同的创建方法。
Builder
:为创建产品各个部分,统一抽象接口ConcreteBuilder
: 具体的创建产品的各个部分,部分A,部分B,部分CDirector
: 构造一个使用Builder
接口的对象Product
:表示被构造的复杂对象
ConcreteBuilder
创建该产品的内部表示并定义它的装配过程,包含定义组成部分的类,包含将这些部件装配成最终产品的接口。
适用情况:
一个对象的构建比较复杂,将一个对象的构建和对象的表示进行分离。
说白了:建造者模式,其实就是相当于一个设计师,指挥建造师造房子,建造师可能是不同的,因为每个建造师建造水平和会造的房子都是不一样的。
创建者模式和工厂模式的对比
Factory
模式中:
- 有一个抽象的工厂
- 实现一个具体的工厂 - 汽车工厂
- 工厂生产的汽车A,得到汽车产品A
- 工厂生产汽车B,得到汽车产品B
实现了购买者和生产线的隔离,强调的是结果
Builder
模式
- 引擎工厂生产引擎产品,得到汽车的部件A
- 轮胎工厂生产轮子产品,得到汽车部件B
- 底盘工厂生产车身产品,得到汽车部件C
- 将这些部件放到一起,形成刚好能够组装成一辆汽车的整体
这样做,目的是为了实现复杂对象生产线和其部件的解耦。强调的是过程
两者的区别在于以下几种情况:
工厂模式不考虑对象的组装过程,而直接生成一个我想要的对象。
Builder
模式先一个个的创建对象的每一个部件,再统一组装成一个对象
工厂模式所解决的问题是,工厂生产产品
而Builder
模式解决的是工厂控制产品 生成器组装各个部件的过程,然后从产品生成器中得到产品。
前期问题的抛出-需要建造者模式的原因
#include <iostream>
#include <string>
using namespace std;
class House
{
public:
void setDoor(string door)
{
this->m_door = door;
}
void setWall(string wall)
{
this->m_wall = wall;
}
void setWindow(string window)
{
this->m_window = window;
}
string getDoor(void)
{
cout << m_door << endl;
return m_door;
}
string getWall(void)
{
cout << m_wall << endl;
return m_wall;
}
string getWindow(void)
{
cout << m_window << endl;;
return m_window;
}
private:
string m_door;
string m_wall;
string m_window;
};
class Builder
{
public:
Builder(void)
{
m_house = new House;
}
~Builder(void)
{
delete m_house;
}
void MakeBuilder(void)
{
BuildDoor(m_house);
BuildWall(m_house);
BuildWindow(m_house);
}
void BuildDoor(House *h)
{
h->setDoor("door");
}
void BuildWall(House *h)
{
h->setWall("wall");
}
void BuildWindow(House *h)
{
h->setWindow("window");
}
House *GetHouse(void)
{
return m_house;
}
private:
House *m_house;
};
int main(int argc, char const *argv[])
{
// 不需要建造者,客户直接造房子
// 门窗 墙体玻璃等都需要用户管理
House *house = new House;
house->setDoor("user door");
house->setWall("user Wall");
house->setWindow("big window");
house->getDoor();
house->getWall();
house->getWindow();
delete house;
// 华丽的分割线
cout << "=========================" << endl;
// 请工程队 建造房子
// 将建造过程交给工程队, 是不是还可以请个指挥建造的? 这样客户就能完全解放出来了
Builder *builder = new Builder;
builder->MakeBuilder();
house = builder->GetHouse();
house->getDoor();
house->getWall();
house->getWindow();
delete builder;
cout << "builder pattern." << endl;
return 0;
}
两者,虽然都建造好了房子,但是建筑队参加之后,客户就不需要参与房子的建造的每个过程了,将建造过程与使用者之间进行分离,这就是建造者模式想要达到的效果
andrew@andrew-G3-3590:/work/linux-sys/DesignPatterns/cpp/build$ ./builder_pattern_question
user door
use Wall
big window
=========================
door
wall
window
builder pattern.
那除了这样其实可以再请一个指挥者过来,因为实际中会有不同的建造者参与竞争,每个建造者精通的地方都是不一样的,指挥者参与之后,用户只需要把自己的需求告知指挥者就行。
- 建造者模式,就是通过虚工程对基类,实现通过指挥者,指挥不同的建筑队建设不同的房子
#include <iostream>
#include <string>
using namespace std;
// 建造者模式,就是通过虚工程对基类,实现
// 通过指挥者,指挥不同的建筑队建设不同的房子
class House
{
public:
void setDoor(string door)
{
this->m_door = door;
}
void setWall(string wall)
{
this->m_wall = wall;
}
void setWindow(string window)
{
this->m_window = window;
}
string getDoor(void)
{
cout << m_door << endl;
return m_door;
}
string getWall(void)
{
cout << m_wall << endl;
return m_wall;
}
string getWindow(void)
{
cout << m_window << endl;;
return m_window;
}
private:
string m_door;
string m_wall;
string m_window;
};
class Builder
{
public:
virtual void BuildWall(void) = 0;
virtual void BuildDoor(void) = 0;
virtual void BuildWindow(void) = 0;
virtual House* GetHouse(void) = 0;
public:
virtual ~Builder(void)
{
}
};
//公寓建筑工程队
class FlatBuilder : public Builder
{
public:
FlatBuilder()
{
m_house = new House;
}
virtual void BuildWall(void)
{
m_house->setWall("flat wall");
}
virtual void BuildDoor(void)
{
m_house->setDoor("flat door");
}
virtual void BuildWindow(void)
{
m_house->setWindow("flat window");
}
virtual House* GetHouse(void)
{
return m_house;
}
private:
House *m_house;
};
// 别墅工程队
class VillaBuilder : public Builder
{
public:
VillaBuilder(void)
{
m_house = new House;
}
virtual void BuildWall(void)
{
m_house->setWall("villa wall");
}
virtual void BuildDoor(void)
{
m_house->setDoor("villa door");
}
virtual void BuildWindow(void)
{
m_house->setWindow("villa window");
}
virtual House* GetHouse(void)
{
return m_house;
}
private:
House *m_house;
};
// 设计师 指挥者,负责建造的逻辑
// 建筑队,干具体的活
class Director
{
public:
Director(Builder *builder)
{
m_build = builder;
}
void Construct(void)
{
m_build->BuildWall();
m_build->BuildWindow();
m_build->BuildDoor();
}
private:
Builder * m_build;
};
int main(int argc, char const *argv[])
{
House *house = NULL;
Builder *builder = NULL;
Director *director = NULL;
cout << "villa build" << endl;
// 请一个建造别墅的工程队,建造一个别墅
builder = new VillaBuilder;
// 设计师 指挥工程队进行建造
director = new Director(builder);
// 指挥干活
director->Construct();
// 建好 交工
house = builder->GetHouse();
// 客户验房
house->getWindow();
house->getDoor();
house->getWall();
delete house;
delete builder;
cout << "flat build" << endl;
builder = new FlatBuilder;
director = new Director(builder);
director->Construct();
house = builder->GetHouse();
house->getDoor();
house->getWall();
house->getWindow();
delete house;
delete builder;
delete director;
cout << "builder pattern." << endl;
return 0;
}
关于23种设计模式UML图相似性分类 3 of 5:左右聚合和uml中的交互图有两种的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于(转载)23种设计模式的uml图表示及通俗介绍、23种设计模式、23种设计模式-依赖、关联、聚合和组合之间区别的理解、23种设计模式C++源码与UML实现--建造者模式的相关知识,请在本站寻找。
本文标签: