Summary of Design patterns (not to be continued)

Source: Internet
Author: User
Tags abstract

One, the strategy model (seehttp://blog.csdn.net/zhangliangzi/article/details/52161211 ) Application Scenarios:A variety of strategies, algorithms for the same purpose (such as all the sorting algorithm), different situations need to use different strategies (such as internal sorting with the fast row, external sorting with the merge), the client to determine the specific call which policy or algorithm. general form of implementation:Each policy class implements the same interface to implement the algorithm. A context class introduces a concrete policy object by constructing a method, encapsulating the algorithm. The client uses context to invoke different algorithms to implement the function in different scenarios. (When the specific policy classes have their own characteristics in addition to the common specifications, you can use the relationship between the abstract class and the subclass instead of the interface and implementation classes to implement the policy mode) Features:is an organizational strategy, algorithm design mode, you can dynamically switch different strategies, but only one strategy at a time. Advantages:Avoids the use of multiple conditional if-else statements, avoids the logic of the selection algorithm and the logical confused of the implementation algorithm. Disadvantages:Because it is the client's own choice behavior, the level of client control is high. second, the factory model Why should we use Factory mode? to put it simply, the factory model provides a simple for the user classfor factory-class safetythe two are low coupling and conform to the opening and shutting principleThe way objects are created, the work of creating objects from the user (requirement Class) to the Factory Master (factory Class). to speak in detail, there are many product classes, if you do not use the Factory mode, the user class (needs the Product object's class) must know the Product object constructs the way, the user and the product production is coupled together, if the product production process is very complex, the user constructs is very difficult, and as long as the product constructs the way to have the change, All users who need the product need to know the new way of construction. One of the metaphors is appropriate, and the person who buys the car needs to know how the car was made. So we urgently need a car factory, users tell the factory what type of car he needs, the factory will return to the user a car. (The effect of the factory is similar to the spring's IOC dependency injection mechanism)
the type of Factory mode. (Refer tohttp://blog.csdn.net/lingfengtengfei/article/details/12374469 ) Simple Factory mode, also known as the static Factory mode, it is through a static factory class, the object's construction details are encapsulated in the method, the requirement class is called directly, the object construction method changes will not affect the demand class. As for the product class can be abstracted, such as jet aircraft, passenger aircraft, fighter jets can be abstracted out of a plane class, each specific class inherit aircraft class. The benefits of doing this are abstract benefits, reuse, expansion, run-time binding. Of course, the relationship between the abstract class and the subclass can also be implemented with interfaces and implementation classes, and all design patterns are less rigid and can be adapted to specific situations.
Factory Method Mode, is the same abstraction of the factory class as the product class, where each plant produces only one product, such as a fighter factory producing fighter jets and passenger aircraft, which inherit abstract aircraft factory classes. Because the abstraction and the static cannot coexist, the method of constructing the product object in the factory method pattern is no longer static.
Abstract Method Pattern, in general, the same as the factory method pattern, the difference is that when the product cluster contains some matching products, such as mouse and keyboard is supporting, each plant not only produces a product, instead of producing a set of products.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.