Object-oriented (OO) thinking of design patterns

Source: Internet
Author: User

[This is my own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020]

Object-oriented (OO) thinking:
1. Consider the class

Noun

2. Consider attributes

Can not be separated from the specific application environment

3. Method of consideration4. Consider the relationship between classes5. Consider hiding (encapsulation)

Reduction of coupling

6. Consider inheritance

Strong coupling and should be used with caution

7. Consider polymorphic

The core of the core, polymorphism brings scalability.

Polymorphism has three properties: 1 inheritance; 2 overrides; 3 references to the parent class object to the subclass

issues to be aware of when designing for objects:1. The design has no absolute right and wrong2. Over design is also a sin3. No actual design will one step4. Beginners do not consider too many principles and rules, the most important thing is to write

Note the distinction between abstract classes and interfaces.


We use "Jesson to write Java programs with Eclipse" as an example to illustrate object-oriented thinking.

1 Consideration Class

Here we look for nouns, "Jesson", "Eclipse", "Java program", so, in general, we need at least three classes to load the words separately. When creating a class, there is a problem, is to create a new Jesson class directly???? Of course not, the Jesson here is only a special case, is a programmer a special case, or to Dali said, is a person's special case, is a person's specific object, but Jesson is not a kind of person. The only emphasis here is "write the program", so you can create a new programmer class, the programmer class, where "Eclipse" is just one of many development tools, so you can create a new Developmenttool class; ProgrammingLanguage class.

2 Consider attributes

When considering the properties of a class, it is important to note that"for any class, it should not be separated from its application environment and should be encapsulated according to the specific application environment" , so for the programmer class, the first thing we can think of is a property is "Programmer's name"; The other two classes can add the appropriate properties as needed.

3  Consider method

For the programmer class It is easy to think of a method that uses use () to select the development tool, and for the Developmenttool class There should be an "edit or write" Writre () method, you can add the appropriate method to the ProgrammingLanguage class as needed.

4. Consider the relationship between classes

According to the demand, is programmer use Developmenttool to write the programminglanguage program.

     specifically,"Programmer useDevelopmenttool ", so you canThe use () method in the programmer class isDevelopmenttoolas a parameter; Further, if, as we consider, programmer uses Developmenttool to write what the problem is, we can overload the use () method to Developmenttool objects andthe ProgrammingLanguage object is a parameter.

5. Consider encapsulation

In general, the properties of the class should be privatized, corresponding to the need to add the set and get methods, so that the function of the class itself, as far as possible to complete its own. The set and get methods here, as well as the use () method in the programmer class, also implement encapsulation.

6. Consider inheritance

If the requirements change, for example, if Jesson uses notepad++ instead of Eclipse, then you can consider using inheritance, since the first is to create a new development tool class, not just the Eclipse class, plus, Both eclipseg and notepad++ belong to the development tools, so they can all inherit from the Developmenttool class.

7. Consider polymorphic

programmer class in the use () method, regardless of whether the Eclipse class or the Notepad class is passed in, whose write () method is called, This is polymorphic, of course, polymorphism is also reflected in other aspects.

   

The code for this example is given below:

Programmer.java

/** * The programmer class * includes attributes: First Name * Includes method: use (development tool) * @author Jesson * */public class Programmer {private string Name;public string Getna Me () {return name;} public void SetName (String name) {this.name = name;} public void Use (Developmenttool tool) {Tool.write (New ProgrammingLanguage ("Java"));} public void use (Developmenttool tool,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 (Pro Gramminglanguage language);}
Eclipse.java

/** * Eclipse class, inheriting from the abstract development tool class, and implementing 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 auto prompt feature, I am writing" + Language.getname () + "program");}}
Notpad.java

/** * Notpad class, inherits from the development tool class, implements the Write abstract method * @author Jesson * */public class Notpad extends Developmenttool {@Overridepublic void Write (ProgrammingLanguage language) {//TODO auto-generated method StubSystem.out.println ("I am Notepad, I have syntax highlighting function, I am writing" + Language.getname () + "program");}}
Programminglanguage.java

/** * Program Language class includes property name and set and get method *  * @author Jesson *  */public class ProgrammingLanguage {private String name;pub Lic 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 Prog Rammer ();p rogrammer.setname ("Jesson");p Rogrammer.use (New Eclipse ());p Rogrammer.use (New Notpad ());}}

Object-oriented (OO) thinking of design patterns

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.