Java object-oriented and design pattern (i)

Source: Internet
Author: User

design principles : SOLID can be said that five design principles of each other.
Single duty Principle (S): Design for classes
Opening and closing principle (O): General guiding ideology
Richter Replacement Principle (L): Design for guiding class inheritance
Interface Isolation principle (I): Designed to guide the interface
Dependency Rollover principle (D): Used to guide how to Abstract

the relationships between classes :
Inheritance = implementation > Composition > Aggregation > association > Dependencies

UML Performance:
Inheritance: A solid line with a triangular arrow pointing to the parent class implementation: a dashed line with a triangular arrow pointing to the interface combination: solid with a solid diamond, a diamond pointing to the whole aggregation: a solid line with a hollow diamond, a diamond pointing to the whole association: a solid line with a normal arrow, pointing to the owner dependency: Dashed lines with ordinary arrows, Pointing to the user is simple, "1" inheritance and implementation of Java comes from, is a strong coupling relationship; "2" combination and aggregation is a special relationship, the relationship between the code is as a member variable, the aggregation relationship is stronger than the association relationship, the code will often pass set () method to complete the assignment of member variables, the composition of the relationship is more strengthened, often in the constructor to complete the initialization, that is, in general, the life cycle on the composition > Aggregation > association; "3" dependency and association on the contrary, the code generally does not present the member variable. Common to local variables, parameters of a method, or calls to a static method.Design Patterns: (classic 23) The core of the design pattern is reuse, which, if used alone, becomes cumbersome.

0th-type simple Factory mode. Pre-Knowledge: interface
The interface is able to isolate the encapsulation and provide a guarantee for the system's pluggable nature. In the interface also abstract class choice, should choose the interface first, only need to provide the public function for the subclass, only need to consider the abstract class.
Here is an example of how the interface is implemented.
 Public InterfaceProduct { Public voidcreateproduct ();} Public classConcreteproductImplementsProduct {@Override Public voidcreateproduct () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Production Products"); }} Public classUser { Public Static voidMain (string[] args) {//TODO auto-generated Method StubProduct Prod1 =Newconcreteproduct ();    Prod1.createproduct (); }}
Although the above example uses interface isolation (and is not completely isolated), there are still some problems. That is, the user needs to know the specific implementation. There is a coupling between the user and the specific product. The addition of a factory class can achieve the purpose of decoupling.
 Public InterfaceProduct { Public voidcreateproduct ();} Public classConcreteproductImplementsProduct {@Override Public voidcreateproduct () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Production Products"); }} Public classFactory { Public voidcreateproduct () {Product Prod1=Newconcreteproduct ();    Prod1.createproduct (); }} Public classUser { Public Static voidMain (string[] args) {//TODO auto-generated Method StubFactory Fact1 =NewFactory ();    Fact1.createproduct (); }}
With the addition of the factory class, the user is not required to focus on the specific implementation. The production of goods to the factory to deal with the matter. But you can focus on consumer products. This is really not worth the candle to a product. However, if there are many kinds of products, the advantage is obvious (user does not need to consider the tedious creation process). Definition of a simple factory model: provides athe ability to create object instances without concern for their specific implementation. The type of object being created, which can be an interface, an abstract class, or a concrete class. The essence of a simple factory is the choice of implementation. The advantage is that it helps encapsulate and decouple, and the disadvantage is that it increases client complexity and is not conducive to the expansion of the sub-factory. Simple factories are often implemented using static methods, so they are sometimes referred to as static factories. In order to select the implementation, you can also configure the selected parts to the configuration file.
 Public InterfaceProduct { Public voidmakeproduct ();} Public classConcreteproductImplementsProduct {@Override Public voidmakeproduct () {//TODO auto-generated Method StubSystem.out.println ("Made in China"); }} Public classFactory { Public StaticProduct makeproduct () {Properties Pro=NewProperties (); InputStream in=NULL; Try{ in= Factory.class. getResourceAsStream ("Factory.properties");        Pro.load (in); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            Try{in.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }} Product prod=NULL; Try{prod= (product) class.forname (Pro.getproperty ("Product1") . newinstance (); } Catch(Instantiationexception | illegalaccessexception |classnotfoundexception e) {            //TODO auto-generated Catch blockE.printstacktrace (); }                returnprod; }    } Public classUser { Public Static voidMain (string[] args) {//TODO auto-generated Method StubProduct prod =factory.makeproduct ();    Prod.makeproduct (); }} configuration file Factory.propertiesproduct1=no0.concreteproduct

Java object-oriented and design pattern (i)

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.