面向对象程序设计(Object Oriented Design)

Posted CaiCongyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面向对象程序设计(Object Oriented Design)相关的知识,希望对你有一定的参考价值。

       OOD的流程:

                  

需求分析——>系统/程序设计——>实现这个设计——>测试

 

Class Design

  1. Identify classes for the system.

  2. Describe attributes and methods in each class.

  3. Establish relationships among classes.

  4. Create classes.

Identity classes and objects

  A fundamental part of object-oriented software design is determining the classes that will contribute to the program.

  One way to identify potential classes is to identify the objects discussed in the program requirements.

  Objects are generally nouns. 

  Obviously, not all nouns are objects, some of them can be attributes of objects.

  A class: a group of objects with similar attributes and behaviours. A plural in the specification may indicates that you need a class for those items, e.g., products, students, users… Some attributes are too complex to be represented by primitive values.

   In such cases, we need to create classes to represent them as well.

 

Static variables and methods

  Static variable (class variable): a common field shared by all objects of the class.

  Static method (class method): can be invoked through the class name.

  We don’t have to instantiate an object of the class in order to invoke the method. E.g.: Math.sqrt(27);

 

finals

  1)final class The class can NOT be used as a superclass(leaves)

    public final class BClass{ ….. }

  2)final method A final method cannot be overridden

  3)Final variable: a final variable can only be initialized once, either via an initializer or an assignment statement  If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable.

        

Relationships among Classes

  Association关联

  Aggregation聚合

  Composition组合

  Inheritance继承

 

1)Association

  Association represents a general relationship that describes an activity between two classes.

   Often the methods of one class will invoke the methods of the other class, i.e., a “uses” relationship.

   Generally, if class A uses class B, then one more methods of class B will be invoked by class A (from one or more methods in class A).

2)Aggregation and Composition

  Some objects are made up of other objects, e.g., a car is made up of its engine, its chassis, its wheels, and several other parts.

We can say that a car is an aggregation, or it is composed of other objects.Aggregation is sometimes described as a has-a relationship, e.g., a car has a chassis.

Aggregation is a special type of association. That is, a class that is defined in part by another class is dependent on that class. The methods of the aggregate object generally invoke the methods of the objects from which it is composed.

  Composition is a special aggregation.

  It defines a “1 to 1” has a relationship

Example:

Composition : Engine is part-of Car, and if you believe it is completely encapsulated by car, not reusable, or switchable, the relationship between them is Composition.

public class Car {

  private final Engine engine;

   Car(String engNumber) {

  engine = new Engine(engNumber);

  }

  void move() {

  engine.work();

  }

}

Aggregation : If you think the engine can exist without a car, and it can be swapped, and the outside world can still have a reference to the engine, then you can define the relationship as an aggregation:

public class Car {

   private Engine engine;

   Car(String engNumber) {

  engine = new Engine(engNumber);

   }

  void move() {

   engine.work();

  }

}

Association: Driver just makes use of a key. A key is not necessary to be a member of a driver.

public class Key{

   public void startEngine() {

   // use key to start the engine of a car

   }

  }

  public class Driver{

    public void drive(Key key) {

      key.startEngine();

  }

}

        

Inheritance:

  Inheritance models the is-an-extension-of relationship between two classes.

          

Field initialisation:

  When creating an object of a subclass: Initialize them by invoking a superclass constructor with the appropriate parameters: Use the key word: super() or super(arg1, arg2, …)

  Placed in the first line

   Choose proper parameters

  If the subclass constructor skips calling the superclass , then Java automatically calls the no-parameter one

   Note: Initialise superclass fields before subclass starts to initialize its part of the object

继承构造器

 

Reference Passing(引用传递)

  SuperClass supObj=new SuperClass(…);

  SubClass subObj=new SubClass(…);

  supObj=subObj;

因为存在SpecialSubclassMember 所以supObj可以访问自己和sub的内存,而sub只能访问自己的。

          

Method Overriding

  Subclass to provide a specific implementation of a method that is already provided by its superclasses

  To override a method: The method has the same name with the method of the superclass Same arguments as well

  public class SuperClass {

  public int methodA (int i, String s) {

  bodyA

  }

  }

   public class SubClass extends SuperClass {

    public int methodA (int i, String s) {

      bodyB

     }

    }

  If we call methodA from an instance of SuperClass bodyA runs;

   if we call methodA from an instance of SubClass bodyB runs In SubClass, we can access bodyA with: super.methodA(inti, String s) Static methods cannot be overwritten

 

Is a vs. Has a

  Model an is-a relationship with inheritance

  If every instance of Class2 is also a Class1,

  then Class2 is-a Class1 Class2 is the subclass of Class1

  Class1 is the superclass of Class2

  In Java, this is expressed as Class2 extends Class1:

  public class Class2 extends Class1 { ... }

   In Java: the is-a relationship is denoted by keyword extends a subclass can ONLY extend ONE superclass

          

 

Inheritance vs. Aggregation

Inheritance: … is a …

  E.g., a Dog is Animal, i.e., Dog is an extension of Animala student is people

Aggregation: … has a …

   E.g., a person has a name, i.e., use aggregation to model the relationship between Person and Name.

 

Interfaces vs. Abstract Classes

  Both interfaces and abstract classes can be used to generalize common features.

  Abstract class defines strong is-an-extension-of relationship.

  It clearly describes a parent-child relationship Interfaces are weaker (… like a …).

  Interfaces are more flexible than abstract classes, because a subclass can extend only one superclass, but implement any number of interfaces.

  Interfaces cannot contain concrete methods.

  You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface.

  So you can use the interface or its companion class whichever is more convenient.

以上是关于面向对象程序设计(Object Oriented Design)的主要内容,如果未能解决你的问题,请参考以下文章

面向对象编程OOP Object oriented programing

面向对象程序设计(Object Oriented Design)

>>>>面向对象(Object Oriented)

面向对象的程序设计:Object-oriented programming

面向对象编程Object Oriented Programming(OOP)

javaScript设计模式之面向对象编程(object-oriented programming,OOP)