Six principles of design patterns

Six principles: a single responsibility, open - closed, Richter replace, reverse dependence, interface isolation, Demeter

 

1. Single Responsibility Principle:

A class is only responsible for the corresponding duties in a functional area, or can be defined as:

           On a category, only one cause why he should change.

           Single responsibility of guiding ideology is to achieve high cohesion, low coupling

example:

 

A construction worker class, the people are more powerful, can do three things

public class Builder
{
    public void Work()
    {
        Console.WriteLine("我开始做泥瓦工的活了");
        Console.WriteLine("我开始做木工的活了");
        Console.WriteLine("我开始做油漆工的活了");
    }
}

A man (like) to do so many things, you will not be "tired" do? He said the white point, is that too many duties that do live masonry, carpentry and live to do, if one day rush, only temporary to a carpenter or other types of work, you have to change classes, carpentry code also It can not reuse, which violates the single responsibility.

public interface IBuilder
{
    void Work();
}
public class TilerBuilder : IBuilder
{
    public void Work()
    {
        Console.WriteLine("我是泥瓦工,开始工作了");
    }
}
public class WoodBuilder : IBuilder
{
    public void Work()
    {
        Console.WriteLine("我是木工,开始工作了");
    }
}
public class PaintBuilder : IBuilder
{
    public void Work()
    {
        Console.WriteLine("我是泥瓦工,开始工作了");
    }
}

2. Open Closed Principle:

The main idea: For extension is open, but closed for modification

Is said software entities should be extended, but can not be modified

 

3. Richter substitution principle:

Any change in the place all to use the base class, its subclasses can be used to replace, but behavior does not

    https://blog.csdn.net/qq_30631063/article/details/85997672

 

4. Dependency Inversion Principle

High-level modules should not depend on the underlying module, one should rely on both abstract

Abstract should not rely on the details, the details should depend abstract

The core idea: Oriented Programming Interface

class Book{

	public String getContent()
	{

		return "很久很久以前有一个阿拉伯的故事……";

	}

}

 

class Mother{

	public void narrate(Book book)
	{

		System.out.println("妈妈开始讲故事");

		System.out.println(book.getContent());

	}

}

 

public class Client{

	public static void main(String[] args){

		Mother mother = new Mother();

		mother.narrate(new Book());

	}

}

运行结果:妈妈开始讲故事
              很久很久以前有一个阿拉伯的故事。。

If one day demand has changed, but do not give the book to the newspaper,

class Newspaper{

	public String getContent(){

		return "林书豪38+7领导尼克斯击败湖人……";

	}

}
//这位母亲却办不到,因为她居然不会读报纸上的故事,这太荒唐了,
//只是将书换成报纸,居然必须要修改Mother才能读。假如以后需求换成杂志呢?
//换成网页呢?还要不断地修改Mother,这显然不是好的设计。
//原因就是Mother与Book之间的耦合性太高了,必须降低他们之间的耦合度才行。

We introduce an interface Ireader: 

我们引入一个抽象的接口Ireader

interface IReader{

	public String getContent();

}

Mother类与接口IReader发生依赖关系,而Book和Newspaper都属于读物的范畴,他们各自都去实现IReader接口,这样就符合依赖倒置原则了,代码修改为:

class Newspaper implements IReader {

	public String getContent(){

		return "林书豪17+9助尼克斯击败老鹰……";

	}

}

class Book implements IReader{

	public String getContent(){

		return "很久很久以前有一个阿拉伯的故事……";

	}

}

 

class Mother{

	public void narrate(IReader reader){

		System.out.println("妈妈开始讲故事");

		System.out.println(reader.getContent());

	}

}

 

public class Client{

	public static void main(String[] args){

		Mother mother = new Mother();

		mother.narrate(new Book());

		mother.narrate(new Newspaper());

	}

}

5. Demeter:

Also known as the principle of least knowledge
if the two classes do not have to communicate directly with each other , then maybe a class should not direct interaction occurs, if one class needs to call one method to another class, you can forward the call by a third party

