In this chapter we introduce the principle of design: separation of changes and invariant parts.
1. What is the unchanging part and what is the part of the change?
(1) Unchanging parts
Just like our previous additions and deletions, is a relatively fixed part, each new class inheritance is the same, but may put in the parameters of the inside is not the same
(2) The changing part
Just like the import and export feature, it will be based on different business scenarios and then have different requirements
2. What should we do?
We should encapsulate the immutable parts into a single class.
3. Code Listing
Add domain to import and export class in base
Package com.raylee.designpattern.strategy.ch04;/** * 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. * * @author Raylee * @data 2016-03-16 * @version 1.0 */public class Baseserviceimpl {private Export Exportobject;priva Te Import importobject;public Baseserviceimpl () {exportobject = new Export (); importobject = new Import ();} Public Export Getexportobject () {return exportobject;} Public Import Getimportobject () {return 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");}}
Import the Export implementation class:
Package Com.raylee.designpattern.strategy.ch04;public class Import {public void Importuser () {System.out.println (" Import users "); public void Importdept () {System.out.println ("import depts"); public void Importcourse () {System.out.println ("import Courses");}}
Package Com.raylee.designpattern.strategy.ch04;public class Export {public void Exportuser () {System.out.println (" Export users "); public void Exportdept () {System.out.println ("Export depts"); public void Exportcourse () {System.out.println ("Export Courses");}
Call the import and export method inside the implementation class:
Package com.raylee.designpattern.strategy.ch04;/** * @TODO 1 for user additions and deletions * @TODO 1.1 add Import and Export function * @author Raylee * @date 2016 March 16 * @version 1.1 */public class Userserviceimpl extends Baseserviceimpl {public void Exportuser () {Getexportobject (). E Xportuser ();} public void Importuser () {getimportobject (). Importuser ();}}
Package com.raylee.designpattern.strategy.ch04;/** * @TODO 1 for department additions and deletions * @TODO 1.1 add Import and Export function * @author Raylee * @date 2016 March 16 * @version 1.1 */public class Deptserviceimpl extends Baseserviceimpl {public void exportdept () {Getexportobject (). E Xportdept ();} public void Importdept () {getimportobject (). Importdept ();}}
Package com.raylee.designpattern.strategy.ch04;/** * @TODO 1 For additional additions to the course * @TODO 1.1 add Import and Export function * * @author Raylee * @date March 16, 2016 * @version 1.1 */public class Courseserviceimpl extends Baseserviceimpl {public void Exportcourse () {Getexport Object (). Exportcourse ();} public void Importcourse () {getimportobject (). Importcourse ();}}
Then test the import and export in the client
Package Com.raylee.designpattern.strategy.ch04;public class Client {//The user, Department, and course are subject to additional deletions public static void main (string[] args) {Courseserviceimpl Courseserviceimpl = new Courseserviceimpl (); Courseserviceimpl.importcourse (); Courseserviceimpl.exportcourse (); Userserviceimpl Userserviceimpl = new Userserviceimpl (); Userserviceimpl.importuser (); Userserviceimpl.exportuser () ;D Eptserviceimpl Deptserviceimpl = new Deptserviceimpl ();d eptserviceimpl.importdept ();d eptserviceimpl.exportdept () ;}}
Output:
Import Courses
Export Courses
Import Users
Export users
Import Depts
Export Depts
4. Questions
It looks like the code above should be fine, but if we add more than just import and export, and there are other features, we still need to change the original code, the extensibility is poor.
Summary: This chapter initially solves the problem of demand change, but there are still deficiencies.
Directory:
My github:https://github.com/raylee2015/designpattern.
Understanding design Patterns from scratch-strategy mode -04-Introducing design principles: separating the changing and unchanging parts