**Seven principles of design mode**
1.Closed development principle: open to expansion and closed to modification. When the program needs to be expanded, the original code cannot be modified to form a hot plug effect. 2.Single responsibility principle: a class interface or method is responsible for only one responsibility, reducing code complexity and Risks caused by changes. 3.Dependency Inversion Principle: for interface programming, it depends on abstract classes or interfaces It does not depend on the specific implementation class. 4.Interface isolation principle: define different functions in different interfaces to realize interface isolation. 5.Richter's substitution principle: wherever a base class can appear, subclasses must appear. 6.Demeter principle: each module should know or rely on other modules as little as possible to reduce code coupling. 7.Composite Reuse Principle: use combination as much as possible/Aggregation rather than inheritance achieves the purpose of software reuse.
**Briefly describe the classification of design patterns**
1. Creation mode: hide logic while creating objects, and directly instantiate objects without using new.
Yes (factory method pattern abstract factory pattern singleton pattern builder pattern prototype pattern)
2. Structural pattern: create objects with complex structure through inheritance and reference between classes and interfaces.
Yes (adapter mode, decorator mode, agent mode, appearance mode, bridge mode, combination mode, sharing mode)
3. Behavior mode: different behaviors are realized through different communication methods between classes.
Yes (policy mode, template method mode, observer mode, iteration sub mode, responsibility chain mode, command mode, memo mode, status mode, visitor mode, mediator mode, interpreter mode)
**What is a policy model**
The policy model defines algorithm families, which are encapsulated separately so that they can be replaced with each other,
This mode makes the change of the algorithm independent of the customer using the algorithm.
Advantages: it follows the opening and closing principle and has good expansibility.
Disadvantages: with the increase of strategy, more and more external exposure.
Take life as an example. For example, if we want to travel, we have a lot of choices. We can choose to ride a bike, drive a car, take a plane, take a train, etc. we can use the strategic mode to encapsulate each travel as a strategy. After adding new modes of transportation, such as super high-speed railway and rocket, we can add new modes of transportation without changing the original categories, This is also in line with the opening and closing principle of software development. The implementation code of policy mode is as follows:
/\* \* Statement travel \*/ interface ITrip { void going(); } class Bike implements ITrip { @Override public void going() { System.out.println("ride on a bicycle"); } } class Drive implements ITrip { @Override public void going() { System.out.println("drive a car"); } } /\* \* Define travel class \*/ class Trip { private ITrip trip; public Trip(ITrip trip) { this.trip = trip; } public void doTrip() { this.trip.going(); } } /\* \* Execution method \*/ public class StrategyTest { public static void main(String[] args) { Trip trip = new Trip(new Bike()); trip.doTrip(); } }
public abstract class Duck { //Interface FlyBehavior flyBehavior; QuackBehavior quackBehavior; public abstract void display(); public void swim() { System.out.println("all duck float,evendecoys"); } public void performFly(){ flyBehavior.fly(); } public void performQucak(){ quackBehavior.quack(); } public FlyBehavior getFlyBehavior() { return flyBehavior; } public void setFlyBehavior(FlyBehavior flyBehavior) { this.flyBehavior = flyBehavior; } public QuackBehavior getQuackBehavior() { return quackBehavior; } public void setQuackBehavior(QuackBehavior quackBehavior) { this.quackBehavior = quackBehavior; } } public class MallardDuck extends Duck{ @Override public void display() { System.out.println("i am real Mallard duck"); } public MallardDuck() { flyBehavior=new FlyWithWings(); quackBehavior=new Qucak(); } } public class ModeDuck extends Duck{ @Override public void display() { System.out.println("i am Model duck"); } public ModeDuck() { flyBehavior=new FylNoWay(); quackBehavior=new Qucak(); } } public interface FlyBehavior { public void fly(); } public class FlyRockPowered implements FlyBehavior{ @Override public void fly() { System.out.println("iam flying with a rocket"); } } public class FlyWithWings implements FlyBehavior{ @Override public void fly() { System.out.println("I can fly"); } } public class FylNoWay implements FlyBehavior{ @Override public void fly() { System.out.println("i am can not fly"); } } public interface QuackBehavior { public void quack(); } public class Qucak implements QuackBehavior{ @Override public void quack() { System.out.println("Qucak"); } } public class MinDuckSimulator { public static void main(String[] args) { Duck duck=new MallardDuck(); duck.performQucak(); duck.performFly(); duck.swim(); duck.display(); Duck model=new ModeDuck(); model.performFly(); model.setFlyBehavior(new FlyRockPowered()); model.performFly(); } }