Java Object-oriented design

Source: Internet
Author: User
Tags object object

First, design principles

1. single principle of responsibility : All objects should have a single responsibility, and all of the services it provides are only around this responsibility.

2. Opening and closing principle : (1) for extensions is open (open for extension): The behavior of the module can be extended, when the application needs change, the module can be extended to meet the new requirements.
(2) for the change is closed (Closed for modification): When the module behavior is extended, you do not have to change the module's source code or the binaries.

3. The principle of substitution : The Richter substitution principle is commonly used to check whether two classes are inheritance relationships. In an inheritance relationship that conforms to the Richter scale substitution principle, where the parent code is used, the child class code is substituted to correctly perform the action processing.

4. dependency Reversal principle : Refers to the inversion of dependencies between two modules to a dependent abstract class or interface. (in the design of the three-tier architecture, the design interface embodies this principle)

There are two meanings: (1) The high-level module should not depend on the low-layer module, both should be dependent on abstraction;
(2) Abstractions should not be dependent on detail, and details should be dependent on abstraction.

5. combination/Aggregation multiplexing principle : Refers to the use of combination/aggregation rather than inheritance to achieve the purpose of reuse. Another explanation is to use some existing objects in a new object to make them part of the new object, and the new object to reuse the objects by delegating the functionality to those objects.

6. interface Isolation Principle : (1) The design of the interface should follow the minimum interface principle, do not use the method that the user does not plug into the same interface.

(2) If an interface a inherits from another interface B, then interface A is equivalent to the method that inherits the interface B, then interface a inherits the interface B should also follow the above principle: should not contain the method that the user does not use. Conversely, interface A is contaminated by B, and the relationship should be redesigned.

7. Dimitri Rule : A software entity should interact with as few interactions as possible with other entities.

Two: Mode

Design mode:

1. Create a design pattern:

(1) Factory mode: Divided into simple Factory mode, factory method mode, abstract Factory mode.

A. Simple Factory mode: (Give the User a choice, that's that, that's how it came out of the user-independent (sub-assembly))

public class factory{
public static Sample creator (int which) {
GetClass Generation sample You can typically use dynamic classes to load a load class.

if (which==1)

return new Samplea ();

else if (which==2)

return new Sampleb ();
}
}
Sample Samplea=factory.creator (1);
Does not involve the specific subclass of sample, achieves the encapsulation effect, also reduces the opportunity of the error modification

B. Factory Method:

C. Abstract Factory:

2. Singleton mode:

The first: (deferred loading, only occurs when the object is really needed, but to ensure thread security, synchronization is not supported for high concurrency)

public class Singletest () {

private static Singletest instance;

Private Singletest () {

}

Public synchronized static Singletest getinstance () {

if (null = = Instance) {

Instance = new Singletest ();

}

return instance;

}

}

The second type: (Pre-load mode generation, whether or not used, all in memory, the thread is absolutely safe)

public class Singletest () {

private static Singletest instance = new Singletest ();

Private Singletest () {

}

public static Singletest getinstance () {

return instance;

}

}

Third: (added a sync lock, both deferred and high concurrency, and thread-safe-------but not supported before JDK1.7)

public class Singletest () {

private static Singletest instance = NULL;

Private Singletest () {

}

public static Singletest getinstance () {

if (null = = Instance) {

Synchronized (Singletest.class) {

if (null = = Instance) {

Instance = new Singletest ();

}

}

}

return instance;

}

}

3. Prototype mode: (Deep clone and shallow clone---to implement the Cloneable interface for classes that can be cloned, as with an object to be serializable to implement the Serializable interface)

A. Shallow clone: (Clone Method Clone (), cloning only one layer of objects, for the underlying object is not cloned. That is: If the object being cloned has a reference data type, the cloned object points to the previous reference data type, which results in a gerze effect)

public class Abstractspoon implements cloneable{

String Spoonname;

public void Setspoonname (String spoonname) {

This.spoonname = Spoonname;

}

Public String Getspoonname () {

return this.spoonname;

}

Public Object Clone () {

Object object = null;

try {

Object = Super.clone ();

} catch (Clonenotsupportedexception exception) {

System.err.println ("Abstractspoon is not cloneable");

}

return object;

}

}

B. Deep cloning: (in the object's implementation of the Cloning method, the object itself is serialized into a byte array, the object is deserialized back---------so that the entire object is cloned)

Public Mans Deepclone () {
try {
Bytearrayoutputstream bo = new Bytearrayoutputstream ();
ObjectOutputStream outobj = new ObjectOutputStream (bo);
Outobj.writeobject (this);

Bytearrayinputstream bi = new Bytearrayinputstream (Bo.tobytearray ());
ObjectInputStream outin = new ObjectInputStream (BI);
Mans m = (man) outin.readobject ();

return m;
} catch (Exception e) {
E.printstacktrace ();
}
return null;
}


 

Java Object-oriented design

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.