[Design mode] design mode is used to solve design problems.

Source: Internet
Author: User
Select object

The most difficult part of object-oriented design is to break down the system into object sets. There are many factors to consider: encapsulation, granularity, dependency, flexibility, performance, evolution, reuse, and so on. They all affect system decomposition, and these factors are usually conflicting with each other.
Many objects of design come from analysis models in the real world. However, the classes produced by the design results usually do not exist in the real world.The design pattern helps you identify non-obvious abstractions and describe these abstract objects.For example, objects describing a process or algorithm do not exist in reality, but they are the key part of the design. The Strategy Mode describes how to implement an Interchangeable algorithm family. State mode describes every state of an object as an object. These objects do not exist in the analysis phase, or even in the early stages of the design phase. They will be discovered only when the design is more flexible and reusable.

Object Granularity

The facade mode describes how to use objects to represent the complete subsystem. The flyweight mode describes how to support a large number of objects with minimum granularity. Other design patterns describe specific methods for breaking an object into many small objects. Abstract Factory and builder generate objects specifically responsible for generating other objects. The objects generated by visitor and command are specifically responsible for requests to other objects or object groups.

Object Interface

The object interface describes the set of all requests that the object can accept. Any requests that match the medium-sized structure of the object interface can be sent to the object.
In an object-oriented system, interfaces are basic components. Objects can only communicate with external users through their interfaces. If they do not pass the interface of an object, they cannot know anything about the object or request the object to do anything. The object interface and its function implementation are separated. Different objects can implement different requests. That is to say, two objects with the same interface can have completely different implementations.

Dynamic binding)

When a request is sent to an object, the specific operation is related to both the request itself and the accepted object. The connection between a request sent to an object and its corresponding operations is called dynamic binding ).
Dynamic binding means that the request sent is not subject to your specific implementation constraints until the runtime. Dynamic binding allows you to replace objects with the same interfaces at runtime. This alternative is called polymorphism, which is one of the core concepts in object-oriented systems.Polymorphism simplifies the definition of customers, makes objects independent from each other, and can dynamically change their relationships at runtime.

Design Mode specify object interface

The design mode helps you define interfaces by determining the main components of interfaces and the data types sent by interfaces.
The memento mode describes how to encapsulate and save the internal state of an object so that the object can be restored to this state after a period of time. It specifies that memento objects must define two interfaces: one is a restricted interface that allows customers to maintain and copy memento, and one
Privileged interfaces that can be used by the original object to store and extract memento states.
The design mode also specifies the relationship between interfaces. In particular, they often require some classes to have similar interfaces; or
They impose restrictions on some class interfaces. For example, the decorator and proxy modes require that the interfaces of the decorator and proxy objects be consistent with the modified objects and the entrusted objects. In the visitor mode, the visitor interface must reflect all the classes of the objects that the visitor can access.

Object implementation

Objects are created by instantiating classes. When instantiating a class, you need to allocate storage space to the internal data of the object (composed of instance variables) and associate the operation with the data. Many similar instances of objects are created by instantiating the same class.
The new class can be defined by the existing class through class inheritance. When subclass inherits the parent class, it contains all data and operations defined by the parent class. Subclass instance objects contain data defined by all subclasses and parent classes, and can complete all operations defined by subclasses and parent classes.

Abstract class

Abstract class defines public interfaces for its subclass. An abstract class will delay the implementation of some or all of its operations to the subclass. Therefore, an abstract class cannot be instantiated. Operations defined but not implemented in abstract classes are called abstract operations ).
The subclass can redefine the operations defined by the parent class so that the subclass can take over the request processing operations of the parent class.

Programming interfaces, rather than implementing Programming

Class inheritance is a basic mechanism for extending the application functions by reusing the parent class function. It allows you to quickly define new objects based on the old objects.
When the inheritance is used properly, all Classes exported from the abstract class will share the interface of this abstract class. This means that the subclass only adds or redefines the operation without hiding the operation of the parent class. In this case, all subclasses can respond to the request in the abstract class interface, so that the subclass type is the subtype of the abstract class.
Manipulating an object based on the APIS defined in an abstract class has the following two benefits:

  1. The customer does not need to know the specific type of the object they use, but only needs the object to have the interface that the customer expects.
  2. Customers do not need to know what classes they use to implement the object. They only need to know the abstract class that defines the interface.

