Some basic but important concepts of object-oriented design and implementation
Source: Internet
Author: User
Object | concept | design
In object-oriented design and implementation, we must spend time and energy figuring out these concepts: abstraction, encapsulation, inheritance, and polymorphism to
and object-oriented design principles, otherwise will not really understand the object-oriented soul, and will not feel the object-oriented design ideas to us
The benefits. After years of learning Oo and several practices, I decided to make a summary of these basics. And take these questions as the analysis
Instance. 1. The difference between interface and Class 2. Why should priority be given to synthetic polymerization 3. Polymorphism will bring us those convenience 4. What is the relationship between several design principles 5. Design Patterns are useful? What is it? How to catch two rabbits running at the same time? Answer: 1. The interface only describes the characteristics of the method, not the implementation of the method, and the class not only gives the feature of the method, but also the realization of the method.
, so the interface separates the features and implementations of the method. This segmentation, embodied in the interface that often represents a role, wraps it with the role
Related actions and attributes, and the class that implements the interface is the actor who plays the role. A character can be played by different actors.
In addition to playing a common role between different actors, there is no need for anything else in common. In order to make it very clear, we can also put the problem more complex, discuss the interface, abstract classes and specific classes between the
First, what is abstraction? It can be said that no abstraction is not object oriented. In my understanding, abstraction is the common sense of finding a bunch of things.
Syndrome In Java, even if any two irrelevant things can be abstract, because everything is a object; can be abstracted into
Object class, which is actually what Java does. For example: we can abstract the role of a manager and a student into an adult or a career; three
Angles and circles are abstracted into shapes. Object-oriented has an important principle of dip (dependency inversion principle) that means to program for abstraction; our
The program relies on abstraction rather than specifics. For example, when we define a method of human beings to set up their transportation, we should pass on the abstract
Parameters for the type of vehicle. void Setvehicle (Vehicle v) so that when invoked, due to the support of polymorphism, you can
Setvehicle (new car); object-oriented there is also an important principle OCP (opening and closing principle), our design should be open to expansion;
Change the closure. How can we do this? is abstraction, we abstract the logic that is expected to expand later, and then the abstract
Programming, extension only need to add new logic on it, do not need to modify the existing logic. It's hard to imagine if there's no abstraction.
Do it. We can also greatly improve the reusability of code through abstraction. Interfaces and abstract classes are all implementations of Java abstraction. The concrete class can implement interfaces or inherit abstract classes (in design mode it also says
Try not to inherit from specific classes. Why do we have an abstract class and an interface? So, the Java language is a single inheritance language, a class can have only one superclass,
So in many cases, this particular class might already have a superclass, and it's impossible to add a new superclass to it.
。 If you want to do it, you have to add the new superclass to the existing superclass, and form a super super class; If this super
The position of the class is also occupied, so you have to move on to the top of the hierarchy of the class. In that case. to a
The pluggable design of the specific class becomes the modification of all classes in the hierarchy. Not to mention if the superclass is provided by a software provider
How to do? Interfaces make it possible to insert sex. You can implement an interface in any class hierarchy, which
The mouth affects all subclasses of this class, so let's summarize the difference between the interface and the abstract class: A. At the language level, a class can implement multiple interfaces, but can inherit only one abstract class, and an abstract class can have specific methods, and
interface is not available; Abstract classes can implement interfaces, but interfaces cannot inherit classes. B. At the design level, if there is a common logic between subclasses, it should be designed as an abstract class; semantically, if it's a is-a relationship,
, consider designing abstract classes, simply containing a function, nature, and considering using interfaces more semantically. For example: abstract class Door {abstract void open (); abstract void Close ();} interface Alarm {void Alarm (); Class Alarmdoor extends Door implements Alarm {void open () {...} void Close () {...} void alarm () {...} This design is more reasonable. An abstract class allows a partial implementation of a class, whereas an interface does not contain an implementation of any members. We can say this: interfaces are more than abstract classes
Abstract. Abstract classes are compared to concrete classes as follows: The concrete class is not used for inheritance, and abstract classes are only used for inheritance. Abstract classes should have as much common code and
As little data as possible. Interfaces are compared to specific classes as follows: If a specific class implements an interface, it must implement all the methods specified by the interface. 2. There are two main forms of reuse: inheritance reuse and synthetic polymerization reuse. However, inheritance reuse is often abused by others. Experience tells us that the inheritance of the LSP (Richter substitution principle) and the Coad rule are not satisfied.
Use is unreasonable, and will get absurd results there are several important disadvantages of inheritance reuse, which can be avoided by synthetic polymerization reuse: a. Inheriting the reuse of broken packages is a white box reuse because inheritance exposes the implementation details of a superclass to subclasses. We know the seal.
Loading is a very important feature of object-oriented, by encapsulating we can easily manage our code. We only care about the public of a class
interface, but ignores the specific implementation. This makes it easier for us to take note of a complex class or a third party component
Focus on the business approach we care about, not the details. Thus reducing the complexity of the problem. B. If the implementation of the superclass changes, the implementation of the subclass will have to change as well. C. Inheritance is static reuse. It is not possible to change during run time, so there is not enough flexibility. It's hard to make
"Decoupling the abstraction from the implementation so that the two can change independently". 3. The goal of software design should be scalable, flexible, and pluggable. To do this, you have to use polymorphism as a means of control.
。 First, what is polymorphism? Do not confuse polymorphism with the concepts of overloading and resetting, and they are not the same thing to say. At this point a lot of books on
The argument is wrong, the overload is not our true sense of polymorphism, at most the compiler polymorphism, the same name of the different methods on the
The translation has actually been transformed into a different method name. Polymorphism is a mechanism that allows us to program on these classes as if we were manipulating a single class instead of each other
Independent of several classes) is convenient, which gives us more flexibility to join or remove any particular classes.
---------------the feeling ———————— after learning design pattern
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.