#author("2023-05-15T21:07:39+09:00","default:ryuichi","ryuichi")
#author("2023-05-15T21:08:50+09:00","default:ryuichi","ryuichi")
* データ指向プログラミングとOOP [#a0c06789]

** データ指向プログラミング [#z5d35a47]

*** task.ts [#rcc6d8bc]
 export type Task = {
   name: string;
   completed: boolean;
 };
 
 function create(name: string, completed: boolean = false): Task {
   return { name, completed };
 }
 
 const TaskManager = { create };
 
 export default TaskManager;


*** project.ts [#e0d446bf]

 import { Task } from "./task";
 
 export type Project = {
   name: string;
   tasks: Task[];
 };
 
 function create(name: string, tasks: Task[] = []): Project {
   return { name, tasks };
 }
 
 function addTask(project: Project, task: Task): Project {
   return { ...project, tasks: [...project.tasks, task] };
 }
 
 function completeTask(project: Project, taskName: string): Project {
   const updatedTasks = project.tasks.map(task =>
     task.name === taskName ? { ...task, completed: true } : task
   );
   return { ...project, tasks: updatedTasks };
 }
 
 const ProjectManager = { create, addTask, completeTask };
 
 export default ProjectManager;

*** main.ts [#te87c2b8]

 import TaskManager, { Task } from "./task";
 import ProjectManager, { Project } from "./project";
 
 // Example usage
 const project: Project = ProjectManager.create('Test Project');
 const task1: Task = TaskManager.create('Task 1');
 const task2: Task = TaskManager.create('Task 2');
 
 const updatedProject: Project = ProjectManager.addTask(
   ProjectManager.addTask(project, task1),
   task2
 );
 let updatedProject: Project = ProjectManager.addTask(project, task1);
 updatedProject = ProjectManager.addTask(updatedProject, task2);
 updatedProject = ProjectManager.completeTask(updatedProject, 'Task 1');
 
 showTasks(updatedProject);
 const completedProject = completeTask(updatedProject, 'Task 1');
 showTasks(completedProject);
 console.log(updatedProject);


** オブジェクト指向プログラミング [#x24cd408]
 class Task {
   constructor(private _name: string, private _completed: boolean = false) {}
 
   get name(): string {
     return this._name;
   }
 
   get completed(): boolean {
     return this._completed;
   }
 
   complete(): Task {
     return new Task(this._name, true);
   }
 }
 
 class Project {
   private tasks: Task[];
 
   constructor(private _name: string, tasks: Task[] = []) {
     this.tasks = tasks;
   }
 
   get name(): string {
     return this._name;
   }
 
   addTask(task: Task): void {
     this.tasks.push(task);
   }
 
   completeTask(taskName: string): void {
     const taskIndex = this.tasks.findIndex((task) => task.name === taskName);
     if (taskIndex >= 0) {
       this.tasks[taskIndex] = this.tasks[taskIndex].complete();
     }
   }
 
   showTasks(): void {
     console.log(`Project: ${this._name}`);
     for (const task of this.tasks) {
       console.log(`- ${task.name}: ${task.completed ? 'Completed' : 'Pending'}`);
     }
   }
 }
 
 // Example usage
 const project = new Project('Test Project');
 const task1 = new Task('Task 1');
 const task2 = new Task('Task 2');
 
 project.addTask(task1);
 project.addTask(task2);
 
 project.showTasks();
 project.completeTask('Task 1');
 project.showTasks();

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS