Understanding design Patterns from scratch-strategy mode -05-Introducing design principles: interface-Oriented programming

Source: Internet
Author: User

In this chapter we discuss how to solve the problem of poor extensibility in the previous chapter.

1. Solution

Interface-Oriented Programming


2. Ideas

Using Java polymorphism, dynamic settings import the exported behavior


3. Code Listing

Using the import and export interface in base, and then adding a generic export import method, in the following implementation, we only need to set different import and export behavior, you can import and export methods to achieve different import and export results.

Package com.raylee.designpattern.strategy.ch05;/** * 1.0 This class is the original class that we need to improve with the design pattern, which is the initial environment for the policy mode application. <br> * 1.1 Using Import Export interface *  * @author Raylee * @data 2016-03-16 * @version 1.1 */public class Baseserviceimpl {privat E export exportobject;private Import importobject;public void Setexportobject (export exportobject) {This.exportobject = ExportObject;} public void Setimportobject (Import importobject) {this.importobject = Importobject;} public void Add () {System.out.println ("Add a New object"); public void Update () {System.out.println ("Update Object");} public void query () {System.out.println ("Query and Return a object");} public void Delete () {System.out.println ("delete the Object");} public void ExportObject () {exportobject.exportobject ();} public void Importobject () {importobject.importobject ();}}

Inherit the implementation of base:

Package com.raylee.designpattern.strategy.ch05;/** * @TODO 1.0 for user additions and deletions * @author Raylee * @date March 16, 2016 * @version 1. 0 */public Class Userserviceimpl extends Baseserviceimpl {}

Package com.raylee.designpattern.strategy.ch05;/** * @TODO 1.0 for Department additions and deletions * @author Raylee * @date March 16, 2016 * @version 1. 0 */public Class Deptserviceimpl extends Baseserviceimpl {}

Package com.raylee.designpattern.strategy.ch05;/** * @TODO 1.0 for Course additions and deletions *  * @author Raylee * @date March 16, 2016 * @vers Ion 1.0 */public Class Courseserviceimpl extends Baseserviceimpl {}

Here is the import Export interface:

Package Com.raylee.designpattern.strategy.ch05;public interface Import {public void Importobject ();}

Package Com.raylee.designpattern.strategy.ch05;public interface Export {public void ExportObject ();}

Implementation of various import and export behaviors:

Package Com.raylee.designpattern.strategy.ch05;public class Importuser implements Import {public void Importobject () { Importuser ();} private void Importuser () {System.out.println ("Import Users");}}

Package Com.raylee.designpattern.strategy.ch05;public class Importdept implements Import {public void Importobject () { Importdept ();} private void Importdept () {System.out.println ("import Depts");}}

Package Com.raylee.designpattern.strategy.ch05;public class Importcourse implements Import {public void Importobject () {Importcourse ();} private void Importcourse () {System.out.println ("import Courses");}}

Package Com.raylee.designpattern.strategy.ch05;public class Exportuser implements Export {public void ExportObject () { Exportuser ();} private void Exportuser () {System.out.println ("Export Users");}}

Package Com.raylee.designpattern.strategy.ch05;public class Exportdept implements Export {public void ExportObject () { Exportdept ();} private void Exportdept () {System.out.println ("Export depts");}

Package Com.raylee.designpattern.strategy.ch05;public class Exportcourse implements Export {public void ExportObject () {Exportcourse ();} private void Exportcourse () {System.out.println ("Export Courses");}

In the client test method, this time you need to set the appropriate export import behavior before each export

Package Com.raylee.designpattern.strategy.ch05;public class Client {//The user, Department, and course are subject to additional deletions public static void main (string[] args) {Courseserviceimpl Courseserviceimpl = new Courseserviceimpl (); Courseserviceimpl.setexportobject (new Exportcourse ()); Courseserviceimpl.setimportobject (new Importuser ()); Courseserviceimpl.importobject (); Courseserviceimpl.exportobject (); Userserviceimpl Userserviceimpl = new Userserviceimpl (); Userserviceimpl.setexportobject (new ExportUser ()); Userserviceimpl.setimportobject (New Importuser ()); Userserviceimpl.importobject (); Userserviceimpl.exportobject () ;D Eptserviceimpl Deptserviceimpl = new Deptserviceimpl ();d eptserviceimpl.setexportobject (New Exportdept ()); Deptserviceimpl.setimportobject (New Importdept ());d eptserviceimpl.importobject ();d eptserviceimpl.exportobject () ;}}

Output:

Import Users
Export Courses
Import Users
Export users
Import Depts
Export Depts


4. Summary:

In fact, whether the principle of separation or interface-oriented programming, are part of the strategy design pattern, the first separation after the interface, which is the use of policy design mode process.

Therefore, the concept of the strategy design pattern is to define a series of algorithms, encapsulate each algorithm, and make them interchangeable with each other. This mode allows the algorithm to be independent of the customers who use it.


5. Advantages and Disadvantages

Pros: Scalability is good. When we add a new module to the export, do not need to change the original code, only need to add a new import and export behavior class, and then the client side by setting the different behavior to determine the different import and export results.

Cons: Increase in code


Therefore, judging the use of design patterns of standards, I think, is the function of the degree of change will be how much? Many changes, like the above import and export, each module results are not the same, this time using the design pattern, is relatively exact, if the change is less, similar to the above additions and deletions to check, there is no need, because the increase of a lot of code volume.


6. More Usage Scenarios

Different search, sorting methods, of course, if the hard to divide, the above additions and deletions can also be divided into several, because now the market database is not the same, we can according to different databases to distinguish the storage behavior


Summary: This chapter shows another important process in the Strategy model-interface-oriented programming.


Directory:


My github:https://github.com/raylee2015/designpattern.



Understanding design Patterns from scratch-strategy mode -05-Introducing design principles: interface-Oriented programming

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.