Demeter is the fundamental ideological emphasis on loose coupling between classes

 

 

6. Interface segregation principle:

There are generally two kinds of definitions: 

1. The client should not rely on his unwanted interfaces

2. dependencies between classes should be based on the smallest Interface

3. When we build the interface, we should try to establish a single, not bloated interface,

 

Interface segregation principle and the principle of a single difference:

 

Survey isolation interfaces different angles, and the principle of single responsibility, the principle of duty single classes and interfaces of a single responsibility , the interface isolation method principle requires as little as possible an interface , thinning.

example:

Beauty, we generally think there is a good face, good body, good temper. Then we define the interface of a beauty, a good face, good body, good temper.

public interface IPettyGirl
    {
        void GoodLooking();//要有好的面孔
        void NiceFigure();//要有好身材
        void GoodTemperament();//要有好气质
    }

实现一个具体的美女
public class PettyGirl : IPettyGirl
    {
        private string name;
        public PettyGirl(string name)
        {
            this.name = name;
        }
        public void GoodLooking()
        {
            Console.WriteLine(name + "---有好的面孔");
        }
        public void NiceFigure()
        {
            Console.WriteLine(name + "---有好身材");
        }
        public void GoodTemperament()
        {
            Console.WriteLine(name + "---有好气质");
        }

    }

However, with changes in people's aesthetic point of view, the definition of beauty is changing, people will face the general, the body in general, particularly good natured girl defined as beauty, such as the beautiful temperament, however, we have defined the interface beauty three conditions are ripe, beautiful temperament is not our definition of beauty standards, then, we should quantify the interface, because the beauty of our previously defined too broadly, the beauty of the interface into two interfaces, one is the shape of the interface beauty, the interface is a beautiful temperament, so I was flexible interface, easy to expand and maintain.

//定义俩个接口,外形美女和气质美女
public interface IGoodBodyGirl
    {
        void GoodLooking();//要有好的面孔
        void NiceFigure();//要有好身材
    }
    public interface IGoodTemperamentGirll
    {
        void GoodTemperament();////要有好气质
    }

 //实现接口的类:

 public class PettyGirl : IGoodBodyGirl, IGoodTemperamentGirl
    {
        private string name;
        public PettyGirl(string name)
        {
            this.name = name;
        }
        public void GoodLooking()
        {
            Console.WriteLine(name + "---有好的面孔");
        }
        public void NiceFigure()
        {
            Console.WriteLine(name + "---有好身材");
        }
        public void GoodTemperament()
        {
            Console.WriteLine(name + "---有好气质");
        }
    }

The core idea: the interface to be as small as possible

If you implement an interface, all methods in the interface must be implemented in a subclass

Extended: This is a puzzled look when I met the code:

    Q: When inheriting an interface when the interface inside the method in the class must all realize it?

    Answer: Yes!

example:

//俩个接口
public interface IGoodBodyGirl
    {
        void GoodLooking();//要有好的面孔
        void NiceFigure();//要有好身材
    }

    public interface IGoodTemperamentGirll
    {
        void GoodTemperament();////要有好气质
    }


    public class PettyGirl : IGoodBodyGirl, IGoodTemperamentGirll
    {
        private string name;
        public PettyGirl(string name)
        {
            this.name = name;
        }
        public void GoodLooking()
        {
            Console.WriteLine(name + "---有好的面孔");
        }
        public void NiceFigure()
        {
            Console.WriteLine(name + "---有好身材");
        }
        public void GoodTemperament()
        {
            Console.WriteLine(name + "---有好气质");
        }
    }

 

 

If I

 

public void NiceFigure()

        {

            Console.WriteLine (name + "--- have a good body");

        }

Class commented this method, then it will error , the error message: There is no implement interface member

Extensions 2:

 

Guess you like

Origin blog.csdn.net/qq_30631063/article/details/87520688