Head first design mode notes

Source: Internet
Author: User

Design Pattern definition:

1. Policy mode. Defines the algorithm family and encapsulates them separately so that they can be replaced with each other. This pattern makes the algorithm changes independent of the customers who use the algorithm.

2. Observer mode. Defines one-to-multiple dependencies between objects. In this way, when an object changes its status, all its dependent persons will receive notifications and update automatically.

3. modifier mode. Dynamically attaches the responsibility to the object. To expand the functionality, the decorator provides an alternative solution that is more flexible than inheritance.

4. Factory method mode. Defines an interface for creating objects, but the subclass determines which class to instantiate. The factory method delays the class Instantiation to the subclass.

5. Abstract Factory mode. Provides an interface for creating families of related or dependent objects without specifying specific classes.

6. Single-piece mode. Make sure that a class has only one instance and provides a global access point.

7. Command mode. Encapsulate "request" into an object to use different requests, queues, or logs to parameterize other objects. The command mode also supports unrecoverable operations.

8. Adapter mode. Converts an interface of a class to another interface that the customer expects. The adapter allows classes that are not compatible with the original interface to work together.

9. Appearance mode. Provides a unified interface to access a group of interfaces in the subsystem. The appearance defines a high-level interface to make the subsystem easier to use.

10. template method mode. Define the skeleton of an algorithm in a method, and delay some steps to the subclass. The template method allows subclass to redefine some steps in the algorithm without changing the algorithm structure.

11. iterator mode. Provides a method to access each element in an aggregate object sequentially without exposing its internal representation.

12. Combination Mode. It allows you to combine objects into a tree structure to express the "whole/part" hierarchy. A combination allows the customer to process a combination of objects and objects in a consistent manner.

13. Status mode. Allows an object to change its behavior when its internal state changes. The object looks like it has modified its class.

14. Proxy mode. Provide a replacement or placeholder for another object to control access to this object.

15. compound mode. Combine two or more models to form a solution to solve repeated general problems.

Design principles:

1. Identify the potential changes in the application, and do not mix them with the code that does not need to be changed.

2. Programming for interfaces, rather than implementing programming.

3. Multi-Purpose Combination and less inheritance.

4. Strive for loose coupling between interaction objects.

5. classes should be open to extensions and closed to modifications.

6. Dependency abstraction is required, rather than specific classes.

7. Minimum knowledge principle: only talk to your close friend.

8. Hollywood principle: Do not call us. We will call you.

9. A class should have only one cause of change.

Design Basics:

1. abstraction.

2. encapsulation.

3. polymorphism.

4. Inheritance.

Key points:

I:

1. Knowing the OO basics is not enough for you to design a good OO system.

2. Good OO design must be reusable, extensible, and can maintain three features.

3. The model allows us to build a system with good OO design quality.

4. The model is considered to be experience-proven OO design experience.

5. The pattern is not code, but a general solution to the design problem. You can apply them to specific applications.

6. patterns are not invented, but discovered.

7. Most models and principles focus on the subject of software changes.

8. Most of the modes allow local system changes to be independent of other components.

9. We often extract the changed part of the system and encapsulate it.

10. The mode allows developers to share languages and maximize the value of communication.

II:

1. The observer mode defines the one-to-many relationship between objects.

2. The topic (that is, the observer) uses a common interface to update the observer.

3. The observer and the observer are loosely coupled. The observer does not know the details of the observer, but only knows that the observer implements the observer interface.

4. When using this mode, you can push or pull data from the observer (however, the push method is considered more "correct ").

5. When multiple observers exist, they cannot depend on the specific notification order.

6. Java has multiple observer modes, including general java. util. observable.

7. Pay attention to some issues arising from the implementation of Java. util. observable.

8. If necessary, you can implement your own Observable. this is not difficult. Don't be afraid.

9. Swing uses the observer mode in large quantities, and so are many GUI frameworks.

10. This mode is also applied in many places, such as JavaBean and RMI.

III:

1. inheritance is one of the extension forms and is not necessarily the best way to achieve elastic design.

2. In our design, actions should be allowed to be extended without having to modify the existing code.

3. Combination and delegation can be used to dynamically add new behaviors during runtime.

4. Apart from inheritance, the modifier mode also allows us to expand our behavior.

5. the decorator Mode means a group of decorator classes which are used to wrap specific components.

6. the modifier class reflects the component type to be decorated (in fact, they have the same type and are implemented through interfaces or inheritance ).

7. The decorator Can add his/her own behavior before the actions of the decorator, or even replace the actions of the decorator to achieve a specific purpose.

8. You can package a component with countless decorators.

9. The decorator is generally transparent to the customer of the component, unless the customer program depends on the specific type of the component.

10. The decorator will lead to many small objects in the design. Excessive use will complicate the program.

IV:

1. All factories are used to encapsulate object creation.

2. Simple factory, although not the real design model, is still a simple method that can decouple customer programs from specific classes.

3. factory methods use inheritance: delegates object creation to subclass, and subclasses implement factory methods to create objects.

4. Abstract Factory use object combination: Object creation is implemented in the method exposed by the factory interface.

5. All factory models promote loose coupling by reducing dependencies between applications and specific classes.

6. The factory method allows classes to delay Instantiation to subclass.

7. Abstract factories create Related Object families without relying on their specific classes.

8. The dependency inversion principle guides us to avoid dependency on specific types, but to rely on abstraction as much as possible.

9. Factory is a very powerful technique that helps us with abstract programming, rather than specific class programming.

V:

