Answer the question
In order to leave comments, you need to log in
Where can I get real code examples of using OOP in web services?
I read literature and online sources and EVERYWHERE examples are given "from life" of the type: there is a car, this is an abstract class, and there is a truck, a bus, etc., these are concrete objects ... And stuff like that, but these examples do not explain HOW to use the OOP approach for example, when writing a Blog or Bulletin Board engine or some other SPECIFIC web project.
I understand what OOP is with examples from life, but I still don’t understand how to use it, for example, when displaying a bulletin board, user registration and other functions. I ask you to help with specific examples to understand how to use it in real web projects, and not on the example of "cats and dogs" or "building airplanes".
Thank you in advance.
Answer the question
In order to leave comments, you need to log in
There is a group of people who do not understand anything in programming, but they have an idea, an understanding of how the product works, and money (but this is secondary) Let's
call this group of people business
The product of this business is a news site. Let's take a news site as an example, because it's essentially the same blog.
There is a programmer who understands how to automate the actions of this idea and digitize the behavior of the product.
First, the business describes the pain that the product solves
. What is the pain? The business used to sell newspapers, and now it wants its own online newspaper.
1. They don't want to spend money on printing, but just make news posts and articles.
2. They do not want to pay money for transport costs to deliver newspapers, but to do mailing lists by e-mail
3. They want to receive feedback (comments)
this is enough for an example.
Ideally, a business orders a design. How it should look.
Ideally, there is also a contract manager who knows UML, but these are wet fantasies, so let's assume that there is a business and there is a programmer.
Then the entities of this product and the actors in this product are described.
What can we understand from this? What entities do we have?
1. post - news or article on the site.
1.1. At this stage, we find out from the business what is the difference between the news and the article.
Business says: news (for example) has only one picture, text.
The article also has text, but there can be several pictures, and there can be no comments.
Business forgot about the fact that there is also a date in the design, here the programmer himself thinks it out by looking at the layouts.
As a result, we get one abstract Post model and two implementing it: Article and News.
public abstract class Post
{
protected Post(string text, int writerId)
{
Text = text;
CreationDate = DateTime.Now;
WriterId = writerId;
}
public int Id { get; set; }
public string Text { get; private set; }
public DateTime CreationDate { get; private set; }
//Идентификатор писателя статьи\новости
public int WriterId { get; private set; }
//Автоматически подтягиваемая из базы модель писателя через ORM по WriterId
public virtual Writer Writer { get; set; }
}
public class Article : Post
{
public Picture[] Pictures { get; private set; }
public Article(string text, int writerId, Picture[] pictures) : base(text, writerId)
{
Pictures = pictures;
}
}
public class News : Post
{
public Picture Picture { get; }
//Массив комментариев к посту
// private set -- говорит о том что массив инкапсулирован
// и управлять массивом можно только через метод AddComment
public List<Commentary> Commentaries { get; private set; }
public News(string text, int writerId, Picture picture) : base(text, writerId)
{
Picture = picture;
}
public void AddComment(Commentary commentary)
{
Commentaries.Add(commentary);
}
}
public abstract class User
{
public int Id { get; set; }
public string Username { get; private set; }
public string Role { get; private set; }
protected User(string role, string username)
{
Role = role;
Username = username;
}
}
public class Subscriber : User
{
public string Email { get; private set; }
public Subscriber(string username, string email) : base(nameof(Subscriber), username)
{
Email = email;
}
}
public class Writer : User
{
public Writer(string username) : base(nameof(Writer), username)
{
}
}
public class Commentary
{
public int Id { get; set; }
public string CommentText { get; private set; }
public int UserId { get; private set; }
public virtual User User { get; private set; }
public DateTime CommentCreationDate { get; private set; }
public Commentary(int userId, string commentText)
{
UserId = userId;
CommentText = commentText;
CommentCreationDate = DateTime.Now;
}
}
when displaying the bulletin board....some entity, let's call it Mapper, returns a collection of objects - declaration objects. Each object == one declaration. An ad can have properties - VIP status (end date of this status), typical fields for ads of the title + text type. Next, the ad can have images. The declaration object has a method that asks another entity for its image objects, which in turn know how to form a URL to an image or other image characteristics. The ad object has a getPaymentSystem() method - an object of a Kassa type class is returned, it can return a Robokassa (or Frikassa) object that can generate a URL that is necessary for a payment transaction for paid services of this ad object....
I still don't understand how to use itand you won't understand without prompting.
Book Techniques of object-oriented design. P... provides very popular practices for using OOP. Written in 1994. To this day, it is considered a "must read" for a programmer.
OOP for the web is the same as in books.
Make a class PostVBlog
Describe its properties (they are also attributes).
Implement methods for reading and writing from/to the database.
It turns out something like
class BlogPost {
public $id;
public $title;
public $text;
public $date;
public function write() {
// код записи в БД
}
public function read() {
// код чтения из БД
}
}
class Comment {
public $id;
public $text;
public $date;
public $blogpostId;
// чёрт, здесь почти такой же код, как и в классе BlogPost
}
class DatabaseRecord {
public function write() {
// универсальный код записи в БД
}
public function read() {
// универсальный код чтения из БД
}
}
class Comment extends DatabaseRecord {
// ....
}
class Comment extends DatabaseRecord {
// ....
}
1) Take a framework (Larva or Symphony) and write your web service on it. These frameworks (especially symphony) have design patterns in their base that show you how to use OOP.
2) Read a book on design patterns (Heal Fest Patterns - on the cover is a girl with pigtails) - it will open your eyes.
3) In reality, most of the work is inheritance from abstract classes (most often that the framework provides you), building classes using DI (composition) and that's it =)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question