Programming for interfaces, rather than implementing programming.
Instead of declaring a variable as an instance object of a specific class, you must make it conform to the interface defined by the abstract class.

Reuse mechanism object combination

The two most common features of function reuse in object-oriented systems are class inheritance and object combination.
Class inheritance allows you to define the implementation of a Class Based on the implementation of other classes. This type of reuse by generating sub-classes is usually called white box reuse (white-box reuse ). In the inheritance mode, the internal details of the parent class are visible to the Child class.
Object combination requires that the combined object has a well-defined interface. This reuse style is called black-box reuse because the internal details of the object are invisible.

Advantages:

  • Class inheritance is statically defined during compilation and can be directly used. Class inheritance can easily change the implementation of reuse.
    Disadvantages:
  • Because inheritance is defined at the Compilation Time, the implementation of inheritance from the parent class cannot be changed at the runtime.
  • Because inheritance reveals the implementation details of its parent class to the Child class, inheritance is often considered to be "corrupt encapsulation"
  • The implementation in the subclass has such a close dependency with its parent class that any change in the implementation of the parent class will inevitably lead to a change in the subclass.

Object combinations are dynamically defined at runtime by obtaining references to other objects. The combination requires that objects comply with their interface conventions and require more careful interface definition. These interfaces do not prevent you from using an object with other objects.
Benefits of this method: Because objects can only be accessed through interfaces, we do not break the encapsulation; as long as the types are consistent, one object can be used to replace another object at the runtime; further, because the implementation of objects is written based on interfaces, there is little dependency on implementation.

Parameterized type

Another method of function reuse (not strictly object-oriented) is parameterization.
Parameterized type allows you to define a type without specifying all other types used by the type. Unspecified types are provided as parameters in use.
Object combination technology allows you to change the behavior of a combination at runtime, but it is indirect and inefficient. Inherit the default implementation that allows you to provide operations and redefine these operations through sub-classes. Parameterized type allows you to change the type used by the class. However, inheritance and parameterization types cannot be changed at runtime.

Support Change Design

The key to maximizing reuse lies in the foresight of changes to new and existing requirements, requiring that your system design be improved accordingly.
The design pattern ensures that the system can change in a specific way, helping you avoid re-designing the system. Each design pattern allows changes in one aspect of the system structure to be independent of other aspects, so that the resulting system will be more robust for a particular change.

Design Patterns play a role in developing major software applications

Internal reusability, maintainability, and scalability of software should be given priority when developing applications. Internal reusability ensures that you do not need to design and implement it. The design mode improves internal reusability by reducing dependencies. Loose coupling also enhances the possibility of a class of objects to collaborate with multiple other objects.
When design patterns are used to stratify the system and restrict the dependence on the platform, they also make an application more maintainable. By displaying how to expand the class hierarchy and how to reuse objects, they can enhance the system's scalability. At the same time, the reduction of coupling degree will also enhance the scalability. If a class does not depend much on other classes, it is easy to expand this isolated class.

Toolbox

An application often uses classes from one or more predefined class libraries called toolkit. Toolbox is a set of related and reusable classes that provide common functions. The Toolbox emphasizes code reuse, which is a "sub-library" in an object-oriented environment ".
Toolbox design is much harder than application design because it is required to be available and effective for many applications. Furthermore, the toolkit designers do not know what applications use the Toolkit and what special requirements they have. In this way, it is important to avoid assumptions and dependencies. Otherwise, the flexibility of the Toolbox will be limited, which affects its applicability and efficiency.

Framework

Framework is a group of collaborative classes that constitute a reusable design for specific software.
The framework specifies the architecture of your application. It defines the overall structure, the division of classes and objects, the main responsibilities of each part, the collaboration between classes and objects, and the control process. The framework predefines these design parameters so that Application designers or implementers can focus on specific details of the application. The framework records the common design decisions in its application fields. Therefore, the Framework emphasizes design reuse, although the framework usually includes specific immediately available subclasses.

[Design mode] design mode is used to solve design problems.

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.