1. Single-piece mode ensures that a class in the program has only one instance at most.

2. The single-piece mode also provides global points for accessing this instance.

3. To implement the single-piece mode in Java, you need a private constructor, a static method, and a static variable.

4. determine the performance and resource restrictions, and carefully select the appropriate solution to implement a single piece to solve the problem of multithreading (we must ensure that all programs are multithreading ).

5. If JDK or a later version is not used, the dual-check locking implementation will become invalid.

6. Be careful. If you use multiple class loaders, multiple instances may be generated when one piece fails.

7. If you use JVM 1.2 or a previous version, you must create a single-piece registry to prevent the garbage collector from recycling the single-piece.

VI:

1. The command mode decouples the requested object from the object that executes the request.

2. the decoupled two communicate through command objects. The command object encapsulates the receiver and one or more actions.

3. The caller sends an request by calling execute () of the command object, which will call the receiver's action.

4. The caller can take the command as a parameter or even perform it dynamically at runtime.

5. The command can be undo. an undo () method is implemented to return to the status before execute () is executed.

6. Macro commands are a simple extension of commands, allowing you to call multiple commands. The macro method also supports revocation.

7. In actual operations, it is common to use the "smart" command object, that is, directly implementing the request, rather than entrusting the work to the recipient.

8. The command can also be used to implement the log and transaction system.

7:

1. When you need to use an existing class and its interface does not meet your needs, use the adapter.

2. Use the appearance when you need to simplify and unify a large interface or a group of complex interfaces.

3. the adapter changes the interface to meet customer expectations.

4. Appearance decouples customers from a complex subsystem.

5. It may take some effort or effort to implement an adapter, depending on the size and complexity of the Target Interface.

6. To achieve an appearance, you need to combine the subsystem into the appearance, and then delegate the work to the subsystem for execution.

7. There are two types of adapter modes: diagonal adapter and class adapter. The class adapter requires multiple inheritance.

8. You can create more than one appearance for a subsystem.

9. The adapter wraps an object to change its interface; the modifier wraps an object to add new behaviors and responsibilities; A group of objects are encapsulated to simplify their interfaces.

8:

1. The "template method" defines steps that fail to be calculated and delays the implementation of these steps to subclass.

2. The template method mode provides an important technique for code reuse.

3. the abstract class of the template method can define specific methods, abstract methods, and hooks.

4. abstract methods are implemented by sub-classes.

5. Hook is a method. It does not do anything in the abstract class, or only does the default thing. The subclass can choose not to overwrite it.

6. To prevent the subclass from modifying the algorithm in the template method, you can declare the template method as final.

7. Hollywood principles tell us to place decision-making power in high-level modules to determine how and when to call lower-level modules.

8. You will see many variants of the template method pattern in real-world code. Don't expect them to be recognized at a glance.

9. Both the policy mode and the template method mode encapsulate the algorithm. One is a combination and the other is an inheritance.

10. The factory method is a special version of the template method.

IX:

1. The iterator allows access to aggregated elements without exposing its internal structure.

2. The iterator encapsulates the traversal and aggregation into an object.

3. When the iterator is used, we rely on Aggregation to provide traversal.

4. The iterator provides a common interface that allows us to use polymorphism when traversing aggregated items.

5. We should try to assign only one responsibility to a class.

6. The combination mode provides a structure that can accommodate both individual objects and composite objects.

7. The combination mode allows the customer to treat individual objects and the combination objects equally.

8. Any object in the combination structure is called a component. A component can be a combination or a leaf node.

9. There are many design compromises when implementing the combination mode. You need to balance transparency and security as needed.

10:

1. The State mode allows an object to have different behaviors based on its internal state.

2. Unlike the program State Machine (PSM), the state mode uses classes to represent the State.

3. Context delegates the behavior to the current State object.

4. By encapsulating each State into a class, we will localize any changes we need to make in the future.

5. The status and Policy modes have the same class diagrams, but their intentions are different.

6. The context class is usually configured using behaviors or algorithms in the Policy mode.

7. The state mode allows context to change behavior as the state changes.

8. The state conversion can be controlled by the state class or context class.

9. Using the status mode usually leads to a large increase in the number of classes in the design.

10. The status class can be shared by multiple context instances.

11:

1. The proxy mode provides representatives for another object to control the access to the object. There are many ways to manage access.

2. The remote agent manages the interaction between the customer and the remote object.

3. Virtual proxy controls access to objects with high instantiation overhead.

4. The proxy controls access to object methods based on callers.

5. There are many variants of the protection mode, such as cache proxy, synchronization proxy, firewall proxy, and replication proxy during write.

6. The proxy structure is similar to the modifier, but the purpose is different.

7. The modifier mode adds behavior to the object, while the proxy controls access.

8. built-in JAVA Proxy Support. You can create a dynamic proxy as needed and allocate all calls to the selected processor.

9. Like other wrapper, proxy will increase the number of classes in your design.

12:

1. MVC is a composite mode that combines the observer mode, policy mode, and combination mode.

2. The model uses the observer mode to allow the observer to update and maintain decoupling between the two.

3. The controller is the view policy, and the view can be implemented using different controllers to get different behaviors.

4. The view uses the combination mode to implement the user interface. The user interface usually combines nested components, such as panels, frames, and buttons.

5. These models work together to decouple the three layers of the MVC model so that the design can be clean and flexible.

6. the adapter mode is used to adapt the new model to an existing view and controller.

7. Model 2 is a Web application of MVC.

8. In Mode 2, the controller is implemented as a servlet, while JSP/html is implemented as a view.

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.