Observe that in the genealogy of Christ, with the exception of His Blessed Mother, only four females are made mention of, three of them harlots—Thamar, Rahab, and Bathsheba—and the fourth a Gentile, Ruth the Moabitess. Rahab, too, was a Gentile, being an inhabitant of Jericho. If the reason of all this be asked, SS. Jerome, Chrysostom, Ambrose answer, that it was so because Christ would signify that “He who came for the abolishing and putting away of sins wished to be born of sinners.” This reason is true, but allegorical. The literal and simple reason is, that these women were united to their husbands, not in the ordinary way, but after a new and extraordinary manner; and so they became types of the Church of Christ, which, when the Jews were rejected, was gathered out of the Gentiles by a new vocation, and after a new manner. Tamar, because Shelah was denied her in marriage, or rather because her union with him was deferred, using deceit, prostituted herself to Judah. Bathsheba, the wife of Uriah, was united to David, first by adultery, then in marriage. Rahab married Salmon because she hospitably received and protected the Hebrew spies who were sent by Joshua to Jericho, and so she became of the same faith and religion. Ruth married Boaz when she had passed with her mother-in-law, Naomi, from Moab into Judæa.
The tropological sense is to show us the vanity of pride of birth, and that true nobility consists, not in ancestry, but in our own good disposition and virtues. Thus S. Chrysostom. Wherefore let no one be ashamed of his birth, nor even of vile and wicked ancestors; but let us say with Cicero, “I have outshone my forefathers in virtue.” There can be no doubt that there are in the ancestry of the most exalted persons, forasmuch as they are sprung from Adam, many ignoble, worthless, wicked, and infamous persons. Plato, according to Seneca (Epis. 44), is of opinion that all kings are descended from servants, and that all servants are sprung from kings; that there is no king who has been entirely free from the plough, and no ploughman who has not been mixed up with kings.
Lastly, Solomon, amongst the other vanities and uncertainties of the world, reckons this: “Because out of prison and chains sometimes a man cometh forth to a kingdom: and another born king is consumed with poverty.” (Eccles. iv. 14.)
Cornelius a Lapide, Commentary on Matthew. It seems a bit harsh to call Bathsheba a harlot, though, and Tamar's story is a bit more complicated than the term suggests.