Object-oriented Thinking of design patterns, design patterns OO

Source: Internet
Author: User

Object-oriented Thinking of design patterns, design patterns OO

[This article is my own learning notes. You are welcome to repost it, but please note the Source: http://blog.csdn.net/jesson20121020]

Object-oriented (OO) thinking:
1. Consider classes

Term

2. Consider attributes

Cannot be separated from the specific application environment

3. Consideration Method 4. Consideration of the relationship between classes 5. Consideration of hiding (encapsulation)

Reducing Coupling

6. Consider inheritance

Strong coupling, should be used with caution

7. Consider Polymorphism

The core of the core, polymorphism brings scalability.

Polymorphism has three features: 1 Inheritance; 2 rewriting; 3 reference of the parent class pointing to the subclass object

Considerations for Object-Oriented Design: 1. there is no absolute right or wrong design 2. over Design is also a sin 3. there is no actual design in place. 4. do not consider too many principles and rules for beginners. The most important thing is to write

Distinguish abstract classes from interfaces.


Let's take "jesson uses eclipse to write java programs" as an example to illustrate Object-oriented thinking.

1. Consideration class

Here, we look for Nouns such as "jesson", "eclipse", and "java program". Therefore, we usually need at least three classes to load these terms separately. When creating a class, is there a problem: is a new jesson class directly created ???? Of course not. Here, the jesson is just a special case. It is a special case for programmers. Or in the big picture, it is a special case for people and a specific object for people. But jesson is not a type of person. Here, we only emphasize "writing programs", so we can create a Programmer class, that is, the Programmer class. Here, "eclipse" is only one of many development tools, so we can create a DevelopmentTool class; similarly, the ProgrammingLanguage class is created.

2. Attribute considerations

When considering the attributes of a class, note that "for any class, it cannot be separated from its application environment and its attributes should be encapsulated according to the specific application environment". Therefore, for the Programmer class, the first thing we can think of is that one attribute is the "programmer's name". Similarly, the other two classes can add corresponding attributes as needed.

3. Consideration

It is easy to think of the Programmer class that there is a "use" () method for selecting a development tool. For the DevelopmentTool class, there should be a "edit or write" writre () you can add corresponding methods to the ProgrammingLanguage class as needed.

4. Consider the relationship between classes

As required, Programmer uses the DevelopmentTool to write the ProgrammingLanguage program.

Specifically, "Programmer uses the DevelopmentTool", so you can use the DevelopmentTool as the parameter in the use () method of the Programmer class. Further, if we consider the problems that Programmer uses the DevelopmentTool to write something, we can reload the use () method to take the DevelopmentTool object and ProgrammingLanguage object as parameters.

5. Encapsulation

In general, we should privatize the attributes of the class and add the set and get methods to the class as needed. In this way, we should try to implement the functions of the class by ourselves. The set and get methods and the use () methods in the Programmer class also implement encapsulation.

6. Consider inheritance

If the requirements change, for example, if jesson uses Notepad ++ instead of eclipse, you can consider using inheritance, because the development tool class is created from the very beginning, it is not just an eclipse class. In addition, both eclipseg and Notepad ++ are development tools, so they can all be inherited from the DevelopmentTool class.

7. Consider Polymorphism

When the user calls the use () method in the Programmer class, no matter whether it is the Eclipse class or the Notepad class, which write () method is called, this is polymorphism. Of course, polymorphism is also reflected in other aspects.

   

The code for this example is as follows:

Programmer. java

/*** Programmer class * includes attributes: name * includes Methods: Use (Development Tool) * @ author jesson **/public class Programmer {private String name; public String getName () {return name;} public void setName (String name) {this. name = name;} public void use (DevelopmentTool) {tool. write (new ProgrammingLanguage ("java");} public void use (DevelopmentTool, ProgrammingLanguage language) {tool. write (language );}}
DevelopmentTool. java

/*** Development tool abstract class * declares a write () abstract Method * @ author jesson **/public abstract class DevelopmentTool {public abstract void write (ProgrammingLanguage language );}
Eclipse. java

/*** Eclipse class, inherited from the abstract development tool class, and implemented its abstract Method * @ author jesson **/public class Eclipse extends DevelopmentTool {@ Overridepublic void write (ProgrammingLanguage language) {// TODO Auto-generated method stubSystem. out. println ("I am eclipse, I have the function of automatic prompt, I am writing" + language. getName () + "program ");}}
Notpad. java

/*** Notpad class, inherited from the development tool class, implemented the write abstract Method * @ author jesson **/public class Notpad extends DevelopmentTool {@ Overridepublic void write (ProgrammingLanguage language) {// TODO Auto-generated method stubSystem. out. println ("I'm notepad, I have the syntax highlighting function, I'm writing" + language. getName () + "program ");}}
ProgrammingLanguage. java

/*** The program language class includes the property name and set and get method ** @ author jesson **/public class ProgrammingLanguage {private String name; public ProgrammingLanguage (String name) {// TODO Auto-generated constructor stubthis. name = name;} public String getName () {return name;} public void setName (String name) {this. name = name ;}}
Test. java

/*** Test class ** @ author jesson **/public class Test {public static void main (String [] args) {Programmer programmer = new Programmer (); programmer. setName ("jesson"); programmer. use (new Eclipse (); programmer. use (new Notpad ());}}

Related Article

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.